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



733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/delorean/nodes.rb', line 733

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



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/delorean/nodes.rb', line 753

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