Class: RubyToRuby
- Defined in:
- lib/spec/test_to_spec/ruby2ruby.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cond_indent_process(pt) ⇒ Object
-
#cond_loop(exp, name) ⇒ Object
def process_dxstr(exp) puts “DXSTR:#expexp.shift” end.
- #indent(s) ⇒ Object
-
#initialize ⇒ RubyToRuby
constructor
A new instance of RubyToRuby.
- #process_and(exp) ⇒ Object
- #process_args(exp) ⇒ Object
- #process_array(exp) ⇒ Object
- #process_attrasgn(exp) ⇒ Object
- #process_begin(exp) ⇒ Object
- #process_block(exp) ⇒ Object
- #process_block_arg(exp) ⇒ Object
- #process_block_pass(exp) ⇒ Object
- #process_bmethod(exp) ⇒ Object
- #process_call(exp) ⇒ Object
- #process_case(exp) ⇒ Object
- #process_class(exp) ⇒ Object
- #process_colon2(exp) ⇒ Object
- #process_const(exp) ⇒ Object
- #process_dasgn_curr(exp) ⇒ Object
- #process_defn(exp) ⇒ Object
- #process_dot2(exp) ⇒ Object
- #process_dot3(exp) ⇒ Object
- #process_dstr(exp) ⇒ Object
- #process_dvar(exp) ⇒ Object
- #process_ensure(exp) ⇒ Object
- #process_false(exp) ⇒ Object
- #process_fbody(exp) ⇒ Object
- #process_fcall(exp) ⇒ Object
- #process_for(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_module(exp) ⇒ Object
- #process_nil(exp) ⇒ Object
- #process_not(exp) ⇒ Object
- #process_or(exp) ⇒ Object
- #process_resbody(exp) ⇒ Object
- #process_rescue(exp) ⇒ Object
- #process_retry(exp) ⇒ Object
- #process_return(exp) ⇒ Object
- #process_scope(exp) ⇒ Object
- #process_self(exp) ⇒ Object
- #process_str(exp) ⇒ Object
- #process_super(exp) ⇒ Object
- #process_true(exp) ⇒ Object
- #process_until(exp) ⇒ Object
- #process_vcall(exp) ⇒ Object
- #process_when(exp) ⇒ Object
- #process_while(exp) ⇒ Object
- #process_yield(exp) ⇒ Object
- #process_zarray(exp) ⇒ Object
- #process_zsuper(exp) ⇒ Object
Constructor Details
#initialize ⇒ RubyToRuby
Returns a new instance of RubyToRuby.
76 77 78 79 80 81 82 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 76 def initialize super @indent = " " self.auto_shift_type = true self.strict = true self.expected = String end |
Class Method Details
.translate(klass, method = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 66 def self.translate(klass, method=nil) RubySource.new( unless method.nil? then self.new.process(ParseTree.new.parse_tree_for_method(klass, method)) else self.new.process(ParseTree.new.parse_tree(klass).first) # huh? why is the :class node wrapped? end ) end |
Instance Method Details
#cond_indent_process(pt) ⇒ Object
317 318 319 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 317 def cond_indent_process(pt) (pt and pt.first == :block) ? process(pt) : indent(process(pt)) end |
#cond_loop(exp, name) ⇒ Object
def process_dxstr(exp)
puts "DXSTR:#{exp.shift}"
end
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 469 def cond_loop(exp, name) cond = process(exp.shift) body = cond_indent_process(exp.shift) head_controlled = exp.empty? ? false : exp.shift code = [] if head_controlled then code << "#{name} (#{cond}) do" code << body code << "end" else code << "begin" code << body code << "end #{name} (#{cond})" end code.join("\n") end |
#indent(s) ⇒ Object
84 85 86 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 84 def indent(s) s.to_s.map{|line| @indent + line}.join end |
#process_and(exp) ⇒ Object
88 89 90 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 88 def process_and(exp) "(#{process exp.shift} and #{process exp.shift})" end |
#process_args(exp) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 92 def process_args(exp) args = [] until exp.empty? do arg = exp.shift if arg.is_a? Array args[-(arg.size-1)..-1] = arg[1..-1].map{|a| process a} else args << arg end end args.empty? ? "" : "(#{args.join ', '})" end |
#process_array(exp) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 107 def process_array(exp) code = [] until exp.empty? do code << process(exp.shift) end return "[" + code.join(", ") + "]" end |
#process_attrasgn(exp) ⇒ Object
115 116 117 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 115 def process_attrasgn(exp) process_call(exp) end |
#process_begin(exp) ⇒ Object
119 120 121 122 123 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 119 def process_begin(exp) s = "begin\n" s << (process(exp.shift).to_s + "\n") until exp.empty? s + "\nend" end |
#process_block(exp) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 125 def process_block(exp) code = [] catch_block_arg = false until exp.empty? do if catch_block_arg if exp.first and exp.first.first == :block_arg code[-1] = code[-1][0..-2] + ", #{process(exp.shift)})" end catch_block_arg = false else if exp.first.first == :args catch_block_arg = true end if [:ensure, :rescue].include? exp.first.first code << process(exp.shift) else code << indent(process(exp.shift)) end end end body = code.join("\n") body += "\n" return body end |
#process_block_arg(exp) ⇒ Object
152 153 154 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 152 def process_block_arg(exp) "&#{exp.shift}" end |
#process_block_pass(exp) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 156 def process_block_pass(exp) bname = process(exp.shift) fcall = process(exp.shift) if fcall[-1,1] == ')' "#{fcall[0..-2]}, &(#{bname}))" else "#{fcall}(&(#{bname}))" end end |
#process_bmethod(exp) ⇒ Object
487 488 489 490 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 487 def process_bmethod(exp) exp.clear "" end |
#process_call(exp) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 166 def process_call(exp) receiver = process exp.shift name = exp.shift args_exp = exp.shift if args_exp && args_exp.first == :array args = "#{process(args_exp)[1..-2]}" else args = process args_exp end case name when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :% then # "(#{receiver} #{name} #{args})" when :[] then "#{receiver}[#{args}]" else "#{receiver}.#{name}#{args ? "(#{args})" : args}" end end |
#process_case(exp) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 186 def process_case(exp) s = "case #{process exp.shift}\n" until exp.empty? pt = exp.shift if pt and pt.first == :when s << "#{process(pt)}\n" else s << "else\n#{indent(process(pt))}\n" end end s + "\nend" end |
#process_class(exp) ⇒ Object
199 200 201 202 203 204 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 199 def process_class(exp) s = "class #{exp.shift} < #{exp.shift}\n" body = "" body << "#{process exp.shift}\n\n" until exp.empty? s + indent(body) + "end" end |
#process_colon2(exp) ⇒ Object
206 207 208 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 206 def process_colon2(exp) "#{process(exp.shift)}::#{exp.shift}" end |
#process_const(exp) ⇒ Object
210 211 212 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 210 def process_const(exp) exp.shift.to_s end |
#process_dasgn_curr(exp) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 214 def process_dasgn_curr(exp) s = exp.shift.to_s unless exp.empty? s += "=" + process(exp.shift) else if(@block_params) s else "" end end end |
#process_defn(exp) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 227 def process_defn(exp) if exp[1].first == :cfunc s = "# method '#{exp.shift}' defined in a C function" exp.shift return s else name = exp.shift args = process(exp.shift) return "def #{name}#{args}end".gsub(/\n\s*\n+/, "\n") end end |
#process_dot2(exp) ⇒ Object
239 240 241 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 239 def process_dot2(exp) "(#{process exp.shift}..#{process exp.shift})" end |
#process_dot3(exp) ⇒ Object
243 244 245 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 243 def process_dot3(exp) "(#{process exp.shift}...#{process exp.shift})" end |
#process_dstr(exp) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 247 def process_dstr(exp) s = exp.shift.dump[0..-2] until exp.empty? pt = exp.shift if pt.first == :str s << process(pt)[1..-2] else s << '#{' + process(pt) + '}' end end s + '"' end |
#process_dvar(exp) ⇒ Object
260 261 262 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 260 def process_dvar(exp) exp.shift.to_s end |
#process_ensure(exp) ⇒ Object
264 265 266 267 268 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 264 def process_ensure(exp) process(exp.shift) + "\n" + "ensure\n" + indent(process(exp.shift)) end |
#process_false(exp) ⇒ Object
270 271 272 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 270 def process_false(exp) "false" end |
#process_fbody(exp) ⇒ Object
274 275 276 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 274 def process_fbody(exp) process(exp.shift) end |
#process_fcall(exp) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 278 def process_fcall(exp) exp_orig = exp.deep_clone # [:fcall, :puts, [:array, [:str, "This is a weird loop"]]] name = exp.shift.to_s args = exp.shift code = [] unless args.nil? then assert_type args, :array args.shift # :array until args.empty? do code << process(args.shift) end end return "#{name}(#{code.join(', ')})" end |
#process_for(exp) ⇒ Object
294 295 296 297 298 299 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 294 def process_for(exp) s = "for #{process(exp[1])} in #{process(exp[0])}\n" 2.times{ exp.shift } s += indent("#{process(exp.shift)}\n") s += "end" end |
#process_gvar(exp) ⇒ Object
301 302 303 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 301 def process_gvar(exp) exp.shift.to_s end |
#process_hash(exp) ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 305 def process_hash(exp) body = [] body << "#{process(exp.shift)} => #{process(exp.shift)}" until exp.empty? body_str = "" body_str = "\n"+indent(body.join(",\n"))+"\n" unless body.empty? "{" + body_str + "}" end |
#process_iasgn(exp) ⇒ Object
313 314 315 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 313 def process_iasgn(exp) "#{exp.shift} = #{process exp.shift}" end |
#process_if(exp) ⇒ Object
321 322 323 324 325 326 327 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 321 def process_if(exp) s = ["if (#{process exp.shift})"] s << "#{cond_indent_process(exp.shift)}" s << "else\n#{cond_indent_process(exp.shift)}" until exp.empty? s << "end" s.join("\n") end |
#process_iter(exp) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 329 def process_iter(exp) owner = exp.shift args = exp.shift if !args.nil? @block_params = true end processed_args = process args block_args = processed_args.nil? ? "" : "|#{processed_args}|" result = "#{process owner} {#{block_args}\n" + indent("#{process exp.shift}\n") + "}" @block_params = false result end |
#process_ivar(exp) ⇒ Object
345 346 347 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 345 def process_ivar(exp) exp.shift.to_s end |
#process_lasgn(exp) ⇒ Object
349 350 351 352 353 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 349 def process_lasgn(exp) s = "#{exp.shift}" s += " = #{process exp.shift}" unless exp.empty? s end |
#process_lit(exp) ⇒ Object
355 356 357 358 359 360 361 362 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 355 def process_lit(exp) obj = exp.shift if obj.is_a? Range # to get around how parsed ranges turn into lits and lose parens "(" + obj.inspect + ")" else obj.inspect end end |
#process_lvar(exp) ⇒ Object
364 365 366 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 364 def process_lvar(exp) exp.shift.to_s end |
#process_masgn(exp) ⇒ Object
368 369 370 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 368 def process_masgn(exp) process(exp.shift)[1..-2] end |
#process_module(exp) ⇒ Object
372 373 374 375 376 377 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 372 def process_module(exp) s = "module #{exp.shift}\n" body = "" body << "#{process exp.shift}\n\n" until exp.empty? s + indent(body) + "end" end |
#process_nil(exp) ⇒ Object
379 380 381 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 379 def process_nil(exp) "nil" end |
#process_not(exp) ⇒ Object
383 384 385 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 383 def process_not(exp) "(not #{process exp.shift})" end |
#process_or(exp) ⇒ Object
387 388 389 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 387 def process_or(exp) "(#{process exp.shift} or #{process exp.shift})" end |
#process_resbody(exp) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 391 def process_resbody(exp) s = "rescue " unless exp.empty? if exp.first.first == :array s << process(exp.shift)[1..-2] end s << "\n" end s << (process(exp.shift).to_s + "\n") until exp.empty? s end |
#process_rescue(exp) ⇒ Object
403 404 405 406 407 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 403 def process_rescue(exp) s = "" s << (process(exp.shift).to_s + "\n") until exp.empty? s end |
#process_retry(exp) ⇒ Object
409 410 411 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 409 def process_retry(exp) "retry" end |
#process_return(exp) ⇒ Object
413 414 415 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 413 def process_return(exp) return "return #{process exp.shift}" end |
#process_scope(exp) ⇒ Object
417 418 419 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 417 def process_scope(exp) return process(exp.shift) end |
#process_self(exp) ⇒ Object
421 422 423 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 421 def process_self(exp) "self" end |
#process_str(exp) ⇒ Object
424 425 426 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 424 def process_str(exp) return exp.shift.dump end |
#process_super(exp) ⇒ Object
428 429 430 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 428 def process_super(exp) "super(#{process(exp.shift)})" end |
#process_true(exp) ⇒ Object
432 433 434 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 432 def process_true(exp) "true" end |
#process_until(exp) ⇒ Object
436 437 438 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 436 def process_until(exp) cond_loop(exp, 'until') end |
#process_vcall(exp) ⇒ Object
440 441 442 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 440 def process_vcall(exp) return exp.shift.to_s end |
#process_when(exp) ⇒ Object
444 445 446 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 444 def process_when(exp) "when #{process(exp.shift).to_s[1..-2]}\n#{indent(process(exp.shift))}" end |
#process_while(exp) ⇒ Object
448 449 450 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 448 def process_while(exp) cond_loop(exp, 'while') end |
#process_yield(exp) ⇒ Object
452 453 454 455 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 452 def process_yield(exp) body = process(exp.shift)[1..-2] unless exp.empty? "yield#{body and "(#{body})"}" end |
#process_zarray(exp) ⇒ Object
457 458 459 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 457 def process_zarray(exp) "[]" end |
#process_zsuper(exp) ⇒ Object
461 462 463 |
# File 'lib/spec/test_to_spec/ruby2ruby.rb', line 461 def process_zsuper(exp) "super" end |