Class: CodeTools::AST::KeywordParameters

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/code/ast/definitions.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(line, block, kwrest) ⇒ KeywordParameters

Returns a new instance of KeywordParameters.



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/rubinius/code/ast/definitions.rb', line 582

def initialize(line, block, kwrest)
  @line = line
  @kwrest = kwrest
  @required = []
  @defaults = []

  if block
    array = block.array
    @names = array.map { |a| a.name }

    array.each do |arg|
      if arg.value.kind_of?(SymbolLiteral) and arg.value.value == :*
        @required << arg
      else
        @defaults << arg
      end
    end

    @arguments = array
  else
    @arguments = []
    @names = []
  end

  if kwrest
    if kwrest.to_s.start_with?("_:")
      kwrest_name = :**
    else
      kwrest_name = :"**#{kwrest}"
    end

    @kwrest = LocalVariableAssignment.new line, kwrest
  end

  @names << kwrest_name if kwrest_name
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



580
581
582
# File 'lib/rubinius/code/ast/definitions.rb', line 580

def arguments
  @arguments
end

#defaultsObject

Returns the value of attribute defaults.



580
581
582
# File 'lib/rubinius/code/ast/definitions.rb', line 580

def defaults
  @defaults
end

#kwrestObject

Returns the value of attribute kwrest.



580
581
582
# File 'lib/rubinius/code/ast/definitions.rb', line 580

def kwrest
  @kwrest
end

#namesObject

Returns the value of attribute names.



580
581
582
# File 'lib/rubinius/code/ast/definitions.rb', line 580

def names
  @names
end

#valueObject

Returns the value of attribute value.



580
581
582
# File 'lib/rubinius/code/ast/definitions.rb', line 580

def value
  @value
end

Instance Method Details

#bytecode(g) ⇒ Object



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/rubinius/code/ast/definitions.rb', line 636

def bytecode(g)
  done = g.new_label
  assignments = g.new_label
  missing_value = g.new_label

  @value.bytecode(g)

  g.dup
  g.goto_if_not_nil assignments

  g.pop
  g.push_cpath_top
  g.find_const :Hash
  g.send :allocate, 0, true

  assignments.set!

  @required.each do |arg|
    g.dup
    g.push_literal arg.name
    g.send :find_item, 1, true

    g.dup
    g.goto_if_false missing_value

    g.send :value, 0, true

    arg.variable.set_bytecode(g)
    g.pop
  end

  defaults = g.new_label
  g.goto defaults

  missing_value.set!
  g.pop
  g.push_rubinius
  g.find_const :Runtime
  g.swap
  g.send :keywords_missing, 1, true
  g.goto done

  defaults.set!

  extra_keys = g.new_label

  if @defaults.empty?
    g.dup
    g.send :size, 0, true
    g.push_int @arguments.size
    g.goto_if_not_equal extra_keys

    if @kwrest
      g.push_cpath_top
      g.find_const :Hash
      g.send :allocate, 0, true
      @kwrest.variable.set_bytecode(g)
      g.pop
    end

    g.goto done
  else
    @defaults.each do |arg|
      next_value = g.new_label
      default_value = g.new_label

      g.dup
      g.push_literal arg.name
      g.send :find_item, 1, true

      g.dup
      g.goto_if_false default_value

      g.send :value, 0, true
      arg.variable.set_bytecode(g)
      g.goto next_value

      default_value.set!
      g.pop
      arg.bytecode(g)

      next_value.set!
      g.pop
    end
  end

  extra_keys.set!

  g.dup
  g.push_rubinius
  g.find_const :Runtime
  g.swap

  if @kwrest
    g.push_true
  else
    g.push_false
  end

  g.send :keywords_extra, 2, true
  @kwrest.variable.set_bytecode(g) if @kwrest
  g.pop

  done.set!
end

#entriesObject



623
624
625
626
627
628
# File 'lib/rubinius/code/ast/definitions.rb', line 623

def entries
  entries = []
  @required.each { |arg| entries << arg.name << true }
  @defaults.each { |arg| entries << arg.name << false }
  entries
end

#map_arguments(scope) ⇒ Object



630
631
632
633
634
# File 'lib/rubinius/code/ast/definitions.rb', line 630

def map_arguments(scope)
  @arguments.each { |var| scope.assign_local_reference var }

  scope.assign_local_reference @kwrest if @kwrest
end

#required?Boolean

Returns:

  • (Boolean)


619
620
621
# File 'lib/rubinius/code/ast/definitions.rb', line 619

def required?
  not @required.empty?
end

#to_sexpObject



742
743
744
745
746
747
# File 'lib/rubinius/code/ast/definitions.rb', line 742

def to_sexp
  sexp = [:kwargs]
  sexp << @names unless @names.empty?
  sexp << @defaults.map { |x| x.to_sexp } unless @defaults.empty?
  sexp
end