dentaku 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ module Dentaku
8
8
  T_STRING = TokenMatcher.new(:string)
9
9
  T_ADDSUB = TokenMatcher.new(:operator, [:add, :subtract])
10
10
  T_MULDIV = TokenMatcher.new(:operator, [:multiply, :divide])
11
+ T_POW = TokenMatcher.new(:operator, :pow)
11
12
  T_COMPARATOR = TokenMatcher.new(:comparator)
12
13
  T_COMP_GT = TokenMatcher.new(:comparator, [:gt, :ge])
13
14
  T_COMP_LT = TokenMatcher.new(:comparator, [:lt, :le])
@@ -26,6 +27,7 @@ module Dentaku
26
27
  P_GROUP = [T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
27
28
  P_MATH_ADD = [T_NUMERIC, T_ADDSUB, T_NUMERIC]
28
29
  P_MATH_MUL = [T_NUMERIC, T_MULDIV, T_NUMERIC]
30
+ P_MATH_POW = [T_NUMERIC, T_POW, T_NUMERIC]
29
31
  P_RANGE_ASC = [T_NUMERIC, T_COMP_LT, T_NUMERIC, T_COMP_LT, T_NUMERIC]
30
32
  P_RANGE_DESC = [T_NUMERIC, T_COMP_GT, T_NUMERIC, T_COMP_GT, T_NUMERIC]
31
33
  P_NUM_COMP = [T_NUMERIC, T_COMPARATOR, T_NUMERIC]
@@ -42,6 +44,7 @@ module Dentaku
42
44
  [P_ROUND_TWO, :round],
43
45
 
44
46
  [P_GROUP, :evaluate_group],
47
+ [P_MATH_POW, :apply],
45
48
  [P_MATH_MUL, :apply],
46
49
  [P_MATH_ADD, :apply],
47
50
  [P_RANGE_ASC, :expand_range],
@@ -110,6 +113,7 @@ module Dentaku
110
113
  r = rvalue.value
111
114
 
112
115
  case operator.value
116
+ when :pow then Token.new(:numeric, l ** r)
113
117
  when :add then Token.new(:numeric, l + r)
114
118
  when :subtract then Token.new(:numeric, l - r)
115
119
  when :multiply then Token.new(:numeric, l * r)
@@ -6,11 +6,12 @@ module Dentaku
6
6
  class Tokenizer
7
7
  SCANNERS = [
8
8
  TokenScanner.new(:whitespace, '\s+'),
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(/^'|'$/, '') }),
12
- TokenScanner.new(:operator, '\+|-|\*|\/', lambda do |raw|
9
+ TokenScanner.new(:numeric, '(\d+(\.\d+)?|\.\d+)\b', 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(/^'|'$/, '') }),
12
+ TokenScanner.new(:operator, '\^|\+|-|\*|\/', lambda do |raw|
13
13
  case raw
14
+ when '^' then :pow
14
15
  when '+' then :add
15
16
  when '-' then :subtract
16
17
  when '*' then :multiply
@@ -35,9 +36,9 @@ module Dentaku
35
36
  when '=' then :eq
36
37
  end
37
38
  end),
38
- TokenScanner.new(:combinator, '(and|or)\b', lambda { |raw| raw.strip.downcase.to_sym }),
39
- TokenScanner.new(:function, '(if|round)\b', lambda { |raw| raw.strip.to_sym }),
40
- TokenScanner.new(:identifier, '[a-z_]+', lambda { |raw| raw.downcase.to_sym })
39
+ TokenScanner.new(:combinator, '(and|or)\b', lambda {|raw| raw.strip.downcase.to_sym }),
40
+ TokenScanner.new(:function, '(if|round)\b', lambda {|raw| raw.strip.to_sym }),
41
+ TokenScanner.new(:identifier, '\w+\b', lambda {|raw| raw.downcase.to_sym })
41
42
  ]
42
43
 
43
44
  LPAREN = TokenMatcher.new(:grouping, :open)
@@ -1,3 +1,3 @@
1
1
  module Dentaku
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -51,6 +51,13 @@ describe Dentaku::Calculator do
51
51
  calculator.evaluate('foo * 2', 'foo' => 4).should eq(8)
52
52
  end
53
53
 
54
+ it 'should accept digits in identifiers' do
55
+ calculator.evaluate('foo1 * 2', :foo1 => 2).should eq(4)
56
+ calculator.evaluate('foo1 * 2', 'foo1' => 4).should eq(8)
57
+ calculator.evaluate('1foo * 2', '1foo' => 2).should eq(4)
58
+ calculator.evaluate('fo1o * 2', :fo1o => 4).should eq(8)
59
+ end
60
+
54
61
  it 'should compare string literals with string variables' do
55
62
  calculator.evaluate('fruit = "apple"', :fruit => 'apple').should be_true
56
63
  calculator.evaluate('fruit = "apple"', :fruit => 'pear').should be_false
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
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70240118188300 !ruby/object:Gem::Requirement
16
+ requirement: &70124074683860 !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: *70240118188300
24
+ version_requirements: *70124074683860
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70240118187880 !ruby/object:Gem::Requirement
27
+ requirement: &70124074683380 !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: *70240118187880
35
+ version_requirements: *70124074683380
36
36
  description: ! ' Dentaku is a parser and evaluator for mathematical formulas
37
37
 
38
38
  '
@@ -75,12 +75,18 @@ 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
78
81
  required_rubygems_version: !ruby/object:Gem::Requirement
79
82
  none: false
80
83
  requirements:
81
84
  - - ! '>='
82
85
  - !ruby/object:Gem::Version
83
86
  version: '0'
87
+ segments:
88
+ - 0
89
+ hash: 2332951367847572293
84
90
  requirements: []
85
91
  rubyforge_project: dentaku
86
92
  rubygems_version: 1.8.11