Class: CodeTools::AST::MultipleAssignment

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

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, 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, left, right, splat) ⇒ MultipleAssignment

Returns a new instance of MultipleAssignment.



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/rubinius/code/ast/variables.rb', line 544

def initialize(line, left, right, splat)
  @line = line
  @left = left
  @right = right
  @splat = nil
  @block = nil # support for |&b|
  @post = nil # in `a,*b,c`, c is in post.

  if splat.kind_of?(PostArg)
    @fixed = false
    @post = splat.rest
    splat = splat.into
  elsif right.kind_of?(ArrayLiteral)
    @fixed = right.body.size > 1
  else
    @fixed = false
  end

  if splat.kind_of? Node
    if @left
      if right
        @splat = SplatAssignment.new line, splat
      else
        @splat = SplatWrapped.new line, splat
      end
    elsif @fixed
      @splat = SplatArray.new line, splat, right.body.size
    elsif right.kind_of? SplatValue
      @splat = splat
    else
      @splat = SplatWrapped.new line, splat
    end
  elsif splat
    # We need a node for eg { |*| } and { |a, *| }
    size = @fixed ? right.body.size : 0
    @splat = EmptySplat.new line, size
  end
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



542
543
544
# File 'lib/rubinius/code/ast/variables.rb', line 542

def block
  @block
end

#leftObject

Returns the value of attribute left.



542
543
544
# File 'lib/rubinius/code/ast/variables.rb', line 542

def left
  @left
end

#postObject

Returns the value of attribute post.



542
543
544
# File 'lib/rubinius/code/ast/variables.rb', line 542

def post
  @post
end

#rightObject

Returns the value of attribute right.



542
543
544
# File 'lib/rubinius/code/ast/variables.rb', line 542

def right
  @right
end

#splatObject

Returns the value of attribute splat.



542
543
544
# File 'lib/rubinius/code/ast/variables.rb', line 542

def splat
  @splat
end

Instance Method Details

#assign_values(g, array, index) ⇒ Object



800
801
802
803
804
805
806
807
# File 'lib/rubinius/code/ast/variables.rb', line 800

def assign_values(g, array, index)
  array.body.each do |x|
    get_element(g, index)
    g.dup if x.kind_of? MultipleAssignment
    x.bytecode(g)
    g.pop
  end
end

#bytecode(g, array_on_stack = false) ⇒ Object



610
611
612
613
614
615
616
617
618
619
620
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
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
# File 'lib/rubinius/code/ast/variables.rb', line 610

def bytecode(g, array_on_stack=false)
  declare_local_scope(g.state.scope)

  case @right
  when ArrayLiteral, SplatValue
    @right.bytecode(g)
    g.dup
  when ToArray
    @right.value.bytecode(g)
    g.dup
    convert_to_ary(g)
  when nil
    convert_to_ary(g)
  else
    @right.bytecode(g)
    g.dup
    convert_to_ary(g)
  end

  size = g.new_stack_local
  g.dup
  g.send :size, 0, true
  g.set_stack_local size
  g.pop

  index = g.new_stack_local
  g.push_int 0
  g.set_stack_local index
  g.pop

  g.state.push_masgn

  assign_values g, @left, index if @left

  if @splat
    g.dup
    g.push_stack_local index

    check_count = g.new_label

    if @post
      g.push_stack_local size
      g.push_int @post.body.size
      g.send :-, 1, true

      g.push_stack_local index
      g.send :-, 1, true

      g.goto check_count
    else
      g.push_stack_local size
      g.push_stack_local index
      g.send :-, 1, true

      g.goto check_count
    end

    underflow = g.new_label
    assign_splat = g.new_label

    underflow.set!
    g.pop_many 3
    g.make_array 0

    g.goto assign_splat

    check_count.set!
    g.dup
    g.push_int 0
    g.send :<, 1, true
    g.goto_if_true underflow

    g.dup
    g.push_stack_local index
    g.send :+, 1, true
    g.set_stack_local index
    g.pop

    g.send :[], 2, true

    assign_splat.set!

    # TODO: Fix nodes to work correctly.
    case @splat
    when EmptySplat
      # nothing
    when SplatArray, SplatWrapped
      @splat.value.bytecode(g)
    else
      @splat.bytecode(g)
    end
    g.pop
  end

  assign_values g, @post, index if @post

  g.state.pop_masgn
  g.pop
