dentaku 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c588f75b508adbbda4b56b6b3286144aff133c01
4
- data.tar.gz: b444936b8e75de4aa64af4a39b0d2e9d667b85f3
3
+ metadata.gz: 5b3af0752b4a5aa4575036f593ead2f9df154721
4
+ data.tar.gz: ab61cb8ebd50a48d179197724019a6f5ec167556
5
5
  SHA512:
6
- metadata.gz: 33f894376d9098500762e3a5aab7e6be70ce95a3da9fb0c7dd09186bbb30db0471b8bcc750dd906bb3dfdc585f37f5bfe59002dbe4f93a4a65f5784b7e9b2d02
7
- data.tar.gz: 0fbf3567fd3b9b5e8e5db7df0647ddc687842f9e6b2bdce9e685d1c2a45fdfa2b1ad9ca80c7c5a1ac4fc2e5a910faf167205520d7f52a40ae1ea4de7c573c32e
6
+ metadata.gz: 58d3524e23835e1c842e35a91390c8601cb59fd5da31efdd234edb2e7551399e5b0c176f4f30cbb2f5b849f6cceab16661ae473a95b8ca32fc56913ec8f0f580
7
+ data.tar.gz: 697075e3d8a11586404c83b2ea0ff685079ab0dc3b80123e87fecfc411d4913a0012573ac7ec87eca55a6205d5957723a7a0e5edcaf8bc295b2937d096ac8667
@@ -98,6 +98,10 @@ module Dentaku
98
98
  Token.new(token.category, token.value * -1)
99
99
  end
100
100
 
101
+ def pow_negate(base, _, _, exp)
102
+ Token.new(base.category, base.value ** (exp.value * -1))
103
+ end
104
+
101
105
  def percentage(token, _)
102
106
  Token.new(token.category, token.value / 100.0)
103
107
  end
data/lib/dentaku/rules.rb CHANGED
@@ -6,25 +6,26 @@ module Dentaku
6
6
  class Rules
7
7
  def self.core_rules
8
8
  [
9
- [ p(:if), :if ],
10
- [ p(:round), :round ],
11
- [ p(:roundup), :round_int ],
12
- [ p(:rounddown), :round_int ],
13
- [ p(:not), :not ],
9
+ [ p(:if), :if ],
10
+ [ p(:round), :round ],
11
+ [ p(:roundup), :round_int ],
12
+ [ p(:rounddown), :round_int ],
13
+ [ p(:not), :not ],
14
14
 
15
- [ p(:group), :evaluate_group ],
16
- [ p(:start_neg), :negate ],
17
- [ p(:math_pow), :apply ],
18
- [ p(:math_mod), :apply ],
19
- [ p(:math_mul), :apply ],
20
- [ p(:math_add), :apply ],
21
- [ p(:percentage), :percentage ],
22
- [ p(:negation), :negate ],
23
- [ p(:range_asc), :expand_range ],
24
- [ p(:range_desc), :expand_range ],
25
- [ p(:num_comp), :apply ],
26
- [ p(:str_comp), :apply ],
27
- [ p(:combine), :apply ]
15
+ [ p(:group), :evaluate_group ],
16
+ [ p(:start_neg), :negate ],
17
+ [ p(:math_pow), :apply ],
18
+ [ p(:math_neg_pow), :pow_negate ],
19
+ [ p(:math_mod), :apply ],
20
+ [ p(:math_mul), :apply ],
21
+ [ p(:math_add), :apply ],
22
+ [ p(:percentage), :percentage ],
23
+ [ p(:negation), :negate ],
24
+ [ p(:range_asc), :expand_range ],
25
+ [ p(:range_desc), :expand_range ],
26
+ [ p(:num_comp), :apply ],
27
+ [ p(:str_comp), :apply ],
28
+ [ p(:combine), :apply ]
28
29
  ]
29
30
  end
30
31
 
@@ -68,7 +69,7 @@ module Dentaku
68
69
  :comparator, :comp_gt, :comp_lt, :fopen, :open, :close, :comma,
69
70
  :non_close_plus, :non_group, :non_group_star, :arguments,
70
71
  :logical, :combinator, :if, :round, :roundup, :rounddown, :not,
71
- :anchored_minus
72
+ :anchored_minus, :math_neg_pow
72
73
  ].each_with_object({}) do |name, matchers|
73
74
  matchers[name] = TokenMatcher.send(name)
74
75
  end
@@ -76,25 +77,26 @@ module Dentaku
76
77
 
77
78
  def self.p(name)
