Class: TPPlus::Nodes::WhileNode
- Inherits:
-
Object
- Object
- TPPlus::Nodes::WhileNode
- Defined in:
- lib/tp_plus/nodes/while_node.rb
Instance Method Summary collapse
- #block(context) ⇒ Object
- #bottom_label(context) ⇒ Object
- #condition(context) ⇒ Object
- #eval(context) ⇒ Object
- #if_statement(context) ⇒ Object
-
#initialize(condition_node, block) ⇒ WhileNode
constructor
A new instance of WhileNode.
- #top_label(context) ⇒ Object
Constructor Details
#initialize(condition_node, block) ⇒ WhileNode
Returns a new instance of WhileNode.
4 5 6 7 |
# File 'lib/tp_plus/nodes/while_node.rb', line 4 def initialize(condition_node, block) @condition_node = condition_node @block = block.flatten.reject {|n| n.is_a?(TerminatorNode) } end |
Instance Method Details
#block(context) ⇒ Object
27 28 29 |
# File 'lib/tp_plus/nodes/while_node.rb', line 27 def block(context) @block.inject("") {|s,n| s << "#{n.eval(context)} ;\n" } end |
#bottom_label(context) ⇒ Object
13 14 15 |
# File 'lib/tp_plus/nodes/while_node.rb', line 13 def bottom_label(context) @bottom_label ||= context.next_label end |
#condition(context) ⇒ Object
22 23 24 |
# File 'lib/tp_plus/nodes/while_node.rb', line 22 def condition(context) @condition_node.eval(context, opposite: true, as_condition: true) end |
#eval(context) ⇒ Object
31 32 33 |
# File 'lib/tp_plus/nodes/while_node.rb', line 31 def eval(context) "LBL[#{top_label(context)}] ;\n#{if_statement(context)}#{block(context)}JMP LBL[#{top_label(context)}] ;\nLBL[#{bottom_label(context)}]" end |
#if_statement(context) ⇒ Object
17 18 19 |
# File 'lib/tp_plus/nodes/while_node.rb', line 17 def if_statement(context) "IF #{condition(context)},JMP LBL[#{bottom_label(context)}] ;\n" end |
#top_label(context) ⇒ Object
9 10 11 |
# File 'lib/tp_plus/nodes/while_node.rb', line 9 def top_label(context) @top_label ||= context.next_label end |