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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3dd94575327cfcb2be0c2ba95bf28013810f0b44
4
- data.tar.gz: 62e55299ddd4fef4e9cf9ab24bdfe245b1657c36
3
+ metadata.gz: 2cd845cb653f28d2e83507b793e3c18d4df0c0b5
4
+ data.tar.gz: 3318e7abc75c676cbd3e56713202f284e2905b81
5
5
  SHA512:
6
- metadata.gz: 2fd9b76b752cc9195600692e8074365f1a1294138477a891add49d8201c1e4208abdc05fa0a927bb87427c2fa4936852f01caefc082f80ca0354b91b2f1787b9
7
- data.tar.gz: cffff8e5cbc59a18ba9fa7ad62f905d1bfc9e85c67b6f2713c27db0af3d3827965104c791c40f2066f1305a77461819cd94092088a50b4fda64085b987233fc0
6
+ metadata.gz: 1229fcf0b4fa7f8ef90eda3550d34924c0a137480361aae09cee032c865d536af56d7bc271723e3fb63db13cc22c33db7ebb39a5af4bb78b389f8ab86bb65800
7
+ data.tar.gz: 3b9c3ffcd9591895f342f0828e96665538c24c3aa9920ffdb739f9a1388772ae83bc78c75890c9ec272f47e4e1029cf71944f53ca0ca8e2739eaed7bfeebc762
@@ -1,3 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.1.2
4
+ - 2.0.0
5
+ - 1.9.3
data/README.md CHANGED
@@ -2,7 +2,23 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/qlang.svg)](http://badge.fury.io/rb/qlang) [![Build Status](https://travis-ci.org/gogotanaka/Q.svg?branch=master)](https://travis-ci.org/gogotanaka/Q) [![Coverage Status](https://coveralls.io/repos/gogotanaka/Q/badge.png?branch=master)](https://coveralls.io/r/gogotanaka/Q?branch=master) [![Code Climate](https://codeclimate.com/github/gogotanaka/Q/badges/gpa.svg)](https://codeclimate.com/github/gogotanaka/Q) [![Dependency Status](https://gemnasium.com/gogotanaka/Q.svg)](https://gemnasium.com/gogotanaka/Q)
4
4
 
5
- Enjoy MATH with Keyboard.
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 "rspec/core/rake_task"
2
+ require "rake/testtask"
3
3
 
4
- RSpec::Core::RakeTask.new(:spec)
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/test_*.rb']
7
+ end
5
8
 
6
- task :default => :spec
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
@@ -1,6 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  require 'qlang'
3
- include Qlang
4
+
5
+ # Q command line
6
+ require 'qlang/exec'
7
+
8
+ # Q interpreter
9
+ require 'qlang/iq'
4
10
 
5
11
  # TODO: There are vanch of todo ..
6
12
  case ARGV.first
File without changes
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("qlang/qlang")
@@ -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
+ // }
@@ -0,0 +1,6 @@
1
+ #ifndef QLANG_H
2
+ #define QLANG_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* QLANG_H */
@@ -1,42 +1,45 @@
1
- # Use Dydx -> https://github.com/gogotanaka/dydx
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
- LANGS_HASH.keys.each do |lang_name|
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
@@ -8,7 +8,7 @@ module Qlang
8
8
  when :ruby
9
9
  "#{func_name}(#{ args.join(' ,') }) <= #{contents}"
10
10
  else
11
- fail "Function is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
11
+ fail "Function is not implemented for #{$meta_info.lang_str}"
12
12
  end
13
13
 
14
14
  end
@@ -7,7 +7,7 @@ module Qlang
7
7
  when :ruby
8
8
  "S(#{func}, #{delta})[#{a}, #{b}]"
9
9
  else
10
- fail "Integral is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
10
+ fail "Integral is not implemented for #{$meta_info.lang_str}"
11
11
  end
12
12
 
13
13
  end
@@ -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 #{LANGS_HASH[$meta_info.lang.to_s]}"
10
+ fail "List is not implemented for #{$meta_info.lang_str}"
11
11
  end
12
12
 
13
13
  end
@@ -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 #{LANGS_HASH[$meta_info.lang.to_s]}"
14
+ fail "Matrix is not implemented for #{$meta_info.lang_str}"
15
15
  end
16
16
  end
17
17
  module_function :execute
@@ -10,7 +10,7 @@ module Qlang
10
10
  when :python
11
11
  "array([#{nums.join(', ')}])"
12
12
  else
13
- fail "Vector is not implemented for #{LANGS_HASH[$meta_info.lang.to_s]}"
13
+ fail "Vector is not implemented for #{$meta_info.lang_str}"
14
14
  end
15
15
  end
16
16
  module_function :execute
@@ -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
@@ -1,10 +1,10 @@
1
1
  require 'qlang/lexer/base'
2
- require 'qlang/lexer/wrap_lexer'
2
+ require 'qlang/lexer/main_lexer'
3
3
 
4
4
  module Qlang
5
5
  module Lexer
6
6
  def execute(str)
7
- WrapLexer.new(str)
7
+ MainLexer.new(str)
8
8
  end
9
9
  module_function :execute
10
10
  end
@@ -7,12 +7,12 @@ module Qlang
7
7
  attr_accessor :lexeds
8
8
  include Tokens
9
9
  class << self
10
- attr_reader :token_hash
10
+ attr_reader :token_rule_hash
11
11
 
12
12
  def rule(pattern, &token)
13
13
  token ||= proc { :NULL }
14
- @token_hash ||= {}
15
- @token_hash[token.call] = pattern
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
- self.class.token_hash.each do |token, patter|
24
- if ss.scan(patter)
25
- (@lexeds << {token => ss[0]}) unless token == :NULL
26
- break
27
- end
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.map { |lexed| lexed.values.first }[num]
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
- def squash!(range, token: :CONT)
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