kalc 0.5.6 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/kalc/ast.rb +0 -1
- data/lib/kalc/grammar.rb +10 -14
- data/lib/kalc/version.rb +1 -1
- data/spec/interpreter_spec.rb +7 -0
- metadata +2 -2
data/lib/kalc/ast.rb
CHANGED
data/lib/kalc/grammar.rb
CHANGED
@@ -156,28 +156,17 @@ 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
159
|
rule(:non_ops_expression) {
|
164
|
-
(
|
160
|
+
(atom.as(:left) >>
|
165
161
|
power_of >>
|
166
|
-
|
162
|
+
atom.as(:right)).as(:non_ops) | atom
|
167
163
|
}
|
168
164
|
|
169
165
|
# IF(1, 2, 3)
|
170
166
|
# AND(1, 2, ...)
|
171
167
|
rule(:function_call_expression) {
|
172
168
|
(identifier.as(:name) >> paren_variable_list.as(:variable_list)).as(:function_call) |
|
173
|
-
(str('-') >>
|
174
|
-
}
|
175
|
-
|
176
|
-
# 1 * 2
|
177
|
-
rule(:multiplicative_expression) {
|
178
|
-
(non_ops_expression | function_call_expression).as(:left) >>
|
179
|
-
((multiply | divide | modulus) >>
|
180
|
-
multiplicative_expression.as(:right)).repeat.as(:ops)
|
169
|
+
(str('-') >> non_ops_expression).as(:negative) | non_ops_expression
|
181
170
|
}
|
182
171
|
|
183
172
|
# 1 + 2
|
@@ -187,6 +176,13 @@ class Kalc::Grammar < Parslet::Parser
|
|
187
176
|
multiplicative_expression.as(:right)).repeat.as(:ops)
|
188
177
|
}
|
189
178
|
|
179
|
+
# 1 * 2
|
180
|
+
rule(:multiplicative_expression) {
|
181
|
+
function_call_expression.as(:left) >>
|
182
|
+
((multiply | divide | modulus) >>
|
183
|
+
function_call_expression.as(:right)).repeat.as(:ops)
|
184
|
+
}
|
185
|
+
|
190
186
|
# 1 < 2
|
191
187
|
# 1 > 2
|
192
188
|
# 1 <= 2
|
data/lib/kalc/version.rb
CHANGED
data/spec/interpreter_spec.rb
CHANGED
@@ -10,6 +10,13 @@ describe Kalc::Interpreter do
|
|
10
10
|
it { evaluate("1 + 1").should == 2.0 }
|
11
11
|
it { evaluate("4 + 1").should == 5 }
|
12
12
|
it { evaluate("5 + 5").should == 10 }
|
13
|
+
|
14
|
+
it { evaluate("5 / 5").should == 1 }
|
15
|
+
|
16
|
+
it { evaluate("5 / 4 / 2").should == 0.625 }
|
17
|
+
it { evaluate("5/4/2").should == 0.625 }
|
18
|
+
|
19
|
+
it { evaluate("6 * 2 / 3").should == 4 }
|
13
20
|
|
14
21
|
it { evaluate("10 > 9").should == true }
|
15
22
|
it { evaluate("10 < 9").should == false }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kalc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|