dentaku 0.2.13 → 0.2.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/dentaku.gemspec +1 -0
- data/lib/dentaku/evaluator.rb +6 -2
- data/lib/dentaku/rules.rb +8 -3
- data/lib/dentaku/version.rb +1 -1
- data/spec/evaluator_spec.rb +6 -1
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2786a55545e9a2472089d941fa6611aea6124991
|
4
|
+
data.tar.gz: 312eacef50b85dac484b9166c378a7ca359d305c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdd35ea4a57f82c6f3c1f9e65ae28fa73d2862e7729225ce714b976466650a2e09473ea3a25c06a18fbaa76e8665294526fed8a3451649140855b79f38337a31
|
7
|
+
data.tar.gz: ea460a2b63b8318bb03a51ecb6c1370794da332395241357f2b486d2495e4079489894a7271b96d4464fac734c4af96caced5cdd3248505726e0841604a6f05c
|
data/dentaku.gemspec
CHANGED
@@ -8,6 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Solomon White"]
|
9
9
|
s.email = ["rubysolo@gmail.com"]
|
10
10
|
s.homepage = "http://github.com/rubysolo/dentaku"
|
11
|
+
s.licenses = %w(MIT)
|
11
12
|
s.summary = %q{A formula language parser and evaluator}
|
12
13
|
s.description = <<-DESC
|
13
14
|
Dentaku is a parser and evaluator for mathematical formulas
|
data/lib/dentaku/evaluator.rb
CHANGED
@@ -75,7 +75,11 @@ module Dentaku
|
|
75
75
|
end
|
76
76
|
|
77
77
|
def negate(_, token)
|
78
|
-
Token.new(token.category,token.value * -1)
|
78
|
+
Token.new(token.category, token.value * -1)
|
79
|
+
end
|
80
|
+
|
81
|
+
def percentage(token, _)
|
82
|
+
Token.new(token.category, token.value / 100.0)
|
79
83
|
end
|
80
84
|
|
81
85
|
def expand_range(left, oper1, middle, oper2, right)
|
@@ -83,7 +87,7 @@ module Dentaku
|
|
83
87
|
end
|
84
88
|
|
85
89
|
def if(*args)
|
86
|
-
|
90
|
+
_if, _open, condition, _, true_value, _, false_value, _close = args
|
87
91
|
|
88
92
|
if condition.value
|
89
93
|
true_value
|
data/lib/dentaku/rules.rb
CHANGED
@@ -17,6 +17,7 @@ module Dentaku
|
|
17
17
|
[ p(:math_mod), :apply ],
|
18
18
|
[ p(:math_mul), :apply ],
|
19
19
|
[ p(:math_add), :apply ],
|
20
|
+
[ p(:percentage), :percentage ],
|
20
21
|
[ p(:negation), :negate ],
|
21
22
|
[ p(:range_asc), :expand_range ],
|
22
23
|
[ p(:range_desc), :expand_range ],
|
@@ -55,7 +56,12 @@ module Dentaku
|
|
55
56
|
end
|
56
57
|
|
57
58
|
def self.t(name)
|
58
|
-
@matchers ||=
|
59
|
+
@matchers ||= generate_matchers
|
60
|
+
@matchers[name]
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.generate_matchers
|
64
|
+
[
|
59
65
|
:numeric, :string, :addsub, :subtract, :muldiv, :pow, :mod,
|
60
66
|
:comparator, :comp_gt, :comp_lt,
|
61
67
|
:open, :close, :comma,
|
@@ -65,8 +71,6 @@ module Dentaku
|
|
65
71
|
].each_with_object({}) do |name, matchers|
|
66
72
|
matchers[name] = TokenMatcher.send(name)
|
67
73
|
end
|
68
|
-
|
69
|
-
@matchers[name]
|
70
74
|
end
|
71
75
|
|
72
76
|
def self.p(name)
|
@@ -77,6 +81,7 @@ module Dentaku
|
|
77
81
|
math_pow: pattern(:numeric, :pow, :numeric),
|
78
82
|
math_mod: pattern(:numeric, :mod, :numeric),
|
79
83
|
negation: pattern(:subtract, :numeric),
|
84
|
+
percentage: pattern(:numeric, :mod),
|
80
85
|
range_asc: pattern(:numeric, :comp_lt, :numeric, :comp_lt, :numeric),
|
81
86
|
range_desc: pattern(:numeric, :comp_gt, :numeric, :comp_gt, :numeric),
|
82
87
|
num_comp: pattern(:numeric, :comparator, :numeric),
|
data/lib/dentaku/version.rb
CHANGED
data/spec/evaluator_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe Dentaku::Evaluator do
|
|
8
8
|
it 'should find a matching rule' do
|
9
9
|
rule = [Dentaku::TokenMatcher.new(:numeric, nil)]
|
10
10
|
stream = [Dentaku::Token.new(:numeric, 1), Dentaku::Token.new(:operator, :add), Dentaku::Token.new(:numeric, 1)]
|
11
|
-
position,
|
11
|
+
position, _match = evaluator.find_rule_match(rule, stream)
|
12
12
|
position.should eq(0)
|
13
13
|
end
|
14
14
|
end
|
@@ -46,6 +46,11 @@ describe Dentaku::Evaluator do
|
|
46
46
|
evaluator.evaluate(token_stream(1, :subtract, :subtract, 1)).should eq(2)
|
47
47
|
end
|
48
48
|
|
49
|
+
it 'supports unary percentage' do
|
50
|
+
evaluator.evaluate(token_stream(50, :mod)).should eq(0.5)
|
51
|
+
evaluator.evaluate(token_stream(50, :mod, :multiply, 100)).should eq(50)
|
52
|
+
end
|
53
|
+
|
49
54
|
describe 'maths' do
|
50
55
|
it 'should perform addition' do
|
51
56
|
evaluator.evaluate(token_stream(1, :add, 1)).should eq(2)
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
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.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Solomon White
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: |2
|
@@ -46,8 +46,8 @@ executables: []
|
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
48
48
|
files:
|
49
|
-
- .gitignore
|
50
|
-
- .travis.yml
|
49
|
+
- ".gitignore"
|
50
|
+
- ".travis.yml"
|
51
51
|
- Gemfile
|
52
52
|
- README.md
|
53
53
|
- Rakefile
|
@@ -73,7 +73,8 @@ files:
|
|
73
73
|
- spec/token_spec.rb
|
74
74
|
- spec/tokenizer_spec.rb
|
75
75
|
homepage: http://github.com/rubysolo/dentaku
|
76
|
-
licenses:
|
76
|
+
licenses:
|
77
|
+
- MIT
|
77
78
|
metadata: {}
|
78
79
|
post_install_message:
|
79
80
|
rdoc_options: []
|
@@ -81,17 +82,17 @@ require_paths:
|
|
81
82
|
- lib
|
82
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
84
|
requirements:
|
84
|
-
- -
|
85
|
+
- - ">="
|
85
86
|
- !ruby/object:Gem::Version
|
86
87
|
version: '0'
|
87
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
89
|
requirements:
|
89
|
-
- -
|
90
|
+
- - ">="
|
90
91
|
- !ruby/object:Gem::Version
|
91
92
|
version: '0'
|
92
93
|
requirements: []
|
93
94
|
rubyforge_project: dentaku
|
94
|
-
rubygems_version: 2.0
|
95
|
+
rubygems_version: 2.2.0
|
95
96
|
signing_key:
|
96
97
|
specification_version: 4
|
97
98
|
summary: A formula language parser and evaluator
|