kalc 0.8.1 → 0.9.0
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 +1 -1
- data/Gemfile.lock +14 -13
- data/bin/ikalc +2 -2
- data/bin/kalc +2 -2
- data/kalc.gemspec +16 -16
- data/lib/kalc.rb +5 -5
- data/lib/kalc/ast.rb +36 -35
- data/lib/kalc/environment.rb +2 -2
- data/lib/kalc/grammar.rb +53 -53
- data/lib/kalc/interpreter.rb +39 -53
- data/lib/kalc/repl.rb +17 -21
- data/lib/kalc/transform.rb +18 -18
- data/lib/kalc/version.rb +1 -1
- data/spec/grammar_spec.rb +51 -51
- data/spec/interpreter_spec.rb +53 -53
- data/spec/stdlib_spec.rb +26 -26
- metadata +37 -32
data/spec/interpreter_spec.rb
CHANGED
@@ -6,90 +6,90 @@ describe Kalc::Interpreter do
|
|
6
6
|
@transform = Kalc::Transform.new
|
7
7
|
end
|
8
8
|
|
9
|
-
it { evaluate(
|
10
|
-
it { evaluate(
|
11
|
-
it { evaluate(
|
12
|
-
it { evaluate(
|
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 }
|
9
|
+
it { evaluate('2 + 2').should == 4 }
|
10
|
+
it { evaluate('1 + 1').should == 2.0 }
|
11
|
+
it { evaluate('4 + 1').should == 5 }
|
12
|
+
it { evaluate('5 + 5').should == 10 }
|
18
13
|
|
19
|
-
it { evaluate(
|
14
|
+
it { evaluate('5 / 5').should == 1 }
|
20
15
|
|
21
|
-
it { evaluate(
|
22
|
-
it { evaluate(
|
16
|
+
it { evaluate('5 / 4 / 2').should == 0.625 }
|
17
|
+
it { evaluate('5/4/2').should == 0.625 }
|
23
18
|
|
24
|
-
it { evaluate(
|
19
|
+
it { evaluate('6 * 2 / 3').should == 4 }
|
25
20
|
|
26
|
-
it { evaluate(
|
21
|
+
it { evaluate('10 > 9').should == true }
|
22
|
+
it { evaluate('10 < 9').should == false }
|
27
23
|
|
28
|
-
it { evaluate(
|
24
|
+
it { evaluate('10 + 19 + 11 * 3').should == 62 }
|
29
25
|
|
30
|
-
it
|
31
|
-
|
32
|
-
|
26
|
+
it { evaluate('10 >= 10').should == true }
|
27
|
+
|
28
|
+
it { evaluate('ABS(-1 + -2)').should == 3 }
|
29
|
+
|
30
|
+
it 'should be able to load variables' do
|
31
|
+
evaluate('a := 1; 1 + a').should == 2
|
32
|
+
evaluate('a := 1; b := 2; 1 + b').should == 3
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it 'should be able to load quoted variables' do
|
36
36
|
evaluate("'Item (a)' := 1; 1 + 'Item (a)'").should == 2
|
37
37
|
evaluate("'a' := 1; 'b[a]' := 2 + 'a'; 1 + 'b[a]'").should == 4
|
38
38
|
end
|
39
39
|
|
40
|
-
it
|
40
|
+
it 'should be able to load single quoted variables' do
|
41
41
|
evaluate("'a' := 1; 1 + 'a'").should == 2
|
42
42
|
evaluate("'a' := 1; 'b' := 2; 'b' + 'a'").should == 3
|
43
|
-
|
43
|
+
|
44
44
|
evaluate("'a b' := 1; 'a b' + 1").should == 2
|
45
45
|
end
|
46
46
|
|
47
|
-
it { evaluate(
|
47
|
+
it { evaluate('((75.0)*(25.0))+((125.0)*(25.0))+((150.0)*(25.0))+((250.0)*(25.0))').should == 15000 }
|
48
48
|
|
49
|
-
it { evaluate(
|
49
|
+
it { evaluate('(((40.0)/1000*(4380.0)*(200.0))-((40.0)/1000*(4380.0)*((((75.0)*(25.0))+((125.0)*(25.0))+((150.0)*(25.0))+((250.0)*(25.0)))/(10.0)/(40.0)/(0.8))))*(0.05)').should == 1341.375 }
|
50
50
|
|
51
|
-
context
|
52
|
-
it { evaluate(
|
53
|
-
it { evaluate(
|
54
|
-
it { evaluate(
|
55
|
-
it { evaluate(
|
56
|
-
it { evaluate(
|
51
|
+
context 'Negative numbers' do
|
52
|
+
it { evaluate('-2').should == -2 }
|
53
|
+
it { evaluate('-1000').should == -1000 }
|
54
|
+
it { evaluate('-1000.0001').should == -1000.0001 }
|
55
|
+
it { evaluate('1 + -1').should == 0 }
|
56
|
+
it { evaluate('1 + -10').should == -9 }
|
57
57
|
end
|
58
58
|
|
59
|
-
context
|
60
|
-
it { evaluate(
|
61
|
-
it { evaluate(
|
62
|
-
it { evaluate(
|
59
|
+
context 'Positive numbers' do
|
60
|
+
it { evaluate('1 + +1').should == 2 }
|
61
|
+
it { evaluate('1 + +1 - 1').should == 1 }
|
62
|
+
it { evaluate('+10000.0001').should == 10000.0001 }
|
63
63
|
end
|
64
64
|
|
65
|
-
context
|
66
|
-
it { evaluate(
|
67
|
-
it { evaluate(
|
68
|
-
it { evaluate(
|
69
|
-
it { evaluate(
|
65
|
+
context 'Boolean value' do
|
66
|
+
it { evaluate('TRUE').should == true }
|
67
|
+
it { evaluate('FALSE').should == false }
|
68
|
+
it { evaluate('FALSE || TRUE').should == true }
|
69
|
+
it { evaluate('FALSE && TRUE').should == false }
|
70
70
|
end
|
71
71
|
|
72
|
-
context
|
73
|
-
it { evaluate(
|
74
|
-
it { evaluate(
|
75
|
-
it { evaluate(
|
76
|
-
it { evaluate(
|
77
|
-
it { evaluate(
|
78
|
-
it { evaluate(
|
72
|
+
context 'Floating point number' do
|
73
|
+
it { evaluate('1.01').should == 1.01 }
|
74
|
+
it { evaluate('1.01 + 0.02').should == 1.03 }
|
75
|
+
it { evaluate('1.01 - 0.01').should == 1 }
|
76
|
+
it { evaluate('1.1 + 1.1').should == 2.2 }
|
77
|
+
it { evaluate('1.01 = 1.01').should == true }
|
78
|
+
it { evaluate('1.01 = 1.02').should == false }
|
79
79
|
end
|
80
80
|
|
81
|
-
context
|
82
|
-
it { evaluate(
|
81
|
+
context 'Ternary' do
|
82
|
+
it { evaluate('1 > 2 ? 1 : 2').should == 2 }
|
83
83
|
end
|
84
84
|
|
85
|
-
context
|
86
|
-
it { evaluate(
|
87
|
-
it { evaluate(
|
85
|
+
context 'Exponents' do
|
86
|
+
it { evaluate('1.23e+10').should == 12300000000.0 }
|
87
|
+
it { evaluate('1.23e-10').should == 1.23e-10 }
|
88
88
|
end
|
89
89
|
|
90
|
-
context
|
91
|
-
it { evaluate(
|
92
|
-
it { evaluate(
|
90
|
+
context 'Numbers starting with a decimal point' do
|
91
|
+
it { evaluate('0.4').should == 0.4 }
|
92
|
+
it { evaluate('.4').should == 0.4 }
|
93
93
|
end
|
94
94
|
|
95
95
|
private
|
data/spec/stdlib_spec.rb
CHANGED
@@ -5,49 +5,49 @@ class Kalc::Stdlib
|
|
5
5
|
end
|
6
6
|
|
7
7
|
describe Kalc::Stdlib do
|
8
|
-
|
8
|
+
|
9
9
|
before(:each) do
|
10
10
|
@grammar = Kalc::Grammar.new
|
11
11
|
@transform = Kalc::Transform.new
|
12
12
|
end
|
13
13
|
|
14
|
-
describe
|
15
|
-
it { evaluate(
|
16
|
-
it { evaluate(
|
17
|
-
it { evaluate(
|
18
|
-
it { evaluate(
|
14
|
+
describe 'PLUS_ONE' do
|
15
|
+
it { evaluate('PLUS_ONE(1)').should == 2 }
|
16
|
+
it { evaluate('PLUS_ONE(101)').should == 102 }
|
17
|
+
it { evaluate('PLUS_ONE(0)').should == 1 }
|
18
|
+
it { evaluate('PLUS_ONE(-1)').should == 0 }
|
19
19
|
end
|
20
20
|
|
21
|
-
describe
|
22
|
-
it { evaluate(
|
23
|
-
it { evaluate(
|
24
|
-
it { evaluate(
|
25
|
-
it { evaluate(
|
21
|
+
describe 'MINUS_ONE' do
|
22
|
+
it { evaluate('MINUS_ONE(1)').should == 0 }
|
23
|
+
it { evaluate('MINUS_ONE(101)').should == 100 }
|
24
|
+
it { evaluate('MINUS_ONE(0)').should == -1 }
|
25
|
+
it { evaluate('MINUS_ONE(-1)').should == -2 }
|
26
26
|
end
|
27
27
|
|
28
|
-
describe
|
29
|
-
it { evaluate(
|
30
|
-
it { evaluate(
|
28
|
+
describe 'SQUARE' do
|
29
|
+
it { evaluate('SQUARE(2)').should == 4 }
|
30
|
+
it { evaluate('SQUARE(4)').should == 16 }
|
31
31
|
end
|
32
32
|
|
33
|
-
describe
|
34
|
-
it { evaluate(
|
35
|
-
it { evaluate(
|
33
|
+
describe 'CUBE' do
|
34
|
+
it { evaluate('CUBE(2)').should == 8 }
|
35
|
+
it { evaluate('CUBE(4)').should == 64 }
|
36
36
|
end
|
37
37
|
|
38
|
-
describe
|
39
|
-
it { evaluate(
|
40
|
-
it { evaluate(
|
38
|
+
describe 'FIB' do
|
39
|
+
it { evaluate('FIB(1)').should == 1 }
|
40
|
+
it { evaluate('FIB(10)').should == 55 }
|
41
41
|
end
|
42
42
|
|
43
|
-
describe
|
44
|
-
it { evaluate(
|
45
|
-
it { evaluate(
|
43
|
+
describe 'FACTORIAL' do
|
44
|
+
it { evaluate('FACTORIAL(1)').should == 1 }
|
45
|
+
it { evaluate('FACTORIAL(5)').should == 120 }
|
46
46
|
end
|
47
47
|
|
48
|
-
describe
|
49
|
-
it { evaluate(
|
50
|
-
it { evaluate(
|
48
|
+
describe 'TOWERS_OF_HANOI' do
|
49
|
+
it { evaluate('TOWERS_OF_HANOI(4)').should == 15 }
|
50
|
+
it { evaluate('TOWERS_OF_HANOI(10)').should == 1023 }
|
51
51
|
end
|
52
52
|
|
53
53
|
private
|
metadata
CHANGED
@@ -1,64 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kalc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chris Parker
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-09-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
|
17
|
-
none: false
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
17
|
requirements:
|
19
|
-
- -
|
18
|
+
- - '>='
|
20
19
|
- !ruby/object:Gem::Version
|
21
20
|
version: '0'
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
21
|
none: false
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
29
|
+
type: :development
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rspec
|
32
|
-
|
33
|
-
none: false
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
34
33
|
requirements:
|
35
|
-
- -
|
34
|
+
- - '>='
|
36
35
|
- !ruby/object:Gem::Version
|
37
36
|
version: '0'
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
37
|
none: false
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
42
39
|
requirements:
|
43
|
-
- -
|
40
|
+
- - '>='
|
44
41
|
- !ruby/object:Gem::Version
|
45
42
|
version: '0'
|
43
|
+
none: false
|
44
|
+
prerelease: false
|
45
|
+
type: :development
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
name: parslet
|
48
|
-
|
49
|
-
none: false
|
48
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
49
|
requirements:
|
51
50
|
- - ~>
|
52
51
|
- !ruby/object:Gem::Version
|
53
|
-
version: '1.
|
54
|
-
type: :runtime
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
version: '1.5'
|
57
53
|
none: false
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
58
55
|
requirements:
|
59
56
|
- - ~>
|
60
57
|
- !ruby/object:Gem::Version
|
61
|
-
version: '1.
|
58
|
+
version: '1.5'
|
59
|
+
none: false
|
60
|
+
prerelease: false
|
61
|
+
type: :runtime
|
62
62
|
description: Calculation language slightly based on Excel's formula language.
|
63
63
|
email:
|
64
64
|
- mrcsparker@gmail.com
|
@@ -93,26 +93,32 @@ files:
|
|
93
93
|
- spec/stdlib_spec.rb
|
94
94
|
homepage: https://github.com/mrcsparker/kalc
|
95
95
|
licenses: []
|
96
|
-
post_install_message:
|
96
|
+
post_install_message:
|
97
97
|
rdoc_options: []
|
98
98
|
require_paths:
|
99
99
|
- lib
|
100
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
101
|
requirements:
|
103
|
-
- -
|
102
|
+
- - '>='
|
104
103
|
- !ruby/object:Gem::Version
|
104
|
+
segments:
|
105
|
+
- 0
|
105
106
|
version: '0'
|
106
|
-
|
107
|
+
hash: 2
|
107
108
|
none: false
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
110
|
requirements:
|
109
|
-
- -
|
111
|
+
- - '>='
|
110
112
|
- !ruby/object:Gem::Version
|
113
|
+
segments:
|
114
|
+
- 0
|
111
115
|
version: '0'
|
116
|
+
hash: 2
|
117
|
+
none: false
|
112
118
|
requirements: []
|
113
119
|
rubyforge_project: kalc
|
114
120
|
rubygems_version: 1.8.24
|
115
|
-
signing_key:
|
121
|
+
signing_key:
|
116
122
|
specification_version: 3
|
117
123
|
summary: Small calculation language.
|
118
124
|
test_files:
|
@@ -120,4 +126,3 @@ test_files:
|
|
120
126
|
- spec/interpreter_spec.rb
|
121
127
|
- spec/spec_helper.rb
|
122
128
|
- spec/stdlib_spec.rb
|
123
|
-
has_rdoc:
|