Class: Code::Node::While

Inherits:
Code::Node show all
Defined in:
lib/code/node/while.rb

Constant Summary collapse

WHILE_KEYWORD =
:while
UNTIL_KEYWORD =
:until

Instance Method Summary collapse

Constructor Details

#initialize(while_parsed) ⇒ While

Returns a new instance of While.



7
8
9
10
11
# File 'lib/code/node/while.rb', line 7

def initialize(while_parsed)
  @operator = while_parsed.fetch(:operator)
  @statement = ::Code::Node::Statement.new(while_parsed.fetch(:statement))
  @body = ::Code::Node::Code.new(while_parsed.fetch(:body))
end

Instance Method Details

#evaluate(**args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/code/node/while.rb', line 13

def evaluate(**args)
  if operator == WHILE_KEYWORD
    object = ::Code::Object::Nothing.new

    while @statement.evaluate(**args).truthy?
      object = @body.evaluate(**args)
    end

    object
  elsif operator == UNTIL_KEYWORD
    object = ::Code::Object::Nothing.new

    until @statement.evaluate(**args).truthy?
      object = @body.evaluate(**args)
    end

    object
  else
    raise NotImplementedError.new(operator.inspect)
  end
end