Class: Delorean::HashComprehension

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

Constant Summary collapse

@@comp_count =

used in generating unique hash names

0

Instance Method Summary collapse

Instance Method Details

#check(context) ⇒ Object



775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/delorean/nodes.rb', line 775

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

  # need to check el/er/ei in a context where the comprehension var
  # is defined.
  elc = el.check(context)
  erc = er.check(context)
  eic = defined?(ifexp.ei) ? ifexp.ei.check(context) : []

  unpack_vars.each do |vname|
    context.parse_undef_var(vname)
    elc.delete(vname)
    erc.delete(vname)
    eic.delete(vname)
  end
  e1c + elc + erc + eic
end

#rewrite(context) ⇒ Object



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/delorean/nodes.rb', line 795

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)

  hid = @@comp_count += 1

  res << ".select{|#{args_str}| (#{ifexp.ei.rewrite(context)}) }" if
    defined?(ifexp.ei)

  unpack_str = unpack_vars.count > 1 ? "(#{args_str})" : args_str

  res << ".each_with_object({}){|#{unpack_str}, _h#{hid}| " \
         "_h#{hid}[#{el.rewrite(context)}]=(#{er.rewrite(context)})}"

  unpack_vars.each { |vname| context.parse_undef_var(vname) }
  res.sum
end