Class: PartialRuby::PureRubyContext

Inherits:
Context
  • Object
show all
Defined in:
lib/partialruby.rb

Instance Method Summary collapse

Methods inherited from Context

#emul, #object_ref, #ruby_emul, #run

Instance Method Details

#process_args(tree) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/partialruby.rb', line 254

def process_args(tree)
  nodetype = tree[0]
  if nodetype == :lasgn
    tree.last.to_s
  elsif nodetype == :masgn
    tree[1][1..-1].map {|subtree|
      process_args(subtree)
    }.join(",")
  elsif nodetype == :splat
    "*" + process_args(tree.last)
  end

end

#ruby_emul_and(tree) ⇒ Object



421
422
423
# File 'lib/partialruby.rb', line 421

def ruby_emul_and(tree)
  "(#{emul tree[1]}) and (#{emul tree[2]})"
end

#ruby_emul_array(tree) ⇒ Object



230
231
232
# File 'lib/partialruby.rb', line 230

def ruby_emul_array(tree)
  "[" + tree[1..-1].map{ |subtree| "(" + emul(subtree) + ")" }.join(",") + "]"
end

#ruby_emul_block(tree) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/partialruby.rb', line 102

def ruby_emul_block(tree)
  last = nil

  code = ""
  tree[1..-1].each do  |subtree|
    code << emul(subtree) << "\n"
  end

  code
end

#ruby_emul_call(tree) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/partialruby.rb', line 433

def ruby_emul_call(tree)
    object_tree = tree[1]
    method_name = tree[2]

    arglisttree = tree[3]
    arglist = arglisttree[1..-1]

    argsstr = arglist.
          map{|subtree|
            if subtree[0] == :splat
              emul(subtree)
            else
              "(" +  emul(subtree) + ")"
            end
             }.
          join(",")

    if (object_tree)
      if arglist.count == 0
      "(#{emul(object_tree)}).#{method_name}"
      else
      "(#{emul(object_tree)}).#{method_name}(#{argsstr})"
      end
    else
      if arglist.count == 0
      "#{method_name}"
      else
      "#{method_name}(#{argsstr})"
      end
    end

end

#ruby_emul_case(tree) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/partialruby.rb', line 285

def ruby_emul_case(tree)
  str = "case #{emul tree[1]}; "

  tree[2..-2].each do |subtree|
    matches = subtree[1][1..-1].map{|subsubtree| "(" + emul(subsubtree) + ")" }.join(",")

    str << "when #{matches}; #{emul subtree[2]};"
  end

  if tree[-1]
    str << "else; #{emul tree[-1]}; "
  end

  str << "end; "
  str
end

#ruby_emul_cdecl(tree) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/partialruby.rb', line 204

def ruby_emul_cdecl(tree)

  if tree[1].instance_of? Sexp
  "(#{emul tree[1]} = #{emul tree[2]})"
  else
  "(#{tree[1]} = #{emul tree[2]})"
  end
end

#ruby_emul_class(tree) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/partialruby.rb', line 153

