dentaku 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dentaku/evaluator.rb +7 -0
- data/lib/dentaku/tokenizer.rb +3 -3
- data/lib/dentaku/version.rb +1 -1
- data/spec/evaluator_spec.rb +5 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/tokenizer_spec.rb +7 -0
- metadata +6 -12
data/lib/dentaku/evaluator.rb
CHANGED
@@ -20,6 +20,7 @@ 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_NOT = TokenMatcher.new(:function, :not)
|
23
24
|
|
24
25
|
T_NON_GROUP_STAR = TokenMatcher.new(:grouping).invert.star
|
25
26
|
|
@@ -37,11 +38,13 @@ module Dentaku
|
|
37
38
|
P_IF = [T_IF, T_OPEN, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_CLOSE]
|
38
39
|
P_ROUND_ONE = [T_ROUND, T_OPEN, T_NUMERIC, T_CLOSE]
|
39
40
|
P_ROUND_TWO = [T_ROUND, T_OPEN, T_NUMERIC, T_COMMA, T_NUMERIC, T_CLOSE]
|
41
|
+
P_NOT = [T_NOT, T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
|
40
42
|
|
41
43
|
RULES = [
|
42
44
|
[P_IF, :if],
|
43
45
|
[P_ROUND_ONE, :round],
|
44
46
|
[P_ROUND_TWO, :round],
|
47
|
+
[P_NOT, :not],
|
45
48
|
|
46
49
|
[P_GROUP, :evaluate_group],
|
47
50
|
[P_MATH_POW, :apply],
|
@@ -167,5 +170,9 @@ module Dentaku
|
|
167
170
|
|
168
171
|
Token.new(:numeric, value)
|
169
172
|
end
|
173
|
+
|
174
|
+
def not(*args)
|
175
|
+
Token.new(:logical, ! evaluate_token_stream(args[2..-2]).value)
|
176
|
+
end
|
170
177
|
end
|
171
178
|
end
|
data/lib/dentaku/tokenizer.rb
CHANGED
@@ -36,9 +36,9 @@ 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)\b', lambda {|raw| raw.strip.to_sym }),
|
41
|
-
TokenScanner.new(:identifier, '\w+\b',
|
39
|
+
TokenScanner.new(:combinator, '(and|or)\b', lambda {|raw| raw.strip.downcase.to_sym }),
|
40
|
+
TokenScanner.new(:function, '(if|round|not)\b', lambda {|raw| raw.strip.to_sym }),
|
41
|
+
TokenScanner.new(:identifier, '\w+\b', lambda {|raw| raw.downcase.to_sym })
|
42
42
|
]
|
43
43
|
|
44
44
|
LPAREN = TokenMatcher.new(:grouping, :open)
|
data/lib/dentaku/version.rb
CHANGED
data/spec/evaluator_spec.rb
CHANGED
@@ -75,6 +75,11 @@ describe Dentaku::Evaluator do
|
|
75
75
|
evaluator.evaluate(token_stream(5, :gt, 1, :or, :false)).should be_true
|
76
76
|
evaluator.evaluate(token_stream(5, :gt, 1, :and, :false)).should be_false
|
77
77
|
end
|
78
|
+
|
79
|
+
it 'should support negation of a logical value' do
|
80
|
+
evaluator.evaluate(token_stream(:not, :open, 5, :gt, 1, :or, :false, :close)).should be_false
|
81
|
+
evaluator.evaluate(token_stream(:not, :open, 5, :gt, 1, :and, :false, :close)).should be_true
|
82
|
+
end
|
78
83
|
end
|
79
84
|
end
|
80
85
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/tokenizer_spec.rb
CHANGED
@@ -98,5 +98,12 @@ describe Dentaku::Tokenizer do
|
|
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
100
|
end
|
101
|
+
|
102
|
+
it 'include NOT' do
|
103
|
+
tokens = tokenizer.tokenize('not(8 < 5)')
|
104
|
+
tokens.length.should eq(6)
|
105
|
+
tokens.map(&:category).should eq([:function, :grouping, :numeric, :comparator, :numeric, :grouping])
|
106
|
+
tokens.map(&:value).should eq([:not, :open, 8, :lt, 5, :close])
|
107
|
+
end
|
101
108
|
end
|
102
109
|
end
|
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.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70134892086100 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70134892086100
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70134892085420 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70134892085420
|
36
36
|
description: ! ' Dentaku is a parser and evaluator for mathematical formulas
|
37
37
|
|
38
38
|
'
|
@@ -75,18 +75,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
-
segments:
|
79
|
-
- 0
|
80
|
-
hash: 2332951367847572293
|
81
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
79
|
none: false
|
83
80
|
requirements:
|
84
81
|
- - ! '>='
|
85
82
|
- !ruby/object:Gem::Version
|
86
83
|
version: '0'
|
87
|
-
segments:
|
88
|
-
- 0
|
89
|
-
hash: 2332951367847572293
|
90
84
|
requirements: []
|
91
85
|
rubyforge_project: dentaku
|
92
86
|
rubygems_version: 1.8.11
|