Class: Dentaku::Calculator
- Inherits:
-
Object
- Object
- Dentaku::Calculator
- Defined in:
- lib/dentaku/calculator.rb
Instance Attribute Summary collapse
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#tokenizer ⇒ Object
readonly
Returns the value of attribute tokenizer.
Instance Method Summary collapse
- #add_function(name, type, body) ⇒ Object
- #add_functions(fns) ⇒ Object
- #ast(expression) ⇒ Object
- #cache_ast? ⇒ Boolean
- #clear ⇒ Object
- #clear_cache(pattern = :all) ⇒ Object
- #dependencies(expression) ⇒ Object
- #disable_cache ⇒ Object
- #empty? ⇒ Boolean
- #evaluate(expression, data = {}) ⇒ Object
- #evaluate!(expression, data = {}) ⇒ Object
-
#initialize(ast_cache = {}) ⇒ Calculator
constructor
A new instance of Calculator.
- #solve(expression_hash, &block) ⇒ Object
- #solve!(expression_hash) ⇒ Object
- #store(key_or_hash, value = nil) ⇒ Object (also: #bind)
- #store_formula(key, formula) ⇒ Object
Constructor Details
#initialize(ast_cache = {}) ⇒ Calculator
Returns a new instance of Calculator.
11 12 13 14 15 16 |
# File 'lib/dentaku/calculator.rb', line 11 def initialize(ast_cache={}) clear @tokenizer = Tokenizer.new @ast_cache = ast_cache @disable_ast_cache = false end |
Instance Attribute Details
#memory ⇒ Object (readonly)
Returns the value of attribute memory.
9 10 11 |
# File 'lib/dentaku/calculator.rb', line 9 def memory @memory end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
9 10 11 |
# File 'lib/dentaku/calculator.rb', line 9 def result @result end |
#tokenizer ⇒ Object (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
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
61 62 63 64 65 66 67 |
# File 'lib/dentaku/calculator.rb', line 61 def ast(expression) @ast_cache.fetch(expression) { Parser.new(tokenizer.tokenize(expression)).parse.tap do |node| @ast_cache[expression] = node if cache_ast? end } end |
#cache_ast? ⇒ Boolean
120 121 122 |
# File 'lib/dentaku/calculator.rb', line 120 def cache_ast? Dentaku.cache_ast? && !@disable_ast_cache end |
#clear ⇒ Object
112 113 114 |
# File 'lib/dentaku/calculator.rb', line 112 def clear @memory = {} end |
#clear_cache(pattern = :all) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dentaku/calculator.rb', line 69 def clear_cache(pattern=:all) case pattern when :all @ast_cache = {} when String @ast_cache.delete(pattern) when Regexp @ast_cache.delete_if { |k,_| k =~ pattern } else fail Dentaku::ArgumentError end end |
#dependencies(expression) ⇒ Object
57 58 59 |
# File 'lib/dentaku/calculator.rb', line 57 def dependencies(expression) ast(expression).dependencies(memory) end |
#disable_cache ⇒ Object
28 29 30 31 32 33 |
# File 'lib/dentaku/calculator.rb', line 28 def disable_cache @disable_ast_cache = true yield(self) if block_given? ensure @disable_ast_cache = false end |
#empty? ⇒ Boolean
116 117 118 |
# File 'lib/dentaku/calculator.rb', line 116 def empty? memory.empty? end |
#evaluate(expression, data = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/dentaku/calculator.rb', line 35 def evaluate(expression, data={}) evaluate!(expression, data) rescue UnboundVariableError, ArgumentError yield expression if block_given? end |
#evaluate!(expression, data = {}) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/dentaku/calculator.rb', line 41 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
53 54 55 |
# File 'lib/dentaku/calculator.rb', line 53 def solve(expression_hash, &block) BulkExpressionSolver.new(expression_hash, self).solve(&block) end |
#solve!(expression_hash) ⇒ Object
49 50 51 |
# File 'lib/dentaku/calculator.rb', line 49 def solve!(expression_hash) BulkExpressionSolver.new(expression_hash, self).solve! end |
#store(key_or_hash, value = nil) ⇒ Object Also known as: bind
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dentaku/calculator.rb', line 82 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
108 109 110 |
# File 'lib/dentaku/calculator.rb', line 108 def store_formula(key, formula) store(key, ast(formula)) end |