keisan 0.8.2 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/keisan/ast/block.rb +4 -0
- data/lib/keisan/ast/cell.rb +4 -0
- data/lib/keisan/ast/function_assignment.rb +16 -6
- data/lib/keisan/ast/hash.rb +4 -0
- data/lib/keisan/ast/node.rb +9 -0
- data/lib/keisan/ast/parent.rb +4 -0
- data/lib/keisan/calculator.rb +17 -3
- data/lib/keisan/context.rb +21 -4
- data/lib/keisan/evaluator.rb +16 -4
- data/lib/keisan/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d96dbb0d0fa1401f10443f46ce6fd273c2a370155a25f8b248f8f387d20c039f
|
4
|
+
data.tar.gz: a02847f4342e2dc70335a7489772a71f5d17dd70b1f6f9f0a845a9bb6e2be40a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd68bfc3f9722a21419fc47c66a92abdfab529be641ea6e2c39385b8f3d014736a5ce9d7ad679554abae9d29c9ce03e1fb37ef0cd806b17f0fa6608aa82491f9
|
7
|
+
data.tar.gz: 242f78fc02f1c45673d977b505ec7f933cb11b5d098f8895c741fa0b8821640badab1046190935996c850296014ab0685864ec2a92bdb951f096c961eadfb2cd
|
data/lib/keisan/ast/block.rb
CHANGED
data/lib/keisan/ast/cell.rb
CHANGED
@@ -29,8 +29,7 @@ module Keisan
|
|
29
29
|
|
30
30
|
def evaluate
|
31
31
|
# Blocks might have local variable/function definitions, so skip check
|
32
|
-
verify_rhs_of_function_assignment_is_valid!
|
33
|
-
|
32
|
+
verify_rhs_of_function_assignment_is_valid!
|
34
33
|
context.register_function!(lhs.name, expression_function, local: local)
|
35
34
|
rhs
|
36
35
|
end
|
@@ -38,15 +37,26 @@ module Keisan
|
|
38
37
|
private
|
39
38
|
|
40
39
|
def verify_rhs_of_function_assignment_is_valid!
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
verify_unbound_functions!
|
41
|
+
verify_unbound_variables!
|
42
|
+
end
|
43
|
+
|
44
|
+
def verify_unbound_functions!
|
45
45
|
# Cannot have undefined functions unless allowed by context
|
46
46
|
unless context.allow_recursive || rhs.unbound_functions(context).empty?
|
47
47
|
raise Exceptions::InvalidExpression.new("Unbound function definitions are not allowed by current context")
|
48
48
|
end
|
49
49
|
end
|
50
|
+
|
51
|
+
def verify_unbound_variables!
|
52
|
+
# We allow unbound variables inside block statements, as they could be temporary
|
53
|
+
# variables assigned locally
|
54
|
+
return if rhs.is_a?(Block)
|
55
|
+
# Only variables that can appear are those that are arguments to the function
|
56
|
+
unless rhs.unbound_variables(context) <= Set.new(argument_names)
|
57
|
+
raise Exceptions::InvalidExpression.new("Unbound variables found in function definition")
|
58
|
+
end
|
59
|
+
end
|
50
60
|
end
|
51
61
|
end
|
52
62
|
end
|
data/lib/keisan/ast/hash.rb
CHANGED
data/lib/keisan/ast/node.rb
CHANGED
data/lib/keisan/ast/parent.rb
CHANGED
data/lib/keisan/calculator.rb
CHANGED
@@ -2,8 +2,14 @@ module Keisan
|
|
2
2
|
class Calculator
|
3
3
|
attr_reader :context
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
# Note, allow_recursive would be more appropriately named:
|
6
|
+
# allow_unbound_functions_in_function_definitions, but it is too late for that.
|
7
|
+
def initialize(context: nil, allow_recursive: false, allow_blocks: true, allow_multiline: true)
|
8
|
+
@context = context || Context.new(
|
9
|
+
allow_recursive: allow_recursive,
|
10
|
+
allow_blocks: allow_blocks,
|
11
|
+
allow_multiline: allow_multiline
|
12
|
+
)
|
7
13
|
end
|
8
14
|
|
9
15
|
def allow_recursive
|
@@ -14,6 +20,14 @@ module Keisan
|
|
14
20
|
context.allow_recursive!
|
15
21
|
end
|
16
22
|
|
23
|
+
def allow_blocks
|
24
|
+
context.allow_blocks
|
25
|
+
end
|
26
|
+
|
27
|
+
def allow_multiline
|
28
|
+
context.allow_multiline
|
29
|
+
end
|
30
|
+
|
17
31
|
def evaluate(expression, definitions = {})
|
18
32
|
Evaluator.new(self).evaluate(expression, definitions)
|
19
33
|
end
|
@@ -23,7 +37,7 @@ module Keisan
|
|
23
37
|
end
|
24
38
|
|
25
39
|
def ast(expression)
|
26
|
-
Evaluator.new(self).
|
40
|
+
Evaluator.new(self).parse_ast(expression)
|
27
41
|
end
|
28
42
|
|
29
43
|
def define_variable!(name, value)
|
data/lib/keisan/context.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
module Keisan
|
2
2
|
class Context
|
3
|
-
attr_reader :function_registry,
|
4
|
-
|
5
|
-
|
3
|
+
attr_reader :function_registry,
|
4
|
+
:variable_registry,
|
5
|
+
:allow_recursive,
|
6
|
+
:allow_multiline,
|
7
|
+
:allow_blocks
|
8
|
+
|
9
|
+
def initialize(parent: nil,
|
10
|
+
random: nil,
|
11
|
+
allow_recursive: false,
|
12
|
+
allow_multiline: true,
|
13
|
+
allow_blocks: true,
|
14
|
+
shadowed: [])
|
6
15
|
@parent = parent
|
7
16
|
@function_registry = Functions::Registry.new(parent: @parent&.function_registry)
|
8
17
|
@variable_registry = Variables::Registry.new(parent: @parent&.variable_registry, shadowed: shadowed)
|
9
18
|
@random = random
|
10
19
|
@allow_recursive = allow_recursive
|
20
|
+
@allow_multiline = allow_multiline
|
21
|
+
@allow_blocks = allow_blocks
|
11
22
|
end
|
12
23
|
|
13
24
|
def allow_recursive!
|
@@ -110,7 +121,13 @@ module Keisan
|
|
110
121
|
end
|
111
122
|
|
112
123
|
def pure_child(shadowed: [])
|
113
|
-
self.class.new(
|
124
|
+
self.class.new(
|
125
|
+
parent: self,
|
126
|
+
shadowed: shadowed,
|
127
|
+
allow_recursive: allow_recursive,
|
128
|
+
allow_multiline: allow_multiline,
|
129
|
+
allow_blocks: allow_blocks
|
130
|
+
)
|
114
131
|
end
|
115
132
|
end
|
116
133
|
end
|
data/lib/keisan/evaluator.rb
CHANGED
@@ -8,7 +8,7 @@ module Keisan
|
|
8
8
|
|
9
9
|
def evaluate(expression, definitions = {})
|
10
10
|
context = calculator.context.spawn_child(definitions: definitions, transient: true)
|
11
|
-
ast =
|
11
|
+
ast = parse_ast(expression)
|
12
12
|
last_line = last_line(ast)
|
13
13
|
|
14
14
|
evaluation = ast.evaluated(context)
|
@@ -24,16 +24,28 @@ module Keisan
|
|
24
24
|
|
25
25
|
def simplify(expression, definitions = {})
|
26
26
|
context = calculator.context.spawn_child(definitions: definitions, transient: true)
|
27
|
-
ast =
|
27
|
+
ast = parse_ast(expression)
|
28
28
|
ast.simplify(context)
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
32
|
-
AST.parse(expression)
|
31
|
+
def parse_ast(expression)
|
32
|
+
AST.parse(expression).tap do |ast|
|
33
|
+
disallowed = disallowed_nodes
|
34
|
+
if !disallowed.empty? && ast.contains_a?(disallowed)
|
35
|
+
raise Keisan::Exceptions::InvalidExpression.new("Context does not permit expressions with #{disallowed}")
|
36
|
+
end
|
37
|
+
end
|
33
38
|
end
|
34
39
|
|
35
40
|
private
|
36
41
|
|
42
|
+
def disallowed_nodes
|
43
|
+
disallowed = []
|
44
|
+
disallowed << Keisan::AST::Block unless calculator.allow_blocks
|
45
|
+
disallowed << Keisan::AST::MultiLine unless calculator.allow_multiline
|
46
|
+
disallowed
|
47
|
+
end
|
48
|
+
|
37
49
|
def last_line(ast)
|
38
50
|
ast.is_a?(AST::MultiLine) ? ast.children.last : ast
|
39
51
|
end
|
data/lib/keisan/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: keisan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Locke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-04-
|
11
|
+
date: 2020-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cmath
|