kalc 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2db4867bb104360a3652e3c033a105eb1e0a80d1
4
+ data.tar.gz: c6d653e2134c519e8b71687b5916f7e0a33f01d2
5
+ SHA512:
6
+ metadata.gz: 4a39757fde5d95b2d5aaddd5231ff21504ce0d2ae4092a421f412fc2888ee9416d46b6afa0a6d6032677cb507454e1a3e1cb0fa3b3892321abc2cc3557276197
7
+ data.tar.gz: 2b6a0377c1a3f0ac04bd8214fb0019c0a2fac3508fb625da55785757aef0f1031ae64ef2bccc3de9c02c6e81e3fad97538a4f67edd23bba78178895476511a79
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kalc (1.0.0)
4
+ kalc (1.0.1)
5
5
  parslet (~> 1.6)
6
6
 
7
7
  GEM
@@ -9,17 +9,22 @@ GEM
9
9
  specs:
10
10
  blankslate (3.1.3)
11
11
  diff-lcs (1.2.5)
12
- parslet (1.6.2)
12
+ parslet (1.7.0)
13
13
  blankslate (>= 2.0, <= 4.0)
14
- rake (10.1.1)
15
- rspec (2.14.1)
16
- rspec-core (~> 2.14.0)
17
- rspec-expectations (~> 2.14.0)
18
- rspec-mocks (~> 2.14.0)
19
- rspec-core (2.14.7)
20
- rspec-expectations (2.14.4)
21
- diff-lcs (>= 1.1.3, < 2.0)
22
- rspec-mocks (2.14.4)
14
+ rake (10.4.2)
15
+ rspec (3.2.0)
16
+ rspec-core (~> 3.2.0)
17
+ rspec-expectations (~> 3.2.0)
18
+ rspec-mocks (~> 3.2.0)
19
+ rspec-core (3.2.2)
20
+ rspec-support (~> 3.2.0)
21
+ rspec-expectations (3.2.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.2.0)
24
+ rspec-mocks (3.2.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.2.0)
27
+ rspec-support (3.2.2)
23
28
 
24
29
  PLATFORMS
25
30
  java
data/README.md CHANGED
@@ -10,6 +10,17 @@ ikalc
10
10
  kalc comes with its own repl, known as `ikalc`. Start it up in your console by
11
11
  typing in `ikalc`
12
12
 
13
+ The Awesomest Contributors in the World!
14
+ ----------------------------------------
15
+
16
+ These people have made kalc a much more awesome language.
17
+
18
+ Cristina Matonte
19
+ https://github.com/anitsirc
20
+
21
+ Mikel Lindsaar
22
+ https://github.com/mikel
23
+
13
24
  Syntax
14
25
  ------
15
26
 
@@ -107,7 +118,7 @@ Some of them are:
107
118
  # Math functions
108
119
  ABS, DEGREES, PRODUCT, RADIANS, ROUND, SUM, TRUNC, LN, ACOS,
109
120
  ACOSH, ASIN, ASINH, ATAN, ATANH, CBRT, COS, COSH, ERF, ERFC, EXP, GAMMA,
110
- LGAMMA, LOG, LOG2, LOG10, SIN, SINH, SQRT, TAN, TANH
121
+ LGAMMA, LOG, LOG2, LOG10, SIN, SINH, SQRT, TAN, TANH, MIN, MAX, FLOOR, CEILING
111
122
 
112
123
  # String functions
113
124
  CHOMP, CHOP, CHR, CLEAR, COUNT, DOWNCASE, HEX, INSPECT, INTERN, TO_SYM, LENGTH, SIZE,
@@ -126,6 +137,8 @@ Some of them are:
126
137
  PLUS_ONE, MINUS_ONE, SQUARE, CUBE, FIB, FACTORIAL,
127
138
  TOWERS_OF_HANOI
128
139
 
140
+ FLOOR and CEILING functions acts as the mathematical definition of floor and ceil, meaning that it has a fixed significance value of 1.
141
+
129
142
  Loops
130
143
  -----
131
144
 
@@ -168,3 +181,6 @@ Contributing
168
181
  ------------
169
182
 
170
183
  Fork on GitHub and after you've committed tested patches, send a pull request.
184
+
185
+
186
+
data/lib/kalc/ast.rb CHANGED
@@ -205,7 +205,7 @@ module Kalc
205
205
  end
