Class: Delorean::ListComprehension

Inherits:
SNode
  • Object
show all
Defined in:
lib/delorean/nodes.rb

Direct Known Subclasses

SetComprehension

Instance Method Summary collapse

Instance Method Details

#check(context) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/delorean/nodes.rb', line 442

def check(context, *)
  unpack_vars = args.check(context)
  e1c = e1.check(context)
  unpack_vars.each {|vname| context.parse_define_var(vname)}

  # need to check e2/e3 in a context where the comprehension var
  # is defined.
  e2c = e2.check(context)
  e3c = defined?(ifexp.e3) ? ifexp.e3.check(context) : []

  unpack_vars.each {
    |vname|
    context.parse_undef_var(vname)
    e2c.delete(vname)
    e3c.delete(vname)
  }

  e1c + e2c + e3c
end

#rewrite(context) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/delorean/nodes.rb', line 462

def rewrite(context)
  res = ["(#{e1.rewrite(context)})"]
  unpack_vars = args.check(context)
  unpack_vars.each {|vname| context.parse_define_var(vname)}
  args_str = args.rewrite(context)

  res << ".select{|#{args_str}|(#{ifexp.e3.rewrite(context)})}" if
    defined?(ifexp.e3)
  res << ".map{|#{args_str}| (#{e2.rewrite(context)}) }"
  unpack_vars.each {|vname| context.parse_undef_var(vname)}
  res.sum
end