Class: CodeTools::AST::OpAssignElement

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

Returns a new instance of OpAssignElement.



108
109
110
111
112
113
114
115
# File 'lib/rubinius/code/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 = Arguments.new line, arguments
  @value = value
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

#opObject

Returns the value of attribute op.



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

def op
  @op
end

#receiverObject

Returns the value of attribute receiver.



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

def receiver
  @receiver
end

#valueObject

Returns the value of attribute value.



106
107
108
# File 'lib/rubinius/code/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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rubinius/code/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
    assign = g.new_label

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

    g.dup
    if @op == :or
      g.goto_if_true fnd
    else
      g.goto_if_false 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

    old_break = g.break
    new_break = g.new_label
    g.break = new_break

    @value.bytecode(g)

    g.goto assign

    new_break.set!
    if old_break
      g.pop_many recv_stack + 1
      g.push_nil
      g.goto old_break
    end

    g.break = old_break

    assign.set!

    # 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
    assign = g.new_label

    old_break = g.break
    new_break = g.new_label
    g.break = new_break

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

    g.goto assign

    new_break.set!
    if old_break
      g.pop_many recv_stack + 2
      g.push_nil
      g.goto old_break
    end

    g.break = old_break

    assign.set!

    # ... 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



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/rubinius/code/ast/operators.rb', line 259

def to_sexp
  arguments = [:arglist]

  case @arguments
  when PushArguments
    arguments << @arguments.to_sexp
  else
    arguments += @arguments.to_sexp
  end

  case @op
  when :or
    op = :"||"
  when :and
    op = :"&&"
  else
    op = @op
  end
  [:op_asgn1, @receiver.to_sexp, arguments, op, @value.to_sexp]
end