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
- #clear ⇒ Object
- #dependencies(expression) ⇒ Object
- #empty? ⇒ Boolean
- #evaluate(expression, data = {}) ⇒ Object
- #evaluate!(expression, data = {}) ⇒ Object
-
#initialize ⇒ 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)
Constructor Details
#initialize ⇒ Calculator
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
#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
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 |
#clear ⇒ Object
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
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 |