Class: Ruby2Ruby
- Inherits:
-
SexpProcessor
- Object
- SexpProcessor
- Ruby2Ruby
- Defined in:
- lib/ruby2ruby.rb
Constant Summary collapse
- VERSION =
'1.2.0'- LINE_LENGTH =
78- ASSIGN_NODES =
Nodes that represent assignment and probably need () around them.
[ :dasgn, :flip2, :flip3, :lasgn, :masgn, :attrasgn, :op_asgn1, :op_asgn2, :op_asgn_and, :op_asgn_or, :return, ]
Class Method Summary collapse
Instance Method Summary collapse
- #cond_loop(exp, name) ⇒ Object
- #indent(s) ⇒ Object
-
#initialize ⇒ Ruby2Ruby
constructor
A new instance of Ruby2Ruby.
-
#process_alias(exp) ⇒ Object
Processors.
- #process_and(exp) ⇒ Object
-
#process_arglist(exp) ⇒ Object
custom made node.
- #process_args(exp) ⇒ Object
- #process_array(exp) ⇒ Object
- #process_attrasgn(exp) ⇒ Object
- #process_back_ref(exp) ⇒ Object
-
#process_begin(exp) ⇒ Object
TODO: figure out how to do rescue and ensure ENTIRELY w/o begin.
- #process_block(exp) ⇒ Object
- #process_block_pass(exp) ⇒ Object
- #process_break(exp) ⇒ Object
- #process_call(exp) ⇒ Object
- #process_case(exp) ⇒ Object
- #process_cdecl(exp) ⇒ Object
- #process_class(exp) ⇒ Object
- #process_colon2(exp) ⇒ Object
- #process_colon3(exp) ⇒ Object
- #process_const(exp) ⇒ Object
- #process_cvar(exp) ⇒ Object
- #process_cvasgn(exp) ⇒ Object
- #process_cvdecl(exp) ⇒ Object
- #process_defined(exp) ⇒ Object
- #process_defn(exp) ⇒ Object
- #process_defs(exp) ⇒ Object
- #process_dot2(exp) ⇒ Object
- #process_dot3(exp) ⇒ Object
- #process_dregx(exp) ⇒ Object
- #process_dregx_once(exp) ⇒ Object
- #process_dstr(exp) ⇒ Object
- #process_dsym(exp) ⇒ Object
- #process_dxstr(exp) ⇒ Object
- #process_ensure(exp) ⇒ Object
- #process_evstr(exp) ⇒ Object
- #process_false(exp) ⇒ Object
- #process_flip2(exp) ⇒ Object
- #process_flip3(exp) ⇒ Object
- #process_for(exp) ⇒ Object
- #process_gasgn(exp) ⇒ Object
- #process_gvar(exp) ⇒ Object
- #process_hash(exp) ⇒ Object
- #process_iasgn(exp) ⇒ Object
- #process_if(exp) ⇒ Object
- #process_iter(exp) ⇒ Object
- #process_ivar(exp) ⇒ Object
- #process_lasgn(exp) ⇒ Object
- #process_lit(exp) ⇒ Object
- #process_lvar(exp) ⇒ Object
- #process_masgn(exp) ⇒ Object
- #process_match(exp) ⇒ Object
- #process_match2(exp) ⇒ Object
- #process_match3(exp) ⇒ Object
- #process_module(exp) ⇒ Object
- #process_next(exp) ⇒ Object
- #process_nil(exp) ⇒ Object
- #process_not(exp) ⇒ Object
- #process_nth_ref(exp) ⇒ Object
- #process_op_asgn1(exp) ⇒ Object
- #process_op_asgn2(exp) ⇒ Object
- #process_op_asgn_and(exp) ⇒ Object
- #process_op_asgn_or(exp) ⇒ Object
- #process_or(exp) ⇒ Object
- #process_postexe(exp) ⇒ Object
- #process_redo(exp) ⇒ Object
- #process_resbody(exp) ⇒ Object
- #process_rescue(exp) ⇒ Object
- #process_retry(exp) ⇒ Object
- #process_return(exp) ⇒ Object
- #process_sclass(exp) ⇒ Object
- #process_scope(exp) ⇒ Object
- #process_self(exp) ⇒ Object
- #process_splat(exp) ⇒ Object
- #process_str(exp) ⇒ Object
- #process_super(exp) ⇒ Object
- #process_svalue(exp) ⇒ Object
- #process_to_ary(exp) ⇒ Object
- #process_true(exp) ⇒ Object
- #process_undef(exp) ⇒ Object
- #process_until(exp) ⇒ Object
- #process_valias(exp) ⇒ Object
- #process_when(exp) ⇒ Object
- #process_while(exp) ⇒ Object
- #process_xstr(exp) ⇒ Object
- #process_yield(exp) ⇒ Object
- #process_zsuper(exp) ⇒ Object
-
#rewrite_attrasgn(exp) ⇒ Object
Rewriters:.
- #rewrite_ensure(exp) ⇒ Object
- #rewrite_rescue(exp) ⇒ Object
- #rewrite_svalue(exp) ⇒ Object
- #splat(sym) ⇒ Object
-
#util_dthing(exp, regx = false) ⇒ Object
Utility Methods:.
- #util_module_or_class(exp, is_class = false) ⇒ Object
Constructor Details
#initialize ⇒ Ruby2Ruby
Returns a new instance of Ruby2Ruby.
35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby2ruby.rb', line 35 def initialize super @indent = " " self.auto_shift_type = true self.strict = true self.expected = String # self.debug[:defn] = /zsuper/ end |
Class Method Details
.translate(klass_or_str, method = nil) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/ruby2ruby.rb', line 28 def self.translate(klass_or_str, method = nil) require 'parse_tree' sexp = ParseTree.translate(klass_or_str, method) sexp = Unifier.new.process(sexp) self.new.process(sexp) end |
Instance Method Details
#cond_loop(exp, name) ⇒ Object
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 |
# File 'lib/ruby2ruby.rb', line 805 def cond_loop(exp, name) cond = process(exp.shift) body = process(exp.shift) head_controlled = exp.shift body = indent(body).chomp if body code = [] if head_controlled then code << "#{name} #{cond} do" code << body if body code << "end" else code << "begin" code << body if body code << "end #{name} #{cond}" end code.join("\n") end |
#indent(s) ⇒ Object
930 931 932 |
# File 'lib/ruby2ruby.rb', line 930 def indent(s) s.to_s.split(/\n/).map{|line| @indent + line}.join("\n") end |
#process_alias(exp) ⇒ Object
Processors
48 49 50 |
# File 'lib/ruby2ruby.rb', line 48 def process_alias(exp) "alias_method #{process(exp.shift)}, #{process(exp.shift)}" end |
#process_and(exp) ⇒ Object
52 53 54 |
# File 'lib/ruby2ruby.rb', line 52 def process_and(exp) "(#{process exp.shift} and #{process exp.shift})" end |
#process_arglist(exp) ⇒ Object
custom made node
56 57 58 59 60 61 62 |
# File 'lib/ruby2ruby.rb', line 56 def process_arglist(exp) # custom made node code = [] until exp.empty? do code << process(exp.shift) end code.join ', ' end |
#process_args(exp) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby2ruby.rb', line 64 def process_args(exp) args = [] until exp.empty? do arg = exp.shift case arg when Symbol then args << arg when Array then case arg.first when :block then asgns = {} arg[1..-1].each do |lasgn| asgns[lasgn[1]] = process(lasgn) end args.each_with_index do |name, index| args[index] = asgns[name] if asgns.has_key? name end else raise "unknown arg type #{arg.first.inspect}" end else raise "unknown arg type #{arg.inspect}" end end return "(#{args.join ', '})" end |
#process_array(exp) ⇒ Object
94 95 96 |
# File 'lib/ruby2ruby.rb', line 94 def process_array(exp) "[#{process_arglist(exp)}]" end |
#process_attrasgn(exp) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby2ruby.rb', line 98 def process_attrasgn(exp) receiver = process exp.shift name = exp.shift args = exp.empty? ? nil : exp.shift case name when :[]= then rhs = process args.pop "#{receiver}[#{process(args)}] = #{rhs}" else name = name.to_s.sub(/=$/, '') if args && args != s(:arglist) then "#{receiver}.#{name} = #{process(args)}" end end end |
#process_back_ref(exp) ⇒ Object
115 116 117 |
# File 'lib/ruby2ruby.rb', line 115 def process_back_ref(exp) "$#{exp.shift}" end |
#process_begin(exp) ⇒ Object
TODO: figure out how to do rescue and ensure ENTIRELY w/o begin
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ruby2ruby.rb', line 120 def process_begin(exp) code = [] code << "begin" until exp.empty? src = process(exp.shift) src = indent(src) unless src =~ /(^|\n)rescue/ # ensure no level 0 rescues code << src end code << "end" return code.join("\n") end |
#process_block(exp) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ruby2ruby.rb', line 132 def process_block(exp) result = [] exp << nil if exp.empty? until exp.empty? do code = exp.shift if code.nil? or code.first == :nil then result << "# do nothing" else result << process(code) end end result = result.join "\n" result = case self.context[1] when nil, :scope, :if, :iter, :resbody, :when, :while then result + "\n" else "(#{result})" end return result end |
#process_block_pass(exp) ⇒ Object
157 158 159 160 161 |
# File 'lib/ruby2ruby.rb', line 157 def process_block_pass exp raise "huh?: #{exp.inspect}" if exp.size > 1 "&#{process exp.shift}" end |
#process_break(exp) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/ruby2ruby.rb', line 163 def process_break(exp) val = exp.empty? ? nil : process(exp.shift) # HACK "break" + (val ? " #{val}" : "") if val then "break #{val}" else "break" end end |
#process_call(exp) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/ruby2ruby.rb', line 173 def process_call(exp) receiver_node_type = exp.first.nil? ? nil : exp.first.first receiver = process exp.shift receiver = "(#{receiver})" if Ruby2Ruby::ASSIGN_NODES.include? receiver_node_type name = exp.shift args_exp = exp.shift rescue nil if args_exp && args_exp.first == :array # FIX args = "#{process(args_exp)[1..-2]}" else args = process args_exp args = nil if args.empty? end case name when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then "(#{receiver} #{name} #{args})" when :[] then "#{receiver}[#{args}]" when :"-@" then "-#{receiver}" when :"+@" then "+#{receiver}" else unless receiver.nil? then "#{receiver}.#{name}#{args ? "(#{args})" : args}" else "#{name}#{args ? "(#{args})" : args}" end end end |
#process_case(exp) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ruby2ruby.rb', line 207 def process_case(exp) result = [] expr = process exp.shift if expr then result << "case #{expr}" else result << "case" end until exp.empty? pt = exp.shift if pt and pt.first == :when result << "#{process(pt)}" else code = indent(process(pt)) code = indent("# do nothing") if code =~ /^\s*$/ result << "else\n#{code}" end end result << "end" result.join("\n") end |
#process_cdecl(exp) ⇒ Object
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/ruby2ruby.rb', line 229 def process_cdecl(exp) lhs = exp.shift lhs = process lhs if Sexp === lhs unless exp.empty? then rhs = process(exp.shift) "#{lhs} = #{rhs}" else lhs.to_s end end |
#process_class(exp) ⇒ Object
240 241 242 |
# File 'lib/ruby2ruby.rb', line 240 def process_class(exp) "class #{util_module_or_class(exp, true)}" end |
#process_colon2(exp) ⇒ Object
244 245 246 |
# File 'lib/ruby2ruby.rb', line 244 def process_colon2(exp) "#{process(exp.shift)}::#{exp.shift}" end |
#process_colon3(exp) ⇒ Object
248 249 250 |
# File 'lib/ruby2ruby.rb', line 248 def process_colon3(exp) "::#{exp.shift}" end |
#process_const(exp) ⇒ Object
252 253 254 |
# File 'lib/ruby2ruby.rb', line 252 def process_const(exp) exp.shift.to_s end |
#process_cvar(exp) ⇒ Object
256 257 258 |
# File 'lib/ruby2ruby.rb', line 256 def process_cvar(exp) "#{exp.shift}" end |
#process_cvasgn(exp) ⇒ Object
260 261 262 |
# File 'lib/ruby2ruby.rb', line 260 def process_cvasgn(exp) "#{exp.shift} = #{process(exp.shift)}" end |
#process_cvdecl(exp) ⇒ Object
264 265 266 |
# File 'lib/ruby2ruby.rb', line 264 def process_cvdecl(exp) "#{exp.shift} = #{process(exp.shift)}" end |
#process_defined(exp) ⇒ Object
268 269 270 |
# File 'lib/ruby2ruby.rb', line 268 def process_defined(exp) "defined? #{process(exp.shift)}" end |
#process_defn(exp) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/ruby2ruby.rb', line 272 def process_defn(exp) type1 = exp[1].first type2 = exp[2].first rescue nil if type1 == :args and [:ivar, :attrset].include? type2 then name = exp.shift case type2 when :ivar then exp.clear return "attr_reader #{name.inspect}" when :attrset then exp.clear return "attr_writer :#{name.to_s[0..-2]}" else raise "Unknown defn type: #{exp.inspect}" end end case type1 when :scope, :args then name = exp.shift args = process(exp.shift) args = "" if args == "()" body = indent(process(exp.shift)) return "def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n") else raise "Unknown defn type: #{type1} for #{exp.inspect}" end end |
#process_defs(exp) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/ruby2ruby.rb', line 302 def process_defs(exp) lhs = exp.shift var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.first name = exp.shift lhs = process(lhs) lhs = "(#{lhs})" unless var exp.unshift "#{lhs}.#{name}" process_defn(exp) end |
#process_dot2(exp) ⇒ Object
314 315 316 |
# File 'lib/ruby2ruby.rb', line 314 def process_dot2(exp) "(#{process exp.shift}..#{process exp.shift})" end |
#process_dot3(exp) ⇒ Object
318 319 320 |
# File 'lib/ruby2ruby.rb', line 318 def process_dot3(exp) "(#{process exp.shift}...#{process exp.shift})" end |
#process_dregx(exp) ⇒ Object
322 323 324 |
# File 'lib/ruby2ruby.rb', line 322 def process_dregx(exp) "/" << util_dthing(exp, true) << "/" end |
#process_dregx_once(exp) ⇒ Object
326 327 328 |
# File 'lib/ruby2ruby.rb', line 326 def process_dregx_once(exp) process_dregx(exp) + "o" end |
#process_dstr(exp) ⇒ Object
330 331 332 |
# File 'lib/ruby2ruby.rb', line 330 def process_dstr(exp) "\"#{util_dthing(exp)}\"" end |
#process_dsym(exp) ⇒ Object
334 335 336 |
# File 'lib/ruby2ruby.rb', line 334 def process_dsym(exp) ":#{process_dstr(exp)}" end |
#process_dxstr(exp) ⇒ Object
338 339 340 |
# File 'lib/ruby2ruby.rb', line 338 def process_dxstr(exp) "`#{process_dstr(exp)[1..-2]}`" end |
#process_ensure(exp) ⇒ Object
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/ruby2ruby.rb', line 342 def process_ensure(exp) body = process exp.shift ens = exp.shift ens = nil if ens == s(:nil) ens = process(ens) || "# do nothing" body.sub!(/\n\s*end\z/, '') return "#{body}\nensure\n#{indent ens}" end |
#process_evstr(exp) ⇒ Object
353 354 355 |
# File 'lib/ruby2ruby.rb', line 353 def process_evstr(exp) exp.empty? ? '' : process(exp.shift) end |
#process_false(exp) ⇒ Object
357 358 359 |
# File 'lib/ruby2ruby.rb', line 357 def process_false(exp) "false" end |
#process_flip2(exp) ⇒ Object
361 362 363 |
# File 'lib/ruby2ruby.rb', line 361 def process_flip2(exp) "#{process(exp.shift)}..#{process(exp.shift)}" end |
#process_flip3(exp) ⇒ Object
365 366 367 |
# File 'lib/ruby2ruby.rb', line 365 def process_flip3(exp) "#{process(exp.shift)}...#{process(exp.shift)}" end |
#process_for(exp) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/ruby2ruby.rb', line 369 def process_for(exp) recv = process exp.shift iter = process exp.shift body = exp.empty? ? nil : process(exp.shift) result = ["for #{iter} in #{recv} do"] result << indent(body ? body : "# do nothing") result << "end" result.join("\n") end |
#process_gasgn(exp) ⇒ Object
381 382 383 |
# File 'lib/ruby2ruby.rb', line 381 def process_gasgn(exp) process_iasgn(exp) end |
#process_gvar(exp) ⇒ Object
385 386 387 |
# File 'lib/ruby2ruby.rb', line 385 def process_gvar(exp) return exp.shift.to_s end |
#process_hash(exp) ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/ruby2ruby.rb', line 389 def process_hash(exp) result = [] until exp.empty? lhs = process(exp.shift) rhs = exp.shift t = rhs.first rhs = process rhs rhs = "(#{rhs})" unless [:lit, :str].include? t # TODO: verify better! result << "#{lhs} => #{rhs}" end case self.context[1] when :arglist, :argscat then return "#{result.join(', ')}" # HACK - this will break w/ 2 hashes as args else return "{ #{result.join(', ')} }" end end |
#process_iasgn(exp) ⇒ Object
409 410 411 412 413 414 415 416 |
# File 'lib/ruby2ruby.rb', line 409 def process_iasgn(exp) lhs = exp.shift if exp.empty? then # part of an masgn lhs.to_s else "#{lhs} = #{process exp.shift}" end end |
#process_if(exp) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/ruby2ruby.rb', line 418 def process_if(exp) = Ruby2Ruby::ASSIGN_NODES.include? exp.first.first c = process exp.shift t = process exp.shift f = process exp.shift c = "(#{c.chomp})" if c =~ /\n/ if t then unless then if f then r = "#{c} ? (#{t}) : (#{f})" r = nil if r =~ /return/ # HACK - need contextual awareness or something else r = "#{t} if #{c}" end return r if r and (@indent+r).size < LINE_LENGTH and r !~ /\n/ end r = "if #{c} then\n#{indent(t)}\n" r << "else\n#{indent(f)}\n" if f r << "end" r else unless then r = "#{f} unless #{c}" return r if (@indent+r).size < LINE_LENGTH and r !~ /\n/ end "unless #{c} then\n#{indent(f)}\nend" end end |
#process_iter(exp) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/ruby2ruby.rb', line 451 def process_iter(exp) iter = process exp.shift args = exp.shift args = (args == 0) ? '' : process(args) body = exp.empty? ? nil : process(exp.shift) b, e = if iter == "END" then [ "{", "}" ] else [ "do", "end" ] end iter.sub!(/\(\)$/, '') # REFACTOR: ugh result = [] result << "#{iter} {" result << " |#{args}|" if args if body then result << " #{body.strip} " else result << ' ' end result << "}" result = result.join return result if result !~ /\n/ and result.size < LINE_LENGTH result = [] result << "#{iter} #{b}" result << " |#{args}|" if args result << "\n" result << indent(body.strip) result << "\n" result << e result.join end |
#process_ivar(exp) ⇒ Object
488 489 490 |
# File 'lib/ruby2ruby.rb', line 488 def process_ivar(exp) exp.shift.to_s end |
#process_lasgn(exp) ⇒ Object
492 493 494 495 496 |
# File 'lib/ruby2ruby.rb', line 492 def process_lasgn(exp) s = "#{exp.shift}" s += " = #{process exp.shift}" unless exp.empty? s end |
#process_lit(exp) ⇒ Object
498 499 500 501 502 503 504 505 506 |
# File 'lib/ruby2ruby.rb', line 498 def process_lit(exp) obj = exp.shift case obj when Range then "(#{obj.inspect})" else obj.inspect end end |
#process_lvar(exp) ⇒ Object
508 509 510 |
# File 'lib/ruby2ruby.rb', line 508 def process_lvar(exp) exp.shift.to_s end |
#process_masgn(exp) ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/ruby2ruby.rb', line 516 def process_masgn(exp) lhs = exp.shift rhs = exp.empty? ? nil : exp.shift unless exp.empty? then rhs[1] = splat(rhs[1]) unless rhs == s(:splat) lhs << rhs rhs = exp.shift end case lhs.first when :array then lhs.shift lhs = lhs.map do |l| case l.first when :masgn then "(#{process(l)})" else process(l) end end when :lasgn then lhs = [ splat(lhs.last) ] when :splat then lhs = [ :"*" ] else raise "no clue: #{lhs.inspect}" end if context[1] == :iter and rhs then lhs << splat(rhs[1]) rhs = nil end unless rhs.nil? then t = rhs.first rhs = process rhs rhs = rhs[1..-2] if t != :to_ary return "#{lhs.join(", ")} = #{rhs}" else return lhs.join(", ") end end |
#process_match(exp) ⇒ Object
561 562 563 |
# File 'lib/ruby2ruby.rb', line 561 def process_match(exp) "#{process(exp.shift)}" end |
#process_match2(exp) ⇒ Object
565 566 567 568 569 |
# File 'lib/ruby2ruby.rb', line 565 def process_match2(exp) lhs = process(exp.shift) rhs = process(exp.shift) "#{lhs} =~ #{rhs}" end |
#process_match3(exp) ⇒ Object
571 572 573 574 575 |
# File 'lib/ruby2ruby.rb', line 571 def process_match3(exp) rhs = process(exp.shift) lhs = process(exp.shift) "#{lhs} =~ #{rhs}" end |
#process_module(exp) ⇒ Object
577 578 579 |
# File 'lib/ruby2ruby.rb', line 577 def process_module(exp) "module #{util_module_or_class(exp)}" end |
#process_next(exp) ⇒ Object
581 582 583 584 585 586 587 588 |
# File 'lib/ruby2ruby.rb', line 581 def process_next(exp) val = exp.empty? ? nil : process(exp.shift) if val then "next #{val}" else "next" end end |
#process_nil(exp) ⇒ Object
590 591 592 |
# File 'lib/ruby2ruby.rb', line 590 def process_nil(exp) "nil" end |
#process_not(exp) ⇒ Object
594 595 596 |
# File 'lib/ruby2ruby.rb', line 594 def process_not(exp) "(not #{process exp.shift})" end |
#process_nth_ref(exp) ⇒ Object
598 599 600 |
# File 'lib/ruby2ruby.rb', line 598 def process_nth_ref(exp) "$#{exp.shift}" end |
#process_op_asgn1(exp) ⇒ Object
602 603 604 605 606 607 608 609 610 |
# File 'lib/ruby2ruby.rb', line 602 def process_op_asgn1(exp) # [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]] lhs = process(exp.shift) index = process(exp.shift) msg = exp.shift rhs = process(exp.shift) "#{lhs}[#{index}] #{msg}= #{rhs}" end |
#process_op_asgn2(exp) ⇒ Object
612 613 614 615 616 617 618 619 620 621 |
# File 'lib/ruby2ruby.rb', line 612 def process_op_asgn2(exp) # [[:lvar, :c], :var=, :"||", [:lit, 20]] lhs = process(exp.shift) index = exp.shift.to_s[0..-2] msg = exp.shift rhs = process(exp.shift) "#{lhs}.#{index} #{msg}= #{rhs}" end |
#process_op_asgn_and(exp) ⇒ Object
623 624 625 626 627 628 |
# File 'lib/ruby2ruby.rb', line 623 def process_op_asgn_and(exp) # a &&= 1 # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] exp.shift process(exp.shift).sub(/\=/, '&&=') end |
#process_op_asgn_or(exp) ⇒ Object
630 631 632 633 634 635 |
# File 'lib/ruby2ruby.rb', line 630 def process_op_asgn_or(exp) # a ||= 1 # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] exp.shift process(exp.shift).sub(/\=/, '||=') end |
#process_or(exp) ⇒ Object
637 638 639 |
# File 'lib/ruby2ruby.rb', line 637 def process_or(exp) "(#{process exp.shift} or #{process exp.shift})" end |
#process_postexe(exp) ⇒ Object
641 642 643 |
# File 'lib/ruby2ruby.rb', line 641 def process_postexe(exp) "END" end |
#process_redo(exp) ⇒ Object
645 646 647 |
# File 'lib/ruby2ruby.rb', line 645 def process_redo(exp) "redo" end |
#process_resbody(exp) ⇒ Object
649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/ruby2ruby.rb', line 649 def process_resbody exp args = exp.shift body = process(exp.shift) || "# do nothing" name = args.lasgn true name ||= args.iasgn true args = process(args)[1..-2] args = " #{args}" unless args.empty? args += " => #{name[1]}" if name "rescue#{args}\n#{indent body}" end |
#process_rescue(exp) ⇒ Object
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'lib/ruby2ruby.rb', line 662 def process_rescue exp body = process(exp.shift) unless exp.first.first == :resbody els = process(exp.pop) unless exp.last.first == :resbody body ||= "# do nothing" simple = exp.size == 1 resbodies = [] until exp.empty? do resbody = exp.shift simple &&= resbody[1] == s(:array) && resbody[2] != nil resbodies << process(resbody) end if els then "#{indent body}\n#{resbodies.join("\n")}\nelse\n#{indent els}" elsif simple then resbody = resbodies.first.sub(/\n\s*/, ' ') "#{body} #{resbody}" else "#{indent body}\n#{resbodies.join("\n")}" end end |
#process_retry(exp) ⇒ Object
686 687 688 |
# File 'lib/ruby2ruby.rb', line 686 def process_retry(exp) "retry" end |
#process_return(exp) ⇒ Object
690 691 692 693 694 695 696 697 698 |
# File 'lib/ruby2ruby.rb', line 690 def process_return(exp) # HACK return "return" + (exp.empty? ? "" : " #{process exp.shift}") if exp.empty? then return "return" else return "return #{process exp.shift}" end end |
#process_sclass(exp) ⇒ Object
700 701 702 |
# File 'lib/ruby2ruby.rb', line 700 def process_sclass(exp) "class << #{process(exp.shift)}\n#{indent(process(exp.shift))}\nend" end |
#process_scope(exp) ⇒ Object
704 705 706 |
# File 'lib/ruby2ruby.rb', line 704 def process_scope(exp) exp.empty? ? "" : process(exp.shift) end |
#process_self(exp) ⇒ Object
708 709 710 |
# File 'lib/ruby2ruby.rb', line 708 def process_self(exp) "self" end |
#process_splat(exp) ⇒ Object
712 713 714 715 716 717 718 |
# File 'lib/ruby2ruby.rb', line 712 def process_splat(exp) if exp.empty? then "*" else "*#{process(exp.shift)}" end end |
#process_str(exp) ⇒ Object
720 721 722 |
# File 'lib/ruby2ruby.rb', line 720 def process_str(exp) return exp.shift.dump end |
#process_super(exp) ⇒ Object
724 725 726 727 728 729 730 731 |
# File 'lib/ruby2ruby.rb', line 724 def process_super(exp) args = [] until exp.empty? do args << process(exp.shift) end "super(#{args.join(', ')})" end |
#process_svalue(exp) ⇒ Object
733 734 735 736 737 738 739 |
# File 'lib/ruby2ruby.rb', line 733 def process_svalue(exp) code = [] until exp.empty? do code << process(exp.shift) end code.join(", ") end |
#process_to_ary(exp) ⇒ Object
741 742 743 |
# File 'lib/ruby2ruby.rb', line 741 def process_to_ary(exp) process(exp.shift) end |
#process_true(exp) ⇒ Object
745 746 747 |
# File 'lib/ruby2ruby.rb', line 745 def process_true(exp) "true" end |
#process_undef(exp) ⇒ Object
749 750 751 |
# File 'lib/ruby2ruby.rb', line 749 def process_undef(exp) "undef #{process(exp.shift)}" end |
#process_until(exp) ⇒ Object
753 754 755 |
# File 'lib/ruby2ruby.rb', line 753 def process_until(exp) cond_loop(exp, 'until') end |
#process_valias(exp) ⇒ Object
757 758 759 |
# File 'lib/ruby2ruby.rb', line 757 def process_valias(exp) "alias #{exp.shift} #{exp.shift}" end |
#process_when(exp) ⇒ Object
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 |
# File 'lib/ruby2ruby.rb', line 761 def process_when(exp) src = [] if self.context[1] == :array then # ugh. matz! why not an argscat?!? val = process(exp.shift) exp.shift # empty body return "*#{val}" end until exp.empty? cond = process(exp.shift).to_s[1..-2] code = indent(process(exp.shift)) code = indent "# do nothing" if code =~ /\A\s*\Z/ src << "when #{cond} then\n#{code.chomp}" end src.join("\n") end |
#process_while(exp) ⇒ Object
780 781 782 |
# File 'lib/ruby2ruby.rb', line 780 def process_while(exp) cond_loop(exp, 'while') end |
#process_xstr(exp) ⇒ Object
784 785 786 |
# File 'lib/ruby2ruby.rb', line 784 def process_xstr(exp) "`#{process_str(exp)[1..-2]}`" end |
#process_yield(exp) ⇒ Object
788 789 790 791 792 793 794 795 796 797 798 799 |
# File 'lib/ruby2ruby.rb', line 788 def process_yield(exp) args = [] until exp.empty? do args << process(exp.shift) end unless args.empty? then "yield(#{args.join(', ')})" else "yield" end end |
#process_zsuper(exp) ⇒ Object
801 802 803 |
# File 'lib/ruby2ruby.rb', line 801 def process_zsuper(exp) "super" end |
#rewrite_attrasgn(exp) ⇒ Object
Rewriters:
828 829 830 831 832 833 834 835 |
# File 'lib/ruby2ruby.rb', line 828 def rewrite_attrasgn exp if context.first(2) == [:array, :masgn] then exp[0] = :call exp[2] = exp[2].to_s.sub(/=$/, '').to_sym end exp end |
#rewrite_ensure(exp) ⇒ Object
837 838 839 840 |
# File 'lib/ruby2ruby.rb', line 837 def rewrite_ensure exp exp = s(:begin, exp) unless context.first == :begin exp end |
#rewrite_rescue(exp) ⇒ Object
842 843 844 845 846 847 848 849 850 851 852 853 854 |
# File 'lib/ruby2ruby.rb', line 842 def rewrite_rescue exp complex = false complex ||= exp.size > 3 complex ||= exp.block complex ||= exp.find_nodes(:resbody).any? { |n| n.array != s(:array) } complex ||= exp.find_nodes(:resbody).any? { |n| n.last.nil? } handled = context.first == :ensure exp = s(:begin, exp) if complex unless handled exp end |
#rewrite_svalue(exp) ⇒ Object
856 857 858 859 860 861 862 863 864 865 |
# File 'lib/ruby2ruby.rb', line 856 def rewrite_svalue(exp) case exp.last.first when :array s(:svalue, *exp[1][1..-1]) when :splat exp else raise "huh: #{exp.inspect}" end end |
#splat(sym) ⇒ Object
512 513 514 |
# File 'lib/ruby2ruby.rb', line 512 def splat(sym) :"*#{sym}" end |
#util_dthing(exp, regx = false) ⇒ Object
Utility Methods:
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 |
# File 'lib/ruby2ruby.rb', line 870 def util_dthing(exp, regx = false) s = [] suck = true x = exp.shift.gsub(/"/, '\"').gsub(/\n/, '\n') x.gsub!(/\//, '\/') if regx s << x until exp.empty? pt = exp.shift case pt when Sexp then case pt.first when :str then x = pt.last.gsub(/"/, '\"').gsub(/\n/, '\n') x.gsub!(/\//, '\/') if regx s << x else s << '#{' << process(pt) << '}' # do not use interpolation here end else # HACK: raise "huh?: #{pt.inspect}" # do nothing for now end end s.join end |
#util_module_or_class(exp, is_class = false) ⇒ Object
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'lib/ruby2ruby.rb', line 898 def util_module_or_class(exp, is_class=false) result = [] name = exp.shift name = process name if Sexp === name result << name if is_class then superk = process(exp.shift) result << " < #{superk}" if superk end result << "\n" body = [] begin code = process(exp.shift).chomp body << code unless code.nil? or code.empty? end until exp.empty? unless body.empty? then body = indent(body.join("\n\n")) + "\n" else body = "" end result << body result << "end" result.join end |