206
206
 
207
207
  def eval(context)
208
- context.add_variable(@variable, @value.eval(context))
208
+ context.add_variable(@variable, @value)
209
209
  end
210
210
  end
211
211
 
@@ -217,7 +217,9 @@ module Kalc
217
217
  end
218
218
 
219
219
  def eval(context)
220
- context.get_variable(@variable)
220
+ var = context.get_variable(@variable)
221
+ fail "Invalid variable: #{@variable}" unless var
222
+ var.class == BigDecimal ? var : var.eval(context)
221
223
  end
222
224
  end
223
225
 
@@ -244,7 +246,11 @@ module Kalc
244
246
 
245
247
  def eval(context)
246
248
  to_call = context.get_function(@name)
247
- to_call.call(context, *@variable_list) if to_call
249
+ fail "Unknown function #{@name}" unless to_call
250
+ to_call.call(context, *@variable_list)
251
+ rescue ArgumentError
252
+ fail "Argument Error. Function #{@name} was called with #{@variable_list.count} parameters. " +
253
+ "It needs at least #{to_call.parameters.select{|a| a.first == :req}.count - 1 } parameters"
248
254
  end
249
255
  end
250
256
 
@@ -83,7 +83,7 @@ module Kalc
83
83
  })
84
84
 
85
85
  env.add_function(:ROUND, lambda { |cxt, num, digits|
86
- num.eval(cxt).round(digits.eval(cxt))
86
+ num.eval(cxt).round(digits.eval(cxt).to_i)
87
87
  })
88
88
 
89
89
  env.add_function(:SUM, lambda { |cxt, *args|
@@ -98,6 +98,14 @@ module Kalc
98
98
  Math.log(val.eval(cxt))
99
99
  })
100
100
 
101
+ env.add_function(:MAX, lambda { |cxt, first, *args|
102
+ (args<<first).map { |a| a.eval(cxt) }.max
103
+ })
104
+
105
+ env.add_function(:MIN, lambda { |cxt, first, *args|
106
+ (args<<first).map { |a| a.eval(cxt) }.min
107
+ })
108
+
101
109
  math_funs =
102
110
  %w(acos acosh asin asinh atan atanh cbrt cos cosh erf erfc exp gamma lgamma log log2 log10 sin sinh sqrt tan tanh)
103
111
 
@@ -246,6 +254,14 @@ module Kalc
246
254
  puts output.eval(cxt)
247
255
  })
248
256
 
257
+ env.add_function(:FLOOR, lambda { |cxt, val|
258
+ val.eval(cxt).floor
259
+ })
260
+
261
+ env.add_function(:CEILING, lambda { |cxt, val|
262
+ val.eval(cxt).ceil
263
+ })
264
+
249
265
  end
250
266
  end
251
267
 
data/lib/kalc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kalc
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/spec/grammar_spec.rb CHANGED
@@ -5,105 +5,116 @@ describe Kalc::Grammar do
5
5
 
6
6
  context 'integers' do
7
7
  1.upto(10) do |i|
8
- it { grammar.should parse("#{i}") }
8
+ it { expect(grammar).to parse("#{i}") }
9
9
  end
10
10
  end
11
11
 
12
12
  context 'decimal numbers' do
13
13
  1.upto(10) do |i|
14
14
  1.upto(10) do |n|
15
- it { grammar.should parse("#{i}.#{n}") }
15
+ it { expect(grammar).to parse("#{i}.#{n}") }
16
16
  end
17
17
  end
18
18
  end
19
19
 
20
20
  context 'basic integer math' do
21
21
  1.upto(10) do |i|
22
- it { grammar.should parse("#{i} - #{i}") }
23
- it { grammar.should parse("#{i} + #{i}") }
24
- it { grammar.should parse("#{i} * #{i}") }
25
- it { grammar.should parse("#{i} / #{i}") }
26
- it { grammar.should parse("#{i} % #{i}") }
22
+ it { expect(grammar).to parse("#{i} - #{i}") }
23
+ it { expect(grammar).to parse("#{i} + #{i}") }
24
+ it { expect(grammar).to parse("#{i} * #{i}") }
25
+ it { expect(grammar).to parse("#{i} / #{i}") }
26
+ it { expect(grammar).to parse("#{i} % #{i}") }
27
27
  end
