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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4fd757c1bc075fe247ba612bf8e3670d6fc03711c332fa297d881513deda678
4
- data.tar.gz: 1a6a5a2d27345d274d8559ea2b0c8a3cc3551db9012da35123f3ad0ce140c22d
3
+ metadata.gz: 9d38e426af60ed279a79b3764e035b3564623f702590337f28958aff8ea5ef1c
4
+ data.tar.gz: 6409234b6be3c95af8c79071f49363d277b15a7a707e6ea1bcf7056d15b0fcda
5
5
  SHA512:
6
- metadata.gz: 28573ad987c775b85a49c31cd74d2bd13b7344c66f8c7d937d1db80a286fd6d3c9ecbb149ef50d7ffc44ce0e4bf516b3f1f935c0edf0e7c4b1b773061a4e7dd1
7
- data.tar.gz: d34ab516d686fcb3428e901625c07b00cd30bd9d67eb43d124b44358e04cc2a3de1abb0dd2adfd89eb9bfcec7f0870227fc5fe28717d07cdaca0b599fc7df36d
6
+ metadata.gz: 676a268b7e66af2752dde9ea44de1c3e56074399220cfe696d89fec1ae85d790723682e4a9a11a2ba56f4013da79d6dd106903b185578e2d76df4ead8166d624
7
+ data.tar.gz: ecdd4b0f06b52a320c7d80fbbfd56c778d2ad9c498a71028a3a45cc78040b66fa8e82d426d14f3a2ddcb379804f924b9af9964173d1b78ad7f93f083a3ff3d2e
@@ -56,13 +56,16 @@ class Kalculator
56
56
  boolean
57
57
  end
58
58
 
59
- def contains(_, string, substring)
60
- string = evaluate(string)
61
- substring = evaluate(substring)
62
- unless string.is_a?(String) && substring.is_a?(String)
63
- raise TypeError, "contains only works with strings, got #{string.inspect} and #{substring.inspect}"
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
@@ -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(/,/) { :COMMA }
7
- rule(/\+/) { :PLUS }
8
- rule(/-/) { :SUB }
9
- rule(/\*/) { :MUL }
10
- rule(/\//) { :DIV }
11
- rule(/>/) { :GT }
12
- rule(/>=/) { :GTE }
13
- rule(/</) { :LT }
14
- rule(/<=/) { :LTE }
15
- rule(/==/) { :EQ }
16
- rule(/AND/) { :AND }
17
- rule(/OR/) { :OR }
18
- rule(/\d+/) { |t| [:NUMBER, t.to_i] }
19
- rule(/\.\d+/) { |t| [:NUMBER, t.to_f] }
20
- rule(/\d+\.\d+/) { |t| [:NUMBER, t.to_f] }
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 }
@@ -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 |_, _, string, _, substring, _|
11
- [:contains, string, substring]
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
@@ -1,3 +1,3 @@
1
1
  class Kalculator
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
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.1
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-05-16 00:00:00.000000000 Z
11
+ date: 2018-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rltk