kalculator 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kalculator.rb +1 -0
- data/lib/kalculator/errors.rb +5 -0
- data/lib/kalculator/evaluator.rb +15 -2
- data/lib/kalculator/lexer.rb +1 -0
- data/lib/kalculator/parser.rb +3 -0
- data/lib/kalculator/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50725a89c0d8187d980f2a8f250a333c52e6c0c0
|
4
|
+
data.tar.gz: f684b6d36c46254dc2dc8336c613c25c8b0e3097
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c07c402094c36cd412a173c8edfe16ac4b2436627cc20aede88a11a4c8fe091d2bc3bc37cc5da479955fb467a7d039ec1864b947a55ee08dd1d7e621f43169a4
|
7
|
+
data.tar.gz: 1cdddcef2cc96ee32753d038dfc39ea9a8ba235ee99b2e90026eae727b3bb2395fea0948d74f4f2fa8c98b6458214dc56fe4b3bf6f76b8e9e3649c5a92951668
|
data/lib/kalculator.rb
CHANGED
data/lib/kalculator/evaluator.rb
CHANGED
@@ -48,6 +48,15 @@ class Kalculator
|
|
48
48
|
boolean
|
49
49
|
end
|
50
50
|
|
51
|
+
def contains(_, string, substring)
|
52
|
+
string = evaluate(string)
|
53
|
+
substring = evaluate(substring)
|
54
|
+
unless string.is_a?(String) && substring.is_a?(String)
|
55
|
+
raise TypeError, "contains only works with strings, got #{string.inspect} and #{substring.inspect}"
|
56
|
+
end
|
57
|
+
string.include?(substring)
|
58
|
+
end
|
59
|
+
|
51
60
|
def if(_, condition, true_clause, false_clause)
|
52
61
|
if evaluate(condition)
|
53
62
|
evaluate(true_clause)
|
@@ -65,12 +74,16 @@ class Kalculator
|
|
65
74
|
end
|
66
75
|
|
67
76
|
def sum(_, array)
|
68
|
-
evaluate(array)
|
77
|
+
array = evaluate(array)
|
78
|
+
unless array.is_a?(Array) && array.all?{|n| n.is_a?(Numeric)}
|
79
|
+
raise TypeError, "sum only works with lists of numbers, got #{array.inspect}"
|
80
|
+
end
|
81
|
+
array.inject(0){|sum, num| sum + num}
|
69
82
|
end
|
70
83
|
|
71
84
|
def variable(_, names)
|
72
85
|
names.inject(@data_source) do |source, name|
|
73
|
-
raise "undefined variable #{names.join(".")} (could not find #{name} in #{source}" unless source.key?(name)
|
86
|
+
raise UndefinedVariableError, "undefined variable #{names.join(".")} (could not find #{name} in #{source}" unless source.key?(name)
|
74
87
|
source[name]
|
75
88
|
end
|
76
89
|
end
|
data/lib/kalculator/lexer.rb
CHANGED
@@ -16,6 +16,7 @@ class Kalculator
|
|
16
16
|
rule(/\d+/) { |t| [:NUMBER, t.to_i] }
|
17
17
|
rule(/\.\d+/) { |t| [:NUMBER, t.to_f] }
|
18
18
|
rule(/\d+\.\d+/) { |t| [:NUMBER, t.to_f] }
|
19
|
+
rule(/contains/) { |t| :CONTAINS }
|
19
20
|
rule(/if/) { |t| :IF }
|
20
21
|
rule(/sum/) { |t| :SUM }
|
21
22
|
rule(/true/) { |t| :TRUE }
|
data/lib/kalculator/parser.rb
CHANGED
@@ -7,6 +7,9 @@ class Kalculator
|
|
7
7
|
#Left 600 '.'.
|
8
8
|
|
9
9
|
production(:expression) do
|
10
|
+
clause('CONTAINS LPAREN expression COMMA expression RPAREN') do |_, _, string, _, substring, _|
|
11
|
+
[:contains, string, substring]
|
12
|
+
end
|
10
13
|
clause('IF LPAREN expression COMMA expression COMMA expression RPAREN') do |_, _, condition, _, true_clause, _, false_clause, _|
|
11
14
|
[:if, condition, true_clause, false_clause]
|
12
15
|
end
|
data/lib/kalculator/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kalculator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Ries
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- kalculator.gemspec
|
91
91
|
- lib/kalculator.rb
|
92
92
|
- lib/kalculator/data_sources.rb
|
93
|
+
- lib/kalculator/errors.rb
|
93
94
|
- lib/kalculator/evaluator.rb
|
94
95
|
- lib/kalculator/lexer.rb
|
95
96
|
- lib/kalculator/parser.rb
|