qlang 0.0.27182000 → 0.0.27182100

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -0
  3. data/README.md +17 -1
  4. data/Rakefile +16 -3
  5. data/bin/qlang +7 -1
  6. data/ext/qlang/QMatrix/q_matrix.c +0 -0
  7. data/ext/qlang/extconf.rb +3 -0
  8. data/ext/qlang/qlang.c +65 -0
  9. data/ext/qlang/qlang.h +6 -0
  10. data/{spec → legacy_rspec}/langs/Haskell/ex1_after.hs +0 -0
  11. data/{spec → legacy_rspec}/langs/Haskell/ex1_before.hs +0 -0
  12. data/{spec → legacy_rspec}/langs/Python/ex1_after.py +0 -0
  13. data/{spec → legacy_rspec}/langs/Python/ex1_before.py +0 -0
  14. data/{spec → legacy_rspec}/langs/R/ex1_after.R +0 -0
  15. data/{spec → legacy_rspec}/langs/R/ex1_before.R +0 -0
  16. data/{spec → legacy_rspec}/objects/list_spec.rb +0 -0
  17. data/{spec → legacy_rspec}/objects/matrix_spec.rb +0 -0
  18. data/{spec → legacy_rspec}/objects/vector_spec.rb +0 -0
  19. data/lib/qlang.rb +21 -18
  20. data/lib/qlang/api/func_api.rb +1 -1
  21. data/lib/qlang/api/integral_api.rb +1 -1
  22. data/lib/qlang/api/list_api.rb +1 -1
  23. data/lib/qlang/api/matrix_api.rb +1 -1
  24. data/lib/qlang/api/vector_api.rb +1 -1
  25. data/lib/qlang/iq.rb +5 -2
  26. data/lib/qlang/lexer.rb +2 -2
  27. data/lib/qlang/lexer/base.rb +31 -14
  28. data/lib/qlang/lexer/formula_lexer.rb +20 -0
  29. data/lib/qlang/lexer/main_lexer.rb +25 -0
  30. data/lib/qlang/lexer/tokens.rb +27 -15
  31. data/lib/qlang/parser.rb +29 -41
  32. data/lib/qlang/parser/formula_parser.rb +25 -25
  33. data/lib/qlang/parser/func_parser.rb +2 -2
  34. data/lib/qlang/parser/integral_parser.rb +2 -2
  35. data/lib/qlang/parser/matrix_parser.rb +3 -3
  36. data/lib/qlang/parser/vector_parser.rb +4 -4
  37. data/lib/qlang/version.rb +1 -1
  38. data/{q_lang.gemspec → qlang.gemspec} +4 -2
  39. data/test/internal/test_tokens.rb +36 -0
  40. data/test/interpreter/base.rb +17 -0
  41. data/test/interpreter/test_differential.rb +44 -0
  42. data/test/interpreter/test_function.rb +45 -0
  43. data/test/interpreter/test_general.rb +13 -0
  44. data/test/interpreter/test_integral.rb +39 -0
  45. data/test/interpreter/test_matrix.rb +71 -0
  46. data/test/interpreter/test_vector.rb +29 -0
  47. data/test/langs/test_r.rb +33 -0
  48. data/test/langs/test_ruby.rb +10 -0
  49. data/{spec/spec_helper.rb → test/minitest_helper.rb} +4 -4
  50. data/test/q_matrix/test_q_matrix.rb +13 -0
  51. data/test/test_qlang.rb +25 -0
  52. metadata +65 -43
  53. data/.rspec +0 -2
  54. data/lib/qlang/lexer/cont_lexer.rb +0 -20
  55. data/lib/qlang/lexer/func_lexer.rb +0 -13
  56. data/lib/qlang/lexer/wrap_lexer.rb +0 -25
  57. data/spec/iq_spec.rb +0 -141
  58. data/spec/lexer/regular_expressions_spec.rb +0 -45
  59. data/spec/objects/function_spec.rb +0 -33
  60. data/spec/objects/integral_spec.rb +0 -21
  61. data/spec/q_lang_spec.rb +0 -47
