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.



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/rubinius/code/ast/definitions.rb', line 621

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.



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

def arguments
  @arguments
end

#defaultsObject

Returns the value of attribute defaults.



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

def defaults
  @defaults
end

#kwrestObject

Returns the value of attribute kwrest.



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

def kwrest
  @kwrest
end

#namesObject

Returns the value of attribute names.



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

def names
  @names
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#bytecode(g) ⇒ Object



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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/rubinius/code/ast/definitions.rb', line 675

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



662
663
664
665
666
667
# File 'lib/rubinius/code/ast/definitions.rb', line 662

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

#map_arguments(scope) ⇒ Object



669
670
671
672
673
# File 'lib/rubinius/code/ast/definitions.rb', line 669

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

  scope.assign_local_reference @kwrest if @kwrest
end

#required?Boolean

Returns:

  • (Boolean)


658
659
660
# File 'lib/rubinius/code/ast/definitions.rb', line 658

def required?
  not @required.empty?
end

#to_sexpObject



781
782
783
784
785
786
# File 'lib/rubinius/code/ast/definitions.rb', line 781

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