28
28
  end
29
29
 
30
30
  context 'basic decimal math' do
31
31
  1.upto(10) do |i|
32
32
  1.upto(10) do |n|
33
- it { grammar.should parse("#{i}.#{n} - #{i}.#{n}") }
34
- it { grammar.should parse("#{i}.#{n} + #{i}.#{n}") }
35
- it { grammar.should parse("#{i}.#{n} * #{i}.#{n}") }
36
- it { grammar.should parse("#{i}.#{n} / #{i}.#{n}") }
37
- it { grammar.should parse("#{i}.#{n} % #{i}.#{n}") }
33
+ it { expect(grammar).to parse("#{i}.#{n} - #{i}.#{n}") }
34
+ it { expect(grammar).to parse("#{i}.#{n} + #{i}.#{n}") }
35
+ it { expect(grammar).to parse("#{i}.#{n} * #{i}.#{n}") }
36
+ it { expect(grammar).to parse("#{i}.#{n} / #{i}.#{n}") }
37
+ it { expect(grammar).to parse("#{i}.#{n} % #{i}.#{n}") }
38
38
  end
39
39
  end
40
40
  end
41
41
 
42
42
  context 'Logical expressions' do
43
- it { grammar.should parse('3 && 1') }
44
- it { grammar.should_not parse('&& 1') }
43
+ it { expect(grammar).to parse('3 && 1') }
44
+ it { expect(grammar).to_not parse('&& 1') }
45
45
 
46
- it { grammar.should parse('3 || 1') }
47
- it { grammar.should_not parse('|| 1') }
46
+ it { expect(grammar).to parse('3 || 1') }
47
+ it { expect(grammar).to_not parse('|| 1') }
48
48
  end
49
49
 
50
50
  context 'Comparison expressions' do
51
- it { grammar.should parse('3 > 1') }
52
- it { grammar.should parse('3 < 1') }
53
- it { grammar.should parse('3 >= 1') }
54
- it { grammar.should parse('3 <= 1') }
55
-
56
- it { grammar.should_not parse('> 1') }
57
- it { grammar.should_not parse('< 1') }
58
- it { grammar.should_not parse('>= 1') }
59
- it { grammar.should_not parse('<= 1') }
51
+ it { expect(grammar).to parse('3 > 1') }
52
+ it { expect(grammar).to parse('3 < 1') }
53
+ it { expect(grammar).to parse('3 >= 1') }
54
+ it { expect(grammar).to parse('3 <= 1') }
55
+
56
+ it { expect(grammar).to_not parse('> 1') }
57
+ it { expect(grammar).to_not parse('< 1') }
58
+ it { expect(grammar).to_not parse('>= 1') }
59
+ it { expect(grammar).to_not parse('<= 1') }
60
60
  end
61
61
 
62
62
  context 'Equality' do
63
- it { grammar.should parse('3 == 1') }
64
- it { grammar.should parse('2 != 1') }
63
+ it { expect(grammar).to parse('3 == 1') }
64
+ it { expect(grammar).to parse('2 != 1') }
65
65
  end
66
66
 
67
67
  context 'Block' do
68
- it { grammar.should parse('(2 + 1)') }
69
- it { grammar.should parse('(2 + 1) + 1') }
70
- it { grammar.should parse('(2 + 1) * (1 / 2) + 3') }
71
- it { grammar.should parse('(2 + 1) + (1 + 2) + ((3 + 2) / (2 + 1)) * 9') }
72
- it { grammar.should parse('(2 + 1) - (1)') }
73
- it { grammar.should parse('(2 ) + ( 1)') }
74
- it { grammar.should parse('((2) + ( 1 ))') }
68
+ it { expect(grammar).to parse('(2 + 1)') }
69
+ it { expect(grammar).to parse('(2 + 1) + 1') }
70
+ it { expect(grammar).to parse('(2 + 1) * (1 / 2) + 3') }
71
+ it { expect(grammar).to parse('(2 + 1) + (1 + 2) + ((3 + 2) / (2 + 1)) * 9') }
72
+ it { expect(grammar).to parse('(2 + 1) - (1)') }
73
+ it { expect(grammar).to parse('(2 ) + ( 1)') }
74
+ it { expect(grammar).to parse('((2) + ( 1 ))') }
75
75
  end
76
76
 
77
77
  context 'Ternary expressions' do
