Class: Dentaku::AST::Arithmetic

Inherits:
Operation show all
Defined in:
lib/dentaku/ast/arithmetic.rb

Instance Attribute Summary

Attributes inherited from Operation

#left, #right

Instance Method Summary collapse

Methods inherited from Operation

#dependencies, max_param_count, min_param_count, right_associative?

Methods inherited from Node

arity, #dependencies, peek, precedence

Constructor Details

#initializeArithmetic

Returns a new instance of Arithmetic.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dentaku/ast/arithmetic.rb', line 9

def initialize(*)
  super

  unless valid_left?
    raise NodeError.new(:numeric, left.type, :left),
          "#{self.class} requires numeric operands"
  end

  unless valid_right?
    raise NodeError.new(:numeric, right.type, :right),
          "#{self.class} requires numeric operands"
  end
end

Instance Method Details

#operatorObject

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/dentaku/ast/arithmetic.rb', line 27

def operator
  raise NotImplementedError
end

#typeObject



23
24
25
# File 'lib/dentaku/ast/arithmetic.rb', line 23

def type
  :numeric
end

#value(context = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/dentaku/ast/arithmetic.rb', line 31

def value(context = {})
  l = cast(left.value(context))
  r = cast(right.value(context))
  begin
    l.public_send(operator, r)
  rescue ::TypeError => e
    # Right cannot be converted to a suitable type for left. e.g. [] + 1
    raise Dentaku::ArgumentError.for(:incompatible_type, value: r, for: l.class), e.message
  end
end