parsecs 0.2.17 → 0.2.18

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
  SHA256:
3
- metadata.gz: d4695aaa62f4e133872d9221c795666205cc147d91d1c05dcc60b40a938ed540
4
- data.tar.gz: c86c2a15be806e8f5a3d6952b94426293214fc325b92a37e556181f48169fdaf
3
+ metadata.gz: d850257e0e5d9d26760ea04e158f9f08e96f1440b8495b0f1dc81973e4b68f23
4
+ data.tar.gz: 270303f76489572601d29a3cee954bd7d4c00908bf0bb5eb6cd05ab6f6f3fe4e
5
5
  SHA512:
6
- metadata.gz: b9a0cb09771ea71a24f2d5397935025bc88bbae6757bc6b6c24aeb18646ebea600afacace90a10b32d1dee4dae371ed49b6fd0499456f9fc4009483bfce19f65
7
- data.tar.gz: 70b6e172a0e102e4a7678a2e9f3a1860e80cbe06018e66060213b1d47df222527a2daa0bb1d6aab3c22318daf6a2ff42196650513b7b1e267decb53575b32233
6
+ metadata.gz: 722c0c885e316e152350f47461bd1fae9e4c3dae0ded6f4bb0347c277ebf741cdec076d06492cb004ebf3b47d628eda31ace66dc041710fa7e2c388a6b39b115
7
+ data.tar.gz: be27c26f0f36f5c7567fb38170bbf9ba386d0ee7b4af7577d1d3bc16e8f96d120aeb0b265434963ec6da6a3f3142a2f524115911eb2f10f977d770a792b391ac
@@ -37,9 +37,11 @@ libs.each do |lib|
37
37
  $LOCAL_LIBS << "#{lib} "
38
38
  end
39
39
 
40
+ GIT_REPOSITORY = 'https://github.com/niltonvasques/equations-parser.git'.freeze
41
+
40
42
  Dir.chdir(BASEDIR) do
41
43
  system('git init')
42
- system('git submodule add https://github.com/niltonvasques/equations-parser.git ext/equations-parser')
44
+ system("git submodule add #{GIT_REPOSITORY} ext/equations-parser")
43
45
  system('git submodule update --init --recursive')
44
46
 
45
47
  Dir.chdir('ext/equations-parser/') do
@@ -1,4 +1,5 @@
1
1
  /* ext/libnativemath.cpp */
2
+ #include <string>
2
3
  #include <iostream>
3
4
  #include <cmath>
4
5
  #include "mpParser.h"
@@ -33,17 +34,21 @@ Value Calc(string input) {
33
34
  return ans;
34
35
  }
35
36
 
