qlang 0.0.27182110 → 0.0.27182120

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: a48ba3d203a109ed171dbbd38e70c404c24b8018
4
- data.tar.gz: f461876d32692c407a4333b31909adab2e12917c
3
+ metadata.gz: 82f393f67556cb1f5e6e37eba4a70f75b5e3357d
4
+ data.tar.gz: 660f56cb8a0fbae5d321ad3e2b9ca888719fa4c1
5
5
  SHA512:
6
- metadata.gz: 892ff464ca7f8614b6dbff92114998edb7c355016cfc6c9ba653a27024174fb301304d16cc9f339cc98e596d575af66890d7fa2c06df29fed03f267580d27f8d
7
- data.tar.gz: 973791d37093853d5ed9ac0c2d19d171d5efdd2a4d46874093cad82bb1a93bce7eb047af4b94eb0ab406ca6a39bf4dfa67e018b7deb981ed9fd4242933d8a52a
6
+ metadata.gz: 2e40198040a6e4046a5d8fa0e9dd04fb3c8ee5e2ea7fbac5ad47cc6cd22c3c0fa07a69960dc938ce695aaf22fa93f0260037b539ae5bb9d2703f4e92460cf1da
7
+ data.tar.gz: 111757064ce64d1048684a1ea79a1cbdaa4c10b8bb81a20c113dbbee5612891c3a51f5e9ec0671c537a3e7c626b371d9b940ac346ecae7fe757b2bbec869937b
data/README.md CHANGED
@@ -10,6 +10,7 @@
10
10
 
11
11
  #### Q-language can be.
12
12
 
13
+ Q let you know the sense of mathematics with Keyboard same as with a pen.
13
14
 
14
15
  ```
15
16
  +---Discret world---+ +------mathematics-----+
@@ -19,6 +20,11 @@
19
20
  +-------------------+ +----------------------+
20
21
  ```
21
22
 
23
+ ## Demo
24
+
25
+ Below code is input and output for interpreter of q-lang
26
+
27
+ (you can try it by `qlang -i`)
22
28
 
23
29
  ### Differentiate
24
30
 
@@ -85,15 +91,15 @@ Install qlang gme.
85
91
 
86
92
  ### Compile into R
87
93
 
88
- $ qlang -R foo.q
94
+ $ qlang -r foo.q
89
95
 
90
96
  ### Compile into Ruby
91
97
 
92
- $ qlang -Ruby foo.q
98
+ $ qlang -rb foo.q
93
99
 
94
100
  ### Compile into Python
95
101
 
96
- $ qlang -Python foo.q
102
+ $ qlang -py foo.q
97
103
 
98
104
 
99
105
  ## Use as math template within other langs
@@ -114,7 +120,7 @@ Q.E.D
114
120
  end
115
121
  ```
116
122
 
117
- $ qlang -Ruby example.rb
123
+ $ qlang -rb example.rb
118
124
 
119
125
 
120
126
  ```rb
@@ -129,6 +135,11 @@ class ExampleClass
129
135
  end
