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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a86a3a471fd726634a4f8417a6ce273c2a06c577
4
- data.tar.gz: 6c806defc93c0a68a7a4cf175a76dbbdcfdbb41c
3
+ metadata.gz: 50725a89c0d8187d980f2a8f250a333c52e6c0c0
4
+ data.tar.gz: f684b6d36c46254dc2dc8336c613c25c8b0e3097
5
5
  SHA512:
6
- metadata.gz: 524bc87684c499ac83d0a715b271ae471f39ec53dc8514f05ad2eae9c41c4c2cc4f2909dd8a7ca5293d067141eab0a71537072609443d08b33a0cbfb8a28d77a
7
- data.tar.gz: 143504c4a02aa61d35ba2103ced6c879a754b12c2304ecee67ff6381b2fb4c4830462868c24de5db014ccd99eece7a3b2774b91f3c79fb305dcca6a42e7fbc87
6
+ metadata.gz: c07c402094c36cd412a173c8edfe16ac4b2436627cc20aede88a11a4c8fe091d2bc3bc37cc5da479955fb467a7d039ec1864b947a55ee08dd1d7e621f43169a4
7
+ data.tar.gz: 1cdddcef2cc96ee32753d038dfc39ea9a8ba235ee99b2e90026eae727b3bb2395fea0948d74f4f2fa8c98b6458214dc56fe4b3bf6f76b8e9e3649c5a92951668
data/lib/kalculator.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "rltk"
2
2
  require "kalculator/data_sources"
3
+ require "kalculator/errors"
3
4
  require "kalculator/evaluator"
4
5
  require "kalculator/lexer"
5
6
  require "kalculator/parser"
@@ -0,0 +1,5 @@
1
+ class Kalculator
2
+ class Error < ::StandardError; end
3
+ class TypeError < Error; end
4
+ class UndefinedVariableError < Error; end
5
+ end
@@ -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).inject(0){|sum, num| sum + num}
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
@@ -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 }
@@ -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
@@ -1,3 +1,3 @@
1
1
  class Kalculator
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
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.0
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