tcl-ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.rubocop.yml +3 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +44 -0
- data/LICENSE +21 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/lib/tcl/ruby/command.rb +68 -0
- data/lib/tcl/ruby/commands/array.rb +56 -0
- data/lib/tcl/ruby/commands/basic.rb +115 -0
- data/lib/tcl/ruby/commands/list.rb +72 -0
- data/lib/tcl/ruby/error.rb +17 -0
- data/lib/tcl/ruby/list_array.rb +75 -0
- data/lib/tcl/ruby/parser.rb +72 -0
- data/lib/tcl/ruby/util.rb +27 -0
- data/lib/tcl/ruby/version.rb +5 -0
- data/lib/tcl/ruby.rb +8 -0
- data/tcl-ruby.gemspec +24 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 05002f0d80add9496fd333278753cb393c1cb467
|
4
|
+
data.tar.gz: cc478beb9d6bb5c8562d089931570efe36fb4c70
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec9711ba1d51886f8ae015af255b5f40bb792278d23f8c1d8714b8ed6cc5ce6a90e9632f40390777e3c3c62e346ea33eb59d3bc68e51d0ccb088ef53f0f0be30
|
7
|
+
data.tar.gz: a932d6e3cbceba76a77fc4250130ab62f6d834a7167bdf63e6981e545d703488a92c0d9e6462c2d43b8952de780cfcbea17af56500e2789648e1b2fedcc81be8
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
tcl-ruby (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.1)
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
method_source (0.8.2)
|
12
|
+
pry (0.10.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.8.1)
|
15
|
+
slop (~> 3.4)
|
16
|
+
rake (10.5.0)
|
17
|
+
rspec (3.4.0)
|
18
|
+
rspec-core (~> 3.4.0)
|
19
|
+
rspec-expectations (~> 3.4.0)
|
20
|
+
rspec-mocks (~> 3.4.0)
|
21
|
+
rspec-core (3.4.4)
|
22
|
+
rspec-support (~> 3.4.0)
|
23
|
+
rspec-expectations (3.4.0)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.4.0)
|
26
|
+
rspec-mocks (3.4.1)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.4.0)
|
29
|
+
rspec-support (3.4.1)
|
30
|
+
slop (3.6.0)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
x86-mingw32
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
bundler (~> 1.11)
|
38
|
+
pry
|
39
|
+
rake (~> 10.0)
|
40
|
+
rspec (~> 3.0)
|
41
|
+
tcl-ruby!
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
1.11.2
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 kiyonori-matsumoto
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
[](https://codeclimate.com/github/kiyonori-matsumoto/tcl-ruby) [](https://travis-ci.org/kiyonori-matsumoto/tcl-ruby)
|
2
|
+
|
3
|
+
# Tcl::Ruby
|
4
|
+
|
5
|
+
Tcl interpreter for ruby
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'tcl-ruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install tcl-ruby
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Instanciate Interpreter
|
26
|
+
```ruby
|
27
|
+
f = Interpreter.new
|
28
|
+
```
|
29
|
+
Parse commands you want to
|
30
|
+
```ruby
|
31
|
+
f.parse('llength {A B C D}')
|
32
|
+
=> 4
|
33
|
+
```
|
34
|
+
You can get variables with Interpreter#variables
|
35
|
+
```ruby
|
36
|
+
f.parse('set A 123')
|
37
|
+
f.variables('A')
|
38
|
+
=> '123'
|
39
|
+
```
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
44
|
+
|
45
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kiyonori-matsumoto/tcl-ruby.
|
50
|
+
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'tcl/ruby'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require 'pry'
|
11
|
+
f = Tcl::Ruby::Interpreter.new
|
12
|
+
# Pry.start
|
13
|
+
binding.pry
|
14
|
+
|
15
|
+
# require "irb"
|
16
|
+
# IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module Tcl
|
2
|
+
module Ruby
|
3
|
+
class Interpreter
|
4
|
+
def command(arg)
|
5
|
+
return @prev if arg[0][0] == '#'
|
6
|
+
# return previous command result when comment statement executed
|
7
|
+
arg.map! { |e| replace(e) }
|
8
|
+
arg.to_string
|
9
|
+
name = arg[0]
|
10
|
+
if @proc.key?(name)
|
11
|
+
exec_proc(arg[1..-1], @proc[name])
|
12
|
+
elsif @hooks.key?(name)
|
13
|
+
@hooks[name].call(arg[1..-1])
|
14
|
+
elsif respond_to?("___#{name}", true)
|
15
|
+
@prev = send("___#{name}", arg[1..-1])
|
16
|
+
else
|
17
|
+
raise(CommandError, "command not found, #{name}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def replace(list)
|
22
|
+
return list if list[0] == '{'
|
23
|
+
# replace variable
|
24
|
+
l = list.gsub(/\$\{(.+?)\}|\$([\w()]+)/) do
|
25
|
+
v = Regexp.last_match(Regexp.last_match(1) ? 1 : 2)
|
26
|
+
h = vv = nil
|
27
|
+
if (m = v.match(/\((\w+)\)\z/))
|
28
|
+
h = m[1]
|
29
|
+
vv = v
|
30
|
+
v = vv.sub(/\((\w+)\)\z/, '')
|
31
|
+
end
|
32
|
+
raise(TclVariableNotFoundError, v.to_s, 'no such variable') unless
|
33
|
+
@variables.key?(v)
|
34
|
+
if h
|
35
|
+
raise(TclVariableNotFoundError, vv.to_s, "variable isn't array") unless @variables[v].is_a?(Hash)
|
36
|
+
@variables[v][h].to_s
|
37
|
+
else
|
38
|
+
raise(TclVariableNotFoundError, v.to_s, 'variable is array') if @variables[v].is_a?(Hash)
|
39
|
+
@variables[v]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
# replace commands
|
43
|
+
l = l.gsub(/\[(.+)\]/) { parse(Regexp.last_match(1)) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def exec_proc(arg, proc_info)
|
47
|
+
proc_arg = parse(proc_info[0], true)
|
48
|
+
raise(TclArgumentError, proc_arg.to_s) if proc_arg.size != arg.size
|
49
|
+
@variables[:___global].each do |v| # FIXME: Buggy
|
50
|
+
@global[v] = @variables[v]
|
51
|
+
end if @variables.key?(:___global)
|
52
|
+
@v_stack.push(@variables)
|
53
|
+
@variables = {}
|
54
|
+
arg.zip(proc_arg).each do |v|
|
55
|
+
@variables[v[1]] = v[0]
|
56
|
+
end
|
57
|
+
ret = catch(:return) do
|
58
|
+
parse(proc_info[1])
|
59
|
+
end
|
60
|
+
@variables[:___global].each do |v| # FIXME: Buggy
|
61
|
+
@global[v] = @variables[v]
|
62
|
+
end if @variables.key?(:___global)
|
63
|
+
@variables = @v_stack.pop
|
64
|
+
ret
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Tcl
|
2
|
+
module Ruby
|
3
|
+
class Interpreter
|
4
|
+
private
|
5
|
+
|
6
|
+
def ___array(arg)
|
7
|
+
send("___array_#{arg[0]}", arg[1..-1])
|
8
|
+
end
|
9
|
+
|
10
|
+
def ___array_set(arg)
|
11
|
+
name = arg[0]
|
12
|
+
raise(CommandError, "#{name} is not array") unless
|
13
|
+
@variables[name].is_a?(Hash) || !@variables.key?(name)
|
14
|
+
l = parse(arg[1], true)
|
15
|
+
raise(TclArgumentError, 'list must have an even number of elements') unless
|
16
|
+
l.size.even?
|
17
|
+
@variables[name] ||= {}
|
18
|
+
@variables[name].merge!(Hash[*l])
|
19
|
+
end
|
20
|
+
|
21
|
+
def ___array_unset(arg)
|
22
|
+
raise(TclArgumentError, 'array unset arrayName ?pattern?') unless
|
23
|
+
(1..2).cover?(arg.size)
|
24
|
+
name = arg[0]
|
25
|
+
return '' unless @variables[name].is_a?(Hash)
|
26
|
+
if arg.size == 2
|
27
|
+
pattern = arg[1]
|
28
|
+
@variables[name].delete(pattern)
|
29
|
+
else
|
30
|
+
@variables.delete(name)
|
31
|
+
end
|
32
|
+
''
|
33
|
+
end
|
34
|
+
|
35
|
+
def ___array_get(arg)
|
36
|
+
raise(TclArgumentError, 'array get arrayName ?pattern?') unless
|
37
|
+
(1..2).cover?(arg.size)
|
38
|
+
name = arg[0]
|
39
|
+
return '' unless @variables[name].is_a?(Hash)
|
40
|
+
if arg.size == 2
|
41
|
+
pattern = arg[1]
|
42
|
+
return '' unless @variables[name].key?(pattern)
|
43
|
+
"#{pattern} #{@variables[name][pattern]}"
|
44
|
+
else
|
45
|
+
@variables[name].flatten.join(' ')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def ___array_exists(arg)
|
50
|
+
raise(TclArgumentError, 'array exists arrayName') unless arg.size == 1
|
51
|
+
name = arg[0]
|
52
|
+
@variables[name].is_a?(Hash) ? '1' : '0'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module Tcl
|
2
|
+
module Ruby
|
3
|
+
class Interpreter
|
4
|
+
private
|
5
|
+
|
6
|
+
def ___set(arg)
|
7
|
+
raise(TclArgumentError, 'set variable ?newValue?') unless
|
8
|
+
(1..2).cover? arg.size
|
9
|
+
@variables[arg[0]] = arg[1] if arg.size == 2
|
10
|
+
@variables[arg[0]]
|
11
|
+
end
|
12
|
+
|
13
|
+
def ___expr(arg)
|
14
|
+
eval arg.join('')
|
15
|
+
end
|
16
|
+
|
17
|
+
def ___if(arg)
|
18
|
+
arg.delete('then')
|
19
|
+
arg.delete('else')
|
20
|
+
arg.delete('elseif')
|
21
|
+
while arg[1]
|
22
|
+
r = eval(replace(arg[0]))
|
23
|
+
return parse(arg[1]) if r && r != 0
|
24
|
+
arg.shift(2)
|
25
|
+
end
|
26
|
+
return parse(arg[0]) if arg[0]
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def ___for(arg)
|
31
|
+
raise(CommandError, 'for start test next body') unless arg.size == 4
|
32
|
+
parse(arg[0])
|
33
|
+
catch(:break) do
|
34
|
+
while eval(replace(arg[1]))
|
35
|
+
catch(:continue) do
|
36
|
+
parse(arg[3])
|
37
|
+
end
|
38
|
+
parse(arg[2])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def ___foreach(arg)
|
44
|
+
varlist = []
|
45
|
+
list = []
|
46
|
+
while arg[2]
|
47
|
+
varlist << parse(arg[0], true)
|
48
|
+
list << parse(arg[1], true)
|
49
|
+
arg.shift(2)
|
50
|
+
end
|
51
|
+
body = arg[0]
|
52
|
+
catch(:break) do
|
53
|
+
while list.any?(&:any?)
|
54
|
+
# assign variables
|
55
|
+
varlist.each_with_index do |v, idx|
|
56
|
+
v.each do |vv|
|
57
|
+
@variables[vv] = list[idx].shift || ''
|
58
|
+
end
|
59
|
+
end
|
60
|
+
catch(:continue) do
|
61
|
+
parse(body)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def ___while(arg)
|
68
|
+
body = arg[1]
|
69
|
+
catch(:break) do
|
70
|
+
while eval(replace(arg[0]))
|
71
|
+
catch(:continue) do
|
72
|
+
parse(body)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def ___break(_arg)
|
79
|
+
throw :break
|
80
|
+
end
|
81
|
+
|
82
|
+
def ___continue(_arg)
|
83
|
+
throw :continue
|
84
|
+
end
|
85
|
+
|
86
|
+
def ___return(arg)
|
87
|
+
throw(:return, arg[0])
|
88
|
+
end
|
89
|
+
|
90
|
+
def ___incr(arg)
|
91
|
+
raise(TclArgumentError, 'incr varName ?increment?') unless (1..2).cover?(arg.size)
|
92
|
+
incr = (arg[1]) ? arg[1].to_i : 1
|
93
|
+
@variables[arg[0]] = ((@variables[arg[0]] || 0).to_i + incr).to_s
|
94
|
+
end
|
95
|
+
|
96
|
+
def ___puts(arg)
|
97
|
+
puts arg[0]
|
98
|
+
end
|
99
|
+
|
100
|
+
def ___proc(arg)
|
101
|
+
raise(TclArgumentError, 'proc name args body') unless arg.size == 3
|
102
|
+
@proc[arg[0]] = arg[1..2]
|
103
|
+
end
|
104
|
+
|
105
|
+
def ___global(arg)
|
106
|
+
@variables[:___global] ||= []
|
107
|
+
arg.each do |v|
|
108
|
+
@variables[:___global] << v
|
109
|
+
@variables[v] = @global[v] if @global
|
110
|
+
end
|
111
|
+
end
|
112
|
+
# define_method('___#') { |_p| nil }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Tcl
|
2
|
+
module Ruby
|
3
|
+
class Interpreter
|
4
|
+
private
|
5
|
+
|
6
|
+
def ___llength(arg)
|
7
|
+
raise(CommandError, 'llength list') if arg.size != 1
|
8
|
+
parse(arg[0], true).size
|
9
|
+
end
|
10
|
+
|
11
|
+
def ___list(arg)
|
12
|
+
arg.join(' ').to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
def ___lindex(arg)
|
16
|
+
if arg[1].nil?
|
17
|
+
arg[0]
|
18
|
+
elsif arg[1] == ''
|
19
|
+
arg[0]
|
20
|
+
else
|
21
|
+
l = arg[0]
|
22
|
+
arg[1..-1].each do |as|
|
23
|
+
parse(as, true).each do |a|
|
24
|
+
l = parse(l, true)
|
25
|
+
a.gsub!(/end/, (l.size - 1).to_s)
|
26
|
+
pos = eval(a)
|
27
|
+
if (0...l.size).cover?(pos)
|
28
|
+
l = l[pos]
|
29
|
+
else
|
30
|
+
return ''
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
l
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def ___join(arg)
|
39
|
+
raise(CommandError, 'join list joinString?') unless (1..2).cover? arg.size
|
40
|
+
separator = arg[1] || ' '
|
41
|
+
parse(arg[0], true).join(separator)
|
42
|
+
end
|
43
|
+
|
44
|
+
def ___linsert(arg)
|
45
|
+
raise(CommandError, 'linsert list insertposition elements') unless arg.size >= 3
|
46
|
+
l = parse(arg[0], true)
|
47
|
+
l.insert(arg[1].to_i, *arg[2..-1])
|
48
|
+
# "{#{l.join(' ')}}"
|
49
|
+
l.to_list
|
50
|
+
end
|
51
|
+
|
52
|
+
def ___lrange(arg)
|
53
|
+
raise(CommandError, 'lrange list first last') unless arg.size == 3
|
54
|
+
first = arg[1].to_i
|
55
|
+
first = 0 if first < 0
|
56
|
+
last = arg[2].to_i
|
57
|
+
l = parse(arg[0], true)
|
58
|
+
if first <= last
|
59
|
+
l[first..last].to_list
|
60
|
+
else
|
61
|
+
''
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def ___lappend(arg)
|
66
|
+
l = parse(variables(arg[0]), true)
|
67
|
+
l.push(*arg[1..-1])
|
68
|
+
@variables[arg[0]] = l.to_list
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Tcl
|
2
|
+
module Ruby
|
3
|
+
class TclError < StandardError; end
|
4
|
+
class ParseError < TclError; end
|
5
|
+
class CommandError < TclError; end
|
6
|
+
class TclArgumentError < TclError
|
7
|
+
def initialize(msg)
|
8
|
+
super("wrong \# args: should be\"#{msg}\"")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
class TclVariableNotFoundError < TclError
|
12
|
+
def initialize(var, type = '')
|
13
|
+
super("can't read \"#{var}\": #{type}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Tcl
|
2
|
+
module Ruby
|
3
|
+
class ListArray
|
4
|
+
Array.public_instance_methods(false).each do |name|
|
5
|
+
next if name == '<<'
|
6
|
+
define_method(name) do |*args, &block|
|
7
|
+
@ary.send(name, *args, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@ary = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def <<(buffer)
|
16
|
+
@ary << buffer.dup unless buffer.empty?
|
17
|
+
buffer.clear
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_string
|
22
|
+
@ary.map! { |e| _to_string(e) }
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_list
|
27
|
+
@ary.map { |e| _to_list(e) }.join(' ')
|
28
|
+
end
|
29
|
+
|
30
|
+
def map!(&block)
|
31
|
+
@ary.map!(&block)
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def map(&block)
|
36
|
+
r = dup
|
37
|
+
r.map!(&block)
|
38
|
+
r
|
39
|
+
end
|
40
|
+
|
41
|
+
def [](arg)
|
42
|
+
if arg.is_a?(Range)
|
43
|
+
r = dup
|
44
|
+
r.ary = r.ary[arg]
|
45
|
+
r
|
46
|
+
else
|
47
|
+
@ary.[](arg)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
attr_accessor :ary
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def _to_string(str)
|
58
|
+
if str[0] == '{' && str[-1] == '}'
|
59
|
+
str = str[1..-2]
|
60
|
+
elsif str[0] == '"' && str[-1] == '"'
|
61
|
+
str = str[1..-2]
|
62
|
+
end
|
63
|
+
str
|
64
|
+
end
|
65
|
+
|
66
|
+
def _to_list(str)
|
67
|
+
if str == '' || str.match(/\s/)
|
68
|
+
"{#{str}}"
|
69
|
+
else
|
70
|
+
str
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
module Tcl
|
4
|
+
module Ruby
|
5
|
+
class Interpreter
|
6
|
+
def parse(str, to_list = false)
|
7
|
+
s = StringScanner.new(str)
|
8
|
+
r = ListArray.new
|
9
|
+
pdepth = ddepth = bdepth = 0
|
10
|
+
buffer = ''
|
11
|
+
ret = nil
|
12
|
+
until s.empty?
|
13
|
+
if s.scan(/\\./m)
|
14
|
+
buffer << s[0] unless s[0][1] =~ /\s/
|
15
|
+
elsif !to_list && s.scan(/\r\n|\r|\n|;/)
|
16
|
+
if pdepth == 0 && ddepth == 0 && bdepth == 0
|
17
|
+
r << buffer
|
18
|
+
ret = command(r) unless r.empty?
|
19
|
+
r = ListArray.new
|
20
|
+
else
|
21
|
+
buffer << s[0]
|
22
|
+
end
|
23
|
+
elsif s.scan(/\s+/)
|
24
|
+
if pdepth == 0 && ddepth == 0 && bdepth == 0
|
25
|
+
r << buffer
|
26
|
+
else
|
27
|
+
buffer << s[0]
|
28
|
+
end
|
29
|
+
else
|
30
|
+
buffer <<
|
31
|
+
if s.scan(/{/)
|
32
|
+
# pdepth += 1 if ddepth == 0
|
33
|
+
pdepth += 1 if buffer == '' || pdepth != 0
|
34
|
+
s[0]
|
35
|
+
elsif s.scan(/}/)
|
36
|
+
ret = s[0] # pdepth -= 1 if ddepth == 0
|
37
|
+
pdepth -= 1 if pdepth != 0
|
38
|
+
raise(ParseError, 'extra characters after close-brace') if
|
39
|
+
buffer[0] == '{' && pdepth == 0 && !s.check(/\s|\z/)
|
40
|
+
ret
|
41
|
+
elsif !to_list && s.scan(/\[/)
|
42
|
+
bdepth += 1 if pdepth == 0
|
43
|
+
s[0]
|
44
|
+
elsif !to_list && s.scan(/\]/)
|
45
|
+
bdepth -= 1 if pdepth == 0
|
46
|
+
s[0]
|
47
|
+
elsif s.scan(/"/)
|
48
|
+
ret = s[0]
|
49
|
+
ddepth = 1 - ddepth if buffer == '' || buffer[0] == '"'
|
50
|
+
raise(ParseError, 'extra characters after close-quote') if
|
51
|
+
buffer[0] == '"' && ddepth == 0 && !s.check(/\s|\z/)
|
52
|
+
ret
|
53
|
+
elsif s.scan(/\S/)
|
54
|
+
s[0]
|
55
|
+
else
|
56
|
+
raise(ParseError, "parse error #{s.rest}")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
r << buffer
|
61
|
+
raise(ParseError, 'unmatched parenthesises') if
|
62
|
+
ddepth != 0 || pdepth != 0 || bdepth != 0
|
63
|
+
if to_list
|
64
|
+
r.to_string
|
65
|
+
else
|
66
|
+
ret = command(r) unless r.empty?
|
67
|
+
ret
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Tcl
|
2
|
+
module Ruby
|
3
|
+
class Interpreter
|
4
|
+
def initialize
|
5
|
+
@variables = {}
|
6
|
+
@global = @variables
|
7
|
+
@v_stack = []
|
8
|
+
@hooks = {}
|
9
|
+
@proc = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def variables(arg)
|
13
|
+
raise(TclVariableNotFoundError, "can't read $#{arg}, no such variables") unless @variables.key?(arg)
|
14
|
+
@variables[arg]
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_hook(name, &block)
|
18
|
+
raise(ArgumentError, 'block is not given') unless block_given?
|
19
|
+
@hooks[name.to_s] = block
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete_hook(name)
|
23
|
+
@hooks.delete(name.to_s)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/tcl/ruby.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'tcl/ruby/command.rb'
|
2
|
+
require 'tcl/ruby/error.rb'
|
3
|
+
require 'tcl/ruby/list_array.rb'
|
4
|
+
require 'tcl/ruby/parser.rb'
|
5
|
+
require 'tcl/ruby/util.rb'
|
6
|
+
require 'tcl/ruby/commands/basic.rb'
|
7
|
+
require 'tcl/ruby/commands/list.rb'
|
8
|
+
require 'tcl/ruby/commands/array.rb'
|
data/tcl-ruby.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 'tcl/ruby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'tcl-ruby'
|
8
|
+
spec.version = Tcl::Ruby::VERSION
|
9
|
+
spec.authors = ['Kiyonori Matsumoto']
|
10
|
+
|
11
|
+
spec.summary = 'Tcl Interpreter for ruby'
|
12
|
+
spec.homepage = "https://github.com/kiyonori-matsumoto/tcl-ruby"
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = 'exe'
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
spec.add_development_dependency 'pry'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tcl-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kiyonori Matsumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-02 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- ".rspec"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/tcl/ruby.rb
|
87
|
+
- lib/tcl/ruby/command.rb
|
88
|
+
- lib/tcl/ruby/commands/array.rb
|
89
|
+
- lib/tcl/ruby/commands/basic.rb
|
90
|
+
- lib/tcl/ruby/commands/list.rb
|
91
|
+
- lib/tcl/ruby/error.rb
|
92
|
+
- lib/tcl/ruby/list_array.rb
|
93
|
+
- lib/tcl/ruby/parser.rb
|
94
|
+
- lib/tcl/ruby/util.rb
|
95
|
+
- lib/tcl/ruby/version.rb
|
96
|
+
- tcl-ruby.gemspec
|
97
|
+
homepage: https://github.com/kiyonori-matsumoto/tcl-ruby
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.5.1
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Tcl Interpreter for ruby
|
121
|
+
test_files: []
|