kalc 0.8.0 → 0.8.1
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/Gemfile.lock +1 -1
- data/README.md +9 -4
- data/lib/kalc/ast.rb +1 -1
- data/lib/kalc/grammar.rb +4 -4
- data/lib/kalc/transform.rb +1 -1
- data/lib/kalc/version.rb +1 -1
- data/spec/interpreter_spec.rb +4 -0
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -22,20 +22,25 @@ Numbers
|
|
22
22
|
|
23
23
|
All numbers are floating point.
|
24
24
|
|
25
|
-
1
|
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
|
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
|
-
|
38
|
-
|
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
|
-------------------
|
data/lib/kalc/ast.rb
CHANGED
data/lib/kalc/grammar.rb
CHANGED
@@ -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(:
|
241
|
+
conditional_expression.as(:true_cond) >>
|
242
242
|
colon >>
|
243
|
-
conditional_expression.as(:
|
243
|
+
conditional_expression.as(:false_cond)).maybe
|
244
244
|
}
|
245
245
|
|
246
246
|
# 'a' = 1
|
data/lib/kalc/transform.rb
CHANGED
@@ -69,7 +69,7 @@ module Kalc
|
|
69
69
|
Ast::Arithmetic.new(left, right, operator)
|
70
70
|
}
|
71
71
|
|
72
|
-
rule(:condition => simple(:condition), :
|
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
|
|
data/lib/kalc/version.rb
CHANGED
data/spec/interpreter_spec.rb
CHANGED
@@ -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 }
|