Class: CodeTools::AST::If

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/code/ast/control_flow.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, condition, body, else_body) ⇒ If

Returns a new instance of If.



309
310
311
312
313
314
# File 'lib/rubinius/code/ast/control_flow.rb', line 309

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



307
308
309
# File 'lib/rubinius/code/ast/control_flow.rb', line 307

def body
  @body
end

#conditionObject

Returns the value of attribute condition.



307
308
309
# File 'lib/rubinius/code/ast/control_flow.rb', line 307

def condition
  @condition
end

#elseObject

Returns the value of attribute else.



307
308
309
# File 'lib/rubinius/code/ast/control_flow.rb', line 307

def else
  @else
end

Instance Method Details

#bytecode(g) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/rubinius/code/ast/control_flow.rb', line 316

def bytecode(g)
  pos(g)

  done = g.new_label
  else_label = g.new_label

  @condition.bytecode(g)
  g.goto_if_false else_label

  @body.bytecode(g)
  g.goto done

  else_label.set!
  @else.bytecode(g)

  # Use line 0 to indicate "compiler generated code"
  # so that debuggers and such don't get confused by
  # thinking the then branch jumps into the else branch.
  g.set_line 0
  done.set!
end

#defined(g) ⇒ Object



338
339
340
# File 'lib/rubinius/code/ast/control_flow.rb', line 338

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

#to_sexpObject



342
343
344
345
# File 'lib/rubinius/code/ast/control_flow.rb', line 342

def to_sexp
  else_sexp = @else.kind_of?(NilLiteral) ? nil : @else.to_sexp
  [:if, @condition.to_sexp, @body.to_sexp, else_sexp]
end