@@ -1,13 +0,0 @@
1
- module Qlang
2
- module Lexer
3
- class FuncLexer < Base
4
- rule(%r@#{FUNCCV}@) { :FDEF }
5
- rule(/\=/) { :EQL }
6
-
7
- rule(/[ \t\f]/)
8
-
9
- rule(/\r\n/) { :NLIN }
10
- rule(/[\w\(].*/) { :FOML }
11
- end
12
- end
13
- end
@@ -1,25 +0,0 @@
1
- module Qlang
2
- module Lexer
3
- class WrapLexer < Base
4
- rule(/#{FUNCCV}#{ANYSP}=#{ANYSP}#{FORMULA}+/) { :def_func }
5
- rule(/#{FUNCCN}/) { :eval_func }
6
- rule(/S#{ANYSP}#{LPRN}#{ANYSTR}#{RPRN}\[#{ANYSTR}\]/) { :integral }
7
- rule(/d\/d#{VAR} #{FORMULA}/) { :differential }
8
- rule(/#{LPRN}#{NUMS_BY_SP}#{RPRN}/) { :vector }
9
- rule(/#{LPRN}#{NUMS_BY_SP_BY_SCLN}#{RPRN}t/) { :tmatrix }
10
- rule(/#{LPRN}#{NUMS_BY_SP_BY_SCLN}#{RPRN}/) { :matrix }
11
-
12
-
13
- rule(/\(/) { :LPRN }
14
- rule(/\)/) { :RPRN }
15
- rule(/\{/) { :LBRC }
16
- rule(/\}/) { :RBRC }
17
-
18
- rule(/[ \t\f]/)
19
-
20
- rule(/(\r|\n)+/) { :NLIN }
21
-
22
- rule(/[^\(\)\{\}(\n\n)]+/) { :CONT }
23
- end
24
- end
25
- end
@@ -1,141 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Qlang do
4
- describe Iq do
5
- def self.def_test(name, input, output)
6
- it name + '_def' do
7
- expect(Iq.execute(input)).to eq(output)
8
- end
9
- end
10
-
11
- def self.cal_test(name, input, output)
12
- it name + '_cal' do
13
- expect(Iq.execute(input)).to eq(output)
14
- reset
15
- end
16
- end
17
-
18
- describe 'General' do
19
- cal_test('ex1', '2x', '2x')
20
- cal_test('ex2', 'x + x', '2x')
21
- cal_test('ex3', 'x * y', 'xy')
22
- end
23
-
24
- describe 'Matrix' do
25
- cal_test('ex1',
26
- '(1 2 3; 4 5 6)',
27
- '(1 2 3; 4 5 6)'
28
- )
29
-
30
- cal_test('ex2',
31
- '(1 2 3; 4 5 6) + (1 2 3; 4 5 6)',
32
- '(2 4 6; 8 10 12)'
33
- )
34
-
35
- cal_test('ex3',
36
- '(1 2 3; 4 5 6) - (2 4 1; 8 3 9)',
37
- '(-1 -2 2; -4 2 -3)'
38
- )
39
-
40
- cal_test('ex4',
41
- '(1 2; 3 4) * (1 2; 3 4)',
42
- '(7 10; 15 22)'
43
- )
44
-
45
- cal_test('ex5',
46
- '(1 2; 3 4) ** 2',
47
- '(7 10; 15 22)'
48
- )
49
-
50
- cal_test('ex6',
51
- '(1 2; 3 4) ** 2',
52
- '(7 10; 15 22)'
53
- )
54
-
55
- cal_test('ex7',
56
- '(1 2; 3 4) * (1 2)',
57
- '(5 11)'
58
- )
59
-
60
- cal_test('ex8',
61
- '(1 2 3; 4 5 6)t',
62
- '(1 4; 2 5; 3 6)'
63
- )
64
- end
65
-
66
- describe 'Vector' do
67
- it do
68
- expect(Iq.execute('(1 2 3)')).to eq('(1 2 3)')
69
- expect(Iq.execute('(1 2 3) + (1 2 3)')).to eq('(2 4 6)')
70
- expect(Iq.execute('(1 2 3 ) + ( 1 2 3 )')).to eq('(2 4 6)')
71
- expect(Iq.execute('(1 2 3) - (1 2 3) - (1 2 3)')).to eq('(-1 -2 -3)')
72
- end
73
- end
74
-
75
- describe 'List' do
76
- it do
77
- end
78
- end
79
-
80
- describe 'Diff' do
81
- it do
82
- expect(Iq.execute('d/dx(e ** x)')).to eq('e ^ x')
83
- expect(Iq.execute('d/dx(x ** 2)')).to eq('2x')
84
- expect(Iq.execute('d/dx(x * 2)')).to eq('2')
85
- expect(Iq.execute('d/dx( sin(x) )')).to eq('cos( x )')
86
- expect(Iq.execute('d/dx(log( x ))')).to eq('1 / x')
87
- end
88
- cal_test('ex1', 'd/dx cos(x)', '- sin( x )')
89
- cal_test('ex2', 'd/dx xx', '2x')
90
- end
91
-
92
- describe 'Integral' do
93
- it do
94
- expect(Iq.execute('S( log(x)dx )[0..1]')).to eq('-oo')
95
- expect(Iq.execute('S( sin(x)dx )[0..pi]')).to eq('2.0')
96
- expect(Iq.execute('S( cos(x)dx )[0..pi]')).to eq('0.0')
97
- expect(Iq.execute('S( cos(x)dx )[0..pi]')).to eq('0.0')
98
- end
99
- cal_test('ex1', 'S(xx dx)[0..1]', '0.33333333')
100
- cal_test('ex2', 'S(2pi dx)[0..1]', '6.28318531')
101
- end
102
-
103
- describe 'Function' do
104
- def_test('ex1', 'f(x, y) = x + y', 'x + y')
105
- cal_test('ex1', 'f( 4, 5 )', '9.0')
106
-
107
- def_test('ex2', 'f( x , y) = xy', 'x * y')
108
- cal_test('ex2', 'f( 3, 9 )', '27.0')
109
-
110
- def_test('ex3', 'f(x, y) = xy^2', 'x * ( y ** 2 )')
111
- cal_test('ex3', 'f( 3, 2 )', '12.0')
112
-
113
- def_test('ex4', 'f(x, y) = xy^2', 'x * ( y ** 2 )')
114
- cal_test('ex4', 'df/dx', 'y ^ 2')
115
-
116
- def_test('ex5', 'g(x) = x ^ 2', 'x ** 2')
117
- cal_test('ex5', 'g(2)', '4.0')
118
-
119
- def_test('ex6', 'h(x) = e ^ 2', 'e ** 2')
120
- cal_test('ex6', 'h(2)', '7.3890560989306495')
121
-
122
- def_test('ex7', 'h(x) = pix', 'pi * x')
123
- cal_test('ex7', 'h(3)', '9.42477796076938')
124
-
125
- def_test('ex8', 'h(x) = pie', 'pi * e')
126
- cal_test('ex8', 'h(2)', '8.539734222673566')
127
-
128
- def_test('ex9', 'h(x) = ( 1 / ( 2pi ) ^ ( 1 / 2.0 ) ) * e ^ ( - x ^ 2 / 2 )', '( ( 4503599627370496 / 6369051672525773 ) / ( pi ** 0.5 ) ) * ( e ** ( ( - ( x ** 2 ) ) / 2 ) )')
129
- cal_test('ex9', 'S( h(x)dx )[-oo..oo]', '1.0')
130
-
131
- def_test('ex10', 'f(x) = sin(x)', 'sin( x )')
132
- cal_test('ex10', 'f(pi)', '0.0')
133
-
134
- def_test('ex11', 'f(x) = cos(x)', 'cos( x )')
135
- cal_test('ex11', 'f(pi)', '-1.0')
136
-
137
- def_test('ex11', 'f(x) = log(x)', 'log( x )')
138
- cal_test('ex11', 'f(e)', '1.0')
139
- end
140
- end
141
- end
@@ -1,45 +0,0 @@
1
- require 'spec_helper'
2
-
3
- include Lexer::Tokens
4
-
5
- describe 'Regular expressions' do
6
- def self.should_match(num, rgx, str)
7
- it 'ex' + num.to_s do
8
- expect(rgx =~ str).to eq(0)
9
- expect($&).to eq(str)
10
- end
11
- end
12
-
13
- def self.should_not_match(num, rgx, str)
14
- it 'ex' + num.to_s do
15
- expect(rgx =~ str).not_to eq(0)
16
- end
17
- end
18
-
19
- describe 'tokens' do
20
- describe 'nums' do
21
- should_match(1, NUM, '1')
22
- should_match(2, NUM, '234987423')
23
- should_match(3, NUM, '23423948.298743')
24
- should_match(4, NUM, 'e')
25
- should_match(5, NUM, 'pi')
26
- should_not_match(6, NUM, 'a')
27
- end
28
- describe 'vars' do
29
- # should_match(1, VAR_MUL, 'ab')
30
- # should_not_match(2, VAR_MUL, 'pi')
31
- # should_not_match(3, VAR_MUL, 'sin')
32
- end
33
- end
34
-
35
-
36
- describe 'function' do
37
- should_match(1, /[fgh]\(\w( ?, ?\w)*\) ?= ?[^\r\n]+/, 'f(x) = xy')
38
- end
39
-
40
- describe 'differentiate' do
41
- rgx = /d\/d[a-zA-Z] .*/
42
- should_match(1, rgx, 'd/dx sin(x)')
43
- should_match(2, rgx, 'd/dz z^2')
44
- end
45
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Qlang do
4
- describe 'Function' do
5
- context 'into R' do
6
- it do
7
- expect(
8
- Q.to_r.compile('f(x, y) = x + y')
9
- ).to eq(
10
- "f <- function(x, y) x + y"
11
- )
12
-
13
- expect(
14
- Q.to_r.compile('g(x) = x ^ 2')
15
- ).to eq(
16
- "g <- function(x) x ^ 2"
17
- )
18
-
19
- expect(
20
- Q.to_r.compile('g(x) = x ^ (2 + 2)')
21
- ).to eq(
22
- "g <- function(x) x ^ (2 + 2)"
23
- )
24
-
25
- expect(
26
- Q.to_r.compile('h(a, b, c) = a ^ 2 + b ^ 2 + c ^ 2')
27
- ).to eq(
28
- "h <- function(a, b, c) a ^ 2 + b ^ 2 + c ^ 2"
29
- )
30
- end
31
- end
32
- end
33
- end
@@ -1,21 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Qlang do
4
- describe 'Integral' do
5
- context 'into R' do
6
- it do
7
-
8
- end
9
- end
10
-
11
- context 'into Ruby' do
12
- it do
13
- expect(
14
- Q.to_ruby.compile('S( log(x)dx )[0..1]')
15
- ).to eq(
16
- 'S(log(x), dx)[0, 1]'
17
- )
18
- end
19
- end
20
- end
21
- end
@@ -1,47 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Qlang do
4
- it 'has a version number' do
5
- expect(Qlang::VERSION).not_to be nil
6
- end
7
-
8
- it 'has alias as Q' do
9
- expect(Qlang).to eq Q
10
- end
11
-
12
- describe Dydx do
13
- it 'check some example' do
14
- expect(d/dx(sin(x))).to eq cos(x)
15
-
16
- expect(
17
- Q.to_ruby.compile('d/dx(sin(x))')
18
- ).to eq(
19
- "d/dx(sin (x))"
20
- )
21
-
22
- expect(
23
- Q.to_ruby.compile('d/dx(log(x))')
24
- ).to eq(
25
- "d/dx(log (x))"
26
- )
27
-
28
- expect(
29
- Q.to_ruby.compile('f(x, y) = x + y')
30
- ).to eq(
31
- "f(x, y) <= x + y"
32
- )
33
-
34
- expect(
35
- Matrix[[1, 2, 3], [4, 5, 6]].to_q
36
- ).to eq(
37
- "(1 2 3; 4 5 6)"
38
- )
39
-
40
- expect(
41
- Vector[1, 2, 3].to_q
42
- ).to eq(
43
- "(1 2 3)"
44
- )
45
- end
46
- end
47
- end