78
79
  @patterns ||= {
79
- group: pattern(:open, :non_group_star, :close),
80
- math_add: pattern(:numeric, :addsub, :numeric),
81
- math_mul: pattern(:numeric, :muldiv, :numeric),
82
- math_pow: pattern(:numeric, :pow, :numeric),
83
- math_mod: pattern(:numeric, :mod, :numeric),
84
- negation: pattern(:subtract, :numeric),
85
- start_neg: pattern(:anchored_minus, :numeric),
86
- percentage: pattern(:numeric, :mod),
87
- range_asc: pattern(:numeric, :comp_lt, :numeric, :comp_lt, :numeric),
88
- range_desc: pattern(:numeric, :comp_gt, :numeric, :comp_gt, :numeric),
89
- num_comp: pattern(:numeric, :comparator, :numeric),
90
- str_comp: pattern(:string, :comparator, :string),
91
- combine: pattern(:logical, :combinator, :logical),
80
+ group: pattern(:open, :non_group_star, :close),
81
+ math_add: pattern(:numeric, :addsub, :numeric),
82
+ math_mul: pattern(:numeric, :muldiv, :numeric),
83
+ math_pow: pattern(:numeric, :pow, :numeric),
84
+ math_neg_pow: pattern(:numeric, :pow, :subtract, :numeric),
85
+ math_mod: pattern(:numeric, :mod, :numeric),
86
+ negation: pattern(:subtract, :numeric),
87
+ start_neg: pattern(:anchored_minus, :numeric),
88
+ percentage: pattern(:numeric, :mod),
89
+ range_asc: pattern(:numeric, :comp_lt, :numeric, :comp_lt, :numeric),
90
+ range_desc: pattern(:numeric, :comp_gt, :numeric, :comp_gt, :numeric),
91
+ num_comp: pattern(:numeric, :comparator, :numeric),
92
+ str_comp: pattern(:string, :comparator, :string),
93
+ combine: pattern(:logical, :combinator, :logical),
92
94
 
93
- if: func_pattern(:if, :non_group, :comma, :non_group, :comma, :non_group),
94
- round: func_pattern(:round, :arguments),
95
- roundup: func_pattern(:roundup, :arguments),
96
- rounddown: func_pattern(:rounddown, :arguments),
97
- not: func_pattern(:not, :arguments)
95
+ if: func_pattern(:if, :non_group, :comma, :non_group, :comma, :non_group),
96
+ round: func_pattern(:round, :arguments),
97
+ roundup: func_pattern(:roundup, :arguments),
98
+ rounddown: func_pattern(:rounddown, :arguments),
99
+ not: func_pattern(:not, :arguments)
98
100
  }
99
101
 
100
102
  @patterns[name]
@@ -1,3 +1,3 @@
1
1
  module Dentaku
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.3"
3
3
  end
@@ -22,6 +22,8 @@ describe Dentaku::Calculator do
22
22
  expect(calculator.evaluate('1 + -2 ^ 2')).to eq(-3)
23
23
  expect(calculator.evaluate('3 + -num', :num => 2)).to eq(1)
24
24
  expect(calculator.evaluate('-num + 3', :num => 2)).to eq(1)
25
+ expect(calculator.evaluate('10 ^ 2')).to eq(100)
26
+ expect(calculator.evaluate('0 * 10 ^ -5')).to eq(0)
25
27
  end
26
28
 
27
29
  describe 'memory' do
@@ -48,6 +48,12 @@ describe Dentaku::Evaluator do
48
48
  expect(evaluator.evaluate(token_stream(:subtract, 1, :add, 1))).to eq(0)
49
49
  end
50
50
 
51
+ it 'evaluates a number multiplied by an exponent' do
52
+ expect(evaluator.evaluate(token_stream(10, :pow, 2))).to eq(100)
53
+ expect(evaluator.evaluate(token_stream(0, :multiply, 10, :pow, 5))).to eq(0)
54
+ expect(evaluator.evaluate(token_stream(0, :multiply, 10, :pow, :subtract, 5))).to eq(0)
55
+ end
56
+
51
57
  it 'supports unary percentage' do
52
58
  expect(evaluator.evaluate(token_stream(50, :mod))).to eq(0.5)
53
59
  expect(evaluator.evaluate(token_stream(50, :mod, :multiply, 100))).to eq(50)
data/spec/spec_helper.rb CHANGED
@@ -15,7 +15,7 @@ def type_for(value)
15
15
  :string
16
16
  when true, false
17
17
  :logical
18
- when :add, :subtract, :multiply, :divide, :mod
18
+ when :add, :subtract, :multiply, :divide, :mod, :pow
19
19
  :operator
20
20
  when :fopen, :open, :close, :comma
21
21
  :grouping
@@ -37,6 +37,18 @@ describe Dentaku::Tokenizer do
37
37
  expect(tokens.map(&:value)).to eq([1, :divide, 1])
38
38
  end
39
39
 
40
+ it 'tokenizes power operations' do
41
+ tokens = tokenizer.tokenize('10 ^ 2')
42
+ expect(tokens.map(&:category)).to eq([:numeric, :operator, :numeric])
43
+ expect(tokens.map(&:value)).to eq([10, :pow, 2])
44
+ end
45
+
46
+ it 'tokenizes power operations' do
47
+ tokens = tokenizer.tokenize('0 * 10 ^ -5')
48
+ expect(tokens.map(&:category)).to eq([:numeric, :operator, :numeric, :operator, :operator, :numeric])
49
+ expect(tokens.map(&:value)).to eq([0, :multiply, 10, :pow, :subtract, 5])
50
+ end
51
+
40
52
  it 'handles floating point' do
41
53
  tokens = tokenizer.tokenize('1.5 * 3.7')
42
54
  expect(tokens.map(&:category)).to eq([:numeric, :operator, :numeric])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dentaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
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-12-19 00:00:00.000000000 Z
11
+ date: 2014-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake