Class: CodeTools::AST::KeywordParameters

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/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.



581
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
# File 'lib/rubinius/ast/definitions.rb', line 581

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

  case kwrest
  when Symbol
    kwrest_name = :"**#{kwrest}"
    @kwrest = LocalVariableAssignment.new line, kwrest
  when true
    kwrest_name = :**
    @kwrest = true
  end

  @names << kwrest_name if kwrest_name
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

#defaultsObject

Returns the value of attribute defaults.



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

def defaults
  @defaults
end

#kwrestObject

Returns the value of attribute kwrest.



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

def kwrest
  @kwrest
end

#namesObject

Returns the value of attribute names.



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

def names
  @names
end

#valueObject

Returns the value of attribute value.



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

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
741
742
# File 'lib/rubinius/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.gif 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 @arguments.size
    g.gine extra_keys

    if @kwrest.kind_of? LocalVariableAssignment
      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.gif 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
  if @kwrest.kind_of? LocalVariableAssignment
    @kwrest.variable.set_bytecode(g)
  end
  g.pop

  done.set!
end

#entriesObject



621
622
623
624
625
626
# File 'lib/rubinius/ast/definitions.rb', line 621

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

#map_arguments(scope) ⇒ Object



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

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

  if @kwrest.kind_of? LocalVariableAssignment
    scope.assign_local_reference @kwrest
  end
end

#required?Boolean

Returns:

  • (Boolean)


617
618
619
# File 'lib/rubinius/ast/definitions.rb', line 617

def required?
  not @required.empty?
end

#to_sexpObject



744
745
746
747
748
749
# File 'lib/rubinius/ast/definitions.rb', line 744

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