Class: Code::Node::While
- Inherits:
-
Code::Node
- Object
- Code::Node
- Code::Node::While
- Defined in:
- lib/code/node/while.rb
Constant Summary collapse
- WHILE_KEYWORD =
"while"
- UNTIL_KEYWORD =
"until"
- LOOP_KEYWORD =
"loop"
Instance Method Summary collapse
- #evaluate(**args) ⇒ Object
-
#initialize(parsed) ⇒ While
constructor
A new instance of While.
Methods inherited from Code::Node
Constructor Details
#initialize(parsed) ⇒ While
Returns a new instance of While.
10 11 12 13 14 15 16 17 18 |
# File 'lib/code/node/while.rb', line 10 def initialize(parsed) return if parsed.blank? @operator = parsed.delete(:operator).presence @statement = Statement.new(parsed.delete(:statement)) if parsed.key?( :statement ) @body = Code.new(parsed.delete(:body).presence) end |
Instance Method Details
#evaluate(**args) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/code/node/while.rb', line 20 def evaluate(**args) case @operator when WHILE_KEYWORD last = Object::Nothing.new last = @body&.evaluate(**args) || Object::Nothing.new while ( @statement&.evaluate(**args) || Object::Nothing.new ).truthy? last when UNTIL_KEYWORD last = Object::Nothing.new last = @body&.evaluate(**args) || Object::Nothing.new while ( @statement&.evaluate(**args) || Object::Nothing.new ).falsy? last when LOOP_KEYWORD loop { @body&.evaluate(**args) || Object::Nothing.new } Object::Nothing.new else Object::Nothing.new end rescue Error::Break => e e.code_value end |