dentaku 0.1.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ require 'dentaku/token_matcher'
3
3
 
4
4
  module Dentaku
5
5
  class Evaluator
6
+ # tokens
6
7
  T_NUMERIC = TokenMatcher.new(:numeric)
7
8
  T_STRING = TokenMatcher.new(:string)
8
9
  T_ADDSUB = TokenMatcher.new(:operator, [:add, :subtract])
@@ -15,7 +16,9 @@ module Dentaku
15
16
  T_LOGICAL = TokenMatcher.new(:logical)
16
17
  T_COMBINATOR = TokenMatcher.new(:combinator)
17
18
  T_IF = TokenMatcher.new(:function, :if)
19
+ T_ROUND = TokenMatcher.new(:function, :round)
18
20
 
21
+ # patterns
19
22
  P_GROUP = [T_OPEN, T_NON_GROUP, T_CLOSE]
20
23
  P_MATH_ADD = [T_NUMERIC, T_ADDSUB, T_NUMERIC]
21
24
  P_MATH_MUL = [T_NUMERIC, T_MULDIV, T_NUMERIC]
@@ -24,15 +27,20 @@ module Dentaku
24
27
  P_COMBINE = [T_LOGICAL, T_COMBINATOR, T_LOGICAL]
25
28
 
26
29
  P_IF = [T_IF, T_OPEN, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_CLOSE]
30
+ P_ROUND_ONE = [T_ROUND, T_OPEN, T_NUMERIC, T_CLOSE]
31
+ P_ROUND_TWO = [T_ROUND, T_OPEN, T_NUMERIC, T_COMMA, T_NUMERIC, T_CLOSE]
27
32
 
28
33
  RULES = [
34
+ [P_IF, :if],
35
+ [P_ROUND_ONE, :round],
36
+ [P_ROUND_TWO, :round],
37
+
29
38
  [P_GROUP, :evaluate_group],
30
39
  [P_MATH_MUL, :apply],
31
40
  [P_MATH_ADD, :apply],
32
41
  [P_NUM_COMP, :apply],
33
42
  [P_STR_COMP, :apply],
34
- [P_COMBINE, :apply],
35
- [P_IF, :if],
43
+ [P_COMBINE, :apply]
36
44
  ]
37
45
 
38
46
  def evaluate(tokens)
@@ -111,5 +119,19 @@ module Dentaku
111
119
  false_value
112
120
  end
113
121
  end
122
+
123
+ def round(*args)
124
+ function = args.shift
125
+ open = args.shift
126
+ input = args.shift.value
127
+ places = 0
128
+
129
+ if args.length > 1
130
+ comma = args.shift
131
+ places = args.shift.value
132
+ end
133
+
134
+ Token.new(:numeric, input.round(places))
135
+ end
114
136
  end
115
137
  end
@@ -36,7 +36,7 @@ module Dentaku
36
36
  end
37
37
  end),
38
38
  TokenScanner.new(:combinator, '(and|or)\b', lambda {|raw| raw.strip.to_sym }),
39
- TokenScanner.new(:function, '(if)\b', lambda {|raw| raw.strip.to_sym }),
39
+ TokenScanner.new(:function, '(if|round)\b', lambda {|raw| raw.strip.to_sym }),
40
40
  TokenScanner.new(:identifier, '[A-Za-z_]+', lambda {|raw| raw.to_sym })
41
41
  ]
42
42
 
@@ -1,3 +1,3 @@
1
1
  module Dentaku
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -60,5 +60,11 @@ describe Dentaku::Calculator do
60
60
  calculator.evaluate('if (foo < 8, 10, 20)', :foo => 2).should eq(10)
61
61
  calculator.evaluate('if (foo < 8, 10, 20)', :foo => 9).should eq(20)
62
62
  end
63
+
64
+ it 'should include ROUND' do
65
+ calculator.evaluate('round(8.2)').should eq(8)
66
+ calculator.evaluate('round(8.8)').should eq(9)
67
+ calculator.evaluate('round(8.75, 1)').should eq(8.8)
68
+ end
63
69
  end
64
70
  end
@@ -86,5 +86,17 @@ describe Dentaku::Tokenizer do
86
86
  tokens.map(&:category).should eq([:function, :grouping, :identifier, :comparator, :numeric, :grouping, :identifier, :grouping, :identifier, :grouping])
87
87
  tokens.map(&:value).should eq([:if, :open, :x, :lt, 10, :comma, :y, :comma, :z, :close])
88
88
  end
89
+
90
+ it 'include ROUND' do
91
+ tokens = tokenizer.tokenize('round(8.2)')
92
+ tokens.length.should eq(4)
93
+ tokens.map(&:category).should eq([:function, :grouping, :numeric, :grouping])
94
+ tokens.map(&:value).should eq([:round, :open, 8.2, :close])
95
+
96
+ tokens = tokenizer.tokenize('round(8.75, 1)')
97
+ tokens.length.should eq(6)
98
+ tokens.map(&:category).should eq([:function, :grouping, :numeric, :grouping, :numeric, :grouping])
99
+ tokens.map(&:value).should eq([:round, :open, 8.75, :comma, 1, :close])
100
+ end
89
101
  end
90
102
  end
metadata CHANGED
@@ -1,47 +1,36 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: dentaku
3
- version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Solomon White
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-01-31 00:00:00 -07:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-02-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rspec
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70160992097380 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :development
34
- version_requirements: *id001
35
- description: " Dentaku is a parser and evaluator for mathematical formulas\n"
36
- email:
23
+ prerelease: false
24
+ version_requirements: *70160992097380
25
+ description: ! ' Dentaku is a parser and evaluator for mathematical formulas
26
+
27
+ '
28
+ email:
37
29
  - rubysolo@gmail.com
38
30
  executables: []
39
-
40
31
  extensions: []
41
-
42
32
  extra_rdoc_files: []
43
-
44
- files:
33
+ files:
45
34
  - .gitignore
46
35
  - Gemfile
47
36
  - README.md
@@ -62,41 +51,31 @@ files:
62
51
  - spec/token_scanner_spec.rb
63
52
  - spec/token_spec.rb
64
53
  - spec/tokenizer_spec.rb
65
- has_rdoc: true
66
54
  homepage: http://github.com/rubysolo/dentaku
67
55
  licenses: []
68
-
69
56
  post_install_message:
70
57
  rdoc_options: []
71
-
72
- require_paths:
58
+ require_paths:
73
59
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
60
+ required_ruby_version: !ruby/object:Gem::Requirement
75
61
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
- version: "0"
83
- required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
67
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- hash: 3
89
- segments:
90
- - 0
91
- version: "0"
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
92
72
  requirements: []
93
-
94
73
  rubyforge_project: dentaku
95
- rubygems_version: 1.3.7
74
+ rubygems_version: 1.8.10
96
75
  signing_key:
97
76
  specification_version: 3
98
77
  summary: A formula language parser and evaluator
99
- test_files:
78
+ test_files:
100
79
  - spec/calculator_spec.rb
101
80
  - spec/dentaku_spec.rb
102
81
  - spec/evaluator_spec.rb