Class: Dhallish::Ast::IfThenElseNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cond, iftrue, iffalse) ⇒ IfThenElseNode

Returns a new instance of IfThenElseNode.



77
78
79
80
81
# File 'lib/ast.rb', line 77

def initialize(cond, iftrue, iffalse)
	@cond = cond
	@iftrue = iftrue
	@iffalse = iffalse
end

Instance Attribute Details

#condObject

Returns the value of attribute cond.



74
75
76
# File 'lib/ast.rb', line 74

def cond
  @cond
end

#iffalseObject

Returns the value of attribute iffalse.



76
77
78
# File 'lib/ast.rb', line 76

def iffalse
  @iffalse
end

#iftrueObject

Returns the value of attribute iftrue.



75
76
77
# File 'lib/ast.rb', line 75

def iftrue
  @iftrue
end

Instance Method Details

#compute_type(ctx) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/ast.rb', line 83

def compute_type(ctx)
	cond_type, _ = @cond.compute_type(ctx)
	assert ("Condition in If-Then-Else-Statement not of type Bool") { cond_type == Types::Bool }
	then_type = @iftrue.compute_type(ctx)
	else_type = @iffalse.compute_type(ctx)
	assert ("If-Then-Else: expressions in both branches have to be of the same type") { then_type == else_type }
	then_type
end

#evaluate(ctx) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/ast.rb', line 92

def evaluate(ctx)
	cond = @cond.evaluate ctx
	if cond
		@iftrue.evaluate ctx
	else
		@iffalse.evaluate ctx
	end
end