Class: Rubinius::ToolSet.current::TS::AST::OpAssign1

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/ast/operators.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, receiver, arguments, op, value) ⇒ OpAssign1

Returns a new instance of OpAssign1.



108
109
110
111
112
113
114
115
# File 'lib/rubinius/ast/operators.rb', line 108

def initialize(line, receiver, arguments, op, value)
  @line = line
  @receiver = receiver
  @op = op
  arguments = nil if arguments.is_a?(EmptyArray)
  @arguments = ActualArguments.new line, arguments
  @value = value
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



106
107
108
# File 'lib/rubinius/ast/operators.rb', line 106

def arguments
  @arguments
end

#opObject

Returns the value of attribute op.



106
107
108
# File 'lib/rubinius/ast/operators.rb', line 106

def op
  @op
end

#receiverObject

Returns the value of attribute receiver.



106
107
108
# File 'lib/rubinius/ast/operators.rb', line 106

def receiver
  @receiver
end

#valueObject

Returns the value of attribute value.



106
107
108
# File 'lib/rubinius/ast/operators.rb', line 106

def value
  @value
end

Instance Method Details

#bytecode(g) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rubinius/ast/operators.rb', line 117

def bytecode(g)
  pos(g)

  # X: Snippet used for explanation: h[:a] += 3
  # X: given h = { :a => 2 }
  # X: Pull h onto the stack
  @receiver.bytecode(g)
  # X: Pull :a in
  @arguments.bytecode(g)
  recv_stack = @arguments.stack_size + 1

  # Dup the receiver and arguments to use later
  g.dup_many recv_stack

  #
  # X: Call [](:a) on h
  #
  # @arguments.size will be 1

  if @arguments.splat?
    g.push :nil
    g.send_with_splat :[], @arguments.size
  else
    g.send :[], @arguments.size
  end

  # X: 2 is now on the top of the stack (TOS)

  # A special case, where we use the value as boolean
  if @op == :or or @op == :and
    fnd = g.new_label
    fin = g.new_label

    # We dup the value from [] to leave it as the value of the
    # expression

    g.dup
    if @op == :or
      g.git fnd
    else
      g.gif fnd
    end

    # Ok, take the extra copy off and pull the value onto the stack
    g.pop

    # The receiver and arguments are still on the stack

    @value.bytecode(g)

    # retain the rhs as the expression value
    g.dup
    g.move_down recv_stack + 1

    if @arguments.splat?
      g.send :push, 1
      g.push :nil
      g.send_with_splat :[]=, @arguments.size
    else
      g.send :[]=, @arguments.size + 1
    end
    g.pop

    # Leaves the value we moved down the stack on the top
    g.goto fin

    fnd.set!

    # Clean up the stack but retain return value from :[]
    g.move_down recv_stack
    g.pop_many recv_stack

    fin.set!
  else
    # @op is something like + or -
    # We pull in @value to the stack
    @value.bytecode(g)
    # X: 3 TOS

    # ... then call it as an argument to @or, called on the return
    # from [].
    # X: 2 + 3

    g.send @op, 1
    # X: 5 TOS

    # The new value is on the stack now. It is the last argument to the call
    # to []= because your dupd versions of recv and arguments are still on the stack.

    # retain the rhs as the expression value
    g.dup
    g.move_down recv_stack + 1

    # X: Call []=(:a, 5) on h
    if @arguments.splat?
      g.send :push, 1
      g.push :nil
      g.send_with_splat :[]=, @arguments.size
    else
      g.send :[]=, @arguments.size + 1
    end
    g.pop
  end
end

#to_sexpObject



222
223
224
225
226
# File 'lib/rubinius/ast/operators.rb', line 222

def to_sexp
  arguments = [:arglist] + @arguments.to_sexp
  op = @op == :or ? :"||" : :"&&"
  [:op_asgn1, @receiver.to_sexp, arguments, op, @value.to_sexp]
end