Class: Dentaku::AST::Arithmetic

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

Constant Summary collapse

DECIMAL =
/\A-?\d*\.\d+\z/.freeze
INTEGER =
/\A-?\d+\z/.freeze

Instance Attribute Summary

Attributes inherited from Operation

#left, #right

Instance Method Summary collapse

Methods inherited from Operation

#accept, #dependencies, #display_operator, max_param_count, min_param_count, right_associative?

Methods inherited from Node

arity, #dependencies, #name, precedence, resolve_class

Constructor Details

#initializeArithmetic

Returns a new instance of Arithmetic.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dentaku/ast/arithmetic.rb', line 12

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)


30
31
32
# File 'lib/dentaku/ast/arithmetic.rb', line 30

def operator
  raise NotImplementedError
end

#typeObject



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

def type
  :numeric
end

#value(context = {}) ⇒ Object



34
35
36
# File 'lib/dentaku/ast/arithmetic.rb', line 34

def value(context = {})
  calculate(left.value(context), right.value(context))
end