Class: Malady::AST::IfNode

Inherits:
Node
  • Object
show all
Defined in:
lib/malady/ast.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#filename, #line

Instance Method Summary collapse

Methods inherited from Node

#pos

Constructor Details

#initialize(filename, line, condition, then_branch, else_branch) ⇒ IfNode

Returns a new instance of IfNode.



145
146
147
148
149
150
# File 'lib/malady/ast.rb', line 145

def initialize(filename, line, condition, then_branch, else_branch)
  super
  @condition = condition
  @then_branch = then_branch
  @else_branch = else_branch
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



144
145
146
# File 'lib/malady/ast.rb', line 144

def condition
  @condition
end

#else_branchObject (readonly)

Returns the value of attribute else_branch.



144
145
146
# File 'lib/malady/ast.rb', line 144

def else_branch
  @else_branch
end

#then_branchObject (readonly)

Returns the value of attribute then_branch.



144
145
146
# File 'lib/malady/ast.rb', line 144

def then_branch
  @then_branch
end

Instance Method Details

#bytecode(g) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/malady/ast.rb', line 152

def bytecode(g)
  pos(g)

  end_label = g.new_label
  else_label = g.new_label

  condition.bytecode(g)
  g.goto_if_false else_label
  then_branch.bytecode(g)
  g.goto end_label

  else_label.set!
  else_branch.bytecode(g)

  end_label.set!
end