36
- string_type native_direct_eval(string_type input) {
37
+ string native_direct_eval(string input) {
37
38
  Value ans = Calc(input);
38
39
  return ans.AsString();
39
40
  }
40
41
 
41
- int native_eval(string input, char *value, char *value_type) {
42
+ string native_eval(string input) {
42
43
  Value ans = Calc(input);
44
+ stringstream_type ss;
43
45
 
44
- value_type[0] = ans.GetType();
45
- value_type[1] = '\0';
46
+ // Converting to json-like string
47
+ ss << _T("{");
48
+ ss << _T("\"value\": \"") << ans.AsString() << _T("\"");
49
+ ss << _T(", ");
50
+ ss << _T("\"type\": \"") << ans.GetType() << _T("\"");
51
+ ss << _T("}");
46
52
 
47
- strcpy(value, ans.AsString().c_str());
48
- return 0;
53
+ return ss.str();
49
54
  }
@@ -1,4 +1,3 @@
1
1
  /* ext/libnativemath.h */
2
2
  std::string native_direct_eval(std::string input);
3
- int native_eval(std::string input, char *value, char *value_type);
4
-
3
+ std::string native_eval(std::string input);
@@ -6,29 +6,4 @@
6
6
  %}
7
7
 
8
8
  extern std::string native_direct_eval(std::string input);
9
-
10
- %typemap(in, numinputs=0) (char *value, char *value_type) {
11
- $1 = (char *)malloc(100 * sizeof(char));
12
- $2 = (char *)malloc(2 * sizeof(char));
13
- };
14
- %typemap(argout) (char *value, char *value_type) {
15
- if(result == 0) {
16
- $result = rb_hash_new();
17
- rb_hash_aset($result, rb_str_new2("value"), rb_str_new2($1));
18
- switch (*$2) {
19
- case 'i': rb_hash_aset($result, rb_str_new2("type"), rb_str_new2("int")); break;
20
- case 'f': rb_hash_aset($result, rb_str_new2("type"), rb_str_new2("float")); break;
21
- case 'm': rb_hash_aset($result, rb_str_new2("type"), rb_str_new2("matrix")); break;
22
- case 's': rb_hash_aset($result, rb_str_new2("type"), rb_str_new2("string")); break;
23
- case 'b': rb_hash_aset($result, rb_str_new2("type"), rb_str_new2("boolean")); break;
24
- }
25
- } else {
26
- $result = Qfalse;
27
- }
28
- }
29
-
30
- %typemap(freearg) (char *value, char *value_type) {
31
- free($1);
32
- free($2);
33
- }
34
- extern int native_eval(std::string input, char *value, char *value_type);
9
+ extern std::string native_eval(std::string input);
data/lib/parsec.rb CHANGED
@@ -1,28 +1,29 @@
1
1
  require 'string_to_boolean_refinements'
2
2
  require 'libnativemath'
3
+ require 'json'
3
4
 
4
5
  module Parsec
5
6
  # This is the main class responsible to evaluate the equations
6
7
  class Parsec
7
8
  using StringToBooleanRefinements
8
9
 
9
- VERSION = '0.2.17'.freeze
10
+ VERSION = '0.2.18'.freeze
10
11
 
11
12
  # evaluates the equation
12
13
  def self.eval_equation(equation)
13
14
  remove_spaces(equation)
14
15
 
15
- convert(Libnativemath.native_eval(equation))
16
+ convert(JSON.parse(Libnativemath.native_eval(equation)))
16
17
  end
17
18
 
18
19
  # returns true or raise an error
19
20
  def self.validate_syntax(equation)
20
- validate(Libnativemath.native_eval(equation), true)
21
+ validate(JSON.parse(Libnativemath.native_eval(equation)), true)
21
22
  end
22
23
 
23
24
  # returns true or an error string
24
25
  def self.verify_syntax(equation)
25
- validate(Libnativemath.native_eval(equation), false)
26
+ validate(JSON.parse(Libnativemath.native_eval(equation)), false)
26
27
  end
27
28
 
28
29
  private_class_method
@@ -40,21 +41,22 @@ module Parsec
40
41
  end
41
42
 
42
43
  case ans['type']
43
- when 'int' then return ans['value'].to_i
44
- when 'float' then return ans['value'].to_f
45
- when 'boolean' then return ans['value'].to_bool
46
- when 'string' then return syntax_check(ans['value'])
47
- when 'c' then return 'complex number'
44
+ when 'i' then return ans['value'].to_i
45
+ when 'f' then return ans['value'].to_f
46
+ when 'b' then return ans['value'].to_bool
47
+ when 's' then return error_check(ans['value'])
48
+ when 'c' then return 'complex number' # Maybe future implementation
49
+ when 'm' then return 'matrix value' # Maybe future implementation
48
50
  end
49
51
  end
50
52
 
51
- def self.syntax_check(output)
53
+ def self.error_check(output)
52
54
  raise SyntaxError, output.sub('Error: ', '') if output.include?('Error')
53
55
  output.delete('\"')
54
56
  end
55
57
 
56
58
  def self.validate(ans, raise_error)
57
- if (ans['type'] == 'string') && ans['value'].include?('Error: ')
59
+ if (ans['type'] == 's') && ans['value'].include?('Error: ')
58
60
  raise SyntaxError, ans['value'].sub('Error: ', '') if raise_error
59
61
  return ans['value'].sub('Error: ', '')
60
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsecs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.17
4
+ version: 0.2.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nilton Vasques
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-06-02 00:00:00.000000000 Z
13
+ date: 2018-06-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest