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.
- #parens(s, context) ⇒ Object
- #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
33 34 35 |
# File 'lib/tp_plus/nodes/while_node.rb', line 33 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
28 29 30 |
# File 'lib/tp_plus/nodes/while_node.rb', line 28 def condition(context) @condition_node.eval(context, opposite: true) end |
#eval(context) ⇒ Object
37 38 39 |
# File 'lib/tp_plus/nodes/while_node.rb', line 37 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
23 24 25 |
# File 'lib/tp_plus/nodes/while_node.rb', line 23 def if_statement(context) "IF #{parens(condition(context), context)},JMP LBL[#{bottom_label(context)}] ;\n" end |
#parens(s, context) ⇒ Object
17 18 19 20 21 |
# File 'lib/tp_plus/nodes/while_node.rb', line 17 def parens(s, context) return s unless @condition_node.requires_mixed_logic?(context) "(#{s})" 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 |