dentaku 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -131,7 +131,13 @@ module Dentaku
131
131
  places = args.shift.value
132
132
  end
133
133
 
134
- Token.new(:numeric, input.round(places))
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
@@ -4,7 +4,7 @@ module Dentaku
4
4
  class TokenScanner
5
5
  def initialize(category, regexp, converter=nil)
6
6
  @category = category
7
- @regexp = %r{\A(#{ regexp })}
7
+ @regexp = %r{\A(#{ regexp })}i
8
8
  @converter = converter
9
9
  end
10
10
 
@@ -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, '"[^"]*"', lambda{|raw| raw.gsub(/^"|"$/, '') }),
11
- TokenScanner.new(:string, "'[^']*'", lambda{|raw| raw.gsub(/^'|'$/, '') }),
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', lambda {|raw| raw.strip.to_sym }),
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, '[A-Za-z_]+', lambda {|raw| raw.to_sym })
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.downcase
49
+ input = string.dup
50
50
 
51
51
  until input.empty?
52
52
  raise "parse error at: '#{ input }'" unless SCANNERS.any? do |scanner|
@@ -1,3 +1,3 @@
1
1
  module Dentaku
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -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
- version: 0.2.1
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
- date: 2012-02-12 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-02-22 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: rspec
16
- requirement: &70160992097380 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
22
32
  type: :development
23
- prerelease: false
24
- version_requirements: *70160992097380
25
- description: ! ' Dentaku is a parser and evaluator for mathematical formulas
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
- files:
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
- require_paths:
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
- version: '0'
66
- required_rubygems_version: !ruby/object:Gem::Requirement
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
- version: '0'
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.10
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