78
- it { grammar.should parse('3 > 2 ? 1 : 5') }
79
- it { grammar.should parse('3 > 2 || 4 <= 5 ? 1 : 5') }
80
- it { grammar.should parse('(3 > (2 + 4)) ? 1 : 5') }
81
- it { grammar.should parse('IF(2 > 3, 3 > 2 ? 1 : 5, 7)') }
82
- it { grammar.should parse('3 > 2 ? 1 : 5 > 4 ? 7 : 5') }
78
+ it { expect(grammar).to parse('3 > 2 ? 1 : 5') }
79
+ it { expect(grammar).to parse('3 > 2 || 4 <= 5 ? 1 : 5') }
80
+ it { expect(grammar).to parse('(3 > (2 + 4)) ? 1 : 5') }
81
+ it { expect(grammar).to parse('IF(2 > 3, 3 > 2 ? 1 : 5, 7)') }
82
+ it { expect(grammar).to parse('3 > 2 ? 1 : 5 > 4 ? 7 : 5') }
83
83
  end
84
84
 
85
85
  context 'AND statements' do
86
- it { grammar.should parse('AND(1, 2, 3)') }
87
- it { grammar.should parse('AND(1, 2, 3, 4, 5, 6)') }
88
- it { grammar.should parse('AND(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)') }
86
+ it { expect(grammar).to parse('AND(1, 2, 3)') }
87
+ it { expect(grammar).to parse('AND(1, 2, 3, 4, 5, 6)') }
88
+ it { expect(grammar).to parse('AND(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)') }
89
89
  end
90
90
 
91
91
  context 'OR statements' do
92
- it { grammar.should parse('OR(1, 2, 3)') }
93
- it { grammar.should parse('OR(1, 2, 3, 4, 5, 6)') }
94
- it { grammar.should parse('OR(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)') }
92
+ it { expect(grammar).to parse('OR(1, 2, 3)') }
93
+ it { expect(grammar).to parse('OR(1, 2, 3, 4, 5, 6)') }
94
+ it { expect(grammar).to parse('OR(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)') }
95
95
  end
96
96
 
97
97
  context 'IF statements' do
98
- it { grammar.should parse('IF(3, 2, 1)') }
99
- it { grammar.should parse('IF(3 > 1, 2, 1)') }
100
- it { grammar.should parse('IF((3 > 1) || (2 < 1), 2, 1)') }
101
- it { grammar.should parse('IF(OR(1 > 3, 2 < 5), 3, 2)') }
98
+ it { expect(grammar).to parse('IF(3, 2, 1)') }
99
+ it { expect(grammar).to parse('IF(3 > 1, 2, 1)') }
100
+ it { expect(grammar).to parse('IF((3 > 1) || (2 < 1), 2, 1)') }
101
+ it { expect(grammar).to parse('IF(OR(1 > 3, 2 < 5), 3, 2)') }
102
102
  end
103
103
 
104
104
  context 'Nested IF statements' do
105
- it { grammar.should parse('IF(3 > 2, IF(2 < 3, 1, 3), 5)') }
106
- it { grammar.should parse('IF(3 > 2, IF(2 < 3, 1, 3), IF(5 > 1, 3, 9))') }
105
+ it { expect(grammar).to parse('IF(3 > 2, IF(2 < 3, 1, 3), 5)') }
106
+ it { expect(grammar).to parse('IF(3 > 2, IF(2 < 3, 1, 3), IF(5 > 1, 3, 9))') }
107
107
  end
108
108
 
109
+ context 'MAX statements' do
110
+ it { expect(grammar).to parse('MAX(3, 1, 2)') }
111
+ it { expect(grammar).to parse('MAX(-1, 2*4, (3-1)*2, 5, 6)') }
112
+ it { expect(grammar).to parse('MAX(1, var, 10)') }
113
+ end
114
+
115
+ context 'MIN statements' do
116
+ it { expect(grammar).to parse('MIN(3, 1, 2)') }
117
+ it { expect(grammar).to parse('MIN(-1, 2*4, (3-1)*2, 5, 6)') }
118
+ it { expect(grammar).to parse('MIN(1, -var, 10)') }
119
+ end
109
120
  end
@@ -6,91 +6,124 @@ describe Kalc::Interpreter do
6
6
  @transform = Kalc::Transform.new
