Class: Dentaku::Calculator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCalculator

Returns a new instance of Calculator.



12
13
14
15
16
# File 'lib/dentaku/calculator.rb', line 12

def initialize
  clear
  @tokenizer = Tokenizer.new
  @ast_cache = {}
end

Instance Attribute Details

#memoryObject (readonly)

Returns the value of attribute memory.



10
11
12
# File 'lib/dentaku/calculator.rb', line 10

def memory
  @memory
end

#resultObject (readonly)

Returns the value of attribute result.



10
11
12
# File 'lib/dentaku/calculator.rb', line 10

def result
  @result
end

#tokenizerObject (readonly)

Returns the value of attribute tokenizer.



10
11
12
# File 'lib/dentaku/calculator.rb', line 10

def tokenizer
  @tokenizer
end

Instance Method Details

#add_function(name, type, body) ⇒ Object



18
19
20
21
# File 'lib/dentaku/calculator.rb', line 18

def add_function(name, type, body)
  Dentaku::AST::Function.register(name, type, body)
  self
end

#add_functions(fns) ⇒ Object



23
24
25
26
# File 'lib/dentaku/calculator.rb', line 23

def add_functions(fns)
  fns.each { |(name, type, body)| add_function(name, type, body) }
  self
end

#ast(expression) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/dentaku/calculator.rb', line 54

def ast(expression)
  @ast_cache.fetch(expression) {
    Parser.new(tokenizer.tokenize(expression)).parse.tap do |node|
      @ast_cache[expression] = node if Dentaku.cache_ast?
    end
  }
end

#clearObject



92
93
94
# File 'lib/dentaku/calculator.rb', line 92

def clear
  @memory = {}
end

#dependencies(expression) ⇒ Object



50
51
52
# File 'lib/dentaku/calculator.rb', line 50

def dependencies(expression)
  ast(expression).dependencies(memory)
end

#empty?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/dentaku/calculator.rb', line 96

def empty?
  memory.empty?
end

#evaluate(expression, data = {}) ⇒ Object



28
29
30
31
32
# File 'lib/dentaku/calculator.rb', line 28

def evaluate(expression, data={})
  evaluate!(expression, data)
rescue UnboundVariableError, ArgumentError
  yield expression if block_given?
end

#evaluate!(expression, data = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/dentaku/calculator.rb', line 34

def evaluate!(expression, data={})
  store(data) do
    node = expression
    node = ast(node) unless node.is_a?(AST::Node)
    node.value(memory)
  end
end

#solve(expression_hash, &block) ⇒ Object



46
47
48
# File 'lib/dentaku/calculator.rb', line 46

def solve(expression_hash, &block)
  BulkExpressionSolver.new(expression_hash, self).solve(&block)
end

#solve!(expression_hash) ⇒ Object



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

def solve!(expression_hash)
  BulkExpressionSolver.new(expression_hash, self).solve!
end

#store(key_or_hash, value = nil) ⇒ Object Also known as: bind



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dentaku/calculator.rb', line 62

def store(key_or_hash, value=nil)
  restore = Hash[memory]

  if value.nil?
    key_or_hash.each do |key, val|
      memory[key.to_s.downcase] = val
    end
  else
    memory[key_or_hash.to_s.downcase] = value
  end

  if block_given?
    begin
      result = yield
      @memory = restore
      return result
    rescue => e
      @memory = restore
      raise e
    end
  end

  self
end

#store_formula(key, formula) ⇒ Object



88
89
90
# File 'lib/dentaku/calculator.rb', line 88

def store_formula(key, formula)
  store(key, ast(formula))
end