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



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/delorean/nodes.rb', line 504

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 do |vname|
    context.parse_undef_var(vname)
    e2c.delete(vname)
    e3c.delete(vname)
  end

  e1c + e2c + e3c
end

#rewrite(context) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/delorean/nodes.rb', line 523

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