Class: Parsec::Parsec
- Inherits:
-
Object
- Object
- Parsec::Parsec
- Defined in:
- lib/parsec.rb
Overview
This is the main class responsible to evaluate the equations
Constant Summary collapse
- VERSION =
'0.11.9'.freeze
Class Method Summary collapse
- .convert(ans) ⇒ Object
- .error_check(output) ⇒ Object
-
.eval_equation(equation) ⇒ Object
evaluates the equation and returns only the result.
-
.eval_equation_with_type(equation) ⇒ Object
evaluates the equation and returns the result and its data type.
- .get_result_type(equation) ⇒ Object
- .sanitize(equation, new_line = false) ⇒ Object
- .validate(ans, raise_error) ⇒ Object
-
.validate_syntax(equation) ⇒ Object
returns true or an error string.
-
.validate_syntax!(equation) ⇒ Object
returns true or raise an error.
Class Method Details
.convert(ans) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/parsec.rb', line 59 def self.convert(ans) case ans['value'] when 'inf' then return 'Infinity' when '-inf' then return '-Infinity' when 'nan', '-nan' then return 'nan' end case ans['type'] when 'int' then return ans['value'].to_i when 'float' then return ans['value'].to_f when 'boolean' then return ans['value'].to_bool when 'string' then return error_check(ans['value'].force_encoding(Encoding::UTF_8)) when 'complex' then return 'complex number' # Maybe future implementation when 'matrix' then return 'matrix value' # Maybe future implementation end end |
.error_check(output) ⇒ Object
76 77 78 79 |
# File 'lib/parsec.rb', line 76 def self.error_check(output) raise SyntaxError, output.sub(/^Error: /, '') if output.match?(/^Error:/) output end |
.eval_equation(equation) ⇒ Object
evaluates the equation and returns only the result
12 13 14 |
# File 'lib/parsec.rb', line 12 def self.eval_equation(equation) eval_equation_with_type(equation)[:value] end |
.eval_equation_with_type(equation) ⇒ Object
evaluates the equation and returns the result and its data type
17 18 19 20 21 22 |
# File 'lib/parsec.rb', line 17 def self.eval_equation_with_type(equation) equation = sanitize(equation, true) result = Libnativemath.native_eval(equation) { value: convert(result), type: result['type'].to_sym } end |
.get_result_type(equation) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/parsec.rb', line 38 def self.get_result_type(equation) equation = sanitize(equation, true) result = Libnativemath.native_eval(equation) result['type'].to_sym if validate(result, true) end |
.sanitize(equation, new_line = false) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/parsec.rb', line 47 def self.sanitize(equation, new_line = false) equation = equation.gsub(/[\n\t\r]/, ' ') # The following regex remove all spaces that are not between quot marks # https://tinyurl.com/ybc7bng3 if new_line == true equation.gsub!(/( |("([^"\\]|\\.)*")|('([^'\\]|\\.)*'))/, '\\2') end equation end |
.validate(ans, raise_error) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/parsec.rb', line 81 def self.validate(ans, raise_error) if (ans['type'] == 'string') && ans['value'].match?(/^Error:/) raise SyntaxError, ans['value'].sub(/^Error: /, '') if raise_error return ans['value'].sub(/^Error: /, '') end true end |
.validate_syntax(equation) ⇒ Object
returns true or an error string
32 33 34 35 36 |
# File 'lib/parsec.rb', line 32 def self.validate_syntax(equation) equation = sanitize(equation) validate(Libnativemath.native_eval(equation), false) end |
.validate_syntax!(equation) ⇒ Object
returns true or raise an error
25 26 27 28 29 |
# File 'lib/parsec.rb', line 25 def self.validate_syntax!(equation) equation = sanitize(equation) validate(Libnativemath.native_eval(equation), true) end |