qlang 0.0.27182000 → 0.0.27182100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/README.md +17 -1
- data/Rakefile +16 -3
- data/bin/qlang +7 -1
- data/ext/qlang/QMatrix/q_matrix.c +0 -0
- data/ext/qlang/extconf.rb +3 -0
- data/ext/qlang/qlang.c +65 -0
- data/ext/qlang/qlang.h +6 -0
- data/{spec → legacy_rspec}/langs/Haskell/ex1_after.hs +0 -0
- data/{spec → legacy_rspec}/langs/Haskell/ex1_before.hs +0 -0
- data/{spec → legacy_rspec}/langs/Python/ex1_after.py +0 -0
- data/{spec → legacy_rspec}/langs/Python/ex1_before.py +0 -0
- data/{spec → legacy_rspec}/langs/R/ex1_after.R +0 -0
- data/{spec → legacy_rspec}/langs/R/ex1_before.R +0 -0
- data/{spec → legacy_rspec}/objects/list_spec.rb +0 -0
- data/{spec → legacy_rspec}/objects/matrix_spec.rb +0 -0
- data/{spec → legacy_rspec}/objects/vector_spec.rb +0 -0
- data/lib/qlang.rb +21 -18
- data/lib/qlang/api/func_api.rb +1 -1
- data/lib/qlang/api/integral_api.rb +1 -1
- data/lib/qlang/api/list_api.rb +1 -1
- data/lib/qlang/api/matrix_api.rb +1 -1
- data/lib/qlang/api/vector_api.rb +1 -1
- data/lib/qlang/iq.rb +5 -2
- data/lib/qlang/lexer.rb +2 -2
- data/lib/qlang/lexer/base.rb +31 -14
- data/lib/qlang/lexer/formula_lexer.rb +20 -0
- data/lib/qlang/lexer/main_lexer.rb +25 -0
- data/lib/qlang/lexer/tokens.rb +27 -15
- data/lib/qlang/parser.rb +29 -41
- data/lib/qlang/parser/formula_parser.rb +25 -25
- data/lib/qlang/parser/func_parser.rb +2 -2
- data/lib/qlang/parser/integral_parser.rb +2 -2
- data/lib/qlang/parser/matrix_parser.rb +3 -3
- data/lib/qlang/parser/vector_parser.rb +4 -4
- data/lib/qlang/version.rb +1 -1
- data/{q_lang.gemspec → qlang.gemspec} +4 -2
- data/test/internal/test_tokens.rb +36 -0
- data/test/interpreter/base.rb +17 -0
- data/test/interpreter/test_differential.rb +44 -0
- data/test/interpreter/test_function.rb +45 -0
- data/test/interpreter/test_general.rb +13 -0
- data/test/interpreter/test_integral.rb +39 -0
- data/test/interpreter/test_matrix.rb +71 -0
- data/test/interpreter/test_vector.rb +29 -0
- data/test/langs/test_r.rb +33 -0
- data/test/langs/test_ruby.rb +10 -0
- data/{spec/spec_helper.rb → test/minitest_helper.rb} +4 -4
- data/test/q_matrix/test_q_matrix.rb +13 -0
- data/test/test_qlang.rb +25 -0
- metadata +65 -43
- data/.rspec +0 -2
- data/lib/qlang/lexer/cont_lexer.rb +0 -20
- data/lib/qlang/lexer/func_lexer.rb +0 -13
- data/lib/qlang/lexer/wrap_lexer.rb +0 -25
- data/spec/iq_spec.rb +0 -141
- data/spec/lexer/regular_expressions_spec.rb +0 -45
- data/spec/objects/function_spec.rb +0 -33
- data/spec/objects/integral_spec.rb +0 -21
- data/spec/q_lang_spec.rb +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cd845cb653f28d2e83507b793e3c18d4df0c0b5
|
4
|
+
data.tar.gz: 3318e7abc75c676cbd3e56713202f284e2905b81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1229fcf0b4fa7f8ef90eda3550d34924c0a137480361aae09cee032c865d536af56d7bc271723e3fb63db13cc22c33db7ebb39a5af4bb78b389f8ab86bb65800
|
7
|
+
data.tar.gz: 3b9c3ffcd9591895f342f0828e96665538c24c3aa9920ffdb739f9a1388772ae83bc78c75890c9ec272f47e4e1029cf71944f53ca0ca8e2739eaed7bfeebc762
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,23 @@
|
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/qlang) [](https://travis-ci.org/gogotanaka/Q) [](https://coveralls.io/r/gogotanaka/Q?branch=master) [](https://codeclimate.com/github/gogotanaka/Q) [](https://gemnasium.com/gogotanaka/Q)
|
4
4
|
|
5
|
-
|
5
|
+
## Do you know one of the best language in this world?
|
6
|
+
|
7
|
+
#### I belive mathematics is that one absolutely.
|
8
|
+
|
9
|
+
## How can we deal such a great mathematics in discret world?
|
10
|
+
|
11
|
+
#### Q-language can be.
|
12
|
+
|
13
|
+
|
14
|
+
```
|
15
|
+
+---Discret world---+ +------mathematics-----+
|
16
|
+
| Ruby | | axiom |
|
17
|
+
| TeX |<------ Q ------>| Uncountable noun |
|
18
|
+
| Pyhthon | | real number topology|
|
19
|
+
+-------------------+ +----------------------+
|
20
|
+
```
|
21
|
+
|
6
22
|
|
7
23
|
### Differentiate
|
8
24
|
|
data/Rakefile
CHANGED
@@ -1,7 +1,20 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
-
require "
|
2
|
+
require "rake/testtask"
|
3
3
|
|
4
|
-
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.test_files = FileList['test/**/test_*.rb']
|
7
|
+
end
|
5
8
|
|
6
|
-
|
9
|
+
require "rake/extensiontask"
|
7
10
|
|
11
|
+
Rake::ExtensionTask.new("qlang") do |ext|
|
12
|
+
ext.lib_dir = "lib/qlang"
|
13
|
+
end
|
14
|
+
|
15
|
+
task :compile_and_test do
|
16
|
+
Rake::Task["compile"].invoke
|
17
|
+
Rake::Task["test"].invoke
|
18
|
+
end
|
19
|
+
|
20
|
+
task default: :compile_and_test
|
data/bin/qlang
CHANGED
File without changes
|
data/ext/qlang/qlang.c
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
#include "qlang.h"
|
2
|
+
#include <stdio.h>
|
3
|
+
#define Need_Float(x) do {if (!RB_TYPE_P(x, T_FLOAT)) {(x) = rb_to_float(x);}} while(0)
|
4
|
+
#define GET_FLOAT(x) Need_Float(x);(x) = RFLOAT_VALUE(x)
|
5
|
+
|
6
|
+
double func(double x)
|
7
|
+
{
|
8
|
+
return((x * x) * 3);
|
9
|
+
}
|
10
|
+
|
11
|
+
static VALUE
|
12
|
+
rb_func(VALUE self, VALUE x)
|
13
|
+
{
|
14
|
+
GET_FLOAT(x);
|
15
|
+
return(DBL2NUM((x * x)/2));
|
16
|
+
}
|
17
|
+
|
18
|
+
|
19
|
+
static VALUE
|
20
|
+
execute(VALUE self, VALUE a, VALUE b, VALUE n)
|
21
|
+
{
|
22
|
+
GET_FLOAT(a);
|
23
|
+
GET_FLOAT(b);
|
24
|
+
GET_FLOAT(n);
|
25
|
+
|
26
|
+
int i;
|
27
|
+
double s1=0.0, s2=0.0, d;
|
28
|
+
double x, y[n+1];
|
29
|
+
d=(b-a)/(double)n;
|
30
|
+
for(i=0; i<=n; i++)
|
31
|
+
{
|
32
|
+
x=(double)i*d+a;
|
33
|
+
y[i]=func(x);
|
34
|
+
}
|
35
|
+
for(i=1; i<=n-1; i+=2)
|
36
|
+
{
|
37
|
+
s1+=y[i];
|
38
|
+
}
|
39
|
+
for(i=2; i<=n-2; i+=2)
|
40
|
+
{
|
41
|
+
s2+=y[i];
|
42
|
+
}
|
43
|
+
|
44
|
+
double s=(y[0]+4.0*s1+2.0*s2+y[n])*d/3.0;
|
45
|
+
|
46
|
+
return DBL2NUM(s);
|
47
|
+
}
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
void
|
52
|
+
Init_qlang(void)
|
53
|
+
{
|
54
|
+
VALUE rb_mQMatrix = rb_define_class("QMatrix", rb_cObject);
|
55
|
+
rb_define_method(rb_mQMatrix, "execute", execute, 3);
|
56
|
+
rb_define_method(rb_mQMatrix, "func", rb_func, 1);
|
57
|
+
}
|
58
|
+
|
59
|
+
// VALUE rb_mQMatrix;
|
60
|
+
|
61
|
+
// void
|
62
|
+
// Init_q_matrix(void)
|
63
|
+
// {
|
64
|
+
// rb_mQMatrix = rb_define_module("QMatrix");
|
65
|
+
// }
|
data/ext/qlang/qlang.h
ADDED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/lib/qlang.rb
CHANGED
@@ -1,42 +1,45 @@
|
|
1
|
-
#
|
2
|
-
require 'dydx'
|
3
|
-
include Dydx
|
4
|
-
|
5
|
-
require "qlang/version"
|
6
|
-
require 'qlang/utils/ruby_ext'
|
7
|
-
require 'qlang/lexer'
|
8
|
-
require 'qlang/parser'
|
9
|
-
|
10
|
-
require 'qlang/exec'
|
11
|
-
|
12
|
-
require 'qlang/iq'
|
13
|
-
|
1
|
+
# Ruby stlib
|
14
2
|
require 'kconv'
|
15
3
|
require 'matrix'
|
16
|
-
require 'yaml'
|
17
4
|
require 'singleton'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
# Q core
|
8
|
+
require 'qlang/lexer'
|
9
|
+
require 'qlang/parser'
|
10
|
+
require 'qlang/utils/ruby_ext'
|
11
|
+
require "qlang/version"
|
18
12
|
|
19
13
|
module Qlang
|
14
|
+
# $meta_info indicate what and how to do.
|
20
15
|
class MetaInfo
|
21
16
|
include Singleton
|
22
|
-
attr_accessor :lang, :opts
|
17
|
+
attr_accessor :lang, :opts, :mode
|
18
|
+
|
19
|
+
LANGS_HASH = YAML.load_file("./lib/qlang/utils/langs.yml")['langs']
|
23
20
|
|
24
21
|
def _load
|
25
22
|
# compiles into R as default.
|
26
23
|
lang = :r
|
27
24
|
end
|
25
|
+
|
26
|
+
def langs_hash
|
27
|
+
LANGS_HASH
|
28
|
+
end
|
29
|
+
|
30
|
+
def lang_str
|
31
|
+
LANGS_HASH[@lang.to_s]
|
32
|
+
end
|
28
33
|
end
|
29
34
|
$meta_info = MetaInfo.instance
|
30
35
|
|
31
|
-
LANGS_HASH = YAML.load_file("./lib/qlang/utils/langs.yml")['langs']
|
32
|
-
|
33
36
|
class << self
|
34
37
|
def compile(str)
|
35
38
|
lexed = Lexer.execute(str)
|
36
39
|
Kconv.tosjis(Parser.execute(lexed))
|
37
40
|
end
|
38
41
|
|
39
|
-
|
42
|
+
$meta_info.langs_hash.keys.each do |lang_name|
|
40
43
|
define_method("to_#{lang_name}") do |*opts|
|
41
44
|
$meta_info.lang = lang_name.to_sym
|
42
45
|
$meta_info.opts = opts
|
data/lib/qlang/api/func_api.rb
CHANGED
data/lib/qlang/api/list_api.rb
CHANGED
@@ -7,7 +7,7 @@ module Qlang
|
|
7
7
|
combineds_by_equal = arys.map { |ary| "#{ary[0]}=#{ary[1]}" }.join(', ')
|
8
8
|
"list(#{combineds_by_equal})"
|
9
9
|
else
|
10
|
-
fail "List is not implemented for #{
|
10
|
+
fail "List is not implemented for #{$meta_info.lang_str}"
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
data/lib/qlang/api/matrix_api.rb
CHANGED
@@ -11,7 +11,7 @@ module Qlang
|
|
11
11
|
arys_str = rows.map { |row| "[#{row.join(', ')}]" }.join(', ')
|
12
12
|
"Matrix[#{arys_str}]"
|
13
13
|
else
|
14
|
-
fail "Matrix is not implemented for #{
|
14
|
+
fail "Matrix is not implemented for #{$meta_info.lang_str}"
|
15
15
|
end
|
16
16
|
end
|
17
17
|
module_function :execute
|
data/lib/qlang/api/vector_api.rb
CHANGED
data/lib/qlang/iq.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'dydx'
|
2
|
+
include Dydx
|
3
|
+
|
1
4
|
module Qlang
|
2
5
|
module Iq
|
3
6
|
class Dydx::Algebra::Formula
|
@@ -9,10 +12,10 @@ module Qlang
|
|
9
12
|
end
|
10
13
|
|
11
14
|
def self.execute(code)
|
12
|
-
ruby_code = Q.to_ruby.compile(code)
|
15
|
+
ruby_code = Q.to_ruby.compile(code.encode('utf-8'))
|
13
16
|
ruby_obj = eval(ruby_code)
|
14
17
|
|
15
|
-
optimize_output(ruby_obj)
|
18
|
+
optimize_output(ruby_obj).encode('utf-8')
|
16
19
|
rescue SyntaxError
|
17
20
|
# TODO: emergency
|
18
21
|
case ruby_code
|
data/lib/qlang/lexer.rb
CHANGED
data/lib/qlang/lexer/base.rb
CHANGED
@@ -7,12 +7,12 @@ module Qlang
|
|
7
7
|
attr_accessor :lexeds
|
8
8
|
include Tokens
|
9
9
|
class << self
|
10
|
-
attr_reader :
|
10
|
+
attr_reader :token_rule_hash
|
11
11
|
|
12
12
|
def rule(pattern, &token)
|
13
13
|
token ||= proc { :NULL }
|
14
|
-
@
|
15
|
-
@
|
14
|
+
@token_rule_hash ||= {}
|
15
|
+
@token_rule_hash[token.call] = pattern
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,26 +20,45 @@ module Qlang
|
|
20
20
|
ss = StringScanner.new(str)
|
21
21
|
@lexeds = []
|
22
22
|
until ss.eos?
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
scan_rslt, ss = scan(ss)
|
24
|
+
if scan_rslt
|
25
|
+
@lexeds << scan_rslt unless scan_rslt == :NULL
|
26
|
+
else
|
27
|
+
fail "I'm so sorry, something wrong. Please feel free to report this."
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
def scan(ss)
|
33
|
+
scan_rslt = nil
|
34
|
+
token_rule_hash.each do |token, patter|
|
35
|
+
if ss.scan(patter)
|
36
|
+
scan_rslt = (token == :NULL) ? :NULL : {token => ss[0], els: [ss[1],ss[2], ss[3]].compact }
|
37
|
+
break
|
38
|
+
end
|
39
|
+
end
|
40
|
+
[scan_rslt, ss]
|
41
|
+
end
|
42
|
+
|
32
43
|
# Accessor
|
33
44
|
## GET(without side effect)
|
45
|
+
def [](index)
|
46
|
+
@lexeds[index.to_i]
|
47
|
+
end
|
48
|
+
|
34
49
|
def get_value(num)
|
35
50
|
num = num.to_i
|
36
|
-
@lexeds.
|
51
|
+
@lexeds[num].values.first
|
37
52
|
end
|
38
53
|
|
39
54
|
def token_str
|
40
55
|
@lexeds.map.with_index { |lexed, i| ":#{lexed.keys.first}#{i}" }.join
|
41
56
|
end
|
42
57
|
|
58
|
+
def token_rule_hash
|
59
|
+
self.class.token_rule_hash
|
60
|
+
end
|
61
|
+
|
43
62
|
## POST(with side effect, without idempotence.)
|
44
63
|
def parsed!(parsed, target)
|
45
64
|
case target
|
@@ -50,7 +69,9 @@ module Qlang
|
|
50
69
|
end
|
51
70
|
end
|
52
71
|
|
53
|
-
|
72
|
+
#squash!(range, token: :CONT)
|
73
|
+
def squash!(range, opts={token: :CONT})
|
74
|
+
token = opts[:token]
|
54
75
|
range = (range.first.to_i)..(range.last.to_i)
|
55
76
|
value = values[range].join
|
56
77
|
range.count.times { @lexeds.delete_at(range.first) }
|
@@ -62,10 +83,6 @@ module Qlang
|
|
62
83
|
@lexeds.map { |lexed| lexed.values.first }
|
63
84
|
end
|
64
85
|
|
65
|
-
def [](index)
|
66
|
-
@lexeds[index]
|
67
|
-
end
|
68
|
-
|
69
86
|
private
|
70
87
|
def parsed_at!(token_position, parsed)
|
71
88
|
@lexeds.delete_at(token_position)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Qlang
|
2
|
+
module Lexer
|
3
|
+
class FormulaLexer < Base
|
4
|
+
rule(/\^/) { :EXP }
|
5
|
+
rule(/#{EMBEDDED_FUNC}/) { :BFUNC }
|
6
|
+
rule(/(pi|[1-9a-z]){2,}/) { :MUL }
|
7
|
+
rule(/(pi|[1-9a-z])/) { :SNGL }
|
8
|
+
rule(/([^\^1-9a-z]|^pi)+/) { :OTHER }
|
9
|
+
|
10
|
+
# rule(/#{OPE}/) { :OPE }
|
11
|
+
# rule(/#{FUNCV}/) { :FUNCV }
|
12
|
+
# rule(/#{VAR}/) { :VAR }
|
13
|
+
# rule(/#{NUM}/) { :NUM }
|
14
|
+
# rule(/#{LPRN}/) { :LPRN }
|
15
|
+
# rule(/#{RPRN}/) { :RPRN }
|
16
|
+
|
17
|
+
# rule(/#{ANYSP}/) { }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Qlang
|
2
|
+
module Lexer
|
3
|
+
class MainLexer < Base
|
4
|
+
rule(/(#{FUNCCV})#{ANYSP}#{EQL}#{ANYSP}(#{FORMULA})/) { :def_func }
|
5
|
+
rule(/#{ITGRLSYM}#{ANYSP}#{LPRN}(#{ANYSTR})#{RPRN}#{LBRCT}(#{ANYSTR})#{RBRCT}/) { :integral }
|
6
|
+
rule(/d\/d(#{VAR}) (#{FORMULA})/) { :differential }
|
7
|
+
rule(/#{LPRN}(#{NUMS_BY_SP})#{RPRN}/) { :vector }
|
8
|
+
rule(/#{LPRN}(#{NUMS_BY_SP_BY_SCLN_OR_NELN})#{RPRN}t/m) { :tmatrix }
|
9
|
+
rule(/#{LPRN}(#{NUMS_BY_SP_BY_SCLN_OR_NELN})#{RPRN}/m) { :matrix }
|
10
|
+
|
11
|
+
rule(/#{FUNCCN}/) { :FUNCCN }
|
12
|
+
|
13
|
+
rule(/#{LPRN}/) { :LPRN }
|
14
|
+
rule(/#{RPRN}/) { :RPRN }
|
15
|
+
rule(/#{LBRCS}/) { :LBRCS }
|
16
|
+
rule(/#{RBRCS}/) { :RBRCS }
|
17
|
+
|
18
|
+
rule(/[ \t\f]/)
|
19
|
+
|
20
|
+
rule(/(\r|\n)+/) { :NULL }
|
21
|
+
|
22
|
+
rule(/[^\(\)\{\}(\n\n)]+/) { :CONT }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|