end

#convert_to_ary(g) ⇒ Object



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
# File 'lib/rubinius/code/ast/variables.rb', line 710

def convert_to_ary(g)
  done = g.new_label
  coerce = g.new_label
  make_array = g.new_label
  dup_as_array = g.new_label

  instance_of_array(g, done)
  kind_of_array(g, dup_as_array)

  g.dup
  g.push_literal :to_ary
  g.push_true
  g.send :respond_to?, 2, true
  g.goto_if_true coerce

  make_array.set!
  g.make_array 1
  g.goto done

  discard = g.new_label

  dup_as_array.set!
  g.dup
  g.push_rubinius
  g.find_const :Runtime
  g.swap
  g.send :dup_as_array, 1, true
  g.goto discard

  coerce.set!
  g.dup
  g.send :to_ary, 0, true

  check_array = g.new_label

  g.dup
  g.goto_if_not_nil check_array

  g.pop
  g.goto make_array

  check_array.set!
  kind_of_array(g, discard)

  g.push_type
  g.move_down 2
  g.push_literal :to_ary
  g.push_cpath_top
  g.find_const :Array
  g.send :coerce_to_type_error, 4, true
  g.goto done

  discard.set!
  g.swap
  g.pop

  done.set!
end

#declare_local_scope(scope) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/rubinius/code/ast/variables.rb', line 587

def declare_local_scope(scope)
  # Fix the scope for locals introduced by the left. We
  # do this before running the code for the right so that
  # right side sees the proper scoping of the locals on the left.

  if @left
    @left.body.each do |var|
      case var
      when LocalVariable
        scope.assign_local_reference var
      when MultipleAssignment
        var.declare_local_scope(scope)
      end
    end
  end

  if @splat and @splat.kind_of?(SplatAssignment)
    if @splat.value.kind_of?(LocalVariable)
      scope.assign_local_reference @splat.value
    end
  end
end

#defined(g) ⇒ Object



809
810
811
# File 'lib/rubinius/code/ast/variables.rb', line 809

def defined(g)
  g.push_literal "assignment"
end

#get_element(g, index) ⇒ Object



787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/rubinius/code/ast/variables.rb', line 787

def get_element(g, index)
  g.dup
  g.push_stack_local index

  g.dup
  g.push_int 1
  g.send :+, 1, true
  g.set_stack_local index
  g.pop

  g.send :[], 1, true
end

#instance_of_array(g, label) ⇒ Object



769
770
771
772
773
774
775
776
# File 'lib/rubinius/code/ast/variables.rb', line 769

def instance_of_array(g, label)
  g.dup
  g.push_cpath_top
  g.find_const :Array
  g.swap
  g.instance_of
  g.goto_if_true label
end

#iter_argumentsObject



583
584
585
# File 'lib/rubinius/code/ast/variables.rb', line 583

def iter_arguments
  @iter_arguments = true
end

#kind_of_array(g, label) ⇒ Object



778
779
780
781
782
783
784
785
# File 'lib/rubinius/code/ast/variables.rb', line 778

def kind_of_array(g, label)
  g.dup
  g.push_cpath_top
  g.find_const :Array
  g.swap
  g.kind_of
  g.goto_if_true label
end

#to_sexpObject



813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'lib/rubinius/code/ast/variables.rb', line 813

def to_sexp
  left = @left ? @left.to_sexp : [:array]
  case @splat
  when EmptySplat
    left << [:splat]
  when nil
  else
    left << [:splat, @splat.to_sexp]
  end
  left << @block.to_sexp if @block

  sexp = [:masgn, left]
  sexp += @post.body.map { |x| x.to_sexp } if @post
  sexp << @right.to_sexp if @right
  sexp
end