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



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/delorean/nodes.rb', line 703

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



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

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