7
7
  end
8
8
 
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 }
9
+ it { expect(evaluate('2 + 2')).to eq(4) }
10
+ it { expect(evaluate('1 + 1')).to eq(2.0) }
11
+ it { expect(evaluate('4 + 1')).to eq(5) }
12
+ it { expect(evaluate('5 + 5')).to eq(10) }
13
13
 
14
- it { evaluate('5 / 5').should == 1 }
14
+ it { expect(evaluate('5 / 5')).to eq(1) }
15
15
 
16
- it { evaluate('5 / 4 / 2').should == 0.625 }
17
- it { evaluate('5/4/2').should == 0.625 }
16
+ it { expect(evaluate('5 / 4 / 2')).to eq(0.625) }
17
+ it { expect(evaluate('5/4/2')).to eq(0.625) }
18
18
 
19
- it { evaluate('6 * 2 / 3').should == 4 }
19
+ it { expect(evaluate('6 * 2 / 3')).to eq(4) }
20
20
 
21
- it { evaluate('10 > 9').should == true }
22
- it { evaluate('10 < 9').should == false }
21
+ it { expect(evaluate('10 > 9')).to eq(true) }
22
+ it { expect(evaluate('10 < 9')).to eq(false) }
23
23
 
24
- it { evaluate('10 + 19 + 11 * 3').should == 62 }
24
+ it { expect(evaluate('10 + 19 + 11 * 3')).to eq(62) }
25
25
 
26
- it { evaluate('10 >= 10').should == true }
26
+ it { expect(evaluate('10 >= 10')).to eq(true) }
27
27
 
28
- it { evaluate('ABS(-1 + -2)').should == 3 }
28
+ it { expect(evaluate('ABS(-1 + -2)')).to eq(3) }
29
29
 
30
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
31
+ expect(evaluate('a := 1; 1 + a')).to eq(2)
32
+ expect(evaluate('a := 1; b := 2; 1 + b')).to eq(3)
33
33
  end
34
34
 
35
35
  it 'should be able to load quoted variables' do
36
- evaluate("'Item (a)' := 1; 1 + 'Item (a)'").should == 2
37
- evaluate("'a' := 1; 'b[a]' := 2 + 'a'; 1 + 'b[a]'").should == 4
36
+ expect(evaluate("'Item (a)' := 1; 1 + 'Item (a)'")).to eq(2)
37
+ expect(evaluate("'a' := 1; 'b[a]' := 2 + 'a'; 1 + 'b[a]'")).to eq(4)
38
38
  end
39
39
 
40
40
  it 'should be able to load single quoted variables' do
41
- evaluate("'a' := 1; 1 + 'a'").should == 2
42
- evaluate("'a' := 1; 'b' := 2; 'b' + 'a'").should == 3
41
+ expect(evaluate("'a' := 1; 1 + 'a'")).to eq(2)
42
+ expect(evaluate("'a' := 1; 'b' := 2; 'b' + 'a'")).to eq(3)
43
43
 
44
- evaluate("'a b' := 1; 'a b' + 1").should == 2
44
+ expect(evaluate("'a b' := 1; 'a b' + 1")).to eq(2)
45
45
  end
46
46
 
47
- it { evaluate('((75.0)*(25.0))+((125.0)*(25.0))+((150.0)*(25.0))+((250.0)*(25.0))').should == 15000 }
47
+ it { expect(evaluate('((75.0)*(25.0))+((125.0)*(25.0))+((150.0)*(25.0))+((250.0)*(25.0))')).to eq(15000) }
48
48
 
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 }
49
+ it { expect(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)')).to eq(1341.375) }
50
50
 
51
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 }
52
+ it { expect(evaluate('-2')).to eq(-2) }
53
+ it { expect(evaluate('-1000')).to eq(-1000) }
54
+ it { expect(evaluate('-1000.0001')).to eq(-1000.0001) }
55
+ it { expect(evaluate('1 + -1')).to eq(0 )}
56
+ it { expect(evaluate('1 + -10')).to eq(-9) }
57
57
  end
58
58
 
59
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 }
60
+ it { expect(evaluate('1 + +1')).to eq(2) }
61
+ it { expect(evaluate('1 + +1 - 1')).to eq(1) }
62
+ it { expect(evaluate('+10000.0001')).to eq(10000.0001) }
63
63
  end
