dentaku 0.2.8 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dentaku/evaluator.rb +20 -0
- data/lib/dentaku/tokenizer.rb +4 -3
- data/lib/dentaku/version.rb +1 -1
- data/spec/evaluator_spec.rb +3 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/tokenizer_spec.rb +11 -1
- metadata +4 -4
data/lib/dentaku/evaluator.rb
CHANGED
@@ -20,6 +20,8 @@ module Dentaku
|
|
20
20
|
T_COMBINATOR = TokenMatcher.new(:combinator)
|
21
21
|
T_IF = TokenMatcher.new(:function, :if)
|
22
22
|
T_ROUND = TokenMatcher.new(:function, :round)
|
23
|
+
T_ROUNDUP = TokenMatcher.new(:function, :roundup)
|
24
|
+
T_ROUNDDOWN = TokenMatcher.new(:function, :rounddown)
|
23
25
|
T_NOT = TokenMatcher.new(:function, :not)
|
24
26
|
|
25
27
|
T_NON_GROUP_STAR = TokenMatcher.new(:grouping).invert.star
|
@@ -38,12 +40,16 @@ module Dentaku
|
|
38
40
|
P_IF = [T_IF, T_OPEN, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_CLOSE]
|
39
41
|
P_ROUND_ONE = [T_ROUND, T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
|
40
42
|
P_ROUND_TWO = [T_ROUND, T_OPEN, T_NON_GROUP_STAR, T_COMMA, T_NUMERIC, T_CLOSE]
|
43
|
+
P_ROUNDUP = [T_ROUNDUP, T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
|
44
|
+
P_ROUNDDOWN = [T_ROUNDDOWN, T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
|
41
45
|
P_NOT = [T_NOT, T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
|
42
46
|
|
43
47
|
RULES = [
|
44
48
|
[P_IF, :if],
|
45
49
|
[P_ROUND_ONE, :round],
|
46
50
|
[P_ROUND_TWO, :round],
|
51
|
+
[P_ROUNDUP, :roundup],
|
52
|
+
[P_ROUNDDOWN, :rounddown],
|
47
53
|
[P_NOT, :not],
|
48
54
|
|
49
55
|
[P_GROUP, :evaluate_group],
|
@@ -171,6 +177,20 @@ module Dentaku
|
|
171
177
|
Token.new(:numeric, value)
|
172
178
|
end
|
173
179
|
|
180
|
+
def roundup(*args)
|
181
|
+
_, _, *tokens, _ = args
|
182
|
+
|
183
|
+
value = evaluate_token_stream(tokens).value
|
184
|
+
Token.new(:numeric, value.ceil)
|
185
|
+
end
|
186
|
+
|
187
|
+
def rounddown(*args)
|
188
|
+
_, _, *tokens, _ = args
|
189
|
+
|
190
|
+
value = evaluate_token_stream(tokens).value
|
191
|
+
Token.new(:numeric, value.floor)
|
192
|
+
end
|
193
|
+
|
174
194
|
def not(*args)
|
175
195
|
Token.new(:logical, ! evaluate_token_stream(args[2..-2]).value)
|
176
196
|
end
|
data/lib/dentaku/tokenizer.rb
CHANGED
@@ -36,9 +36,10 @@ module Dentaku
|
|
36
36
|
when '=' then :eq
|
37
37
|
end
|
38
38
|
end),
|
39
|
-
TokenScanner.new(:combinator, '(and|or)\b',
|
40
|
-
TokenScanner.new(:function, '(if|round|not)\b',
|
41
|
-
|
39
|
+
TokenScanner.new(:combinator, '(and|or)\b', lambda {|raw| raw.strip.downcase.to_sym }),
|
40
|
+
TokenScanner.new(:function, '(if|round(up|down)?|not)\b',
|
41
|
+
lambda {|raw| raw.strip.downcase.to_sym }),
|
42
|
+
TokenScanner.new(:identifier, '\w+\b', lambda {|raw| raw.strip.downcase.to_sym })
|
42
43
|
]
|
43
44
|
|
44
45
|
LPAREN = TokenMatcher.new(:grouping, :open)
|
data/lib/dentaku/version.rb
CHANGED
data/spec/evaluator_spec.rb
CHANGED
@@ -57,7 +57,9 @@ describe Dentaku::Evaluator do
|
|
57
57
|
|
58
58
|
describe 'functions' do
|
59
59
|
it 'should evaluate function' do
|
60
|
-
evaluator.evaluate(token_stream(:round,
|
60
|
+
evaluator.evaluate(token_stream(:round, :open, 5, :divide, 3.0, :close)).should eq 2
|
61
|
+
evaluator.evaluate(token_stream(:roundup, :open, 5, :divide, 1.2, :close)).should eq 5
|
62
|
+
evaluator.evaluate(token_stream(:rounddown, :open, 5, :divide, 1.2, :close)).should eq 4
|
61
63
|
end
|
62
64
|
end
|
63
65
|
|
data/spec/spec_helper.rb
CHANGED
data/spec/tokenizer_spec.rb
CHANGED
@@ -87,7 +87,7 @@ describe Dentaku::Tokenizer do
|
|
87
87
|
tokens.map(&:value).should eq([:if, :open, :x, :lt, 10, :comma, :y, :comma, :z, :close])
|
88
88
|
end
|
89
89
|
|
90
|
-
it 'include ROUND' do
|
90
|
+
it 'include ROUND/UP/DOWN' do
|
91
91
|
tokens = tokenizer.tokenize('round(8.2)')
|
92
92
|
tokens.length.should eq(4)
|
93
93
|
tokens.map(&:category).should eq([:function, :grouping, :numeric, :grouping])
|
@@ -97,6 +97,16 @@ describe Dentaku::Tokenizer do
|
|
97
97
|
tokens.length.should eq(6)
|
98
98
|
tokens.map(&:category).should eq([:function, :grouping, :numeric, :grouping, :numeric, :grouping])
|
99
99
|
tokens.map(&:value).should eq([:round, :open, 8.75, :comma, 1, :close])
|
100
|
+
|
101
|
+
tokens = tokenizer.tokenize('ROUNDUP(8.2)')
|
102
|
+
tokens.length.should eq(4)
|
103
|
+
tokens.map(&:category).should eq([:function, :grouping, :numeric, :grouping])
|
104
|
+
tokens.map(&:value).should eq([:roundup, :open, 8.2, :close])
|
105
|
+
|
106
|
+
tokens = tokenizer.tokenize('RoundDown(8.2)')
|
107
|
+
tokens.length.should eq(4)
|
108
|
+
tokens.map(&:category).should eq([:function, :grouping, :numeric, :grouping])
|
109
|
+
tokens.map(&:value).should eq([:rounddown, :open, 8.2, :close])
|
100
110
|
end
|
101
111
|
|
102
112
|
it 'include NOT' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dentaku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -87,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
segments:
|
89
89
|
- 0
|
90
|
-
hash:
|
90
|
+
hash: -4230531870219194848
|
91
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
92
|
none: false
|
93
93
|
requirements:
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
segments:
|
98
98
|
- 0
|
99
|
-
hash:
|
99
|
+
hash: -4230531870219194848
|
100
100
|
requirements: []
|
101
101
|
rubyforge_project: dentaku
|
102
102
|
rubygems_version: 1.8.23
|