kalculator 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kalculator/evaluator.rb +13 -6
- data/lib/kalculator/lexer.rb +19 -17
- data/lib/kalculator/parser.rb +8 -2
- data/lib/kalculator/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d38e426af60ed279a79b3764e035b3564623f702590337f28958aff8ea5ef1c
|
4
|
+
data.tar.gz: 6409234b6be3c95af8c79071f49363d277b15a7a707e6ea1bcf7056d15b0fcda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 676a268b7e66af2752dde9ea44de1c3e56074399220cfe696d89fec1ae85d790723682e4a9a11a2ba56f4013da79d6dd106903b185578e2d76df4ead8166d624
|
7
|
+
data.tar.gz: ecdd4b0f06b52a320c7d80fbbfd56c778d2ad9c498a71028a3a45cc78040b66fa8e82d426d14f3a2ddcb379804f924b9af9964173d1b78ad7f93f083a3ff3d2e
|
data/lib/kalculator/evaluator.rb
CHANGED
@@ -56,13 +56,16 @@ class Kalculator
|
|
56
56
|
boolean
|
57
57
|
end
|
58
58
|
|
59
|
-
def contains(_,
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
59
|
+
def contains(_, collection, item)
|
60
|
+
collection = evaluate(collection)
|
61
|
+
item = evaluate(item)
|
62
|
+
if collection.is_a?(Array)
|
63
|
+
collection.include?(item)
|
64
|
+
elsif collection.is_a?(String) && item.is_a?(String)
|
65
|
+
collection.include?(item)
|
66
|
+
else
|
67
|
+
raise TypeError, "contains only works with strings or lists, got #{collection.inspect} and #{item.inspect}"
|
64
68
|
end
|
65
|
-
string.include?(substring)
|
66
69
|
end
|
67
70
|
|
68
71
|
def if(_, condition, true_clause, false_clause)
|
@@ -73,6 +76,10 @@ class Kalculator
|
|
73
76
|
end
|
74
77
|
end
|
75
78
|
|
79
|
+
def list(_, expressions)
|
80
|
+
expressions.map{|expression| evaluate(expression) }
|
81
|
+
end
|
82
|
+
|
76
83
|
def number(_, number)
|
77
84
|
number
|
78
85
|
end
|
data/lib/kalculator/lexer.rb
CHANGED
@@ -1,23 +1,25 @@
|
|
1
1
|
class Kalculator
|
2
2
|
class Lexer < RLTK::Lexer
|
3
3
|
rule(/\s/)
|
4
|
-
rule(/\(/) { :LPAREN
|
5
|
-
rule(/\)/) { :RPAREN
|
6
|
-
rule(
|
7
|
-
rule(
|
8
|
-
rule(
|
9
|
-
rule(
|
10
|
-
rule(
|
11
|
-
rule(
|
12
|
-
rule(
|
13
|
-
rule(
|
14
|
-
rule(
|
15
|
-
rule(
|
16
|
-
rule(
|
17
|
-
rule(
|
18
|
-
rule(
|
19
|
-
rule(
|
20
|
-
rule(
|
4
|
+
rule(/\(/) { :LPAREN }
|
5
|
+
rule(/\)/) { :RPAREN }
|
6
|
+
rule(/\[/) { :LBRACKET }
|
7
|
+
rule(/\]/) { :RBRACKET }
|
8
|
+
rule(/,/) { :COMMA }
|
9
|
+
rule(/\+/) { :PLUS }
|
10
|
+
rule(/-/) { :SUB }
|
11
|
+
rule(/\*/) { :MUL }
|
12
|
+
rule(/\//) { :DIV }
|
13
|
+
rule(/>/) { :GT }
|
14
|
+
rule(/>=/) { :GTE }
|
15
|
+
rule(/</) { :LT }
|
16
|
+
rule(/<=/) { :LTE }
|
17
|
+
rule(/==/) { :EQ }
|
18
|
+
rule(/AND/) { :AND }
|
19
|
+
rule(/OR/) { :OR }
|
20
|
+
rule(/\-?\d+/) { |t| [:NUMBER, t.to_i] }
|
21
|
+
rule(/\-?\.\d+/) { |t| [:NUMBER, t.to_f] }
|
22
|
+
rule(/\-?\d+\.\d+/) { |t| [:NUMBER, t.to_f] }
|
21
23
|
rule(/contains/) { |t| :CONTAINS }
|
22
24
|
rule(/if/) { |t| :IF }
|
23
25
|
rule(/sum/) { |t| :SUM }
|
data/lib/kalculator/parser.rb
CHANGED
@@ -7,8 +7,8 @@ class Kalculator
|
|
7
7
|
#Left 600 '.'.
|
8
8
|
|
9
9
|
production(:expression) do
|
10
|
-
clause('CONTAINS LPAREN expression COMMA expression RPAREN') do |_, _,
|
11
|
-
[:contains,
|
10
|
+
clause('CONTAINS LPAREN expression COMMA expression RPAREN') do |_, _, collection, _, item, _|
|
11
|
+
[:contains, collection, item]
|
12
12
|
end
|
13
13
|
clause('IF LPAREN expression COMMA expression COMMA expression RPAREN') do |_, _, condition, _, true_clause, _, false_clause, _|
|
14
14
|
[:if, condition, true_clause, false_clause]
|
@@ -17,6 +17,7 @@ class Kalculator
|
|
17
17
|
[:sum, e0]
|
18
18
|
end
|
19
19
|
clause('LPAREN expression RPAREN') { |_, expression, _| expression }
|
20
|
+
clause('LBRACKET expressions RBRACKET') { |_, expressions, _| [:list, expressions] }
|
20
21
|
|
21
22
|
clause('NUMBER') { |n| [:number, n] }
|
22
23
|
clause('STRING') { |s| [:string, s] }
|
@@ -39,6 +40,11 @@ class Kalculator
|
|
39
40
|
clause('expression DIV expression') { |e0, _, e1| [:/, e0, e1] }
|
40
41
|
end
|
41
42
|
|
43
|
+
production(:expressions) do
|
44
|
+
clause('expression') { |expression| [expression] }
|
45
|
+
clause('expression COMMA expressions') { |expression, _, expressions| [expression].concat(expressions) }
|
46
|
+
end
|
47
|
+
|
42
48
|
finalize()
|
43
49
|
end
|
44
50
|
end
|
data/lib/kalculator/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kalculator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Ries
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rltk
|