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



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/delorean/nodes.rb', line 726

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



745
746
747
748
749
750
751
752
753
754
755
756
# File 'lib/delorean/nodes.rb', line 745

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