qlang 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9bb9bd866002f8aee8d711749a702d8e02d94be6
4
+ data.tar.gz: da4d28e0d720d3c3f7c7ee9795f0710c6c8b43b8
5
+ SHA512:
6
+ metadata.gz: 0899aa57b3f32ca74c360c1268c398893458221bbea101beaf3d390a55ed6422377986eff7b51b3adbdf783c4cdb3e53719fe3aa00ddbb63003bdc31b5efc48c
7
+ data.tar.gz: 2200864ef4a7b8376d7e4c5c27d5de9270a14b175d4335c577238fdbfaf6dcf8e3803625001f3ef65f2c4605bd75fb95d1c05c19a31051e8ab275973c1a3b788
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ Eval:
2
+ Enabled: false
3
+
4
+ LineLength:
5
+ Enabled: false
6
+
7
+ # Configuration parameters: AllowedVariables.
8
+ GlobalVars:
9
+ Enabled: false
10
+
11
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
12
+ MethodName:
13
+ Enabled: false
14
+
15
+ OpMethod:
16
+ Enabled: false
17
+
18
+ Void:
19
+ Enabled: false
20
+
21
+ SpaceInsideParens:
22
+ Enabled: false
23
+
24
+ SpaceAroundOperators:
25
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.1.2'
3
+
4
+ gem 'dydx'
5
+
6
+ gem 'rubocop'
7
+ gem 'pry'
8
+
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 gogotanaka
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Qlang
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'qlang'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install qlang
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/qlang/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/bin/dydx ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # The command line Haml parser.
3
+
4
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'q'
6
+
7
+ opts = Q::Exec::Compiler.new(ARGV)
8
+ opts.parse!
data/bin/q_lang ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'qlang'
data/lib/qlang.rb ADDED
@@ -0,0 +1,53 @@
1
+ # Use Dydx -> https://github.com/gogotanaka/dydx
2
+ require 'dydx'
3
+ include Dydx
4
+
5
+ require "qlang/version"
6
+ require 'qlang/lexer'
7
+ require 'qlang/parser'
8
+
9
+ require 'qlang/exec'
10
+
11
+ require 'qlang/q_on_irb'
12
+
13
+ require "kconv"
14
+
15
+ module Qlang
16
+ # compiles into R as default.
17
+ $type = :R
18
+
19
+ class << self
20
+ def compile(str)
21
+ lexed = Lexer.execute(str)
22
+ Kconv.tosjis(Parser.execute(lexed))
23
+ end
24
+
25
+ def to_ruby
26
+ $type = :Ruby
27
+ Qlang
28
+ end
29
+
30
+ def to_r
31
+ $type = :R
32
+ Qlang
33
+ end
34
+
35
+ def to_haskell
36
+ $type = :Hskl
37
+ Qlang
38
+ end
39
+
40
+ def to_scala
41
+ $type = :Scla
42
+ Qlang
43
+ end
44
+
45
+ def to_java
46
+ $type = :Scla
47
+ Qlang
48
+ end
49
+ end
50
+ end
51
+
52
+ # Make alias as Q
53
+ Q = Qlang
data/lib/qlang/api.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'qlang/api/matrix_api'
2
+ require 'qlang/api/vector_api'
3
+ require 'qlang/api/list_api'
4
+ require 'qlang/api/func_api'
5
+
6
+ module Qlang
7
+ module Api
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Qlang
2
+ module Api
3
+ module FuncApi
4
+ def execute(func_name, args, contents)
5
+ "#{func_name} <- function(#{ args.join(' ,') }) #{contents}"
6
+ end
7
+ module_function :execute
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module Qlang
2
+ module Api
3
+ module ListApi
4
+ def execute(arys)
5
+ combineds_by_equal = arys.map { |ary| "#{ary[0]}=#{ary[1]}" }.join(', ')
6
+ "list(#{combineds_by_equal})"
7
+ end
8
+ module_function :execute
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ module Qlang
2
+ module Api
3
+ module MatrixApi
4
+ def execute(rows)
5
+ row_count = rows.count
6
+ column_count = rows.first.count
7
+ case $type
8
+ when :R
9
+ "matrix(#{VectorApi.execute(rows.flatten)}, #{row_count}, #{column_count}, byrow = TRUE)"
10
+ when :Ruby
11
+ arys_str = rows.map { |row| "[#{row.join(', ')}]" }.join(', ')
12
+ "Matrix[#{arys_str}]"
13
+ end
14
+ end
15
+ module_function :execute
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Qlang
2
+ module Api
3
+ module VectorApi
4
+ def execute(nums)
5
+ case $type
6
+ when :R
7
+ "c(#{nums.join(', ')})"
8
+ when :Ruby
9
+ "Vector[#{nums.join(', ')}]"
10
+ end
11
+ end
12
+ module_function :execute
13
+ end
14
+ end
15
+ end
data/lib/qlang/exec.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Qlang
2
+ module Exec
3
+ class Compiler
4
+ def initialize(args)
5
+ @args = args
6
+ end
7
+
8
+ def parse!
9
+ parse
10
+ rescue Exception => e
11
+ raise e if @options[:trace] || e.is_a?(SystemExit)
12
+
13
+ print "#{e.class}: " unless e.class == RuntimeError
14
+ puts "#{e.message}"
15
+ puts " Use --trace for backtrace."
16
+ exit 1
17
+ ensure
18
+ exit 0
19
+ end
20
+
21
+ private def parse
22
+ raise '#{@args[0]} is unsupported option' unless @args[0] == '-q'
23
+ filename = @args[1]
24
+ file = open_file(filename)
25
+ string = read_file(file)
26
+ print(Kconv.tosjis(Qlang.compile(string)), '\n')
27
+ file.close
28
+ end
29
+
30
+ private def open_file(filename, flag = 'r')
31
+ return if filename.nil?
32
+ File.open(filename, flag)
33
+ end
34
+
35
+ private def read_file(file)
36
+ file.read
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ require 'qlang/lexer/base'
2
+ require 'qlang/lexer/wrap_lexer'
3
+
4
+ module Qlang
5
+ module Lexer
6
+ def execute(str)
7
+ WrapLexer.new(str)
8
+ end
9
+ module_function :execute
10
+ end
11
+ end
@@ -0,0 +1,105 @@
1
+ require 'strscan'
2
+
3
+ module Qlang
4
+ module Lexer
5
+ class Base
6
+ class << self
7
+ attr_reader :token_hash
8
+
9
+ def rule(pattern, &token)
10
+ token ||= proc { :NULL }
11
+ @token_hash ||= {}
12
+ @token_hash[token.call] = pattern
13
+ end
14
+ end
15
+
16
+ def initialize(str)
17
+ ss = StringScanner.new(str)
18
+ @lexeds = []
19
+ until ss.eos?
20
+ self.class.token_hash.each do |token, patter|
21
+ if ss.scan(patter)
22
+ (@lexeds << {token => ss[0]}) unless token == :NULL
23
+ break
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def get_value(token_with_num)
30
+ num = to_num(token_with_num)
31
+ values[num]
32
+ end
33
+
34
+ def squash_with_prn(token_with_num, value)
35
+ num = to_num(token_with_num)
36
+ 3.times do
37
+ @lexeds.delete_at(num - 1)
38
+ end
39
+ @lexeds.insert(num - 1, {R: ":%|#{value}|%:"})
40
+ end
41
+
42
+ def squash_to_cont(token_with_num, count)
43
+ num = to_num(token_with_num)
44
+ value = ''
45
+ count.times do
46
+ value += values[num]
47
+ @lexeds.delete_at(num)
48
+ end
49
+ @lexeds.insert(num, {CONT: value})
50
+ end
51
+
52
+ def ch_token(token_with_num, token)
53
+ num = to_num(token_with_num)
54
+ before_hash = @lexeds.delete_at(num)
55
+ @lexeds.insert(num, {token => before_hash.values.first})
56
+ end
57
+
58
+ def tokens
59
+ @lexeds.map { |lexed| lexed.keys.first }
60
+ end
61
+
62
+ def token_with_nums
63
+ @lexeds.map.with_index { |lexed, i| ":#{lexed.keys.first}#{i}" }
64
+ end
65
+
66
+ def ch_value(token_with_num, value)
67
+ num = to_num(token_with_num)
68
+ before_hash = @lexeds.delete_at(num)
69
+ @lexeds.insert(num, {before_hash.keys.first => value })
70
+ end
71
+
72
+ def values
73
+ @lexeds.map { |lexed| lexed.values.first }
74
+ end
75
+
76
+ def token_str
77
+ token_with_nums.join
78
+ end
79
+
80
+ def [](index)
81
+ @lexeds[index]
82
+ end
83
+
84
+ def split(separator)
85
+ values.chunk { |e| e == separator }.reject { |sep, _| sep }.map { |_, ans| ans }
86
+ end
87
+
88
+ def fix_r_txt!
89
+ @lexeds.map! do |hash|
90
+ if value = (hash[:R] || hash[:CONT])
91
+ ary = hash.first
92
+ ary[1] = value.gsub(/:%\|/,'').gsub(/\|%:/,'')
93
+ hash = Hash[*ary]
94
+ end
95
+ hash
96
+ end
97
+ end
98
+
99
+ private def to_num(token_with_num)
100
+ token_with_num =~ /\d+/
101
+ $&.to_i
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,17 @@
1
+ module Qlang
2
+ module Lexer
3
+ class ContLexer < Base
4
+ rule(/:%\|.+\|%:/) { :R }
5
+ rule(/('|")\w+('|")/) { :STR }
6
+ rule(/[0-9]+/) { :NUM }
7
+ rule(/\:/) { :CLN }
8
+ rule(/\;/) { :SCLN }
9
+ rule(/\,/) { :CMA }
10
+
11
+ rule(/[ \t\f]/)
12
+
13
+ rule(/\r\n/) { :NLIN }
14
+ rule(/\w+/) { :OTHER }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Qlang
2
+ module Lexer
3
+ class FuncLexer < Base
4
+ rule(/\w\(\w( ?, ?\w)*\)/) { :FDEF }
5
+ rule(/\=/) { :EQL }
6
+
7
+ rule(/[ \t\f]/)
8
+
9
+ rule(/\r\n/) { :NLIN }
10
+ rule(/\w.*/) { :OTHER }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module Qlang
2
+ module Lexer
3
+ class WrapLexer < Base
4
+ rule(/\w\(\w( ?, ?\w)*\) ?= ?[^\r\n]+/) { :FUNC }
5
+ rule(/\(/) { :LPRN }
6
+ rule(/\)/) { :RPRN }
7
+ rule(/\{/) { :LBRC }
8
+ rule(/\}/) { :RBRC }
9
+
10
+ rule(/[ \t\f]/)
11
+
12
+ rule(/(\r|\n)+/) { :NLIN }
13
+
14
+ rule(/[^\(\)\{\}(\n\n)]+/) { :CONT }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,65 @@
1
+ require 'qlang/api'
2
+
3
+ require 'qlang/lexer/cont_lexer'
4
+ require 'qlang/lexer/func_lexer'
5
+
6
+ require 'qlang/parser/base'
7
+ require 'qlang/parser/matrix_parser'
8
+ require 'qlang/parser/vector_parser'
9
+ require 'qlang/parser/list_parser'
10
+ require 'qlang/parser/func_parser'
11
+
12
+ module Qlang
13
+ module Parser
14
+ def execute(lexed)
15
+ time = Time.now
16
+ until lexed.token_str =~ /\A(:NLIN\d|:R\d)+\z/
17
+ binding.pry if Time.now > time + 10
18
+
19
+ case lexed.token_str
20
+ when /:LPRN\d(:CONT\d):RPRN\d/
21
+ cont_token_with_num = $1
22
+ cont_lexed = Lexer::ContLexer.new(lexed.get_value(cont_token_with_num))
23
+
24
+ case cont_lexed.token_str
25
+ when /(:NUM\d)+(:SCLN\d|:NLIN\d)(:NUM\d)/
26
+ cont = MatrixParser.execute(cont_lexed)
27
+ lexed.squash_with_prn(cont_token_with_num, cont)
28
+ when /(:NUM\d)+/
29
+ cont = VectorParser.execute(cont_lexed)
30
+ lexed.squash_with_prn(cont_token_with_num, cont)
31
+ end
32
+
33
+ when /:LBRC\d(:CONT\d):RBRC\d/
34
+ cont_token_with_num = $1
35
+ cont_lexed = Lexer::ContLexer.new(lexed.get_value(cont_token_with_num))
36
+
37
+ case cont_lexed.token_str
38
+ when /(:OTHER\d:CLN\d(:STR\d|:NUM\d|:R\d):CMA)*(:OTHER\d:CLN\d(:STR\d|:NUM\d|:R\d))/
39
+ cont = ListParser.execute(cont_lexed)
40
+ lexed.squash_with_prn(cont_token_with_num, cont)
41
+ end
42
+
43
+ when /:FUNC\d/
44
+ cont_token_with_num = $&
45
+ cont_lexed = Lexer::FuncLexer.new(lexed.get_value(cont_token_with_num))
46
+
47
+ case cont_lexed.token_str
48
+ when /:FDEF\d:EQL\d:OTHER\d/
49
+ cont = FuncParser.execute(cont_lexed)
50
+ lexed.ch_value(cont_token_with_num, cont)
51
+ lexed.ch_token(cont_token_with_num, :R)
52
+ end
53
+
54
+ when /:CONT\d/
55
+ lexed.ch_token($&, :R)
56
+ end
57
+
58
+ lexed.squash_to_cont($1, 2) if lexed.token_str =~ /(:CONT\d|:R\d)(:CONT\d|:R\d)/
59
+ end
60
+ lexed.fix_r_txt!
61
+ lexed.values.join
62
+ end
63
+ module_function :execute
64
+ end
65
+ end
@@ -0,0 +1,20 @@
1
+ module Qlang
2
+ module Parser
3
+ module Base
4
+ include ::Qlang::Api
5
+ # TODO:
6
+ class ::String
7
+ def rm(str_or_rgx)
8
+ gsub!(str_or_rgx, '')
9
+ end
10
+
11
+ def rms(*str_or_rgxs)
12
+ str_or_rgxs.each do |str_or_rgx|
13
+ rm(str_or_rgx)
14
+ end
15
+ self
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module Qlang
2
+ module Parser
3
+ module FuncParser
4
+ include Base
5
+ def execute(lexed)
6
+ lexed.fix_r_txt!
7
+ fdef_ary = lexed[0][:FDEF].split('')
8
+ func_name = fdef_ary.shift
9
+ args = fdef_ary.join.rms('(', ')', ',', ' ').split('')
10
+
11
+ FuncApi.execute(func_name, args, lexed[-1][:OTHER])
12
+ end
13
+ module_function :execute
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module Qlang
2
+ module Parser
3
+ module ListParser
4
+ include Base
5
+ def execute(lexed)
6
+ lexed.fix_r_txt!
7
+ arys = lexed.split(',').map { |ary| [ary[0], ary[2]] }
8
+ ListApi.execute(arys)
9
+ end
10
+ module_function :execute
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Qlang
2
+ module Parser
3
+ module MatrixParser
4
+ include Base
5
+ def execute(lexed)
6
+ rows = lexed.split(';')
7
+ rows.all? { |row| row.count == rows.first.count }
8
+ MatrixApi.execute(rows)
9
+ end
10
+ module_function :execute
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Qlang
2
+ module Parser
3
+ module VectorParser
4
+ include Base
5
+ def execute(lexed)
6
+ VectorApi.execute(lexed.values)
7
+ end
8
+ module_function :execute
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Qlang
2
+ module QOnIrb
3
+ def _(code)
4
+ super(code) unless code.is_a?(String)
5
+ Q.to_ruby.compile()
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Qlang
2
+ VERSION = "0.0.1"
3
+ end
data/q_lang.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'qlang/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'qlang'
8
+ spec.version = Qlang::VERSION
9
+ spec.authors = ['gogotanaka']
10
+ spec.email = ['mail@tanakakazuki.com']
11
+ spec.summary = %q{Enjoy MATH!}
12
+ spec.description = %q{Enjoy MATH!}
13
+ spec.homepage = 'http://q-language.org/'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'Function' do
5
+ it do
6
+ expect(
7
+ Q.compile('f(x, y) = x + y')
8
+ ).to eq(
9
+ "f <- function(x ,y) x + y"
10
+ )
11
+
12
+ expect(
13
+ Q.compile('g(x) = x ** 2')
14
+ ).to eq(
15
+ "g <- function(x) x ** 2"
16
+ )
17
+
18
+ expect(
19
+ Q.compile('g(x) = x ** (2 + 2)')
20
+ ).to eq(
21
+ "g <- function(x) x ** (2 + 2)"
22
+ )
23
+
24
+ expect(
25
+ Q.compile('h(a, b, c) = a ** 2 + b ** 2 + c ** 2')
26
+ ).to eq(
27
+ "h <- function(a ,b ,c) a ** 2 + b ** 2 + c ** 2"
28
+ )
29
+ end
30
+ end
31
+ end
data/spec/list_spec.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'List' do
5
+ it do
6
+ expect(
7
+ Q.compile('{name: "Gogotanaka", age: 21, birth: (1992 8 10) }')
8
+ ).to eq(
9
+ "list(name=\"Gogotanaka\", age=21, birth=c(1992, 8, 10))"
10
+ )
11
+
12
+ expect(
13
+ Q.compile('{key1: 234234, key2: 387342 }')
14
+ ).to eq(
15
+ "list(key1=234234, key2=387342)"
16
+ )
17
+
18
+ expect(
19
+ Q.compile('{key1:234234,key2:387342,key3:38733242}')
20
+ ).to eq(
21
+ "list(key1=234234, key2=387342, key3=38733242)"
22
+ )
23
+
24
+ expect(
25
+ Q.compile('{key1:(1 3 2; 8 2 3),key2:387342,key3:38733242}')
26
+ ).to eq(
27
+ "list(key1=matrix(c(1, 3, 2, 8, 2, 3), 2, 3, byrow = TRUE), key2=387342, key3=38733242)"
28
+ )
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'Matrix' do
5
+ context 'into R' do
6
+ it do
7
+ expect(
8
+ Q.to_r.compile('(1 2 3; 4 5 6)')
9
+ ).to eq(
10
+ "matrix(c(1, 2, 3, 4, 5, 6), 2, 3, byrow = TRUE)"
11
+ )
12
+
13
+ expect(
14
+ Q.to_r.compile('(1 2 3 ; 4 5 6)')
15
+ ).to eq(
16
+ "matrix(c(1, 2, 3, 4, 5, 6), 2, 3, byrow = TRUE)"
17
+ )
18
+
19
+ expect(
20
+ Q.to_r.compile('(1 2 3 ; 4 5 6; 7 8 9)')
21
+ ).to eq(
22
+ "matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 3, 3, byrow = TRUE)"
23
+ )
24
+ expect(
25
+ Q.to_r.compile('(1;2;3)')
26
+ ).to eq(
27
+ "matrix(c(1, 2, 3), 3, 1, byrow = TRUE)"
28
+ )
29
+ end
30
+ end
31
+
32
+ context 'into Ruby' do
33
+ it do
34
+ expect(
35
+ Q.to_ruby.compile('(1 2 3; 4 5 6)')
36
+ ).to eq(
37
+ "Matrix[[1, 2, 3], [4, 5, 6]]"
38
+ )
39
+ expect(
40
+ Q.to_ruby.compile('(1 2 3 ; 4 5 6; 7 8 9)')
41
+ ).to eq(
42
+ "Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
43
+ )
44
+ expect(
45
+ Q.to_ruby.compile('(1;2;3)')
46
+ ).to eq(
47
+ "Matrix[[1], [2], [3]]"
48
+ )
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,17 @@
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
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'qlang'
3
+ require 'pry'
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'Vector' do
5
+ context 'into R' do
6
+ it do
7
+ expect(
8
+ Q.to_r.compile('(1 2 3)')
9
+ ).to eq(
10
+ "c(1, 2, 3)"
11
+ )
12
+
13
+ expect(
14
+ Q.to_r.compile('(1 2 3 4 5 6)')
15
+ ).to eq(
16
+ "c(1, 2, 3, 4, 5, 6)"
17
+ )
18
+
19
+ expect(
20
+ Q.to_r.compile('(1 2 3 4 5 6)')
21
+ ).to eq(
22
+ "c(1, 2, 3, 4, 5, 6)"
23
+ )
24
+ end
25
+ end
26
+ context 'into Ruby' do
27
+ it do
28
+ expect(
29
+ Q.to_ruby.compile('(1 2 3)')
30
+ ).to eq(
31
+ "Vector[1, 2, 3]"
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qlang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - gogotanaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Enjoy MATH!
56
+ email:
57
+ - mail@tanakakazuki.com
58
+ executables:
59
+ - dydx
60
+ - q_lang
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".rubocop.yml"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/dydx
73
+ - bin/q_lang
74
+ - lib/qlang.rb
75
+ - lib/qlang/api.rb
76
+ - lib/qlang/api/func_api.rb
77
+ - lib/qlang/api/list_api.rb
78
+ - lib/qlang/api/matrix_api.rb
79
+ - lib/qlang/api/vector_api.rb
80
+ - lib/qlang/exec.rb
81
+ - lib/qlang/lexer.rb
82
+ - lib/qlang/lexer/base.rb
83
+ - lib/qlang/lexer/cont_lexer.rb
84
+ - lib/qlang/lexer/func_lexer.rb
85
+ - lib/qlang/lexer/wrap_lexer.rb
86
+ - lib/qlang/parser.rb
87
+ - lib/qlang/parser/base.rb
88
+ - lib/qlang/parser/func_parser.rb
89
+ - lib/qlang/parser/list_parser.rb
90
+ - lib/qlang/parser/matrix_parser.rb
91
+ - lib/qlang/parser/vector_parser.rb
92
+ - lib/qlang/q_on_irb.rb
93
+ - lib/qlang/version.rb
94
+ - q_lang.gemspec
95
+ - spec/function_spec.rb
96
+ - spec/list_spec.rb
97
+ - spec/matrix_spec.rb
98
+ - spec/q_lang_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/vector_spec.rb
101
+ homepage: http://q-language.org/
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Enjoy MATH!
125
+ test_files:
126
+ - spec/function_spec.rb
127
+ - spec/list_spec.rb
128
+ - spec/matrix_spec.rb
129
+ - spec/q_lang_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/vector_spec.rb