Class: Keisan::Calculator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: nil, allow_recursive: false, allow_blocks: true, allow_multiline: true, allow_random: true, cache: nil) ⇒ Calculator

Note, allow_recursive would be more appropriately named: allow_unbound_functions_in_function_definitions, but it is too late for that.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/keisan/calculator.rb', line 7

def initialize(context: nil, allow_recursive: false, allow_blocks: true, allow_multiline: true, allow_random: true, cache: nil)
  @context = context || Context.new(
    allow_recursive: allow_recursive,
    allow_blocks: allow_blocks,
    allow_multiline: allow_multiline,
    allow_random: allow_random
  )
  @cache = case cache
           when nil, false
             nil
           when true
             AST::Cache.new
           when AST::Cache
             cache
           else
             raise Exceptions::StandardError.new("cache must be either nil, false, true, or an instance of Keisan::AST::Cache")
           end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/keisan/calculator.rb', line 3

def context
  @context
end

Instance Method Details

#allow_blocksObject



34
35
36
# File 'lib/keisan/calculator.rb', line 34

def allow_blocks
  context.allow_blocks
end

#allow_multilineObject



38
39
40
# File 'lib/keisan/calculator.rb', line 38

def allow_multiline
  context.allow_multiline
end

#allow_randomObject



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

def allow_random
  context.allow_random
end

#allow_recursiveObject



26
27
28
# File 'lib/keisan/calculator.rb', line 26

def allow_recursive
  context.allow_recursive
end

#allow_recursive!Object



30
31
32
# File 'lib/keisan/calculator.rb', line 30

def allow_recursive!
  context.allow_recursive!
end

#ast(expression) ⇒ Object



62
63
64
# File 'lib/keisan/calculator.rb', line 62

def ast(expression)
  Evaluator.new(self, cache: @cache).parse_ast(expression)
end

#define_function!(name, function) ⇒ Object



70
71
72
# File 'lib/keisan/calculator.rb', line 70

def define_function!(name, function)
  context.register_function!(name, function)
end

#define_variable!(name, value) ⇒ Object



66
67
68
# File 'lib/keisan/calculator.rb', line 66

def define_variable!(name, value)
  context.register_variable!(name, value)
end

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



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

def evaluate(expression, definitions = {})
  Evaluator.new(self, cache: @cache).evaluate(expression, definitions)
end

#evaluate_ast(ast, definitions = {}) ⇒ Object



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

def evaluate_ast(ast, definitions = {})
  Evaluator.new(self, cache: @cache).evaluate_ast(ast, definitions: definitions)
end

#simplify(expression, definitions = {}) ⇒ Object



54
55
56
# File 'lib/keisan/calculator.rb', line 54

def simplify(expression, definitions = {})
  Evaluator.new(self, cache: @cache).simplify(expression, definitions)
end

#simplify_ast(ast, definitions = {}) ⇒ Object



58
59
60
# File 'lib/keisan/calculator.rb', line 58

def simplify_ast(ast, definitions = {})
  Evaluator.new(self, cache: @cache).simplify_ast(ast, definitions: definitions)
end