Class: CodeTools::AST::OpAssignAttribute

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

Direct Known Subclasses

AndOpAssignAttribute

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, name, op, value) ⇒ OpAssignAttribute

Returns a new instance of OpAssignAttribute.



284
285
286
287
288
289
290
291
# File 'lib/rubinius/code/ast/operators.rb', line 284

def initialize(line, receiver, name, op, value)
  @line = line
  @receiver = receiver
  @name = name
  @op = op
  @value = value
  @assign = name.to_s[-1] == ?= ? name : :"#{name}="
end

Instance Attribute Details

#assignObject

Returns the value of attribute assign.



282
283
284
# File 'lib/rubinius/code/ast/operators.rb', line 282

def assign
  @assign
end

#nameObject

Returns the value of attribute name.



282
283
284
# File 'lib/rubinius/code/ast/operators.rb', line 282

def name
  @name
end

#opObject

Returns the value of attribute op.



282
283
284
# File 'lib/rubinius/code/ast/operators.rb', line 282

def op
  @op
end

#receiverObject

Returns the value of attribute receiver.



282
283
284
# File 'lib/rubinius/code/ast/operators.rb', line 282

def receiver
  @receiver
end

#valueObject

Returns the value of attribute value.



282
283
284
# File 'lib/rubinius/code/ast/operators.rb', line 282

def value
  @value
end

Instance Method Details

#bytecode(g, anddot = false) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/rubinius/code/ast/operators.rb', line 293

def bytecode(g, anddot=false)
  pos(g)

  # X: h.a += 3, given h.a == 2
  @receiver.bytecode(g) unless anddot
  # X: TOS = h
  g.dup
  g.send @name, 0
  # X: TOS = 2

  if @op == :or or @op == :and
    fnd = g.new_label
    fin = g.new_label
    assign = g.new_label

    g.dup
    if @op == :or
      g.goto_if_true fnd
    else
      g.goto_if_false fnd
    end

    # Remove the copy of 2 and push @value on the stack
    g.pop

    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 2
      g.push_nil
      g.goto old_break
    end

    g.break = old_break

    assign.set!

    # Retain the this value to use as the expression value
    g.dup
    g.move_down 2

    # Call the assignement method, passing @value as the argument
    g.send @assign, 1
    g.pop

    g.goto fin

    fnd.set!

    # Clean up the stack
    g.swap
    g.pop

    fin.set!
  else
    assign = g.new_label

    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 3
      g.push_nil
      g.goto old_break
    end

    g.break = old_break

    assign.set!

    # X: TOS = 3
    # X: 2 + 3
    g.send @op, 1

    # Retain the this value to use as the expression value
    g.dup
    g.move_down 2
    # X: TOS = 5
    g.send @assign, 1
    # X: TOS = 5 (or whatever a=() returns)

    # Discard the methods return value
    g.pop
  end
end

#sexp_nameObject



392
393
394
# File 'lib/rubinius/code/ast/operators.rb', line 392

def sexp_name
  :op_asgn2
end

#to_sexpObject



396
397
398
399
400
401
402
403
404
405
406
# File 'lib/rubinius/code/ast/operators.rb', line 396

def to_sexp
  case @op
  when :or
    op = :"||"
  when :and
    op = :"&&"
  else
    op = @op
  end
  [sexp_name, @receiver.to_sexp, :"#{@name}=", op, @value.to_sexp]
end