kalc 0.5.6 → 0.5.7

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 CHANGED
@@ -107,7 +107,6 @@ module Kalc
107
107
  end
108
108
 
109
109
  def eval(context)
110
-
111
110
  @ops.inject(@left.eval(context)) { |x, op|
112
111
  a = Arithmetic.new(x, op[:right].eval(context), op[:operator])
113
112
  a.eval(context)
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
- (non_ops_atom.as(:left) >>
160
+ (atom.as(:left) >>
165
161
  power_of >>
166
- non_ops_atom.as(:right)).as(:non_ops)
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('-') >> atom).as(:negative) | atom
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
@@ -1,3 +1,3 @@
1
1
  module Kalc
2
- VERSION = "0.5.6"
2
+ VERSION = "0.5.7"
3
3
  end
@@ -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.6
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-03-29 00:00:00.000000000 Z
12
+ date: 2012-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake