Class: Pione::Lang::IfBranch

Inherits:
ConditionalBranch show all
Defined in:
lib/pione/lang/conditional-branch.rb

Overview

IfBranch is a class for +if+ branches.

Instance Method Summary collapse

Methods included from Util::Positionable

#line_and_column, #pos, #set_source_position

Instance Method Details

#eval(env) ⇒ Object

Return suitable context in the environment.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pione/lang/conditional-branch.rb', line 22

def eval(env)
  # evaluate the condition
  res = expr.eval!(env)

  # check type of the result
  unless res.is_a?(BooleanSequence)
    raise StructuralError.new(BooleanSequence, expr.pos)
  end

  # return true_context when it is true
  return true_context if res.value

  # or return else_context when the context exists
  return else_context if else_context

  # otherwise return empty context
  return ConditionalBranchContext.new
end

#eval!(env) ⇒ Object



41
42
43
# File 'lib/pione/lang/conditional-branch.rb', line 41

def eval!(env)
  eval(env).eval!(env)
end

#validate(acceptances) ⇒ Object

Validate inner contexts based on the list of acceptances.



16
17
18
19
# File 'lib/pione/lang/conditional-branch.rb', line 16

def validate(acceptances)
  true_context.validate(acceptances)
  else_context.validate(acceptances)
end