def ruby_emul_class(tree)
    classtree = tree[1]
    subtree = tree[3]

    classname = ""
    if classtree.instance_of? Symbol then
      classname = classtree
    else
      classname = emul classtree
    end

    return ("
      class #{classname}
        #{emul subtree}
      end
    ")
end

#ruby_emul_colon2(tree) ⇒ Object



149
150
151
# File 'lib/partialruby.rb', line 149

def ruby_emul_colon2(tree)
  "#{emul tree[1]}::#{tree[2]}"
end

#ruby_emul_colon3(tree) ⇒ Object



145
146
147
# File 'lib/partialruby.rb', line 145

def ruby_emul_colon3(tree)
  "::" + tree[1].to_s
end

#ruby_emul_const(tree) ⇒ Object



141
142
143
# File 'lib/partialruby.rb', line 141

def ruby_emul_const(tree)
  tree[1].to_s
end

#ruby_emul_defn(tree) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/partialruby.rb', line 180

def ruby_emul_defn(tree)
  method_name = tree[1]
  args = tree[2]
  impl = tree[3][1]

  "def #{method_name}(#{args[1..-1].map(&:to_s).join(",")})
      #{emul impl}
    end
  "
end

#ruby_emul_defs(tree) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/partialruby.rb', line 171

def ruby_emul_defs(tree)
  methodname = tree[2]
  args = tree[3]
  "def (#{emul tree[1]}).#{methodname}(#{args[1..-1].map(&:to_s).join(",")})
    #{emul tree[4]}
  end
  "
end

#ruby_emul_dstr(tree) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/partialruby.rb', line 213

def ruby_emul_dstr(tree)
  firststr = tree[1]
  retstr = firststr
  tree[2..-1].each do |subtree|

    subtreetype = subtree[0]

    if subtreetype == :evstr
      retstr << "\#{(#{emul subtree[1]})}"
    else
      retstr << "\#{(#{emul subtree})}"
    end
  end

  '"' + retstr + '"'
end

#ruby_emul_dxstr(tree) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/partialruby.rb', line 335

def ruby_emul_dxstr(tree)
  firststr = tree[1]
  retstr = firststr
  tree[2..-1].each do |subtree|

    subtreetype = subtree[0]

    if subtreetype == :evstr
      retstr << "\#{(#{emul subtree[1]})}"
    else
      retstr << "\#{(#{emul subtree})}"
    end
  end

  '`' + retstr + '`'
end

#ruby_emul_ensure(tree) ⇒ Object



306
307
308
309
310
311
312
313
# File 'lib/partialruby.rb', line 306

def ruby_emul_ensure(tree)
  "begin;
     #{emul tree[1]}
  ensure;
    #{emul tree[2] }
  end;
  "
end

#ruby_emul_false(tree) ⇒ Object



242
243
244
# File 'lib/partialruby.rb', line 242

def ruby_emul_false(tree)
  "false"
end

#ruby_emul_gasgn(tree) ⇒ Object



327
328
329
# File 'lib/partialruby.rb', line 327

def ruby_emul_gasgn(tree)
  "(#{tree[1].to_s} = (#{emul tree[2]}))"
end

#ruby_emul_gvar(tree) ⇒ Object



323
324
325
# File 'lib/partialruby.rb', line 323

def ruby_emul_gvar(tree)
  "(" + tree[1].to_s + ")"
end

#ruby_emul_hash(tree) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/partialruby.rb', line 113

def ruby_emul_hash(tree)
  pairs = Array.new
  (0..((tree.size - 1) / 2)-1).each do |i|
    pairs << [ tree[i*2+1], tree[i*2+2] ]
  end

  "{" + pairs.map{|pair| "(#{emul pair.first})=>(#{emul pair.last} )" }.join(",") + "}"
end

#ruby_emul_iasgn(tree) ⇒ Object



319
320
321
# File 'lib/partialruby.rb', line 319

def ruby_emul_iasgn(tree)
  "(#{tree[1].to_s} = (#{emul tree[2]}))"
end

#ruby_emul_if(tree) ⇒ Object



246
247
248
# File 'lib/partialruby.rb', line 246

def ruby_emul_if(tree)
  "if (#{emul tree[1]}); (#{emul tree[2]}) else (#{emul tree[3]}) end"
end

#ruby_emul_iter(tree) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/partialruby.rb', line 268

def ruby_emul_iter(tree)
  callnode = tree[1]
  innernode = tree[3]

  arguments = tree[2]
  argumentsstr = ""

  if arguments
    argumentsstr = "|" + process_args(arguments) + "|"
  end
  "#{emul callnode} { #{argumentsstr} #{emul innernode} }"
end

#ruby_emul_ivar(tree) ⇒ Object



315
316
317
# File 'lib/partialruby.rb', line 315

def ruby_emul_ivar(tree)
  "(" + tree[1].to_s + ")"
end

#ruby_emul_lasgn(tree) ⇒ Object

end



131
132
133
134
# File 'lib/partialruby.rb', line 131

def ruby_emul_lasgn(tree)
  varname = tree[1]
  "#{varname} = ( #{emul(tree[2])} );"
end

#ruby_emul_lit(tree) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/partialruby.rb', line 191

def ruby_emul_lit(tree)
  obj = tree[1]
  if obj.instance_of? Symbol or obj.instance_of? Fixnum or obj == nil or obj == true or obj == false
  "(#{obj.inspect})"
  else
  "(#{object_ref tree[1]})"
  end
end

#ruby_emul_lvar(tree) ⇒ Object



136
137
138
139
# File 'lib/partialruby.rb', line 136

def ruby_emul_lvar(tree)
  varname = tree[1]
  varname.to_s
end

#ruby_emul_module(tree) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/partialruby.rb', line 365

def ruby_emul_module(tree)

  if tree[1].instance_of? Symbol
    modulename = tree[1].to_s
  else
    modulename = emul tree[1]
  end

  "module #{modulename}
    #{emul tree[2]}
  end
  "
end

#ruby_emul_nil(tree) ⇒ Object

end



94
95
96
# File 'lib/partialruby.rb', line 94

def ruby_emul_nil(tree)
  "(nil)"
end

#ruby_emul_not(tree) ⇒ Object



429
430
431
# File 'lib/partialruby.rb', line 429

def ruby_emul_not(tree)
  "not (#{emul tree[1]})"
end

#ruby_emul_or(tree) ⇒ Object



425
426
427
# File 'lib/partialruby.rb', line 425

def ruby_emul_or(tree)
  "(#{emul tree[1]}) or (#{emul tree[2]})"
end

#ruby_emul_rescue(tree) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/partialruby.rb', line 379

def ruby_emul_rescue(tree)

  resbody = tree[2][2]

  strresbody = ""
  if resbody
    strresbody = emul resbody
  else
    strresbody = ""
  end

  exceptionarray = tree[2][1][1..-1]

  exceptionstrarray = []

  i = 0
  while i < exceptionarray.size
    if exceptionarray[i+1]
      if exceptionarray[i+1][0] == :lasgn
       exceptionstrarray << "(" + (emul(exceptionarray[i]) + ") => " + exceptionarray[i+1][1].to_s)
        i = i + 1
      else
        exceptionstrarray << "(" + (emul(exceptionarray[i])) + ")"
      end
    else
      exceptionstrarray << "(" + (emul(exceptionarray[i])) + ")"
    end
    i = i + 1
  end

  "begin;
    #{emul tree[1]}
  rescue #{exceptionstrarray.join(",")};
    #{strresbody}
  end;
  "
end

#ruby_emul_return(tree) ⇒ Object



417
418
419
# File 'lib/partialruby.rb', line 417

def ruby_emul_return(tree)
  "; return #{emul tree[1]}; "
end

#ruby_emul_scope(tree) ⇒ Object



98
99
100
# File 'lib/partialruby.rb', line 98

def ruby_emul_scope(tree)
  emul tree[1]
end

#ruby_emul_self(tree) ⇒ Object



234
235
236
# File 'lib/partialruby.rb', line 234

def ruby_emul_self(tree)
  "(self)"
end

#ruby_emul_splat(tree) ⇒ Object



302
303
304
# File 'lib/partialruby.rb', line 302

def ruby_emul_splat(tree)
  "*(#{emul(tree[1])})"
end

#ruby_emul_str(tree) ⇒ Object



200
201
202
# File 'lib/partialruby.rb', line 200

def ruby_emul_str(tree)
  "(#{tree[1].inspect})"
end

#ruby_emul_true(tree) ⇒ Object



238
239
240
# File 'lib/partialruby.rb', line 238

def ruby_emul_true(tree)
  "true"
end

#ruby_emul_until(tree) ⇒ Object



281
282
283
# File 'lib/partialruby.rb', line 281

def ruby_emul_until(tree)
  "until (#{emul tree[1]}); (#{emul tree[2]}); end "
end

#ruby_emul_while(tree) ⇒ Object



250
251
252
# File 'lib/partialruby.rb', line 250

def ruby_emul_while(tree)
  "while (#{emul tree[1]}); (#{emul tree[2]}); end "
end

#ruby_emul_xstr(tree) ⇒ Object



331
332
333
# File 'lib/partialruby.rb', line 331

def ruby_emul_xstr(tree)
  "`#{tree[1].gsub("`","")}`"
end

#ruby_emul_yield(tree) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/partialruby.rb', line 352

def ruby_emul_yield(tree)

  strargs = tree[1..-1].map{ |subtree|
    if subtree[0] == :splat
      "*(" + emul(subtree[1]) + ")"
    else
      "(" + emul(subtree) + ")"
    end
  }.join(",")

  "(yield(#{strargs}))"
end