Class: CodeTools::AST::While

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

Direct Known Subclasses

Until

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, condition, body, check_first) ⇒ While

Returns a new instance of While.



351
352
353
354
355
356
# File 'lib/rubinius/code/ast/control_flow.rb', line 351

def initialize(line, condition, body, check_first)
  @line = line
  @condition = condition
  @body = body || NilLiteral.new(line)
  @check_first = check_first
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



349
350
351
# File 'lib/rubinius/code/ast/control_flow.rb', line 349

def body
  @body
end

#check_firstObject

Returns the value of attribute check_first.



349
350
351
# File 'lib/rubinius/code/ast/control_flow.rb', line 349

def check_first
  @check_first
end

#conditionObject

Returns the value of attribute condition.



349
350
351
# File 'lib/rubinius/code/ast/control_flow.rb', line 349

def condition
  @condition
end

Instance Method Details

#body_bytecode(g, lbl) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
# File 'lib/rubinius/code/ast/control_flow.rb', line 367

def body_bytecode(g, lbl)
  g.state.push_loop
  @body.bytecode(g)
  g.state.pop_loop

  # This is a loop epilogue. Nothing that changes
  # computation should be put here.
  lbl.set!
  g.pop
  g.check_interrupts
end

#bytecode(g, use_gif = true) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/rubinius/code/ast/control_flow.rb', line 379

def bytecode(g, use_gif=true)
  pos(g)

  g.push_modifiers

  top = g.new_label
  post = g.next = g.new_label
  bottom = g.new_label

  g.break = g.new_label

  if @check_first
    g.redo = g.new_label

    top.set!
    condition_bytecode(g, bottom, use_gif)

    g.redo.set!
    body_bytecode(g, post)
  else
    g.redo = top

    top.set!
    body_bytecode(g, post)

    condition_bytecode(g, bottom, use_gif)
  end

  g.goto top

  # See other set_line(0) comments
  g.set_line 0

  bottom.set!
  g.push_nil
  g.break.set!

  g.pop_modifiers
end

#condition_bytecode(g, bottom, use_gif) ⇒ Object



358
359
360
361
362
363
364
365
# File 'lib/rubinius/code/ast/control_flow.rb', line 358

def condition_bytecode(g, bottom, use_gif)
  @condition.bytecode(g)
  if use_gif
    g.goto_if_false bottom
  else
    g.goto_if_true bottom
  end
end

#defined(g) ⇒ Object



419
420
421
# File 'lib/rubinius/code/ast/control_flow.rb', line 419

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

#sexp_nameObject



423
424
425
# File 'lib/rubinius/code/ast/control_flow.rb', line 423

def sexp_name
  :while
end

#to_sexpObject



427
428
429
# File 'lib/rubinius/code/ast/control_flow.rb', line 427

def to_sexp
  [sexp_name, @condition.to_sexp, @body.to_sexp, @check_first]
end