kalc 0.5.4 → 0.5.5
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.
- data/lib/kalc/ast.rb +12 -0
- data/lib/kalc/grammar.rb +12 -2
- data/lib/kalc/transform.rb +12 -0
- data/lib/kalc/version.rb +1 -1
- metadata +1 -1
data/lib/kalc/ast.rb
CHANGED
@@ -85,6 +85,18 @@ module Kalc
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
+
class NonOps
|
89
|
+
attr_reader :value
|
90
|
+
|
91
|
+
def initialize(value)
|
92
|
+
@value = value
|
93
|
+
end
|
94
|
+
|
95
|
+
def eval(context)
|
96
|
+
Arithmetic.new(@value.left.eval(context), @value.right.eval(context), @value.operator).eval(context)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
88
100
|
class Ops
|
89
101
|
attr_reader :left
|
90
102
|
attr_reader :ops
|
data/lib/kalc/grammar.rb
CHANGED
@@ -156,6 +156,16 @@ class Kalc::Grammar < Parslet::Parser
|
|
156
156
|
left_paren >> conditional_expression >> right_paren
|
157
157
|
}
|
158
158
|
|
159
|
+
rule(:non_ops_atom) {
|
160
|
+
paren_expression.as(:paren_expression) | variable.as(:variable) | number
|
161
|
+
}
|
162
|
+
|
163
|
+
rule(:non_ops_expression) {
|
164
|
+
(non_ops_atom.as(:left) >>
|
165
|
+
power_of >>
|
166
|
+
non_ops_atom.as(:right)).as(:non_ops)
|
167
|
+
}
|
168
|
+
|
159
169
|
# IF(1, 2, 3)
|
160
170
|
# AND(1, 2, ...)
|
161
171
|
rule(:function_call_expression) {
|
@@ -165,8 +175,8 @@ class Kalc::Grammar < Parslet::Parser
|
|
165
175
|
|
166
176
|
# 1 * 2
|
167
177
|
rule(:multiplicative_expression) {
|
168
|
-
function_call_expression.as(:left) >>
|
169
|
-
((
|
178
|
+
(non_ops_expression | function_call_expression).as(:left) >>
|
179
|
+
((multiply | divide | modulus) >>
|
170
180
|
multiplicative_expression.as(:right)).repeat.as(:ops)
|
171
181
|
}
|
172
182
|
|
data/lib/kalc/transform.rb
CHANGED
@@ -13,6 +13,14 @@ module Kalc
|
|
13
13
|
right
|
14
14
|
}
|
15
15
|
|
16
|
+
rule(:left => subtree(:left), :non_ops => []) {
|
17
|
+
left
|
18
|
+
}
|
19
|
+
|
20
|
+
rule(:right => subtree(:right), :non_ops => []) {
|
21
|
+
right
|
22
|
+
}
|
23
|
+
|
16
24
|
rule(:commands => sequence(:commands)) {
|
17
25
|
Ast::Commands.new(commands)
|
18
26
|
}
|
@@ -45,6 +53,10 @@ module Kalc
|
|
45
53
|
Ast::FloatingPointNumber.new(number)
|
46
54
|
}
|
47
55
|
|
56
|
+
rule(:non_ops => subtree(:non_ops)) {
|
57
|
+
Ast::NonOps.new(non_ops)
|
58
|
+
}
|
59
|
+
|
48
60
|
rule(:left => simple(:left), :ops => subtree(:ops)) {
|
49
61
|
Ast::Ops.new(left, ops)
|
50
62
|
}
|
data/lib/kalc/version.rb
CHANGED