130
136
  ```
131
137
 
138
+ ## Interpreter
139
+
140
+ $ qlang -i
141
+ Q:->
142
+
132
143
  ## Contributing
133
144
 
134
145
  Welcome any your PR or issue.
data/bin/qlang CHANGED
@@ -15,7 +15,7 @@ when '-i'
15
15
  print 'Q:-> '
16
16
  begin
17
17
  input = $stdin.gets
18
- output = Iq.execute(input)
18
+ output = Qlang::Iq.execute(input)
19
19
  $stdout.puts output
20
20
  rescue => e
21
21
  puts e
data/lib/qlang.rb CHANGED
@@ -4,11 +4,11 @@ require 'matrix'
4
4
  require 'singleton'
5
5
  require 'yaml'
6
6
 
7
+ $:.unshift(File.dirname(__FILE__))
7
8
  # Q core
9
+ require 'qlang/utils/ruby_ext'
8
10
  require 'qlang/lexer'
9
11
  require 'qlang/parser'
10
- require 'qlang/utils/ruby_ext'
11
- require "qlang/version"
12
12
 
13
13
  module Qlang
14
14
  # $meta_info indicate what and how to do.
data/lib/qlang/api.rb CHANGED
@@ -3,6 +3,8 @@ require 'qlang/api/vector_api'
3
3
  require 'qlang/api/list_api'
4
4
  require 'qlang/api/func_api'
5
5
  require 'qlang/api/integral_api'
6
+ require 'qlang/api/limit_api'
7
+ require 'qlang/api/sigma_api'
6
8
 
7
9
  module Qlang
8
10
  module Api
@@ -0,0 +1,22 @@
1
+ module Qlang
2
+ module Api
3
+ module LimitApi
4
+ def self.execute(formula, var, close_to)
5
+ case $meta_info.lang
6
+ # TODO: I know what you want to say.......!
7
+ when :ruby
8
+ case close_to
9
+ when 'oo'
10
+ "temp_cal_f(#{var}) <= #{formula};
11
+ temp_cal_f(100000)"
12
+ else
13
+ "temp_cal_f(#{var}) <= #{formula};
14
+ temp_cal_f(#{close_to} + Float::EPSILON ** 20)"
15
+ end
16
+ else
17
+ fail "List is not implemented for #{$meta_info.lang_str}"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module Qlang
2
+ module Api
3
+ module SigmaApi
4
+ def self.execute(formula, var, from, to)
5
+ case $meta_info.lang
6
+ # TODO: I know what you want to say.
7
+ when :ruby
8
+ "temp_cal_f(#{var}) <= #{formula};
9
+ (#{from}..#{to}).inject(0) {|sum, i| sum+=temp_cal_f(i) }"
10
+ else
11
+ fail "List is not implemented for #{$meta_info.lang_str}"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -12,7 +12,7 @@ module Qlang
12
12
  def rule(pattern, &token)
13
13
  token ||= proc { :NULL }
14
14
  @token_rule_hash ||= {}
15
- @token_rule_hash[token.call] = pattern
15
+ @token_rule_hash[pattern] = token.call
16
16
  end
17
17
  end
18
18
 
@@ -22,7 +22,7 @@ module Qlang
22
22
  until ss.eos?
23
23
  scan_rslt, ss = scan(ss)
24
24
  if scan_rslt
25
- @lexeds << scan_rslt unless scan_rslt == :NULL
25
+ @lexeds << scan_rslt unless scan_rslt[:token] == :NULL
26
26
  else
27
27
  fail "I'm so sorry, something wrong. Please feel free to report this."
28
28
  end
@@ -31,9 +31,13 @@ module Qlang
31
31
 
32
32
  def scan(ss)
33
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 }
34
+ token_rule_hash.each do |pattern, token|
35
+ if ss.scan(pattern)
36
+ scan_rslt = {
37
+ token: token,
38
+ value: ss[0],
39
+ els: 4.times.inject([]) { |s,i|s << ss[i+1] }.compact
40
+ }
37
41
  break
38
42
  end
39
43
  end
@@ -42,17 +46,18 @@ module Qlang
42
46
 
43
47
  # Accessor
44
48
  ## GET(without side effect)
45
- def [](index)
46
- @lexeds[index.to_i]
49
+ def get_value(num)
50
+ num = num.to_i
51
+ @lexeds[num][:value]
47
52
  end
48
53
 
49
- def get_value(num)
54
+ def get_els(num)
50
55
  num = num.to_i
51
- @lexeds[num].values.first
56
+ @lexeds[num][:els]
52
57
  end
53
58
 
54
59
  def token_str
55
- @lexeds.map.with_index { |lexed, i| ":#{lexed.keys.first}#{i}" }.join
60
+ @lexeds.map.with_index { |lexed, i| ":#{lexed[:token]}#{i}" }.join
56
61
  end
57
62
 
58
63
  def token_rule_hash
@@ -75,18 +80,18 @@ module Qlang
75
80
  range = (range.first.to_i)..(range.last.to_i)
76
81
  value = values[range].join
77
82
  range.count.times { @lexeds.delete_at(range.first) }
78
- @lexeds.insert(range.first, { token => value })
83
+ @lexeds.insert(range.first, { token: token, value: value })
79
84
  end
80
85
 
81
86
  # Legacy Accessor
82
87
  def values
83
- @lexeds.map { |lexed| lexed.values.first }
88
+ @lexeds.map { |lexed| lexed[:value] }
84
89
  end
85
90
 
86
91
  private
87
92
  def parsed_at!(token_position, parsed)
88
93
  @lexeds.delete_at(token_position)
89
- @lexeds.insert(token_position, { R: parsed })
94
+ @lexeds.insert(token_position, { token: :R, value: parsed })
90
95
  end
91
96
 
92
97
  def parsed_between!(token_range, parsed)
@@ -94,7 +99,7 @@ module Qlang
94
99
  token_range.count.times do
95
100
  @lexeds.delete_at(start_pos)
96
101
  end
97
- @lexeds.insert(start_pos, { R: parsed })
102
+ @lexeds.insert(start_pos, { token: :R, value: parsed })
98
103
  end
99
104
 
100
105
  end
@@ -4,10 +4,15 @@ module Qlang
4
4
  rule(/(#{FUNCCV})#{ANYSP}#{EQL}#{ANYSP}(#{FORMULA})/) { :def_func }
5
5
  rule(/#{ITGRLSYM}#{ANYSP}#{LPRN}(#{ANYSTR})#{RPRN}#{LBRCT}(#{ANYSTR})#{RBRCT}/) { :integral }
6
6
  rule(/d\/d(#{VAR}) (#{FORMULA})/) { :differential }
7
+
8
+ rule(/lim#{LBRCT}(#{VAR})#{RSARW}(#{VARNUM})#{RBRCT} (#{FORMULA})/) { :limit }
9
+
7
10
  rule(/#{LPRN}(#{NUMS_BY_SP})#{RPRN}/) { :vector }
8
11
  rule(/#{LPRN}(#{NUMS_BY_SP_BY_SCLN_OR_NELN})#{RPRN}t/m) { :tmatrix }
9
12
  rule(/#{LPRN}(#{NUMS_BY_SP_BY_SCLN_OR_NELN})#{RPRN}/m) { :matrix }
10
13
 
14
+ rule(/∑#{LBRCT}(#{VAR})=(#{INT}),#{ANYSP}(#{INT})#{RBRCT} (#{FORMULA})/) { :sigma }
15
+
11
16
  rule(/#{FUNCCN}/) { :FUNCCN }
12
17
 
13
18
  rule(/#{LPRN}/) { :LPRN }
@@ -6,7 +6,8 @@ module Qlang
6
6
  FLO = /[0-9]+\.[0-9]+/
7
7
  E = /e/
8
8
  PI = /pi/
9
- NUM = /(?:#{FLO}|#{INT}|#{E}|#{PI})/
9
+ INF = /oo/
10
+ NUM = /(?:#{FLO}|#{INT}|#{E}|#{PI}|#{INF})/
10
11
 
11
12
  # FUNCTION
12
13
  LPRN = /\(/
@@ -55,6 +56,11 @@ module Qlang
55
56
  SCLN = /;/
56
57
  CMA = /\,/
57
58
  EQL = /\=/
59
+
60
+ RSARW = '->'
61
+ LSARW = '<-'
62
+ RDARW = '=>'
63
+ LDARW = '<='
58
64
  SP = / /
59
65
  NLIN = /(\r|\n)/
60
66
 
data/lib/qlang/parser.rb CHANGED
@@ -6,6 +6,9 @@ require 'qlang/parser/vector_parser'
6
6
  require 'qlang/parser/list_parser'
7
7
  require 'qlang/parser/func_parser'
8
8
  require 'qlang/parser/integral_parser'
9
+ require 'qlang/parser/limit_parser'
10
+ require 'qlang/parser/sigma_parser'
11
+
9
12
  require 'qlang/parser/formula_parser'
10
13
 
11
14
  module Qlang
@@ -19,8 +22,8 @@ module Qlang
19
22
  fail "I'm so sorry, something wrong. Please feel free to report this." if Time.now > time + 10
20
23
 
21
24
  case lexed.token_str
22
- when /:(vector)(\d+)/, /:(matrix)(\d+)/, /:(tmatrix)(\d+)/, /:(integral)(\d+)/, /:(def_func)(\d+)/, /:(differential)(\d+)/
23
- token_els = lexed[$2][:els]
25
+ when /:(vector)(\d+)/, /:(matrix)(\d+)/, /:(tmatrix)(\d+)/, /:(integral)(\d+)/, /:(def_func)(\d+)/, /:(differential)(\d+)/, /:(limit)(\d+)/, /:(sigma)(\d+)/
26
+ token_els = lexed.get_els($2)
24
27
 
25
28
  parsed = case $1
26
29
  when 'vector'
@@ -29,10 +32,14 @@ module Qlang
29
32
  MatrixParser.execute(token_els)
30
33
  when 'tmatrix'
31
34
  MatrixParser.execute(token_els, trans: true)
35
+ when 'limit'
36
+ LimitParser.execute(token_els)
32
37
  when 'integral'
33
38
  IntegralParser.execute(token_els)
34
39
  when 'def_func'
35
40
  FuncParser.execute(token_els)
41
+ when 'sigma'
42
+ SigmaParser.execute(token_els)
36
43
  when 'differential'
37
44
  del_var, formula = token_els
38
45
  "d/d#{del_var}(#{FormulaParser.execute(formula)})"
@@ -41,13 +48,16 @@ module Qlang
41
48
 
42
49
  when /:LPRN(\d+):CONT(\d+):RPRN(\d+)/
43
50
  tokens_range = $1.to_i..$3.to_i
44
- token_val = lexed[$2][:CONT]
51
+ token_val =
45
52
 
46
- lexed.parsed!(token_val.parentheses, tokens_range)
53
+ lexed.parsed!(
54
+ lexed.get_value($2).parentheses,
55
+ tokens_range
56
+ )
47
57
 
48
58
  when /:LBRCS(\d+):CONT(\d+):RBRCS(\d+)/
49
59
  tokens_range = $1.to_i..$3.to_i
50
- token_val = lexed[$2][:CONT]
60
+ token_val = lexed.get_value($2)
51
61
 
52
62
  cont = case token_val
53
63
  when /#{ONEHASH}(#{CMA}#{ONEHASH})*/
@@ -0,0 +1,16 @@
1
+ module Qlang
2
+ module Parser
3
+ module LimitParser
4
+ include Base
5
+ def self.execute(els)
6
+ var, close_to, formula = els
7
+
8
+ LimitApi.execute(
9
+ FormulaParser.execute(formula),
10
+ var,
11
+ close_to
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Qlang
2
+ module Parser
3
+ module SigmaParser
4
+ include Base
5
+ def self.execute(els)
6
+ var, from, to, formula = els
7
+
8
+ SigmaApi.execute(
9
+ FormulaParser.execute(formula),
10
+ var,
11
+ from,
12
+ to
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,11 @@
1
+ # module ::Kernel
2
+ # def recursive_require(str)
3
+ # Dir["#{$:.first}/#{str}"].each do |file|
4
+ # require file
5
+ # end
6
+ # end
7
+ # end
8
+
1
9
  class ::String
2
10
  def parentheses
3
11
  "(#{self})"
data/lib/qlang/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Qlang
2
- VERSION = "0.0.27182110"
2
+ VERSION = "0.0.27182120"
3
3
  end
@@ -0,0 +1,28 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestLimit < TestInterpreterBase
4
+ def setup
5
+
6
+ end
7
+
8
+ def assert_iq_equal(output, input)
9
+ assert_equal(Qlang::Iq.execute(input), output)
10
+ end
11
+
12
+ def test_general
13
+ assert_iq_equal(
14
+ 'oo',
15
+ 'lim[x->0] 1/x'
16
+ )
17
+
18
+ assert_iq_equal(
19
+ '10.0',
20
+ 'lim[x->10] x'
21
+ )
22
+
23
+ assert_iq_equal(
24
+ '2.7182682371744895',
25
+ 'lim[x->oo] (1 + 1/x)^x'
26
+ )
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestSigma < TestInterpreterBase
4
+ def setup
5
+
6
+ end
7
+
8
+ def test_general
9
+ assert_iq_equal(
10
+ '∑[x=0,10] x',
11
+ '55.0'
12
+ )
13
+
14
+ assert_iq_equal(
15
+ '∑[x=0, 10] x^2',
16
+ '385.0'
17
+ )
18
+
19
+ assert_iq_equal(
20
+ '∑[x=0, 10] x^3',
21
+ '3025.0'
22
+ )
23
+ end
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qlang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.27182110
4
+ version: 0.0.27182120
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-27 00:00:00.000000000 Z
11
+ date: 2014-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dydx
@@ -118,8 +118,10 @@ files:
118
118
  - lib/qlang/api.rb
119
119
  - lib/qlang/api/func_api.rb
120
120
  - lib/qlang/api/integral_api.rb
121
+ - lib/qlang/api/limit_api.rb
121
122
  - lib/qlang/api/list_api.rb
122
123
  - lib/qlang/api/matrix_api.rb
124
+ - lib/qlang/api/sigma_api.rb
123
125
  - lib/qlang/api/vector_api.rb
124
126
  - lib/qlang/exec.rb
125
127
  - lib/qlang/iq.rb
@@ -133,8 +135,10 @@ files:
133
135
  - lib/qlang/parser/formula_parser.rb
134
136
  - lib/qlang/parser/func_parser.rb
135
137
  - lib/qlang/parser/integral_parser.rb
138
+ - lib/qlang/parser/limit_parser.rb
136
139
  - lib/qlang/parser/list_parser.rb
137
140
  - lib/qlang/parser/matrix_parser.rb
141
+ - lib/qlang/parser/sigma_parser.rb
138
142
  - lib/qlang/parser/vector_parser.rb
139
143
  - lib/qlang/utils/langs.yml
140
144
  - lib/qlang/utils/ruby_ext.rb
@@ -146,7 +150,9 @@ files:
146
150
  - test/interpreter/test_function.rb
147
151
  - test/interpreter/test_general.rb
148
152
  - test/interpreter/test_integral.rb
153
+ - test/interpreter/test_limit.rb
149
154
  - test/interpreter/test_matrix.rb
155
+ - test/interpreter/test_sigma.rb
150
156
  - test/interpreter/test_vector.rb
151
157
  - test/langs/test_r.rb
152
158
  - test/langs/test_ruby.rb
@@ -184,7 +190,9 @@ test_files:
184
190
  - test/interpreter/test_function.rb
185
191
  - test/interpreter/test_general.rb
186
192
  - test/interpreter/test_integral.rb
193
+ - test/interpreter/test_limit.rb
187
194
  - test/interpreter/test_matrix.rb
195
+ - test/interpreter/test_sigma.rb
188
196
  - test/interpreter/test_vector.rb
189
197
  - test/langs/test_r.rb
190
198
  - test/langs/test_ruby.rb