kalc 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kalc (0.7.0)
4
+ kalc (0.8.0)
5
5
  parslet (~> 1.4)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -22,20 +22,25 @@ Numbers
22
22
 
23
23
  All numbers are floating point.
24
24
 
25
- 1 =\> 1.0 2.020 =\> 2.02 1.23E =\> 12300000000.0
25
+ 1 => 1.0
26
+ 2.020 => 2.02
27
+ 1.23E => 12300000000.0
26
28
 
27
29
  Arithmetic
28
30
  ----------
29
31
 
30
- 1 + 1 / (10 \* 100) - 3 + 3 - (3 - 2) 1 \> 1 SUM(1, 2, 3, 4, 5)
32
+ 1 + 1 / (10 * 100) - 3 + 3 - (3 - 2)
33
+ SUM(1, 2, 3, 4, 5)
31
34
 
32
35
  Arithmetic is standard infix with nesting via parenthesis.
33
36
 
34
37
  Logical operations
35
38
  ------------------
36
39
 
37
- 1 \> 2 ? 1 : 3 # Ternary (1 || 2) \> 3 1 \> 2 or 3 <2 # false OR(1 > 2, 3 \<
38
- 2, 8 == 8) # true
40
+ 2 < 1 ? 1 : 3 # Ternary
41
+ (1 || 2) # 1.0
42
+ 2 or 3 # 2.0
43
+ OR(1 > 2, 3 < 2, 8 == 8) # true
39
44
 
40
45
  Variable assignment
41
46
  -------------------
@@ -193,7 +193,7 @@ module Kalc
193
193
  end
194
194
 
195
195
  def eval(context)
196
- @condition ? true_cond : false_cond
196
+ @condition.eval(context) ? @true_cond.eval(context) : @false_cond.eval(context)
197
197
  end
198
198
  end
199
199
 
@@ -33,7 +33,8 @@ class Kalc::Grammar < Parslet::Parser
33
33
  :left_brace => '{',
34
34
  :right_brace => '}',
35
35
  :comma => ',',
36
- :colon => ':'
36
+ :colon => ':',
37
+ :question_mark => '?'
37
38
 
38
39
  def self.operators(operators={})
39
40
  trailing_chars = Hash.new { |hash,symbol| hash[symbol] = [] }
@@ -76,7 +77,6 @@ class Kalc::Grammar < Parslet::Parser
76
77
 
77
78
  :assign => ':=',
78
79
  :excel_equal => '=',
79
- :question_mark => '?',
80
80
 
81
81
  :subtract => '-',
82
82
  :add => '+',
@@ -238,9 +238,9 @@ class Kalc::Grammar < Parslet::Parser
238
238
  rule(:conditional_expression) {
239
239
  logical_or_expression.as(:condition) >>
240
240
  (question_mark >>
241
- conditional_expression.as(:true) >>
241
+ conditional_expression.as(:true_cond) >>
242
242
  colon >>
243
- conditional_expression.as(:false)).maybe
243
+ conditional_expression.as(:false_cond)).maybe
244
244
  }
245
245
 
246
246
  # 'a' = 1
@@ -69,7 +69,7 @@ module Kalc
69
69
  Ast::Arithmetic.new(left, right, operator)
70
70
  }
71
71
 
72
- rule(:condition => simple(:condition), :true => simple(:true_cond), :false => simple(:false_cond)) {
72
+ rule(:condition => simple(:condition), :true_cond => simple(:true_cond), :false_cond => simple(:false_cond)) {
73
73
  Ast::Conditional.new(condition, true_cond, false_cond)
74
74
  }
75
75
 
@@ -1,3 +1,3 @@
1
1
  module Kalc
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.1"
3
3
  end
@@ -78,6 +78,10 @@ describe Kalc::Interpreter do
78
78
  it { evaluate("1.01 = 1.02").should == false }
79
79
  end
80
80
 
81
+ context "Ternary" do
82
+ it { evaluate("1 > 2 ? 1 : 2").should == 2 }
83
+ end
84
+
81
85
  context "Exponents" do
82
86
  it { evaluate("1.23e+10").should == 12300000000.0 }
83
87
  it { evaluate("1.23e-10").should == 1.23e-10 }
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.8.0
4
+ version: 0.8.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: