Class: Dhallish::Ast::ComparisonOpNode

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

Overview

Ast-Node that takes two Ints, Naturals, Bools or Texts and returns a Boolean

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lhs, rhs, op, &block) ⇒ ComparisonOpNode

Returns a new instance of ComparisonOpNode.



45
46
47
48
49
50
# File 'lib/ast.rb', line 45

def initialize(lhs, rhs, op, &block)
	@block = block
	@op = op
	@lhs = lhs
	@rhs = rhs
end

Instance Attribute Details

#lhsObject

Returns the value of attribute lhs.



41
42
43
# File 'lib/ast.rb', line 41

def lhs
  @lhs
end

#opObject

Returns the value of attribute op.



43
44
45
# File 'lib/ast.rb', line 43

def op
  @op
end

#rhsObject

Returns the value of attribute rhs.



42
43
44
# File 'lib/ast.rb', line 42

def rhs
  @rhs
end

Instance Method Details

#compute_type(ctx) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ast.rb', line 52

def compute_type(ctx)
	lhs_type, _ = @lhs.compute_type(ctx)
	rhs_type, _ = @rhs.compute_type(ctx)
	case @op
	when "<", "<=" ">", ">="
		assert ("operator \"#{@op}\" expects two numbers as operators") { (Types::Numbers + [Types::Text]).include? lhs_type }
	when "==", "!="
		assert ("operator \"#{@op}\" bad operator type") { (Types::Numbers + [Types::Text, Types::Bool]).include? lhs_type }
	end
	assert ("operator \"#{@op}\" expects two operators of same Type. left: #{lhs_type}, right: #{rhs_type}") { lhs_type == rhs_type }
	Types::Bool

end

#evaluate(ctx) ⇒ Object



66
67
68
69
70
# File 'lib/ast.rb', line 66

def evaluate(ctx)
	lhs = @lhs.evaluate ctx
	rhs = @rhs.evaluate ctx
	@block.call lhs, rhs
end