qlang 0.0.141421 → 0.0.1414213
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/qlang/lexer/wrap_lexer.rb +1 -0
- data/lib/qlang/parser.rb +10 -0
- data/lib/qlang/parser/formula_parser.rb +36 -0
- data/lib/qlang/parser/func_parser.rb +1 -34
- data/lib/qlang/parser/integral_parser.rb +1 -1
- data/lib/qlang/version.rb +1 -1
- data/spec/iq_spec.rb +17 -14
- data/spec/lexer/regular_expressions_spec.rb +18 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 087827187ce756b6d28e8305ce2512ddfbc78797
|
4
|
+
data.tar.gz: 37c2844006f797e71138379e69a6001f0d5b64ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f655efba3cd208fab88a65eb55fec2177e172655c5b22c6fd2f9b941604f9a190fdcec7747a9ceb8da37cf4b8cbdfd97bcb6c01c4564807de8db1cedf21911d
|
7
|
+
data.tar.gz: 407d8151bd533ecdc10a704db91f32e8f740ef122e09ab0b157ec0e27af73a9c91a506a7e7b7c459434c760d9e5dcd26871416641ffe6d4f74d6b658862d8ee2
|
data/lib/qlang/parser.rb
CHANGED
@@ -9,6 +9,7 @@ require 'qlang/parser/vector_parser'
|
|
9
9
|
require 'qlang/parser/list_parser'
|
10
10
|
require 'qlang/parser/func_parser'
|
11
11
|
require 'qlang/parser/integral_parser'
|
12
|
+
require 'qlang/parser/formula_parser'
|
12
13
|
|
13
14
|
module Qlang
|
14
15
|
module Parser
|
@@ -65,6 +66,15 @@ module Qlang
|
|
65
66
|
cont_token_with_num = $&
|
66
67
|
cont = lexed.get_value(cont_token_with_num)
|
67
68
|
lexed.squash_with_prn(cont_token_with_num, cont)
|
69
|
+
|
70
|
+
when /:DIFF\d/
|
71
|
+
cont_token_with_num = $&
|
72
|
+
cont = lexed.get_value(cont_token_with_num)
|
73
|
+
cont =~ /(d\/d[a-zA-Z]) (.*)/
|
74
|
+
cont = "#{$1}(#{FormulaParser.execute($2)})"
|
75
|
+
# FIX: Refactor
|
76
|
+
#cont.gsub!(/(d\/d[a-zA-Z]) (.*)/, "\1(\2)")
|
77
|
+
lexed.squash_with_prn(cont_token_with_num, cont)
|
68
78
|
when /:CONT\d/
|
69
79
|
lexed.ch_token($&, :R)
|
70
80
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Qlang
|
2
|
+
module Parser
|
3
|
+
# FIX:
|
4
|
+
module FormulaParser
|
5
|
+
def execute(lexed)
|
6
|
+
ss = StringScanner.new(lexed)
|
7
|
+
result = ''
|
8
|
+
until ss.eos?
|
9
|
+
{ EXP: /\^/, BFUNC: /sin|cos|tan|log/, MUL: /(pi|[1-9a-z]){2,}/, SNGL: /(pi|[1-9a-z])/, OTHER: /([^\^1-9a-z]|^pi)+/ }.each do |token , rgx|
|
10
|
+
if ss.scan(rgx)
|
11
|
+
item = case token
|
12
|
+
when :EXP
|
13
|
+
$type == :Ruby ? '**' : '^'
|
14
|
+
when :MUL
|
15
|
+
sss = StringScanner.new(ss[0])
|
16
|
+
ary = []
|
17
|
+
until sss.eos?
|
18
|
+
[/pi/, /[1-9a-z]/].each do |rgx2|
|
19
|
+
ary << sss[0] if sss.scan(rgx2)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
ary.join(' * ')
|
23
|
+
else
|
24
|
+
ss[0]
|
25
|
+
end
|
26
|
+
result += item
|
27
|
+
break
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
result
|
32
|
+
end
|
33
|
+
module_function :execute
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -8,42 +8,9 @@ module Qlang
|
|
8
8
|
func_name = fdef_ary.shift
|
9
9
|
args = fdef_ary.join.rms!('(', ')', ',', ' ').split('')
|
10
10
|
|
11
|
-
FuncApi.execute(func_name, args,
|
11
|
+
FuncApi.execute(func_name, args, FormulaParser.execute(lexed[-1][:FOML]))
|
12
12
|
end
|
13
13
|
module_function :execute
|
14
|
-
|
15
|
-
# FIX:
|
16
|
-
module FomlParser
|
17
|
-
def execute(lexed)
|
18
|
-
ss = StringScanner.new(lexed)
|
19
|
-
result = ''
|
20
|
-
until ss.eos?
|
21
|
-
{ EXP: /\^/, BFUNC: /sin|cos|tan|log/, MUL: /(pi|[1-9a-z]){2,}/, SNGL: /(pi|[1-9a-z])/, OTHER: /([^\^1-9a-z]|^pi)+/ }.each do |token , rgx|
|
22
|
-
if ss.scan(rgx)
|
23
|
-
item = case token
|
24
|
-
when :EXP
|
25
|
-
$type == :Ruby ? '**' : '^'
|
26
|
-
when :MUL
|
27
|
-
sss = StringScanner.new(ss[0])
|
28
|
-
ary = []
|
29
|
-
until sss.eos?
|
30
|
-
[/pi/, /[1-9a-z]/].each do |rgx2|
|
31
|
-
ary << sss[0] if sss.scan(rgx2)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
ary.join(' * ')
|
35
|
-
else
|
36
|
-
ss[0]
|
37
|
-
end
|
38
|
-
result += item
|
39
|
-
break
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
result
|
44
|
-
end
|
45
|
-
module_function :execute
|
46
|
-
end
|
47
14
|
end
|
48
15
|
end
|
49
16
|
end
|
data/lib/qlang/version.rb
CHANGED
data/spec/iq_spec.rb
CHANGED
@@ -2,6 +2,19 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Qlang do
|
4
4
|
describe Iq do
|
5
|
+
def self.def_test(name, input, output)
|
6
|
+
it name + '_def' do
|
7
|
+
expect(Iq.execute(input)).to eq(output)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.cal_test(name, input, output)
|
12
|
+
it name + '_cal' do
|
13
|
+
expect(Iq.execute(input)).to eq(output)
|
14
|
+
reset
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
5
18
|
describe 'Matrix' do
|
6
19
|
it do
|
7
20
|
expect(Iq.execute('(1 2 3; 4 5 6)')).to eq('(1 2 3; 4 5 6)')
|
@@ -35,6 +48,8 @@ describe Qlang do
|
|
35
48
|
expect(Iq.execute('d/dx( sin(x) )')).to eq('cos( x )')
|
36
49
|
expect(Iq.execute('d/dx(log( x ))')).to eq('1 / x')
|
37
50
|
end
|
51
|
+
cal_test('ex1', 'd/dx cos(x)', '- sin( x )')
|
52
|
+
cal_test('ex2', 'd/dx xx', '2x')
|
38
53
|
end
|
39
54
|
|
40
55
|
describe 'Integral' do
|
@@ -44,22 +59,10 @@ describe Qlang do
|
|
44
59
|
expect(Iq.execute('S( cos(x)dx )[0..pi]')).to eq('0.0')
|
45
60
|
expect(Iq.execute('S( cos(x)dx )[0..pi]')).to eq('0.0')
|
46
61
|
end
|
62
|
+
cal_test('ex1', 'S(xx dx)[0..1]', '0.33333333')
|
63
|
+
cal_test('ex2', 'S(2pi dx)[0..1]', '6.28318531')
|
47
64
|
end
|
48
65
|
|
49
|
-
def self.def_test(name, input, output)
|
50
|
-
it name + '_def' do
|
51
|
-
expect(Iq.execute(input)).to eq(output)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.cal_test(name, input, output)
|
56
|
-
it name + '_cal' do
|
57
|
-
expect(Iq.execute(input)).to eq(output)
|
58
|
-
reset
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
|
63
66
|
describe 'Function' do
|
64
67
|
def_test('ex1', 'f(x, y) = x + y', 'x + y')
|
65
68
|
cal_test('ex1', 'f( 4, 5 )', '9.0')
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Regular expressions' do
|
4
|
+
def self.should_match(num, rgx, str)
|
5
|
+
it 'ex' + num.to_s do
|
6
|
+
expect(rgx =~ str).to eq(0)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
describe 'function' do
|
10
|
+
should_match(1, /[fgh]\(\w( ?, ?\w)*\) ?= ?[^\r\n]+/, 'f(x) = xy')
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'differentiate' do
|
14
|
+
rgx = /d\/d[a-zA-Z] .*/
|
15
|
+
should_match(1, rgx, 'd/dx sin(x)')
|
16
|
+
should_match(2, rgx, 'd/dz z^2')
|
17
|
+
end
|
18
|
+
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.
|
4
|
+
version: 0.0.1414213
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gogotanaka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dydx
|
@@ -100,6 +100,7 @@ files:
|
|
100
100
|
- lib/qlang/lexer/wrap_lexer.rb
|
101
101
|
- lib/qlang/parser.rb
|
102
102
|
- lib/qlang/parser/base.rb
|
103
|
+
- lib/qlang/parser/formula_parser.rb
|
103
104
|
- lib/qlang/parser/func_parser.rb
|
104
105
|
- lib/qlang/parser/integral_parser.rb
|
105
106
|
- lib/qlang/parser/list_parser.rb
|
@@ -108,6 +109,7 @@ files:
|
|
108
109
|
- lib/qlang/version.rb
|
109
110
|
- q_lang.gemspec
|
110
111
|
- spec/iq_spec.rb
|
112
|
+
- spec/lexer/regular_expressions_spec.rb
|
111
113
|
- spec/objects/function_spec.rb
|
112
114
|
- spec/objects/integral_spec.rb
|
113
115
|
- spec/objects/list_spec.rb
|
@@ -141,6 +143,7 @@ specification_version: 4
|
|
141
143
|
summary: Enjoy MATH!
|
142
144
|
test_files:
|
143
145
|
- spec/iq_spec.rb
|
146
|
+
- spec/lexer/regular_expressions_spec.rb
|
144
147
|
- spec/objects/function_spec.rb
|
145
148
|
- spec/objects/integral_spec.rb
|
146
149
|
- spec/objects/list_spec.rb
|