class Dhallish::Ast::ComparisonOpNode

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

Attributes

lhs[RW]
op[RW]
rhs[RW]

Public Class Methods

new(lhs, rhs, op, &block) click to toggle source
# File lib/ast.rb, line 45
def initialize(lhs, rhs, op, &block)
        @block = block
        @op = op
        @lhs = lhs
        @rhs = rhs
end

Public Instance Methods

compute_type(ctx) click to toggle source
# 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) click to toggle source
# File lib/ast.rb, line 66
def evaluate(ctx)
        lhs = @lhs.evaluate ctx
        rhs = @rhs.evaluate ctx
        @block.call lhs, rhs
end