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



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/delorean/nodes.rb', line 492

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 {
    |vname|
    context.parse_undef_var(vname)
    elc.delete(vname)
    erc.delete(vname)
    eic.delete(vname)
  }
  e1c + elc + erc + eic
end

#rewrite(context) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/delorean/nodes.rb', line 513

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