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.



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

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

Instance Attribute Details

#memoryObject (readonly)

Returns the value of attribute memory.



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

def memory
  @memory
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#tokenizerObject (readonly)

Returns the value of attribute tokenizer.



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

def tokenizer
  @tokenizer
end

Instance Method Details

#add_function(name, type, body) ⇒ Object



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

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

#add_functions(fns) ⇒ Object



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

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

#ast(expression) ⇒ Object



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

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



82
83
84
# File 'lib/dentaku/calculator.rb', line 82

def clear
  @memory = {}
end

#dependencies(expression) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dentaku/calculator.rb', line 86

def empty?
  memory.empty?
end

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



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

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

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



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

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

#solve(expression_hash, &block) ⇒ Object



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

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

#solve!(expression_hash) ⇒ Object



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

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

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



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

def store(key_or_hash, value=nil)
  restore = memory.dup

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

  if block_given?
    result = yield
    @memory = restore
    return result
  end

  self
end