dentaku 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dentaku/evaluator.rb +7 -1
- data/lib/dentaku/token_scanner.rb +1 -1
- data/lib/dentaku/tokenizer.rb +5 -5
- data/lib/dentaku/version.rb +1 -1
- data/spec/calculator_spec.rb +5 -0
- metadata +51 -32
data/lib/dentaku/evaluator.rb
CHANGED
@@ -131,7 +131,13 @@ module Dentaku
|
|
131
131
|
places = args.shift.value
|
132
132
|
end
|
133
133
|
|
134
|
-
|
134
|
+
begin
|
135
|
+
value = input.round(places)
|
136
|
+
rescue ArgumentError
|
137
|
+
value = (input * 10 ** places).round / (10 ** places).to_f
|
138
|
+
end
|
139
|
+
|
140
|
+
Token.new(:numeric, value)
|
135
141
|
end
|
136
142
|
end
|
137
143
|
end
|
data/lib/dentaku/tokenizer.rb
CHANGED
@@ -7,8 +7,8 @@ module Dentaku
|
|
7
7
|
SCANNERS = [
|
8
8
|
TokenScanner.new(:whitespace, '\s+'),
|
9
9
|
TokenScanner.new(:numeric, '(\d+(\.\d+)?|\.\d+)', lambda{|raw| raw =~ /\./ ? raw.to_f : raw.to_i }),
|
10
|
-
TokenScanner.new(:string, '"[^"]*"',
|
11
|
-
TokenScanner.new(:string, "'[^']*'",
|
10
|
+
TokenScanner.new(:string, '"[^"]*"', lambda{|raw| raw.gsub(/^"|"$/, '') }),
|
11
|
+
TokenScanner.new(:string, "'[^']*'", lambda{|raw| raw.gsub(/^'|'$/, '') }),
|
12
12
|
TokenScanner.new(:operator, '\+|-|\*|\/', lambda do |raw|
|
13
13
|
case raw
|
14
14
|
when '+' then :add
|
@@ -35,9 +35,9 @@ module Dentaku
|
|
35
35
|
when '=' then :eq
|
36
36
|
end
|
37
37
|
end),
|
38
|
-
TokenScanner.new(:combinator, '(and|or)\b',
|
38
|
+
TokenScanner.new(:combinator, '(and|or)\b', lambda {|raw| raw.strip.downcase.to_sym }),
|
39
39
|
TokenScanner.new(:function, '(if|round)\b', lambda {|raw| raw.strip.to_sym }),
|
40
|
-
TokenScanner.new(:identifier, '[
|
40
|
+
TokenScanner.new(:identifier, '[a-z_]+', lambda {|raw| raw.downcase.to_sym })
|
41
41
|
]
|
42
42
|
|
43
43
|
LPAREN = TokenMatcher.new(:grouping, :open)
|
@@ -46,7 +46,7 @@ module Dentaku
|
|
46
46
|
def tokenize(string)
|
47
47
|
nesting = 0
|
48
48
|
tokens = []
|
49
|
-
input = string.dup
|
49
|
+
input = string.dup
|
50
50
|
|
51
51
|
until input.empty?
|
52
52
|
raise "parse error at: '#{ input }'" unless SCANNERS.any? do |scanner|
|
data/lib/dentaku/version.rb
CHANGED
data/spec/calculator_spec.rb
CHANGED
@@ -55,6 +55,11 @@ describe Dentaku::Calculator do
|
|
55
55
|
calculator.evaluate('fruit = "apple"', :fruit => 'pear').should be_false
|
56
56
|
end
|
57
57
|
|
58
|
+
it 'should do case-sensitive comparison' do
|
59
|
+
calculator.evaluate('fruit = "Apple"', :fruit => 'apple').should be_false
|
60
|
+
calculator.evaluate('fruit = "Apple"', :fruit => 'Apple').should be_true
|
61
|
+
end
|
62
|
+
|
58
63
|
describe 'functions' do
|
59
64
|
it 'should include IF' do
|
60
65
|
calculator.evaluate('if (foo < 8, 10, 20)', :foo => 2).should eq(10)
|
metadata
CHANGED
@@ -1,36 +1,46 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: dentaku
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Solomon White
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-02-22 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: rspec
|
16
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
22
32
|
type: :development
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
'
|
28
|
-
email:
|
33
|
+
version_requirements: *id001
|
34
|
+
description: " Dentaku is a parser and evaluator for mathematical formulas\n"
|
35
|
+
email:
|
29
36
|
- rubysolo@gmail.com
|
30
37
|
executables: []
|
38
|
+
|
31
39
|
extensions: []
|
40
|
+
|
32
41
|
extra_rdoc_files: []
|
33
|
-
|
42
|
+
|
43
|
+
files:
|
34
44
|
- .gitignore
|
35
45
|
- Gemfile
|
36
46
|
- README.md
|
@@ -53,29 +63,38 @@ files:
|
|
53
63
|
- spec/tokenizer_spec.rb
|
54
64
|
homepage: http://github.com/rubysolo/dentaku
|
55
65
|
licenses: []
|
66
|
+
|
56
67
|
post_install_message:
|
57
68
|
rdoc_options: []
|
58
|
-
|
69
|
+
|
70
|
+
require_paths:
|
59
71
|
- lib
|
60
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
73
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
|
66
|
-
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
82
|
none: false
|
68
|
-
requirements:
|
69
|
-
- -
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
72
90
|
requirements: []
|
91
|
+
|
73
92
|
rubyforge_project: dentaku
|
74
|
-
rubygems_version: 1.8.
|
93
|
+
rubygems_version: 1.8.15
|
75
94
|
signing_key:
|
76
95
|
specification_version: 3
|
77
96
|
summary: A formula language parser and evaluator
|
78
|
-
test_files:
|
97
|
+
test_files:
|
79
98
|
- spec/calculator_spec.rb
|
80
99
|
- spec/dentaku_spec.rb
|
81
100
|
- spec/evaluator_spec.rb
|