eqn 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3929425e71b09257fcf386a38166a5fcf2adc7e5
4
- data.tar.gz: 2d47d23b59c716ca5adb87cfdaaa206d3aa5b17f
3
+ metadata.gz: 4c54bb4df4dbcf3350edbaa0c13994c8372cea5c
4
+ data.tar.gz: bde508e34f9491145d4c5b7968602a8dd220d5a2
5
5
  SHA512:
6
- metadata.gz: 3c74b42add0068a547fa8b547fdb4c7a342067747bde79791e89996816205c2ff1655ca60a53c184f38d6b04cb78b4229a4f079ef8d8c4457a26a8285833fade
7
- data.tar.gz: da9ae663707fa4b07c41b60b5b87c9c3d82ed8ce530a98d7c87008afd4b81ccaa3974be56ff30c014540717424ab899a8cf45a91a4b3d1c6b27ef31515a209a1
6
+ metadata.gz: d3f356c8c7eb30b335c972085eaf70e23b30b2611cb46d0857dfad95b4fca6f3a9e8426f9fab50c22b546ca0533335a0421fb0ad358c0e44f3ff2f355367bbf5
7
+ data.tar.gz: 466d2723c70a6933469d1a3bbfa91affeb64e487edff0aa11525503af7b8c163359ca806b2b5584a4dc6be9c3f355c38fb49c04bdc7b13e3cc5699ae1c290a13
data/eqn.gemspec CHANGED
@@ -4,7 +4,7 @@ lib = File.expand_path('../lib', __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'eqn/version'
6
6
 
7
- config_files = %w(.rspec .rubocop.yml .ruby-version Appraisals circle.yml)
7
+ config_files = %w[.rspec .rubocop.yml .ruby-version Appraisals circle.yml]
8
8
 
9
9
  Gem::Specification.new do |spec|
10
10
  spec.name = 'eqn'
@@ -14,7 +14,7 @@ module Eqn
14
14
  end
15
15
 
16
16
  def method_missing(method, *args)
17
- if %i(calculate calc valid?).include?(method)
17
+ if %i[calculate calc valid?].include?(method)
18
18
  self.class.send(method, @eqn, @vars)
19
19
  elsif (match = method.to_s.match(/^[A-Za-z]+=$/))
20
20
  @vars[match.to_s.delete('=').intern] = args.first
@@ -26,19 +26,14 @@ module Eqn
26
26
  end
27
27
 
28
28
  def respond_to_missing?(method)
29
- %i(calculate calc valid?).include?(method) || method.to_s.match(/^[A-Za-z]+=$/) || (var = method.to_s.match(/^[A-Za-z]+$/).to_s.intern) && @vars.key?(var)
29
+ %i[calculate calc valid?].include?(method) || method.to_s.match(/^[A-Za-z]+=$/) || (var = method.to_s.match(/^[A-Za-z]+$/).to_s.intern) && @vars.key?(var)
30
30
  end
31
31
 
32
32
  class << self
33
33
  def calculate(data, vars = {})
34
- @@vars = vars
35
- begin
36
- result = Parser.parse(data).value
37
- raise ZeroDivisionError if result.is_a?(Float) && (result.abs == Float::INFINITY || result.nan?)
38
- result
39
- ensure
40
- @@vars = nil
41
- end
34
+ result = Parser.parse(data).value(vars)
35
+ raise ZeroDivisionError if result.is_a?(Float) && (result.abs == Float::INFINITY || result.nan?)
36
+ result
42
37
  end
43
38
  alias calc calculate
44
39
 
@@ -1,17 +1,17 @@
1
1
  module Eqn
2
2
  class Comparation < Treetop::Runtime::SyntaxNode
3
- def value
4
- val_one = elements.shift.value
3
+ def value(vars = {})
4
+ val_one = elements.shift.value(vars)
5
5
  if elements.empty?
6
6
  val_one
7
7
  else
8
- val_one.send(*elements.shift.value)
8
+ val_one.send(*elements.shift.value(vars))
9
9
  end
10
10
  end
11
11
 
12
12
  class CompGroup < Treetop::Runtime::SyntaxNode
13
- def value
14
- [elements.shift.value, elements.shift.value]
13
+ def value(vars = {})
14
+ [elements.shift.value(vars), elements.shift.value(vars)]
15
15
  end
16
16
  end
17
17
  end
@@ -8,17 +8,17 @@ module Eqn
8
8
  elements.empty?
9
9
  end
10
10
 
11
- def value
11
+ def value(vars = {})
12
12
  if elements.count == 1
13
- elements.shift.value
13
+ elements.shift.value(vars)
14
14
  else
15
- base = elements.shift.value
15
+ base = elements.shift.value(vars)
16
16
 
17
17
  # Aggressively consume left associative operators to maintain associativity.
18
18
  while left_associative?
19
- op, num_expr = elements.shift.value
19
+ op, num_expr = elements.shift.value(vars)
20
20
  num_expr_operand = num_expr.elements.shift
21
- base = base.send(op, num_expr_operand.value)
21
+ base = base.send(op, num_expr_operand.value(vars))
22
22
  elements.push num_expr.elements.shift unless num_expr.term?
23
23
  end
24
24
 
@@ -26,8 +26,8 @@ module Eqn
26
26
  if term?
27
27
  base
28
28
  else
29
- op, num_expr = elements.shift.value
30
- base.send(op, num_expr.value)
29
+ op, num_expr = elements.shift.value(vars)
30
+ base.send(op, num_expr.value(vars))
31
31
  end
32
32
  end
33
33
  end
@@ -37,8 +37,8 @@ module Eqn
37
37
  elements.first.left_associative?
38
38
  end
39
39
 
40
- def value
41
- [elements.shift.value, elements.shift]
40
+ def value(vars = {})
41
+ [elements.shift.value(vars), elements.shift]
42
42
  end
43
43
  end
44
44
  end
data/lib/eqn/function.rb CHANGED
@@ -1,43 +1,43 @@
1
1
  module Eqn
2
2
  module Function
3
3
  class If < Treetop::Runtime::SyntaxNode
4
- def value
5
- comp_val = elements.shift.value
6
- ls = elements.shift.value
7
- rs = elements.shift.value
4
+ def value(vars = {})
5
+ comp_val = elements.shift.value(vars)
6
+ ls = elements.shift.value(vars)
7
+ rs = elements.shift.value(vars)
8
8
 
9
9
  comp_val ? ls : rs
10
10
  end
11
11
  end
12
12
 
13
13
  class RoundBase < Treetop::Runtime::SyntaxNode
14
- def value(fn)
15
- value = elements.shift.value
14
+ def value(fn, vars)
15
+ value = elements.shift.value(vars)
16
16
  raise ZeroDivisionError if value.is_a?(Float) && (value.abs == Float::INFINITY || value.nan?)
17
17
  if elements.empty?
18
18
  value.send(fn)
19
19
  else
20
- decimals = elements.shift.value
20
+ decimals = elements.shift.value(vars)
21
21
  (value * 10**decimals).send(fn).to_f / 10**decimals
22
22
  end
23
23
  end
24
24
  end
25
25
 
26
26
  class Round < RoundBase
27
- def value
28
- super(:round)
27
+ def value(vars = {})
28
+ super(:round, vars)
29
29
  end
30
30
  end
31
31
 
32
32
  class RoundUp < RoundBase
33
- def value
34
- super(:ceil)
33
+ def value(vars = {})
34
+ super(:ceil, vars)
35
35
  end
36
36
  end
37
37
 
38
38
  class RoundDown < RoundBase
39
- def value
40
- super(:floor)
39
+ def value(vars = {})
40
+ super(:floor, vars)
41
41
  end
42
42
  end
43
43
  end
data/lib/eqn/number.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  module Eqn
2
2
  class Number < Treetop::Runtime::SyntaxNode
3
- def value
4
- base = elements.shift.value
3
+ def value(vars = {})
4
+ base = elements.shift.value(vars)
5
5
  # Apply any exponent.
6
- base *= elements.shift.value unless elements.empty?
6
+ base *= elements.shift.value(vars) unless elements.empty?
7
7
  base
8
8
  end
9
9
 
10
10
  class SignedNumber < Treetop::Runtime::SyntaxNode
11
- def value
11
+ def value(vars = {})
12
12
  # Store sign if any.
13
13
  sign_negative = elements.shift.negative? if elements.first.is_a? Terminal::Sign
14
14
 
15
15
  # Evaluate float.
16
- value = elements.shift.value
16
+ value = elements.shift.value(vars)
17
17
 
18
18
  # Apply sign if any.
19
19
  sign_negative ? -value : value
@@ -21,25 +21,25 @@ module Eqn
21
21
  end
22
22
 
23
23
  class Float < Treetop::Runtime::SyntaxNode
24
- def value
25
- base = elements.shift.value
24
+ def value(vars = {})
25
+ base = elements.shift.value(vars)
26
26
 
27
27
  # Add any decimal.
28
- base += elements.shift.value unless elements.empty?
28
+ base += elements.shift.value(vars) unless elements.empty?
29
29
 
30
30
  base
31
31
  end
32
32
  end
33
33
 
34
34
  class Decimal < Treetop::Runtime::SyntaxNode
35
- def value
35
+ def value(_vars = {})
36
36
  elements.shift.dec_value
37
37
  end
38
38
  end
39
39
 
40
40
  class Exponent < Treetop::Runtime::SyntaxNode
41
- def value
42
- 10**elements.shift.value
41
+ def value(vars = {})
42
+ 10**elements.shift.value(vars)
43
43
  end
44
44
  end
45
45
  end
data/lib/eqn/terminal.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Eqn
2
2
  module Terminal
3
3
  class Variable < Treetop::Runtime::SyntaxNode
4
- def value
5
- val = Eqn::Calculator.class_variable_get(:@@vars)[text_value.intern]
4
+ def value(vars = {})
5
+ val = vars[text_value.intern]
6
6
  raise NoVariableValueError, "No value given for: #{text_value}" unless val
7
7
  raise NonNumericVariableError, "Variable #{text_value} value is nonnumeric: #{val}" unless val.is_a? Numeric
8
8
  val
@@ -14,7 +14,7 @@ module Eqn
14
14
  ".#{text_value}".to_f
15
15
  end
16
16
 
17
- def value
17
+ def value(_vars = {})
18
18
  text_value.to_f
19
19
  end
20
20
  end
@@ -30,7 +30,7 @@ module Eqn
30
30
  is_a? LeftAssociativeOp
31
31
  end
32
32
 
33
- def value
33
+ def value(_vars = {})
34
34
  text_value == '^' ? :** : text_value.intern
35
35
  end
36
36
 
@@ -39,7 +39,7 @@ module Eqn
39
39
  end
40
40
 
41
41
  class CompOp < Treetop::Runtime::SyntaxNode
42
- def value
42
+ def value(_vars = {})
43
43
  text_value == '=' ? :== : text_value.intern
44
44
  end
45
45
  end
data/lib/eqn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eqn
2
- VERSION = '1.5.0'.freeze
2
+ VERSION = '1.6.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eqn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Schneider
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-29 00:00:00.000000000 Z
11
+ date: 2017-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: treetop