64
64
 
65
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 }
66
+ it { expect(evaluate('TRUE')).to eq(true) }
67
+ it { expect(evaluate('FALSE')).to eq(false) }
68
+ it { expect(evaluate('FALSE || TRUE')).to eq(true) }
69
+ it { expect(evaluate('FALSE && TRUE')).to eq(false) }
70
70
  end
71
71
 
72
72
  context 'Decimal numbers' 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.2 - 1.0').should == 0.2 }
78
- it { evaluate('1.01 = 1.01').should == true }
79
- it { evaluate('1.01 = 1.02').should == false }
73
+ it { expect(evaluate('1.01')).to eq(1.01) }
74
+ it { expect(evaluate('1.01 + 0.02')).to eq(1.03) }
75
+ it { expect(evaluate('1.01 - 0.01')).to eq(1) }
76
+ it { expect(evaluate('1.1 + 1.1')).to eq(2.2) }
77
+ it { expect(evaluate('1.2 - 1.0')).to eq(0.2) }
78
+ it { expect(evaluate('1.01 = 1.01')).to eq(true) }
79
+ it { expect(evaluate('1.01 = 1.02')).to eq(false) }
80
80
  end
81
81
 
82
82
  context 'Ternary' do
83
- it { evaluate('1 > 2 ? 1 : 2').should == 2 }
83
+ it { expect(evaluate('1 > 2 ? 1 : 2')).to eq(2) }
84
84
  end
85
85
 
86
86
  context 'Exponents' do
87
- it { evaluate('1.23e+10').should == 12300000000.0 }
88
- it { evaluate('1.23e-10').should == 1.23e-10 }
87
+ it { expect(evaluate('1.23e+10')).to eq(12300000000.0) }
88
+ it { expect(evaluate('1.23e-10')).to eq(1.23e-10) }
89
89
  end
90
90
 
91
91
  context 'Numbers starting with a decimal point' do
92
- it { evaluate('0.4').should == 0.4 }
93
- it { evaluate('.4').should == 0.4 }
92
+ it { expect(evaluate('0.4')).to eq(0.4) }
93
+ it { expect(evaluate('.4')).to eq(0.4) }
94
+ end
95
+
96
+ context 'Min and Max Functions' do
97
+ it { expect(evaluate('MAX(3, 1, 2)')).to eq(3) }
98
+ it { expect(evaluate('MAX(-1, 2*4, (3-1)*2, 5, 6)')).to eq(8) }
99
+ it { expect(evaluate('MIN(3, 1, 2)')).to eq(1) }
100
+ it { expect(evaluate('MIN(-1, 2*4, (3-1)*2, 5, 6)')).to eq(-1) }
101
+ context 'having variables' do
102
+ it { expect(evaluate('var := 15; MAX(1, var, 10)')).to eq(15) }
103
+ it { expect(evaluate('var := 15; MIN(1, var, -10)')).to eq(-10) }
104
+ end
105
+ end
106
+
107
+ context 'Ceil and Floor functions' do
108
+ it { expect(evaluate('FLOOR(3.4)')).to eq(3) }
109
+ it { expect(evaluate('FLOOR(3.8)')).to eq(3) }
110
+ it { expect(evaluate('FLOOR(-3.4)')).to eq(-4) }
111
+ it { expect(evaluate('FLOOR(3)')).to eq(3) }
112
+ it { expect(evaluate('CEILING(3)')).to eq(3) }
113
+ it { expect(evaluate('CEILING(3.8)')).to eq(4) }
114
+ it { expect(evaluate('CEILING(3.8)')).to eq(4) }
115
+ it { expect(evaluate('CEILING(-3.2)')).to eq(-3) }
116
+
117
+ context 'having variables' do
118
+ it { expect(evaluate('var := 2.456; FLOOR(var)')).to eq(2) }
119
+ it { expect(evaluate('var := 2.444; CEILING(var)')).to eq(3) }
120
+ end
121
+ end
122
+
123
+ context 'Round function' do
124
+ it { expect(evaluate('ROUND(3.256,2)')).to eq(3.26) }
125
+ it { expect(evaluate('ROUND(3.2,2)')).to eq(3.20) }
126
+ it { expect(evaluate('ROUND(233.256,-2)')).to eq(200) }
94
127
  end
95
128
 
96
129
  private
data/spec/stdlib_spec.rb CHANGED
@@ -12,42 +12,42 @@ describe Kalc::Stdlib do
12
12
  end
13
13
 
14
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 }
15
+ it { expect(evaluate('PLUS_ONE(1)')).to eq(2) }
16
+ it { expect(evaluate('PLUS_ONE(101)')).to eq(102) }
17
+ it { expect(evaluate('PLUS_ONE(0)')).to eq(1) }
18
+ it { expect(evaluate('PLUS_ONE(-1)')).to eq(0) }
19
19
  end
20
20
 
21
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 }
22
+ it { expect(evaluate('MINUS_ONE(1)')).to eq(0) }
23
+ it { expect(evaluate('MINUS_ONE(101)')).to eq(100) }
24
+ it { expect(evaluate('MINUS_ONE(0)')).to eq(-1) }
25
+ it { expect(evaluate('MINUS_ONE(-1)')).to eq(-2) }
26
26
  end
27
27
 
28
28
  describe 'SQUARE' do
29
- it { evaluate('SQUARE(2)').should == 4 }
30
- it { evaluate('SQUARE(4)').should == 16 }
29
+ it { expect(evaluate('SQUARE(2)')).to eq(4) }
30
+ it { expect(evaluate('SQUARE(4)')).to eq(16) }
31
31
  end
32
32
 
33
33
  describe 'CUBE' do
34
- it { evaluate('CUBE(2)').should == 8 }
35
- it { evaluate('CUBE(4)').should == 64 }
34
+ it { expect(evaluate('CUBE(2)')).to eq(8) }
35
+ it { expect(evaluate('CUBE(4)')).to eq(64) }
36
36
  end
37
37
 
38
38
  describe 'FIB' do
39
- it { evaluate('FIB(1)').should == 1 }
40
- it { evaluate('FIB(10)').should == 55 }
39
+ it { expect(evaluate('FIB(1)')).to eq(1) }
40
+ it { expect(evaluate('FIB(10)')).to eq(55) }
41
41
  end
42
42
 
43
43
  describe 'FACTORIAL' do
44
- it { evaluate('FACTORIAL(1)').should == 1 }
45
- it { evaluate('FACTORIAL(5)').should == 120 }
44
+ it { expect(evaluate('FACTORIAL(1)')).to eq(1) }
45
+ it { expect(evaluate('FACTORIAL(5)')).to eq(120) }
46
46
  end
47
47
 
48
48
  describe 'TOWERS_OF_HANOI' do
49
- it { evaluate('TOWERS_OF_HANOI(4)').should == 15 }
50
- it { evaluate('TOWERS_OF_HANOI(10)').should == 1023 }
49
+ it { expect(evaluate('TOWERS_OF_HANOI(4)')).to eq(15) }
50
+ it { expect(evaluate('TOWERS_OF_HANOI(10)')).to eq(1023) }
51
51
  end
52
52
 
53
53
  private
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kalc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Chris Parker
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-12-12 00:00:00.000000000 Z
11
+ date: 2015-03-25 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: parslet
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '1.6'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '1.6'
62
55
  description: Calculation language slightly based on Excel's formula language.
@@ -93,27 +86,26 @@ files:
93
86
  - spec/stdlib_spec.rb
94
87
  homepage: https://github.com/mrcsparker/kalc
95
88
  licenses: []
89
+ metadata: {}
96
90
  post_install_message:
97
91
  rdoc_options: []
98
92
  require_paths:
99
93
  - lib
100
94
  required_ruby_version: !ruby/object:Gem::Requirement
101
- none: false
102
95
  requirements:
103
- - - ! '>='
96
+ - - ">="
104
97
  - !ruby/object:Gem::Version
105
98
  version: '0'
106
99
  required_rubygems_version: !ruby/object:Gem::Requirement
107
- none: false
108
100
  requirements:
109
- - - ! '>='
101
+ - - ">="
110
102
  - !ruby/object:Gem::Version
111
103
  version: '0'
112
104
  requirements: []
113
105
  rubyforge_project: kalc
114
- rubygems_version: 1.8.23.2
106
+ rubygems_version: 2.4.5
115
107
  signing_key:
116
- specification_version: 3
108
+ specification_version: 4
117
109
  summary: Small calculation language.
118
110
  test_files:
119
111
  - spec/grammar_spec.rb