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



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/delorean/nodes.rb', line 684

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



703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/delorean/nodes.rb', line 703

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