qlang 0.0.27180000 → 0.0.27182000
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -12
- data/bin/qlang +2 -0
- data/lib/qlang.rb +21 -24
- data/lib/qlang/api.rb +2 -37
- data/lib/qlang/api/func_api.rb +5 -3
- data/lib/qlang/api/integral_api.rb +4 -4
- data/lib/qlang/api/list_api.rb +4 -4
- data/lib/qlang/api/matrix_api.rb +5 -3
- data/lib/qlang/api/vector_api.rb +7 -3
- data/lib/qlang/exec.rb +3 -2
- data/lib/qlang/lexer/base.rb +30 -62
- data/lib/qlang/lexer/tokens.rb +67 -34
- data/lib/qlang/lexer/wrap_lexer.rb +6 -7
- data/lib/qlang/parser.rb +47 -30
- data/lib/qlang/parser/formula_parser.rb +1 -1
- data/lib/qlang/parser/func_parser.rb +1 -1
- data/lib/qlang/parser/list_parser.rb +1 -2
- data/lib/qlang/utils/langs.yml +7 -0
- data/lib/qlang/utils/ruby_ext.rb +46 -0
- data/lib/qlang/version.rb +1 -1
- data/spec/langs/Haskell/ex1_after.hs +74 -0
- data/spec/langs/Haskell/ex1_before.hs +74 -0
- data/spec/langs/Python/ex1_after.py +426 -0
- data/spec/langs/Python/ex1_before.py +426 -0
- data/spec/langs/R/ex1_after.R +1 -1
- data/spec/lexer/regular_expressions_spec.rb +27 -0
- data/spec/objects/vector_spec.rb +11 -0
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3dd94575327cfcb2be0c2ba95bf28013810f0b44
|
4
|
+
data.tar.gz: 62e55299ddd4fef4e9cf9ab24bdfe245b1657c36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fd9b76b752cc9195600692e8074365f1a1294138477a891add49d8201c1e4208abdc05fa0a927bb87427c2fa4936852f01caefc082f80ca0354b91b2f1787b9
|
7
|
+
data.tar.gz: cffff8e5cbc59a18ba9fa7ad62f905d1bfc9e85c67b6f2713c27db0af3d3827965104c791c40f2066f1305a77461819cd94092088a50b4fda64085b987233fc0
|
data/README.md
CHANGED
@@ -60,27 +60,60 @@ f(1, 2)
|
|
60
60
|
|
61
61
|
## How to use
|
62
62
|
|
63
|
-
Install
|
63
|
+
Install qlang gme.
|
64
64
|
|
65
65
|
$ gem install qlang
|
66
|
+
|
67
|
+
|
68
|
+
## Use as native language
|
66
69
|
|
67
|
-
###
|
70
|
+
### Compile into R
|
71
|
+
|
72
|
+
$ qlang -R foo.q
|
73
|
+
|
74
|
+
### Compile into Ruby
|
68
75
|
|
69
|
-
$ qlang -
|
76
|
+
$ qlang -Ruby foo.q
|
70
77
|
|
71
|
-
###
|
78
|
+
### Compile into Python
|
79
|
+
|
80
|
+
$ qlang -Python foo.q
|
81
|
+
|
82
|
+
|
83
|
+
## Use as math template within other langs
|
72
84
|
|
73
|
-
$ qlang -c -Ruby foo.q
|
74
85
|
|
75
|
-
|
86
|
+
```rb
|
87
|
+
class ExampleClass
|
88
|
+
def example_method
|
89
|
+
#your Ruby codes
|
90
|
+
......
|
76
91
|
|
77
|
-
|
92
|
+
I love mathematics.
|
93
|
+
a = (1 3 4)
|
94
|
+
# your Q codes
|
95
|
+
Q.E.D
|
78
96
|
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
$ qlang -Ruby example.rb
|
102
|
+
|
103
|
+
|
104
|
+
```rb
|
105
|
+
class ExampleClass
|
106
|
+
def example_method
|
107
|
+
#your Ruby codes
|
108
|
+
......
|
109
|
+
|
110
|
+
a = Vector[1, 3, 4]
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
```
|
79
115
|
|
80
116
|
## Contributing
|
81
117
|
|
82
|
-
|
83
|
-
|
84
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
85
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
86
|
-
5. Create a new Pull Request
|
118
|
+
Welcome any your PR or issue.
|
119
|
+
You can become commiter, if you commit even once.
|
data/bin/qlang
CHANGED
data/lib/qlang.rb
CHANGED
@@ -3,6 +3,7 @@ require 'dydx'
|
|
3
3
|
include Dydx
|
4
4
|
|
5
5
|
require "qlang/version"
|
6
|
+
require 'qlang/utils/ruby_ext'
|
6
7
|
require 'qlang/lexer'
|
7
8
|
require 'qlang/parser'
|
8
9
|
|
@@ -12,10 +13,22 @@ require 'qlang/iq'
|
|
12
13
|
|
13
14
|
require 'kconv'
|
14
15
|
require 'matrix'
|
16
|
+
require 'yaml'
|
17
|
+
require 'singleton'
|
15
18
|
|
16
19
|
module Qlang
|
17
|
-
|
18
|
-
|
20
|
+
class MetaInfo
|
21
|
+
include Singleton
|
22
|
+
attr_accessor :lang, :opts
|
23
|
+
|
24
|
+
def _load
|
25
|
+
# compiles into R as default.
|
26
|
+
lang = :r
|
27
|
+
end
|
28
|
+
end
|
29
|
+
$meta_info = MetaInfo.instance
|
30
|
+
|
31
|
+
LANGS_HASH = YAML.load_file("./lib/qlang/utils/langs.yml")['langs']
|
19
32
|
|
20
33
|
class << self
|
21
34
|
def compile(str)
|
@@ -23,30 +36,14 @@ module Qlang
|
|
23
36
|
Kconv.tosjis(Parser.execute(lexed))
|
24
37
|
end
|
25
38
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
$type = :R
|
33
|
-
Qlang
|
39
|
+
LANGS_HASH.keys.each do |lang_name|
|
40
|
+
define_method("to_#{lang_name}") do |*opts|
|
41
|
+
$meta_info.lang = lang_name.to_sym
|
42
|
+
$meta_info.opts = opts
|
43
|
+
Qlang
|
44
|
+
end
|
34
45
|
end
|
35
46
|
|
36
|
-
def to_haskell
|
37
|
-
$type = :Hskl
|
38
|
-
Qlang
|
39
|
-
end
|
40
|
-
|
41
|
-
def to_scala
|
42
|
-
$type = :Scla
|
43
|
-
Qlang
|
44
|
-
end
|
45
|
-
|
46
|
-
def to_java
|
47
|
-
$type = :Scla
|
48
|
-
Qlang
|
49
|
-
end
|
50
47
|
end
|
51
48
|
end
|
52
49
|
|
data/lib/qlang/api.rb
CHANGED
@@ -6,50 +6,15 @@ require 'qlang/api/integral_api'
|
|
6
6
|
|
7
7
|
module Qlang
|
8
8
|
module Api
|
9
|
-
# TODO:
|
10
|
-
class ::String
|
11
|
-
def rm(str_or_rgx)
|
12
|
-
gsub(str_or_rgx, '')
|
13
|
-
end
|
14
|
-
|
15
|
-
def rm!(str_or_rgx)
|
16
|
-
gsub!(str_or_rgx, '')
|
17
|
-
self
|
18
|
-
end
|
19
|
-
|
20
|
-
def rms!(*str_or_rgxs)
|
21
|
-
str_or_rgxs.each do |str_or_rgx|
|
22
|
-
rm!(str_or_rgx)
|
23
|
-
end
|
24
|
-
self
|
25
|
-
end
|
26
|
-
|
27
|
-
def split_by_sp
|
28
|
-
split(/ +/)
|
29
|
-
end
|
30
|
-
|
31
|
-
# FIX:
|
32
|
-
def equalize!
|
33
|
-
rms!(/\A +/, / +\z/)
|
34
|
-
if self =~ /\A\(/ && self =~ /\)\z/
|
35
|
-
rms!(/\A\(/, /\)\z/)
|
36
|
-
rms!(/\A +/, / +\z/)
|
37
|
-
else
|
38
|
-
self
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
9
|
class ::Matrix
|
44
10
|
def to_q
|
45
|
-
|
46
|
-
"(#{q_rows})"
|
11
|
+
rows.map(&:join_by_sp).join('; ').parentheses
|
47
12
|
end
|
48
13
|
end
|
49
14
|
|
50
15
|
class ::Vector
|
51
16
|
def to_q
|
52
|
-
|
17
|
+
elements.join_by_sp.parentheses
|
53
18
|
end
|
54
19
|
end
|
55
20
|
end
|
data/lib/qlang/api/func_api.rb
CHANGED
@@ -2,11 +2,13 @@ module Qlang
|
|
2
2
|
module Api
|
3
3
|
module FuncApi
|
4
4
|
def execute(func_name, args, contents)
|
5
|
-
case $
|
6
|
-
when :
|
5
|
+
case $meta_info.lang
|
6
|
+
when :r
|
7
7
|
"#{func_name} <- function(#{ args.join(' ,') }) #{contents}"
|
8
|
-
when :
|
8
|
+
when :ruby
|
9
9
|
"#{func_name}(#{ args.join(' ,') }) <= #{contents}"
|
10
|
+
else
|
11
|
+
fail "Function is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
|
10
12
|
end
|
11
13
|
|
12
14
|
end
|
@@ -3,11 +3,11 @@ module Qlang
|
|
3
3
|
module IntegralApi
|
4
4
|
def execute(func, delta, range)
|
5
5
|
a, b = range.split('..')
|
6
|
-
case $
|
7
|
-
when :
|
8
|
-
fail 'Integral is not implemented for R'
|
9
|
-
when :Ruby
|
6
|
+
case $meta_info.lang
|
7
|
+
when :ruby
|
10
8
|
"S(#{func}, #{delta})[#{a}, #{b}]"
|
9
|
+
else
|
10
|
+
fail "Integral is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
data/lib/qlang/api/list_api.rb
CHANGED
@@ -2,12 +2,12 @@ module Qlang
|
|
2
2
|
module Api
|
3
3
|
module ListApi
|
4
4
|
def execute(arys)
|
5
|
-
case $
|
6
|
-
when :
|
5
|
+
case $meta_info.lang
|
6
|
+
when :r
|
7
7
|
combineds_by_equal = arys.map { |ary| "#{ary[0]}=#{ary[1]}" }.join(', ')
|
8
8
|
"list(#{combineds_by_equal})"
|
9
|
-
|
10
|
-
fail
|
9
|
+
else
|
10
|
+
fail "List is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
data/lib/qlang/api/matrix_api.rb
CHANGED
@@ -4,12 +4,14 @@ module Qlang
|
|
4
4
|
def execute(rows)
|
5
5
|
row_count = rows.count
|
6
6
|
column_count = rows.first.count
|
7
|
-
case $
|
8
|
-
when :
|
7
|
+
case $meta_info.lang
|
8
|
+
when :r
|
9
9
|
"matrix(#{VectorApi.execute(rows.flatten)}, #{row_count}, #{column_count}, byrow = TRUE)"
|
10
|
-
when :
|
10
|
+
when :ruby
|
11
11
|
arys_str = rows.map { |row| "[#{row.join(', ')}]" }.join(', ')
|
12
12
|
"Matrix[#{arys_str}]"
|
13
|
+
else
|
14
|
+
fail "Matrix is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
|
13
15
|
end
|
14
16
|
end
|
15
17
|
module_function :execute
|
data/lib/qlang/api/vector_api.rb
CHANGED
@@ -2,11 +2,15 @@ module Qlang
|
|
2
2
|
module Api
|
3
3
|
module VectorApi
|
4
4
|
def execute(nums)
|
5
|
-
case $
|
6
|
-
when :
|
5
|
+
case $meta_info.lang
|
6
|
+
when :r
|
7
7
|
"c(#{nums.join(', ')})"
|
8
|
-
when :
|
8
|
+
when :ruby
|
9
9
|
"Vector[#{nums.join(', ')}]"
|
10
|
+
when :python
|
11
|
+
"array([#{nums.join(', ')}])"
|
12
|
+
else
|
13
|
+
fail "Vector is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
|
10
14
|
end
|
11
15
|
end
|
12
16
|
module_function :execute
|
data/lib/qlang/exec.rb
CHANGED
@@ -37,9 +37,10 @@ module Qlang
|
|
37
37
|
def parse(file_path)
|
38
38
|
file = open_file(file_path)
|
39
39
|
input_string = read_file(file)
|
40
|
-
input_string =~ /I love mathematics\.(.*)Q\.E\.D/m
|
41
40
|
file.close
|
42
|
-
|
41
|
+
input_string.gsub(/(.*)I love mathematics\.(.*)Q\.E\.D(.*)/m) {
|
42
|
+
"#{$1}#{Kconv.tosjis(Qlang.compile($2))}#{$3}"
|
43
|
+
}
|
43
44
|
end
|
44
45
|
|
45
46
|
def write!(output_path, string)
|
data/lib/qlang/lexer/base.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'strscan'
|
2
2
|
require 'qlang/lexer/tokens'
|
3
3
|
|
4
|
-
|
5
4
|
module Qlang
|
6
5
|
module Lexer
|
7
6
|
class Base
|
@@ -30,88 +29,57 @@ module Qlang
|
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
# Accessor
|
33
|
+
## GET(without side effect)
|
34
|
+
def get_value(num)
|
35
|
+
num = num.to_i
|
36
|
+
@lexeds.map { |lexed| lexed.values.first }[num]
|
36
37
|
end
|
37
38
|
|
38
|
-
def
|
39
|
-
|
40
|
-
3.times do
|
41
|
-
@lexeds.delete_at(num - 1)
|
42
|
-
end
|
43
|
-
@lexeds.insert(num - 1, {R: ":%|#{value}|%:"})
|
39
|
+
def token_str
|
40
|
+
@lexeds.map.with_index { |lexed, i| ":#{lexed.keys.first}#{i}" }.join
|
44
41
|
end
|
45
42
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
43
|
+
## POST(with side effect, without idempotence.)
|
44
|
+
def parsed!(parsed, target)
|
45
|
+
case target
|
46
|
+
when Range
|
47
|
+
parsed_between!((target.first.to_i)..(target.last.to_i), parsed)
|
48
|
+
else
|
49
|
+
parsed_at!(target.to_i, parsed)
|
52
50
|
end
|
53
|
-
@lexeds.insert(num, {CONT: value})
|
54
51
|
end
|
55
52
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
def tokens
|
63
|
-
@lexeds.map { |lexed| lexed.keys.first }
|
64
|
-
end
|
65
|
-
|
66
|
-
def token_with_nums
|
67
|
-
@lexeds.map.with_index { |lexed, i| ":#{lexed.keys.first}#{i}" }
|
68
|
-
end
|
69
|
-
|
70
|
-
def ch_value(token_with_num, value)
|
71
|
-
num = to_num(token_with_num)
|
72
|
-
before_hash = @lexeds.delete_at(num)
|
73
|
-
@lexeds.insert(num, {before_hash.keys.first => value })
|
53
|
+
def squash!(range, token: :CONT)
|
54
|
+
range = (range.first.to_i)..(range.last.to_i)
|
55
|
+
value = values[range].join
|
56
|
+
range.count.times { @lexeds.delete_at(range.first) }
|
57
|
+
@lexeds.insert(range.first, { token => value })
|
74
58
|
end
|
75
59
|
|
60
|
+
# Legacy Accessor
|
76
61
|
def values
|
77
62
|
@lexeds.map { |lexed| lexed.values.first }
|
78
63
|
end
|
79
64
|
|
80
|
-
def token_str
|
81
|
-
token_with_nums.join
|
82
|
-
end
|
83
|
-
|
84
65
|
def [](index)
|
85
66
|
@lexeds[index]
|
86
67
|
end
|
87
68
|
|
88
|
-
|
89
|
-
|
90
|
-
|
69
|
+
private
|
70
|
+
def parsed_at!(token_position, parsed)
|
71
|
+
@lexeds.delete_at(token_position)
|
72
|
+
@lexeds.insert(token_position, { R: parsed })
|
73
|
+
end
|
91
74
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
ary[1] = value.gsub(/:%\|/,'').gsub(/\|%:/,'')
|
97
|
-
hash = Hash[*ary]
|
75
|
+
def parsed_between!(token_range, parsed)
|
76
|
+
start_pos = token_range.first
|
77
|
+
token_range.count.times do
|
78
|
+
@lexeds.delete_at(start_pos)
|
98
79
|
end
|
99
|
-
|
80
|
+
@lexeds.insert(start_pos, { R: parsed })
|
100
81
|
end
|
101
|
-
end
|
102
82
|
|
103
|
-
# NEW APIs
|
104
|
-
def parsed!(token_position, parsed)
|
105
|
-
@lexeds.delete_at(token_position)
|
106
|
-
@lexeds.insert(token_position, { R: parsed })
|
107
|
-
end
|
108
|
-
|
109
|
-
private
|
110
|
-
|
111
|
-
def to_num(token_with_num)
|
112
|
-
token_with_num =~ /\d+/
|
113
|
-
$&.to_i
|
114
|
-
end
|
115
83
|
end
|
116
84
|
end
|
117
85
|
end
|