Class: Dhallish::Ast::BinaryArithOpNode

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

Overview

Ast-Node for all operations that take two args of the same type and returns something of that type. ‘op` must be an ruby-Operator as String/Symbol

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types, lhs, rhs, op, &block) ⇒ BinaryArithOpNode

Returns a new instance of BinaryArithOpNode.



16
17
18
19
20
21
22
# File 'lib/ast.rb', line 16

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

Instance Attribute Details

#lhsObject

Returns the value of attribute lhs.



13
14
15
# File 'lib/ast.rb', line 13

def lhs
  @lhs
end

#rhsObject

Returns the value of attribute rhs.



14
15
16
# File 'lib/ast.rb', line 14

def rhs
  @rhs
end

Instance Method Details

#compute_type(ctx) ⇒ Object



24
25
26
27
28
29
# File 'lib/ast.rb', line 24

def compute_type(ctx)
  lhs_type = @lhs.compute_type(ctx)
  rhs_type = @rhs.compute_type(ctx)
  assert ("Wrong Operator types for #{@op}. left: #{lhs_type}, right: #{rhs_type}") { lhs_type == rhs_type and @types.include? lhs_type }
  lhs_type
end

#evaluate(ctx) ⇒ Object



31
32
33
34
35
# File 'lib/ast.rb', line 31

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