eqn 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.rubocop.yml +2 -0
- data/README.md +13 -0
- data/bin/console +0 -0
- data/bin/setup +0 -0
- data/lib/eqn.treetop +5 -1
- data/lib/eqn/calculator.rb +12 -7
- data/lib/eqn/errors.rb +6 -0
- data/lib/eqn/parser.rb +2 -2
- data/lib/eqn/terminal.rb +12 -0
- data/lib/eqn/version.rb +1 -1
- metadata +3 -3
- data/lib/eqn/parse_error.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39b0293f4a288d1ef8c13ad3627ce4aa23b62ebd
|
4
|
+
data.tar.gz: d4e32c1a62b84f9821c4782e3bc7751134a7c56f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91490640f171957c8b7fe85967d74f04f6620898d0bf4a7c67fa7d05f915952bf7b818735054a9c97d1369d72de8821cae8acc9e01a287954cb11dd2a778d00a
|
7
|
+
data.tar.gz: c2f498b84bda0b3cb31ca054c6012d94ba79f8ebcbe80e8025ce6ca966c185f38a6e2992bb5a274ba3a9c574b3101578aae4ee175b61138dedfac6e545ff5377
|
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -45,6 +45,19 @@ If you want to peek at how Eqn is parsing an equation, run the following to get
|
|
45
45
|
|
46
46
|
Eqn follows the standard mathematical order of operations: parentheses, exponentiation, multiplication/division, addition/subtraction. It ignores whitespace, so `1 + 1` === `1+1`. (However, it does not ignore whitespace between two numbers, so `1 1` is invalid.)
|
47
47
|
|
48
|
+
### Variables
|
49
|
+
|
50
|
+
Eqn supports dynamically inserting values into an equation. Variables are passed to the calculator method via a hash; variable names may contain any lowercase or uppercase letters. Some examples:
|
51
|
+
|
52
|
+
$ Eqn::Calculator.calc('a + 1', a: 1)
|
53
|
+
# => 2
|
54
|
+
|
55
|
+
$ Eqn::Calculator.calc('5 * value', value: 2.5)
|
56
|
+
# => 12.5
|
57
|
+
|
58
|
+
$ Eqn::Calculator.calc('if(a > 10, b, c)', a: 15, b: 1, c: 0) # see below for function documentation
|
59
|
+
# => 1
|
60
|
+
|
48
61
|
### Functions
|
49
62
|
|
50
63
|
Eqn presently supports four functions:
|
data/bin/console
CHANGED
File without changes
|
data/bin/setup
CHANGED
File without changes
|
data/lib/eqn.treetop
CHANGED
@@ -64,7 +64,7 @@ grammar Eqn
|
|
64
64
|
end
|
65
65
|
|
66
66
|
rule signed_float
|
67
|
-
sign? space? (float / digits) <Eqn::Number::SignedNumber>
|
67
|
+
sign? space? (float / digits / variable) <Eqn::Number::SignedNumber>
|
68
68
|
end
|
69
69
|
|
70
70
|
rule float
|
@@ -115,6 +115,10 @@ grammar Eqn
|
|
115
115
|
[0-9]+ <Eqn::Terminal::Digits>
|
116
116
|
end
|
117
117
|
|
118
|
+
rule variable
|
119
|
+
[a-zA-Z]+ <Eqn::Terminal::Variable>
|
120
|
+
end
|
121
|
+
|
118
122
|
rule space
|
119
123
|
("\s" / "\t")*
|
120
124
|
end
|
data/lib/eqn/calculator.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
module Eqn
|
2
2
|
class Calculator
|
3
3
|
class << self
|
4
|
-
def calc(data)
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def calc(data, vars = {})
|
5
|
+
@@vars = vars
|
6
|
+
begin
|
7
|
+
result = Parser.parse(data).value
|
8
|
+
fail ZeroDivisionError if result.is_a?(Float) && (result.abs == Float::INFINITY || result.nan?)
|
9
|
+
result
|
10
|
+
ensure
|
11
|
+
@@vars = nil
|
12
|
+
end
|
8
13
|
end
|
9
14
|
|
10
|
-
def valid?(data)
|
11
|
-
calc(data)
|
15
|
+
def valid?(data, vars = {})
|
16
|
+
calc(data, vars)
|
12
17
|
true
|
13
|
-
rescue ParseError, ZeroDivisionError
|
18
|
+
rescue NonNumericVariableError, NoVariableValueError, ParseError, ZeroDivisionError
|
14
19
|
false
|
15
20
|
end
|
16
21
|
end
|
data/lib/eqn/errors.rb
ADDED
data/lib/eqn/parser.rb
CHANGED
data/lib/eqn/terminal.rb
CHANGED
@@ -2,6 +2,18 @@ module Eqn
|
|
2
2
|
module Terminal
|
3
3
|
class Node < Treetop::Runtime::SyntaxNode; end
|
4
4
|
|
5
|
+
class Variable < Node
|
6
|
+
def value
|
7
|
+
unless Eqn::Calculator.class_variable_get(:@@vars).key? text_value.intern
|
8
|
+
fail NoVariableValueError, "No value given for: #{text_value}"
|
9
|
+
end
|
10
|
+
unless Eqn::Calculator.class_variable_get(:@@vars)[text_value.intern].is_a? Numeric
|
11
|
+
fail NonNumericVariableError, "Variable #{text_value} value is nonnumeric: #{Eqn::Calculator.class_variable_get(:@@vars)[text_value.intern]}"
|
12
|
+
end
|
13
|
+
Eqn::Calculator.class_variable_get(:@@vars)[text_value.intern]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
5
17
|
class Digits < Node
|
6
18
|
def dec_value
|
7
19
|
".#{text_value}".to_f
|
data/lib/eqn/version.rb
CHANGED
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.
|
4
|
+
version: 1.3.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: 2015-
|
11
|
+
date: 2015-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: treetop
|
@@ -131,10 +131,10 @@ files:
|
|
131
131
|
- lib/eqn/calculator.rb
|
132
132
|
- lib/eqn/comparation.rb
|
133
133
|
- lib/eqn/engine.rb
|
134
|
+
- lib/eqn/errors.rb
|
134
135
|
- lib/eqn/expression.rb
|
135
136
|
- lib/eqn/function.rb
|
136
137
|
- lib/eqn/number.rb
|
137
|
-
- lib/eqn/parse_error.rb
|
138
138
|
- lib/eqn/parser.rb
|
139
139
|
- lib/eqn/terminal.rb
|
140
140
|
- lib/eqn/version.rb
|
data/lib/eqn/parse_error.rb
DELETED