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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38ea44cfe5b2bff006fc553503a35c4d089f8109
4
- data.tar.gz: 458fc83a85f7847b4f2258d8a8be725949b4ce32
3
+ metadata.gz: 3dd94575327cfcb2be0c2ba95bf28013810f0b44
4
+ data.tar.gz: 62e55299ddd4fef4e9cf9ab24bdfe245b1657c36
5
5
  SHA512:
6
- metadata.gz: dd3ba3a14aec2546795f4cac2dd533736afbd3b02213b355b7a61ee9548874514cf9f5c6db5965197bf44d085926a7e89896d000d5d95fd59527285eda9d848e
7
- data.tar.gz: 2fc98ec6103e6f16d5b90314365b412068f6abc1807e1cd75da2b94827e6bae40fedb1e743a6c286f21bcc656296860116f42173ca12100ee66ebd083f598ec4
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 it yourself as:
63
+ Install qlang gme.
64
64
 
65
65
  $ gem install qlang
66
+
67
+
68
+ ## Use as native language
66
69
 
67
- ### Compiler into R
70
+ ### Compile into R
71
+
72
+ $ qlang -R foo.q
73
+
74
+ ### Compile into Ruby
68
75
 
69
- $ qlang -c -R foo.q
76
+ $ qlang -Ruby foo.q
70
77
 
71
- ### Compiler into Ruby
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
- ### Interpreter
86
+ ```rb
87
+ class ExampleClass
88
+ def example_method
89
+ #your Ruby codes
90
+ ......
76
91
 
77
- $ qlang -i
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
- 1. Fork it ( https://github.com/gogotanaka/Q/fork )
83
- 2. Create your feature branch (`git checkout -b my-new-feature`)
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
@@ -15,6 +15,8 @@ when '-i'
15
15
  puts e
16
16
  end
17
17
  end
18
+ when '-v'
19
+ puts Qlang::VERSION
18
20
  when '-R', '-Ruby'
19
21
  exec = Qlang::Exec::Compiler.new(ARGV)
20
22
  exec.output!
@@ -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
- # compiles into R as default.
18
- $type = :R
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
- def to_ruby
27
- $type = :Ruby
28
- Qlang
29
- end
30
-
31
- def to_r
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
 
@@ -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
- q_rows = rows.map { |row| row.join(' ') }.join('; ')
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
- "(#{elements.join(' ')})"
17
+ elements.join_by_sp.parentheses
53
18
  end
54
19
  end
55
20
  end
@@ -2,11 +2,13 @@ module Qlang
2
2
  module Api
3
3
  module FuncApi
4
4
  def execute(func_name, args, contents)
5
- case $type
6
- when :R
5
+ case $meta_info.lang
6
+ when :r
7
7
  "#{func_name} <- function(#{ args.join(' ,') }) #{contents}"
8
- when :Ruby
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 $type
7
- when :R
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
@@ -2,12 +2,12 @@ module Qlang
2
2
  module Api
3
3
  module ListApi
4
4
  def execute(arys)
5
- case $type
6
- when :R
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
- when :Ruby
10
- fail 'List is not implemented for Ruby'
9
+ else
10
+ fail "List is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
11
11
  end
12
12
 
13
13
  end
@@ -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 $type
8
- when :R
7
+ case $meta_info.lang
8
+ when :r
9
9
  "matrix(#{VectorApi.execute(rows.flatten)}, #{row_count}, #{column_count}, byrow = TRUE)"
10
- when :Ruby
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
@@ -2,11 +2,15 @@ module Qlang
2
2
  module Api
3
3
  module VectorApi
4
4
  def execute(nums)
5
- case $type
6
- when :R
5
+ case $meta_info.lang
6
+ when :r
7
7
  "c(#{nums.join(', ')})"
8
- when :Ruby
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
@@ -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
- Kconv.tosjis(Qlang.compile($1))
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)
@@ -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
- def get_value(token_with_num)
34
- num = to_num(token_with_num)
35
- values[num]
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 squash_with_prn(token_with_num, value)
39
- num = to_num(token_with_num)
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
- def squash_to_cont(token_with_num, count)
47
- num = to_num(token_with_num)
48
- value = ''
49
- count.times do
50
- value += values[num]
51
- @lexeds.delete_at(num)
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 ch_token(token_with_num, token)
57
- num = to_num(token_with_num)
58
- before_hash = @lexeds.delete_at(num)
59
- @lexeds.insert(num, {token => before_hash.values.first})
60
- end
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
- def split(separator)
89
- values.chunk { |e| e == separator }.reject { |sep, _| sep }.map { |_, ans| ans }
90
- end
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
- def fix_r_txt!
93
- @lexeds.map! do |hash|
94
- if value = (hash[:R] || hash[:CONT])
95
- ary = hash.first
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
- hash
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