meta-ruby 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +19 -0
- data/README.md +52 -0
- data/Rakefile +7 -0
- data/bin/meta-hello +17 -0
- data/lib/meta-ruby.rb +6 -0
- data/meta-ruby.gemspec +17 -0
- data/pool/meta-ruby.cut +118 -0
- data/pool/meta-ruby.jar +0 -0
- data/ruby/MetaRuby.rb +3 -0
- data/ruby/MetaRuby/Directory.rb +19 -0
- data/ruby/MetaRuby/Generator.rb +9 -0
- data/ruby/MetaRuby/Processor.rb +99 -0
- data/ruby/MetaRuby/Processor/SourceProcessor.rb +788 -0
- data/ruby/MetaRuby/Sexp.rb +50 -0
- data/spec/spec_helper.rb +1 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 884fc5bb96caeddf63c54c52d783fe32d9d87088
|
4
|
+
data.tar.gz: 253e3b4c1d462ce002152facea5231f4a39ce948
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 56b0d554a1854a34a2332431975c2c94889461157c36822af3ce0f6260a865273ec367d77babbb0f24b400e1ebdbdde15b68c75d6176682893aa6f87419a3129
|
7
|
+
data.tar.gz: 107905c4b9eda3d6e7a3b3c959050f9ad7b6c3f8ce0d4e78872da502cd207163843eed1bde304dcf1aea836f5539ffed95b9e6bb2af80c5935d37e473e7c4d09
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2011-2015 InfraRuby Vision
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
+
copy of this software and associated documentation files (the "Software"),
|
5
|
+
to deal in the Software without restriction, including without limitation
|
6
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
7
|
+
and/or sell copies of the Software, and to permit persons to whom the
|
8
|
+
Software is furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
19
|
+
DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
InfraRuby meta-ruby
|
2
|
+
===================
|
3
|
+
|
4
|
+
This gem provides classes to generate Ruby source code.
|
5
|
+
|
6
|
+
|
7
|
+
Example
|
8
|
+
-------
|
9
|
+
|
10
|
+
require "meta-ruby"
|
11
|
+
|
12
|
+
class Generator < MetaRuby::Generator
|
13
|
+
def initialize_sexp
|
14
|
+
s(:defn, "initialize", s(:args, "x", "y"),
|
15
|
+
s(:iasgn, "@x", s(:lvar, "x")),
|
16
|
+
s(:iasgn, "@y", s(:lvar, "y")),
|
17
|
+
s(:return),
|
18
|
+
)
|
19
|
+
end
|
20
|
+
|
21
|
+
def attr_reader_sexp(name)
|
22
|
+
s(:defn, name, s(:args),
|
23
|
+
s(:return, s(:ivar, "@#{name}")),
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def xy_class_sexp
|
28
|
+
s(:class, "XY", nil,
|
29
|
+
initialize_sexp,
|
30
|
+
attr_reader_sexp("x"),
|
31
|
+
attr_reader_sexp("y"),
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
g = Generator.new
|
37
|
+
|
38
|
+
d = MetaRuby::Directory.new
|
39
|
+
d.instance_eval do
|
40
|
+
write("XY.rb", g.xy_class_sexp)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
Support
|
45
|
+
-------
|
46
|
+
|
47
|
+
InfraRuby Vision
|
48
|
+
rubygems@infraruby.com
|
49
|
+
|
50
|
+
http://infraruby.com/
|
51
|
+
https://github.com/InfraRuby
|
52
|
+
https://twitter.com/InfraRuby
|
data/Rakefile
ADDED
data/bin/meta-hello
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "meta-ruby"
|
4
|
+
|
5
|
+
class Generator < MetaRuby::Generator
|
6
|
+
## -> MetaRuby::Sexp
|
7
|
+
def hello_sexp
|
8
|
+
s(:call, nil, "puts", s(:arglist, s(:str, "hello, world")))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
g = Generator.new
|
13
|
+
|
14
|
+
d = MetaRuby::Directory.new
|
15
|
+
d.instance_eval do
|
16
|
+
write("hello.rb", g.hello_sexp)
|
17
|
+
end
|
data/lib/meta-ruby.rb
ADDED
data/meta-ruby.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.platform = "ruby"
|
3
|
+
s.name = "meta-ruby"
|
4
|
+
s.version = "3.1.0"
|
5
|
+
s.licenses = ["MIT"]
|
6
|
+
s.author = "InfraRuby Vision"
|
7
|
+
s.email = "rubygems@infraruby.com"
|
8
|
+
s.homepage = "http://infraruby.com/"
|
9
|
+
s.summary = "Ruby metaprogramming with s-expressions"
|
10
|
+
s.description = "Ruby metaprogramming with s-expressions"
|
11
|
+
s.files = Dir["**/*"]
|
12
|
+
s.add_runtime_dependency "infraruby-shim", "~> 3.7"
|
13
|
+
s.add_runtime_dependency "s-expression", "~> 3.1"
|
14
|
+
s.add_runtime_dependency "fab", "~> 0.2"
|
15
|
+
s.add_development_dependency "infraruby-task", "~> 3.7"
|
16
|
+
s.add_development_dependency "rspec", "~> 3.0"
|
17
|
+
end
|
data/pool/meta-ruby.cut
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
## <>
|
2
|
+
module MetaRuby
|
3
|
+
end
|
4
|
+
|
5
|
+
## <>
|
6
|
+
## :write: String, MetaRuby::Sexp -> void
|
7
|
+
## :write: String, String -> void
|
8
|
+
## :initialize: -> void
|
9
|
+
## :initialize: String -> void
|
10
|
+
class MetaRuby::Directory < Fab::Directory
|
11
|
+
end
|
12
|
+
|
13
|
+
## <>
|
14
|
+
## :s: Symbol, *java.lang.Object? -> MetaRuby::Sexp
|
15
|
+
## :initialize: -> void
|
16
|
+
class MetaRuby::Generator < Object
|
17
|
+
end
|
18
|
+
|
19
|
+
## <>
|
20
|
+
## :process: MetaRuby::Sexp -> String
|
21
|
+
## #process_array: MetaRuby::Sexp -> String
|
22
|
+
## #process_attrasgn: MetaRuby::Sexp -> String
|
23
|
+
## #process_call: MetaRuby::Sexp -> String
|
24
|
+
## #process_case: MetaRuby::Sexp -> String
|
25
|
+
## #process_cdecl: MetaRuby::Sexp -> String
|
26
|
+
## #process_class: MetaRuby::Sexp -> String
|
27
|
+
## #process_colon2: MetaRuby::Sexp -> String
|
28
|
+
## #process_colon3: MetaRuby::Sexp -> String
|
29
|
+
## #process_const: MetaRuby::Sexp -> String
|
30
|
+
## #process_defn: MetaRuby::Sexp -> String
|
31
|
+
## #process_ensure: MetaRuby::Sexp -> String
|
32
|
+
## #process_false: MetaRuby::Sexp -> String
|
33
|
+
## #process_hash: MetaRuby::Sexp -> String
|
34
|
+
## #process_iasgn: MetaRuby::Sexp -> String
|
35
|
+
## #process_if: MetaRuby::Sexp -> String
|
36
|
+
## #process_iter: MetaRuby::Sexp -> String
|
37
|
+
## #process_ivar: MetaRuby::Sexp -> String
|
38
|
+
## #process_lasgn: MetaRuby::Sexp -> String
|
39
|
+
## #process_lit: MetaRuby::Sexp -> String
|
40
|
+
## #process_lvar: MetaRuby::Sexp -> String
|
41
|
+
## #process_module: MetaRuby::Sexp -> String
|
42
|
+
## #process_nil: MetaRuby::Sexp -> String
|
43
|
+
## #process_op_asgn1: MetaRuby::Sexp -> String
|
44
|
+
## #process_op_asgn2: MetaRuby::Sexp -> String
|
45
|
+
## #process_op_asgn_and: MetaRuby::Sexp -> String
|
46
|
+
## #process_op_asgn_or: MetaRuby::Sexp -> String
|
47
|
+
## #process_rescue: MetaRuby::Sexp -> String
|
48
|
+
## #process_return: MetaRuby::Sexp -> String
|
49
|
+
## #process_sclass: MetaRuby::Sexp -> String
|
50
|
+
## #process_self: MetaRuby::Sexp -> String
|
51
|
+
## #process_str: MetaRuby::Sexp -> String
|
52
|
+
## #process_super: MetaRuby::Sexp -> String
|
53
|
+
## #process_true: MetaRuby::Sexp -> String
|
54
|
+
## #process_until: MetaRuby::Sexp -> String
|
55
|
+
## #process_while: MetaRuby::Sexp -> String
|
56
|
+
## #process_yield: MetaRuby::Sexp -> String
|
57
|
+
## :initialize: -> void
|
58
|
+
class MetaRuby::Processor < Object
|
59
|
+
end
|
60
|
+
|
61
|
+
## <>
|
62
|
+
## INSTANCE: MetaRuby::SourceProcessor
|
63
|
+
## @depth: int32
|
64
|
+
## @level: int32
|
65
|
+
## .process: MetaRuby::Sexp -> String
|
66
|
+
## :initialize: -> void
|
67
|
+
## :process: MetaRuby::Sexp -> String
|
68
|
+
## :process_array: MetaRuby::Sexp -> String
|
69
|
+
## :process_attrasgn: MetaRuby::Sexp -> String
|
70
|
+
## :process_call: MetaRuby::Sexp -> String
|
71
|
+
## :process_case: MetaRuby::Sexp -> String
|
72
|
+
## :process_cdecl: MetaRuby::Sexp -> String
|
73
|
+
## :process_class: MetaRuby::Sexp -> String
|
74
|
+
## :process_colon2: MetaRuby::Sexp -> String
|
75
|
+
## :process_colon3: MetaRuby::Sexp -> String
|
76
|
+
## :process_const: MetaRuby::Sexp -> String
|
77
|
+
## :process_defn: MetaRuby::Sexp -> String
|
78
|
+
## :process_ensure: MetaRuby::Sexp -> String
|
79
|
+
## :process_false: MetaRuby::Sexp -> String
|
80
|
+
## :process_hash: MetaRuby::Sexp -> String
|
81
|
+
## :process_iasgn: MetaRuby::Sexp -> String
|
82
|
+
## :process_if: MetaRuby::Sexp -> String
|
83
|
+
## :process_iter: MetaRuby::Sexp -> String
|
84
|
+
## :process_ivar: MetaRuby::Sexp -> String
|
85
|
+
## :process_lasgn: MetaRuby::Sexp -> String
|
86
|
+
## :process_lit: MetaRuby::Sexp -> String
|
87
|
+
## :process_lvar: MetaRuby::Sexp -> String
|
88
|
+
## :process_module: MetaRuby::Sexp -> String
|
89
|
+
## :process_nil: MetaRuby::Sexp -> String
|
90
|
+
## :process_op_asgn1: MetaRuby::Sexp -> String
|
91
|
+
## :process_op_asgn2: MetaRuby::Sexp -> String
|
92
|
+
## :process_op_asgn_and: MetaRuby::Sexp -> String
|
93
|
+
## :process_op_asgn_or: MetaRuby::Sexp -> String
|
94
|
+
## :process_rescue: MetaRuby::Sexp -> String
|
95
|
+
## :process_return: MetaRuby::Sexp -> String
|
96
|
+
## :process_sclass: MetaRuby::Sexp -> String
|
97
|
+
## :process_self: MetaRuby::Sexp -> String
|
98
|
+
## :process_str: MetaRuby::Sexp -> String
|
99
|
+
## :process_super: MetaRuby::Sexp -> String
|
100
|
+
## :process_true: MetaRuby::Sexp -> String
|
101
|
+
## :process_until: MetaRuby::Sexp -> String
|
102
|
+
## :process_while: MetaRuby::Sexp -> String
|
103
|
+
## :process_yield: MetaRuby::Sexp -> String
|
104
|
+
class MetaRuby::SourceProcessor < MetaRuby::Processor
|
105
|
+
end
|
106
|
+
|
107
|
+
## <>
|
108
|
+
## :initialize: Symbol -> void
|
109
|
+
## :initialize: Symbol, Array<java.lang.Object?> -> void
|
110
|
+
## :fetch_as_s: int32 -> String
|
111
|
+
## :fetch_as_s: Fixnum -> String
|
112
|
+
## :fetch_as_sexp: int32 -> MetaRuby::Sexp
|
113
|
+
## :fetch_as_sexp: Fixnum -> MetaRuby::Sexp
|
114
|
+
## :comments: -> Array<String>?
|
115
|
+
## :comments=: Array<String>? -> void
|
116
|
+
class MetaRuby::Sexp < Sexp
|
117
|
+
end
|
118
|
+
|
data/pool/meta-ruby.jar
ADDED
Binary file
|
data/ruby/MetaRuby.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module MetaRuby
|
2
|
+
## <>
|
3
|
+
class Directory < Fab::Directory
|
4
|
+
## String, Sexp -> void
|
5
|
+
## String, String -> void
|
6
|
+
def write(path, o)
|
7
|
+
case o
|
8
|
+
when Sexp
|
9
|
+
s = SourceProcessor.process(o)
|
10
|
+
when String
|
11
|
+
s = o
|
12
|
+
else
|
13
|
+
raise RuntimeError
|
14
|
+
end
|
15
|
+
super(path, s)
|
16
|
+
return
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module MetaRuby
|
2
|
+
## <>
|
3
|
+
## #process_array: Sexp -> String
|
4
|
+
## #process_attrasgn: Sexp -> String
|
5
|
+
## #process_call: Sexp -> String
|
6
|
+
## #process_case: Sexp -> String
|
7
|
+
## #process_cdecl: Sexp -> String
|
8
|
+
## #process_class: Sexp -> String
|
9
|
+
## #process_colon2: Sexp -> String
|
10
|
+
## #process_colon3: Sexp -> String
|
11
|
+
## #process_const: Sexp -> String
|
12
|
+
## #process_defn: Sexp -> String
|
13
|
+
## #process_ensure: Sexp -> String
|
14
|
+
## #process_false: Sexp -> String
|
15
|
+
## #process_hash: Sexp -> String
|
16
|
+
## #process_iasgn: Sexp -> String
|
17
|
+
## #process_if: Sexp -> String
|
18
|
+
## #process_iter: Sexp -> String
|
19
|
+
## #process_ivar: Sexp -> String
|
20
|
+
## #process_lasgn: Sexp -> String
|
21
|
+
## #process_lit: Sexp -> String
|
22
|
+
## #process_lvar: Sexp -> String
|
23
|
+
## #process_module: Sexp -> String
|
24
|
+
## #process_nil: Sexp -> String
|
25
|
+
## #process_op_asgn1: Sexp -> String
|
26
|
+
## #process_op_asgn2: Sexp -> String
|
27
|
+
## #process_op_asgn_and: Sexp -> String
|
28
|
+
## #process_op_asgn_or: Sexp -> String
|
29
|
+
## #process_rescue: Sexp -> String
|
30
|
+
## #process_return: Sexp -> String
|
31
|
+
## #process_sclass: Sexp -> String
|
32
|
+
## #process_self: Sexp -> String
|
33
|
+
## #process_str: Sexp -> String
|
34
|
+
## #process_super: Sexp -> String
|
35
|
+
## #process_true: Sexp -> String
|
36
|
+
## #process_until: Sexp -> String
|
37
|
+
## #process_while: Sexp -> String
|
38
|
+
## #process_yield: Sexp -> String
|
39
|
+
class Processor
|
40
|
+
## % nojava
|
41
|
+
## <>
|
42
|
+
module X
|
43
|
+
class << self
|
44
|
+
## &(Processor, Sexp -> String) -> (Processor, Sexp -> String)
|
45
|
+
def process_block(&block)
|
46
|
+
return block
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
BLOCKS = {
|
51
|
+
:array => process_block { |processor, sexp| processor.process_array(sexp) },
|
52
|
+
:attrasgn => process_block { |processor, sexp| processor.process_attrasgn(sexp) },
|
53
|
+
:call => process_block { |processor, sexp| processor.process_call(sexp) },
|
54
|
+
:case => process_block { |processor, sexp| processor.process_case(sexp) },
|
55
|
+
:cdecl => process_block { |processor, sexp| processor.process_cdecl(sexp) },
|
56
|
+
:class => process_block { |processor, sexp| processor.process_class(sexp) },
|
57
|
+
:colon2 => process_block { |processor, sexp| processor.process_colon2(sexp) },
|
58
|
+
:colon3 => process_block { |processor, sexp| processor.process_colon3(sexp) },
|
59
|
+
:const => process_block { |processor, sexp| processor.process_const(sexp) },
|
60
|
+
:defn => process_block { |processor, sexp| processor.process_defn(sexp) },
|
61
|
+
:ensure => process_block { |processor, sexp| processor.process_ensure(sexp) },
|
62
|
+
:false => process_block { |processor, sexp| processor.process_false(sexp) },
|
63
|
+
:hash => process_block { |processor, sexp| processor.process_hash(sexp) },
|
64
|
+
:iasgn => process_block { |processor, sexp| processor.process_iasgn(sexp) },
|
65
|
+
:if => process_block { |processor, sexp| processor.process_if(sexp) },
|
66
|
+
:iter => process_block { |processor, sexp| processor.process_iter(sexp) },
|
67
|
+
:ivar => process_block { |processor, sexp| processor.process_ivar(sexp) },
|
68
|
+
:lasgn => process_block { |processor, sexp| processor.process_lasgn(sexp) },
|
69
|
+
:lit => process_block { |processor, sexp| processor.process_lit(sexp) },
|
70
|
+
:lvar => process_block { |processor, sexp| processor.process_lvar(sexp) },
|
71
|
+
:module => process_block { |processor, sexp| processor.process_module(sexp) },
|
72
|
+
:nil => process_block { |processor, sexp| processor.process_nil(sexp) },
|
73
|
+
:op_asgn1 => process_block { |processor, sexp| processor.process_op_asgn1(sexp) },
|
74
|
+
:op_asgn2 => process_block { |processor, sexp| processor.process_op_asgn2(sexp) },
|
75
|
+
:op_asgn_and => process_block { |processor, sexp| processor.process_op_asgn_and(sexp) },
|
76
|
+
:op_asgn_or => process_block { |processor, sexp| processor.process_op_asgn_or(sexp) },
|
77
|
+
:rescue => process_block { |processor, sexp| processor.process_rescue(sexp) },
|
78
|
+
:return => process_block { |processor, sexp| processor.process_return(sexp) },
|
79
|
+
:sclass => process_block { |processor, sexp| processor.process_sclass(sexp) },
|
80
|
+
:self => process_block { |processor, sexp| processor.process_self(sexp) },
|
81
|
+
:str => process_block { |processor, sexp| processor.process_str(sexp) },
|
82
|
+
:super => process_block { |processor, sexp| processor.process_super(sexp) },
|
83
|
+
:true => process_block { |processor, sexp| processor.process_true(sexp) },
|
84
|
+
:until => process_block { |processor, sexp| processor.process_until(sexp) },
|
85
|
+
:while => process_block { |processor, sexp| processor.process_while(sexp) },
|
86
|
+
:yield => process_block { |processor, sexp| processor.process_yield(sexp) },
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
## Sexp -> String
|
91
|
+
def process(sexp)
|
92
|
+
block = X::BLOCKS[sexp.type]
|
93
|
+
if block.nil?
|
94
|
+
raise ArgumentError, sexp.inspect
|
95
|
+
end
|
96
|
+
return block.call(self, sexp)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,788 @@
|
|
1
|
+
module MetaRuby
|
2
|
+
## <>
|
3
|
+
## @depth: int32
|
4
|
+
## @level: int32
|
5
|
+
class SourceProcessor < Processor
|
6
|
+
## % nojava
|
7
|
+
## <>
|
8
|
+
module X
|
9
|
+
BINARY_L = {
|
10
|
+
"*" => 12.i32, "/" => 12.i32, "%" => 12.i32,
|
11
|
+
"+" => 11.i32, "-" => 11.i32,
|
12
|
+
"<<" => 10.i32, ">>" => 10.i32,
|
13
|
+
"&" => 9.i32,
|
14
|
+
"|" => 8.i32, "^" => 8.i32,
|
15
|
+
"<" => 7.i32, "<=" => 7.i32, ">=" => 7.i32, ">" => 7.i32,
|
16
|
+
"&&" => 5.i32,
|
17
|
+
"||" => 4.i32,
|
18
|
+
}
|
19
|
+
|
20
|
+
BINARY_R = {
|
21
|
+
"**" => 14.i32,
|
22
|
+
}
|
23
|
+
|
24
|
+
BINARY_X = {
|
25
|
+
"==" => 6.i32, "!=" => 6.i32, "=~" => 6.i32, "!~" => 6.i32, "===" => 6.i32, "<=>" => 6.i32,
|
26
|
+
".." => 3.i32, "..." => 3.i32,
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
# ===
|
31
|
+
|
32
|
+
class << self
|
33
|
+
## Sexp -> String
|
34
|
+
def process(sexp)
|
35
|
+
return INSTANCE.process(sexp)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# ===
|
40
|
+
|
41
|
+
## -> void
|
42
|
+
def initialize
|
43
|
+
super()
|
44
|
+
@depth = 0.i32
|
45
|
+
@level = 0.i32
|
46
|
+
return
|
47
|
+
end
|
48
|
+
|
49
|
+
# ===
|
50
|
+
|
51
|
+
INSTANCE = SourceProcessor.__new__
|
52
|
+
|
53
|
+
# ===
|
54
|
+
|
55
|
+
## -> String
|
56
|
+
def __tabs
|
57
|
+
return "\t" * @depth
|
58
|
+
end
|
59
|
+
|
60
|
+
## java.lang.Object? -> String
|
61
|
+
def __with_depth(o)
|
62
|
+
if o.nil?
|
63
|
+
return ""
|
64
|
+
elsif o.is_a?(Sexp)
|
65
|
+
@depth += 1
|
66
|
+
begin
|
67
|
+
return process(o)
|
68
|
+
ensure
|
69
|
+
@depth -= 1
|
70
|
+
end
|
71
|
+
else
|
72
|
+
raise ArgumentError, o.inspect
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
## int32, &{-> void} -> void
|
77
|
+
def __with_level(new_level)
|
78
|
+
old_level = @level
|
79
|
+
@level = new_level
|
80
|
+
begin
|
81
|
+
yield
|
82
|
+
return
|
83
|
+
ensure
|
84
|
+
@level = old_level
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
## int32, &{-> String} -> String
|
89
|
+
def __with_level_s(new_level)
|
90
|
+
old_level = @level
|
91
|
+
@level = new_level
|
92
|
+
begin
|
93
|
+
return yield
|
94
|
+
ensure
|
95
|
+
@level = old_level
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
## Sexp, int32 -> Sexp
|
100
|
+
def __arglist_sexp(sexp, i)
|
101
|
+
arglist_sexp = Sexp.__new__(:arglist)
|
102
|
+
n = sexp.size_as_int32
|
103
|
+
while i < n
|
104
|
+
e = sexp.fetch_as_sexp(i)
|
105
|
+
if e.type == :arglist
|
106
|
+
arglist_sexp.concat(e.args)
|
107
|
+
else
|
108
|
+
arglist_sexp << e
|
109
|
+
end
|
110
|
+
i += 1
|
111
|
+
end
|
112
|
+
return arglist_sexp
|
113
|
+
end
|
114
|
+
|
115
|
+
## Sexp -> Array<String>
|
116
|
+
def __process_arglist(sexp)
|
117
|
+
a = []
|
118
|
+
__with_level(2.i32) do
|
119
|
+
n = sexp.size_as_int32
|
120
|
+
n.times do |i|
|
121
|
+
arg_sexp = sexp.fetch_as_sexp(i)
|
122
|
+
case arg_sexp.type
|
123
|
+
when :block_pass
|
124
|
+
s = "&" + process(arg_sexp.fetch_as_sexp(0.i32))
|
125
|
+
when :splat
|
126
|
+
s = "*" + process(arg_sexp.fetch_as_sexp(0.i32))
|
127
|
+
else
|
128
|
+
s = process(arg_sexp)
|
129
|
+
end
|
130
|
+
a.push(s)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
return a
|
134
|
+
end
|
135
|
+
|
136
|
+
## Sexp -> Hash<Symbol, Sexp>
|
137
|
+
def __lasgn_hash(sexp)
|
138
|
+
h = {}
|
139
|
+
n = sexp.size_as_int32
|
140
|
+
n.times do |i|
|
141
|
+
lasgn_sexp = sexp.fetch_as_sexp(i)
|
142
|
+
raise ArgumentError if lasgn_sexp.type != :lasgn
|
143
|
+
k = lasgn_sexp.fetch_as_s(0.i32)
|
144
|
+
v = lasgn_sexp.fetch_as_sexp(1.i32)
|
145
|
+
h[k] = v
|
146
|
+
end
|
147
|
+
return h
|
148
|
+
end
|
149
|
+
|
150
|
+
## Sexp -> Array<String>
|
151
|
+
def __process_args(sexp)
|
152
|
+
a = []
|
153
|
+
n = sexp.size_as_int32
|
154
|
+
if n == 0
|
155
|
+
return a
|
156
|
+
end
|
157
|
+
o = sexp.fetch(n - 1)
|
158
|
+
if o.is_a?(Sexp)
|
159
|
+
if o.type == :block
|
160
|
+
h = __lasgn_hash(o)
|
161
|
+
n -= 1
|
162
|
+
n.times do |i|
|
163
|
+
k = sexp.fetch_as_s(i)
|
164
|
+
v = h[k]
|
165
|
+
if v.nil?
|
166
|
+
a.push(k)
|
167
|
+
else
|
168
|
+
s = __with_level_s(2.i32) { process(v) }
|
169
|
+
a.push(k + " = " + s)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
return a
|
173
|
+
end
|
174
|
+
end
|
175
|
+
n.times do |i|
|
176
|
+
o = sexp.fetch(i)
|
177
|
+
case o
|
178
|
+
when String
|
179
|
+
a.push(o.to_s)
|
180
|
+
when Symbol
|
181
|
+
a.push(o.to_s)
|
182
|
+
when Sexp
|
183
|
+
lasgn_sexp = o
|
184
|
+
raise ArgumentError if lasgn_sexp.type != :lasgn
|
185
|
+
k = lasgn_sexp.fetch_as_s(0.i32)
|
186
|
+
v = lasgn_sexp.fetch_as_sexp(1.i32)
|
187
|
+
s = __with_level_s(2.i32) { process(v) }
|
188
|
+
a.push(k + " = " + s)
|
189
|
+
else
|
190
|
+
raise TypeError
|
191
|
+
end
|
192
|
+
end
|
193
|
+
return a
|
194
|
+
end
|
195
|
+
|
196
|
+
## Sexp, int32 -> String
|
197
|
+
def __process_scope(sexp, i)
|
198
|
+
a = []
|
199
|
+
@depth += 1
|
200
|
+
n = sexp.size_as_int32
|
201
|
+
while i < n
|
202
|
+
e = sexp.fetch_as_sexp(i)
|
203
|
+
if e.type == :scope
|
204
|
+
o = e[0.i32]
|
205
|
+
unless o.nil?
|
206
|
+
s = process(o.is_a!(Sexp))
|
207
|
+
a.push(s)
|
208
|
+
end
|
209
|
+
else
|
210
|
+
s = process(e)
|
211
|
+
a.push(s)
|
212
|
+
end
|
213
|
+
i += 1
|
214
|
+
end
|
215
|
+
@depth -= 1
|
216
|
+
return a.join
|
217
|
+
end
|
218
|
+
|
219
|
+
## Sexp -> Array<String>
|
220
|
+
def __process_hash(sexp)
|
221
|
+
a = []
|
222
|
+
__with_level(2.i32) do
|
223
|
+
n = sexp.size_as_int32
|
224
|
+
symbol = true
|
225
|
+
i = 0.i32
|
226
|
+
while i < n
|
227
|
+
key_sexp = sexp.fetch_as_sexp(i)
|
228
|
+
if key_sexp.type != :lit || !key_sexp.fetch(0.i32).is_a?(Symbol)
|
229
|
+
symbol = false
|
230
|
+
end
|
231
|
+
i += 2
|
232
|
+
end
|
233
|
+
i = 0.i32
|
234
|
+
while i < n
|
235
|
+
key_sexp = sexp.fetch_as_sexp(i)
|
236
|
+
i += 1
|
237
|
+
value_sexp = sexp.fetch_as_sexp(i)
|
238
|
+
i += 1
|
239
|
+
if symbol
|
240
|
+
k = key_sexp.fetch_as_s(0.i32)
|
241
|
+
v = process(value_sexp)
|
242
|
+
a.push("#{k}: #{v}")
|
243
|
+
else
|
244
|
+
k = process(key_sexp)
|
245
|
+
v = process(value_sexp)
|
246
|
+
a.push("#{k} => #{v}")
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
return a
|
251
|
+
end
|
252
|
+
|
253
|
+
## java.lang.Object?, Sexp, String, int32 -> String
|
254
|
+
def __unary(o, arglist_sexp, name, new_level)
|
255
|
+
if o.nil?
|
256
|
+
r = "self"
|
257
|
+
else
|
258
|
+
r = __with_level_s(new_level) { process(o.is_a!(Sexp)) }
|
259
|
+
end
|
260
|
+
s = "#{name}#{r}"
|
261
|
+
return @level > new_level ? "(#{s})" : s
|
262
|
+
end
|
263
|
+
|
264
|
+
## java.lang.Object?, Sexp, String, int32, int32, int32 -> String
|
265
|
+
def __binary(o, arglist_sexp, name, new_level, dl, dr)
|
266
|
+
if o.nil?
|
267
|
+
r = "self"
|
268
|
+
else
|
269
|
+
r = __with_level_s(new_level + dl) { process(o.is_a!(Sexp)) }
|
270
|
+
end
|
271
|
+
a = __with_level_s(new_level + dr) { process(arglist_sexp.fetch_as_sexp(0.i32)) }
|
272
|
+
s = "#{r} #{name} #{a}"
|
273
|
+
return @level > new_level ? "(#{s})" : s
|
274
|
+
end
|
275
|
+
|
276
|
+
# ===
|
277
|
+
|
278
|
+
## Sexp -> String
|
279
|
+
def process(sexp)
|
280
|
+
if @level > 0
|
281
|
+
return super(sexp)
|
282
|
+
end
|
283
|
+
if sexp.type == :block
|
284
|
+
a = []
|
285
|
+
n = sexp.size_as_int32
|
286
|
+
n.times do |i|
|
287
|
+
o = sexp.fetch(i)
|
288
|
+
if o.nil?
|
289
|
+
# ...
|
290
|
+
elsif o.is_a?(Sexp)
|
291
|
+
a.push(process(o))
|
292
|
+
else
|
293
|
+
raise ArgumentError, sexp.inspect
|
294
|
+
end
|
295
|
+
end
|
296
|
+
return a.join
|
297
|
+
end
|
298
|
+
s = __tabs + super(sexp) + "\n"
|
299
|
+
comments = sexp.comments
|
300
|
+
if comments.nil?
|
301
|
+
return s
|
302
|
+
end
|
303
|
+
return comments.map { |c| __tabs + c + "\n" }.join + s
|
304
|
+
end
|
305
|
+
|
306
|
+
## Sexp -> String
|
307
|
+
def process_array(sexp)
|
308
|
+
a = __process_arglist(sexp)
|
309
|
+
if @level < 2 && a.size_as_int32 > 0
|
310
|
+
s = a.map { |c| __tabs + "\t" + c + "," + "\n" }.join
|
311
|
+
return "[" + "\n" + s + __tabs + "]"
|
312
|
+
else
|
313
|
+
s = a.join(", ")
|
314
|
+
return "[#{s}]"
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
## Sexp -> String
|
319
|
+
def process_attrasgn(sexp)
|
320
|
+
raise ArgumentError if @level > 1
|
321
|
+
arglist_sexp = __arglist_sexp(sexp, 2.i32)
|
322
|
+
r = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
323
|
+
name = sexp.fetch_as_s(1.i32)
|
324
|
+
raise ArgumentError unless name.end_with?("=")
|
325
|
+
name = name.chop
|
326
|
+
if name == "[]"
|
327
|
+
k = __with_level_s(2.i32) { process(arglist_sexp.fetch_as_sexp(0.i32)) }
|
328
|
+
v = __with_level_s(1.i32) { process(arglist_sexp.fetch_as_sexp(1.i32)) }
|
329
|
+
return "#{r}[#{k}] = #{v}"
|
330
|
+
else
|
331
|
+
s = __with_level_s(1.i32) { process(arglist_sexp.fetch_as_sexp(0.i32)) }
|
332
|
+
return "#{r}.#{name} = #{s}"
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
## Sexp -> String
|
337
|
+
def process_call(sexp)
|
338
|
+
name = sexp.fetch_as_s(1.i32)
|
339
|
+
arglist_sexp = __arglist_sexp(sexp, 2.i32)
|
340
|
+
o = sexp.fetch(0.i32)
|
341
|
+
new_level = X::BINARY_L[name]
|
342
|
+
unless new_level.nil?
|
343
|
+
return __binary(o, arglist_sexp, name, new_level, 0.i32, 1.i32)
|
344
|
+
end
|
345
|
+
new_level = X::BINARY_R[name]
|
346
|
+
unless new_level.nil?
|
347
|
+
return __binary(o, arglist_sexp, name, new_level, 1.i32, 0.i32)
|
348
|
+
end
|
349
|
+
new_level = X::BINARY_X[name]
|
350
|
+
unless new_level.nil?
|
351
|
+
return __binary(o, arglist_sexp, name, new_level, 1.i32, 1.i32)
|
352
|
+
end
|
353
|
+
case name
|
354
|
+
when "[]"
|
355
|
+
if o.nil?
|
356
|
+
r = "self"
|
357
|
+
else
|
358
|
+
r = __with_level_s(15.i32) { process(o.is_a!(Sexp)) }
|
359
|
+
end
|
360
|
+
a = __process_arglist(arglist_sexp)
|
361
|
+
s = a.join(", ")
|
362
|
+
return "#{r}[#{s}]"
|
363
|
+
when "[]="
|
364
|
+
raise ArgumentError if @level > 1
|
365
|
+
if o.nil?
|
366
|
+
r = "self"
|
367
|
+
else
|
368
|
+
r = __with_level_s(15.i32) { process(o.is_a!(Sexp)) }
|
369
|
+
end
|
370
|
+
n = arglist_sexp.size_as_int32
|
371
|
+
if n == 0
|
372
|
+
raise ArgumentError
|
373
|
+
end
|
374
|
+
n -= 1
|
375
|
+
a = []
|
376
|
+
__with_level(2.i32) do
|
377
|
+
n.times do |i|
|
378
|
+
arg_sexp = arglist_sexp.fetch_as_sexp(i)
|
379
|
+
a.push(process(arg_sexp))
|
380
|
+
end
|
381
|
+
end
|
382
|
+
s = a.join(", ")
|
383
|
+
v = __with_level_s(1.i32) { process(arglist_sexp.fetch_as_sexp(n)) }
|
384
|
+
return "#{r}[#{s}] = #{v}"
|
385
|
+
when "!"
|
386
|
+
return __unary(o, arglist_sexp, "!", 13.i32)
|
387
|
+
when "~"
|
388
|
+
return __unary(o, arglist_sexp, "~", 13.i32)
|
389
|
+
when "+@"
|
390
|
+
return __unary(o, arglist_sexp, "+", 13.i32)
|
391
|
+
when "-@"
|
392
|
+
return __unary(o, arglist_sexp, "-", 13.i32)
|
393
|
+
end
|
394
|
+
if o.nil?
|
395
|
+
f = name
|
396
|
+
else
|
397
|
+
r = __with_level_s(15.i32) { process(o.is_a!(Sexp)) }
|
398
|
+
f = r + "." + name
|
399
|
+
end
|
400
|
+
n = arglist_sexp.size_as_int32
|
401
|
+
if n == 0
|
402
|
+
return f
|
403
|
+
end
|
404
|
+
a = __process_arglist(arglist_sexp)
|
405
|
+
s = a.join(", ")
|
406
|
+
return "#{f}(#{s})"
|
407
|
+
end
|
408
|
+
|
409
|
+
## Sexp -> String
|
410
|
+
def process_case(sexp)
|
411
|
+
c = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
412
|
+
a = []
|
413
|
+
n = sexp.size_as_int32
|
414
|
+
if n == 0
|
415
|
+
raise ArgumentError
|
416
|
+
end
|
417
|
+
n -= 1
|
418
|
+
i = 1.i32
|
419
|
+
while i < n
|
420
|
+
when_sexp = sexp.fetch_as_sexp(i)
|
421
|
+
raise ArgumentError if when_sexp.type != :when
|
422
|
+
array_sexp = when_sexp.fetch_as_sexp(0.i32)
|
423
|
+
raise ArgumentError if array_sexp.type != :array
|
424
|
+
if array_sexp.size_as_int32 != 1
|
425
|
+
raise ArgumentError
|
426
|
+
end
|
427
|
+
w = __with_level_s(2.i32) { process(array_sexp.fetch_as_sexp(0.i32)) }
|
428
|
+
s = __with_depth(when_sexp[1.i32])
|
429
|
+
a.push(__tabs + "when" + " " + w + "\n" + s)
|
430
|
+
i += 1
|
431
|
+
end
|
432
|
+
o = sexp[n]
|
433
|
+
unless o.nil?
|
434
|
+
s = __with_depth(o)
|
435
|
+
a.push(__tabs + "else" + "\n" + s)
|
436
|
+
end
|
437
|
+
return "case" + " " + c + "\n" + a.join + __tabs + "end"
|
438
|
+
end
|
439
|
+
|
440
|
+
## Sexp -> String
|
441
|
+
def process_cdecl(sexp)
|
442
|
+
raise ArgumentError if @level > 1
|
443
|
+
name = sexp.fetch_as_s(0.i32)
|
444
|
+
s = __with_level_s(1.i32) { process(sexp.fetch_as_sexp(1.i32)) }
|
445
|
+
return "#{name} = #{s}"
|
446
|
+
end
|
447
|
+
|
448
|
+
## Sexp -> String
|
449
|
+
def process_class(sexp)
|
450
|
+
raise ArgumentError if @level > 0
|
451
|
+
name = sexp.fetch_as_s(0.i32)
|
452
|
+
s = __process_scope(sexp, 2.i32)
|
453
|
+
o = sexp[1.i32]
|
454
|
+
if o.nil?
|
455
|
+
return "class" + " " + name + "\n" + s + __tabs + "end"
|
456
|
+
else
|
457
|
+
c = __with_level_s(2.i32) { process(o.is_a!(Sexp)) }
|
458
|
+
return "class" + " " + name + " < " + c + "\n" + s + __tabs + "end"
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
## Sexp -> String
|
463
|
+
def process_colon2(sexp)
|
464
|
+
name = sexp.fetch_as_s(1.i32)
|
465
|
+
s = __with_level_s(16.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
466
|
+
return "#{s}::#{name}"
|
467
|
+
end
|
468
|
+
|
469
|
+
## Sexp -> String
|
470
|
+
def process_colon3(sexp)
|
471
|
+
name = sexp.fetch_as_s(0.i32)
|
472
|
+
return "::#{name}"
|
473
|
+
end
|
474
|
+
|
475
|
+
## Sexp -> String
|
476
|
+
def process_const(sexp)
|
477
|
+
name = sexp.fetch_as_s(0.i32)
|
478
|
+
return name
|
479
|
+
end
|
480
|
+
|
481
|
+
## Sexp -> String
|
482
|
+
def process_defn(sexp)
|
483
|
+
raise ArgumentError if @level > 0
|
484
|
+
name = sexp.fetch_as_s(0.i32)
|
485
|
+
s = __process_scope(sexp, 2.i32)
|
486
|
+
args_sexp = sexp.fetch_as_sexp(1.i32)
|
487
|
+
raise ArgumentError if args_sexp.type != :args
|
488
|
+
a = __process_args(args_sexp)
|
489
|
+
if a.empty?
|
490
|
+
return "def" + " " + name + "\n" + s + __tabs + "end"
|
491
|
+
end
|
492
|
+
t = a.join(", ")
|
493
|
+
return "def" + " " + name + "(" + t + ")" + "\n" + s + __tabs + "end"
|
494
|
+
end
|
495
|
+
|
496
|
+
## Sexp -> String
|
497
|
+
def process_ensure(sexp)
|
498
|
+
s0 = __with_depth(sexp[0.i32])
|
499
|
+
s1 = __with_depth(sexp[1.i32])
|
500
|
+
return "begin" + "\n" + s0 + __tabs + "ensure" + "\n" + s1 + __tabs + "end"
|
501
|
+
end
|
502
|
+
|
503
|
+
## Sexp -> String
|
504
|
+
def process_false(sexp)
|
505
|
+
return "false"
|
506
|
+
end
|
507
|
+
|
508
|
+
## Sexp -> String
|
509
|
+
def process_hash(sexp)
|
510
|
+
a = __process_hash(sexp)
|
511
|
+
if @level < 2 && a.size_as_int32 > 0
|
512
|
+
s = a.map { |c| __tabs + "\t" + c + "," + "\n" }.join
|
513
|
+
return "{" + "\n" + s + __tabs + "}"
|
514
|
+
else
|
515
|
+
s = a.join(", ")
|
516
|
+
return "{#{s}}"
|
517
|
+
end
|
518
|
+
end
|
519
|
+
|
520
|
+
## Sexp -> String
|
521
|
+
def process_iasgn(sexp)
|
522
|
+
raise ArgumentError if @level > 1
|
523
|
+
name = sexp.fetch_as_s(0.i32)
|
524
|
+
s = __with_level_s(1.i32) { process(sexp.fetch_as_sexp(1.i32)) }
|
525
|
+
return "#{name} = #{s}"
|
526
|
+
end
|
527
|
+
|
528
|
+
## Sexp -> String
|
529
|
+
def process_if(sexp)
|
530
|
+
then_o = sexp[1.i32]
|
531
|
+
else_o = sexp[2.i32]
|
532
|
+
if @level == 0
|
533
|
+
c = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
534
|
+
if then_o.nil? && else_o.nil?
|
535
|
+
raise ArgumentError
|
536
|
+
end
|
537
|
+
if then_o.nil?
|
538
|
+
s1 = __with_depth(else_o)
|
539
|
+
return "unless" + " " + c + "\n" + s1 + __tabs + "end"
|
540
|
+
end
|
541
|
+
if else_o.nil?
|
542
|
+
s0 = __with_depth(then_o)
|
543
|
+
return "if" + " " + c + "\n" + s0 + __tabs + "end"
|
544
|
+
end
|
545
|
+
s0 = __with_depth(then_o)
|
546
|
+
else_sexp = else_o.is_a!(Sexp)
|
547
|
+
if else_sexp.type == :if
|
548
|
+
if else_sexp[1.i32].not_nil?
|
549
|
+
s1 = process_if(else_sexp)
|
550
|
+
return "if" + " " + c + "\n" + s0 + __tabs + "els" + s1
|
551
|
+
end
|
552
|
+
end
|
553
|
+
s1 = __with_depth(else_o)
|
554
|
+
return "if" + " " + c + "\n" + s0 + __tabs + "else" + "\n" + s1 + __tabs + "end"
|
555
|
+
else
|
556
|
+
c = __with_level_s(3.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
557
|
+
s0 = __with_level_s(2.i32) { process(then_o.is_a!(Sexp)) }
|
558
|
+
s1 = __with_level_s(2.i32) { process(else_o.is_a!(Sexp)) }
|
559
|
+
s = c + " " + "?" + " " + s0 + " " + ":" + " " + s1
|
560
|
+
return @level > 2 ? "(#{s})" : s
|
561
|
+
end
|
562
|
+
end
|
563
|
+
|
564
|
+
## Sexp -> String
|
565
|
+
def process_iter(sexp)
|
566
|
+
c = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
567
|
+
s = __with_depth(sexp[2.i32])
|
568
|
+
o = sexp.fetch(1.i32)
|
569
|
+
if o.nil?
|
570
|
+
return c + " " + "{" + "\n" + s + __tabs + "}"
|
571
|
+
else
|
572
|
+
args_sexp = o.is_a!(Sexp)
|
573
|
+
case args_sexp.type
|
574
|
+
when :args
|
575
|
+
a = __process_args(args_sexp)
|
576
|
+
if a.empty?
|
577
|
+
return c + " " + "{" + "\n" + s + __tabs + "}"
|
578
|
+
end
|
579
|
+
when :lasgn
|
580
|
+
a = []
|
581
|
+
name = args_sexp.fetch_as_s(0.i32)
|
582
|
+
a.push(name)
|
583
|
+
when :masgn
|
584
|
+
a = []
|
585
|
+
array_sexp = args_sexp.fetch_as_sexp(0.i32)
|
586
|
+
raise ArgumentError if array_sexp.type != :array
|
587
|
+
n = array_sexp.size_as_int32
|
588
|
+
n.times do |i|
|
589
|
+
name = array_sexp.fetch_as_sexp(i).fetch_as_s(0.i32)
|
590
|
+
a.push(name)
|
591
|
+
end
|
592
|
+
else
|
593
|
+
raise ArgumentError
|
594
|
+
end
|
595
|
+
t = a.join(", ")
|
596
|
+
return c + " " + "{" + " " + "|#{t}|" + "\n" + s + __tabs + "}"
|
597
|
+
end
|
598
|
+
end
|
599
|
+
|
600
|
+
## Sexp -> String
|
601
|
+
def process_ivar(sexp)
|
602
|
+
name = sexp.fetch_as_s(0.i32)
|
603
|
+
return name
|
604
|
+
end
|
605
|
+
|
606
|
+
## Sexp -> String
|
607
|
+
def process_lasgn(sexp)
|
608
|
+
raise ArgumentError if @level > 1
|
609
|
+
name = sexp.fetch_as_s(0.i32)
|
610
|
+
arg_sexp = sexp.fetch_as_sexp(1.i32)
|
611
|
+
s = __with_level_s(1.i32) { process(arg_sexp) }
|
612
|
+
return "#{name} = #{s}"
|
613
|
+
end
|
614
|
+
|
615
|
+
## Sexp -> String
|
616
|
+
def process_lit(sexp)
|
617
|
+
return sexp.fetch(0.i32).inspect
|
618
|
+
end
|
619
|
+
|
620
|
+
## Sexp -> String
|
621
|
+
def process_lvar(sexp)
|
622
|
+
name = sexp.fetch_as_s(0.i32)
|
623
|
+
return name
|
624
|
+
end
|
625
|
+
|
626
|
+
## Sexp -> String
|
627
|
+
def process_module(sexp)
|
628
|
+
raise ArgumentError if @level > 0
|
629
|
+
name = sexp.fetch_as_s(0.i32)
|
630
|
+
s = __process_scope(sexp, 1.i32)
|
631
|
+
return "module" + " " + name + "\n" + s + __tabs + "end"
|
632
|
+
end
|
633
|
+
|
634
|
+
## Sexp -> String
|
635
|
+
def process_nil(sexp)
|
636
|
+
return "nil"
|
637
|
+
end
|
638
|
+
|
639
|
+
## Sexp -> String
|
640
|
+
def process_op_asgn1(sexp)
|
641
|
+
raise ArgumentError if @level > 1
|
642
|
+
r = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
643
|
+
arg_sexp = sexp.fetch_as_sexp(1.i32)
|
644
|
+
if arg_sexp.type == :arglist
|
645
|
+
a = __process_arglist(arg_sexp)
|
646
|
+
t = a.join(", ")
|
647
|
+
else
|
648
|
+
t = __with_level_s(2.i32) { process(arg_sexp) }
|
649
|
+
end
|
650
|
+
operator = sexp.fetch_as_s(2.i32)
|
651
|
+
s = __with_level_s(1.i32) { process(sexp.fetch_as_sexp(3.i32)) }
|
652
|
+
return "#{r}[#{t}] #{operator}= #{s}"
|
653
|
+
end
|
654
|
+
|
655
|
+
## Sexp -> String
|
656
|
+
def process_op_asgn2(sexp)
|
657
|
+
raise ArgumentError if @level > 1
|
658
|
+
r = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
659
|
+
name = sexp.fetch_as_s(1.i32)
|
660
|
+
raise ArgumentError unless name.end_with?("=")
|
661
|
+
name = name.chop
|
662
|
+
operator = sexp.fetch_as_s(2.i32)
|
663
|
+
s = __with_level_s(1.i32) { process(sexp.fetch_as_sexp(3.i32)) }
|
664
|
+
return "#{r}.#{name} #{operator}= #{s}"
|
665
|
+
end
|
666
|
+
|
667
|
+
## Sexp -> String
|
668
|
+
def process_op_asgn_and(sexp)
|
669
|
+
raise ArgumentError if @level > 1
|
670
|
+
lasgn_sexp = sexp.fetch_as_sexp(1.i32)
|
671
|
+
raise ArgumentError if lasgn_sexp.type != :lasgn
|
672
|
+
name = lasgn_sexp.fetch_as_s(0.i32)
|
673
|
+
v = __with_level_s(1.i32) { process(lasgn_sexp.fetch_as_sexp(1.i32)) }
|
674
|
+
return "#{name} &&= #{v}"
|
675
|
+
end
|
676
|
+
|
677
|
+
## Sexp -> String
|
678
|
+
def process_op_asgn_or(sexp)
|
679
|
+
raise ArgumentError if @level > 1
|
680
|
+
lasgn_sexp = sexp.fetch_as_sexp(1.i32)
|
681
|
+
raise ArgumentError if lasgn_sexp.type != :lasgn
|
682
|
+
name = lasgn_sexp.fetch_as_s(0.i32)
|
683
|
+
v = __with_level_s(1.i32) { process(lasgn_sexp.fetch_as_sexp(1.i32)) }
|
684
|
+
return "#{name} ||= #{v}"
|
685
|
+
end
|
686
|
+
|
687
|
+
## Sexp -> String
|
688
|
+
def process_rescue(sexp)
|
689
|
+
raise ArgumentError if @level > 0
|
690
|
+
n = sexp.size_as_int32
|
691
|
+
a = []
|
692
|
+
i = 1.i32
|
693
|
+
while i < n
|
694
|
+
resbody_sexp = sexp.fetch_as_sexp(i)
|
695
|
+
raise ArgumentError if resbody_sexp.type != :resbody
|
696
|
+
array_sexp = resbody_sexp.fetch_as_sexp(0.i32)
|
697
|
+
raise ArgumentError if array_sexp.type != :array
|
698
|
+
if array_sexp.size_as_int32 != 1
|
699
|
+
raise ArgumentError
|
700
|
+
end
|
701
|
+
r = __with_level_s(2.i32) { process(array_sexp.fetch_as_sexp(0.i32)) }
|
702
|
+
s = __with_depth(resbody_sexp[1.i32])
|
703
|
+
a.push(__tabs + "rescue" + " " + r + "\n" + s)
|
704
|
+
i += 1
|
705
|
+
end
|
706
|
+
s = __with_depth(sexp[0.i32])
|
707
|
+
return "begin" + "\n" + s + a.join + __tabs + "end"
|
708
|
+
end
|
709
|
+
|
710
|
+
## Sexp -> String
|
711
|
+
def process_return(sexp)
|
712
|
+
raise ArgumentError if @level > 0
|
713
|
+
case sexp.size_as_int32
|
714
|
+
when 0.i32
|
715
|
+
return "return"
|
716
|
+
when 1.i32
|
717
|
+
s = __with_level_s(1.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
718
|
+
return "return" + " " + s
|
719
|
+
else
|
720
|
+
raise ArgumentError
|
721
|
+
end
|
722
|
+
end
|
723
|
+
|
724
|
+
## Sexp -> String
|
725
|
+
def process_sclass(sexp)
|
726
|
+
raise ArgumentError if @level > 0
|
727
|
+
c = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
728
|
+
s = __process_scope(sexp, 1.i32)
|
729
|
+
return "class" + " << " + c + "\n" + s + __tabs + "end"
|
730
|
+
end
|
731
|
+
|
732
|
+
## Sexp -> String
|
733
|
+
def process_self(sexp)
|
734
|
+
return "self"
|
735
|
+
end
|
736
|
+
|
737
|
+
## Sexp -> String
|
738
|
+
def process_str(sexp)
|
739
|
+
return sexp.fetch(0.i32).is_a!(String).inspect
|
740
|
+
end
|
741
|
+
|
742
|
+
## Sexp -> String
|
743
|
+
def process_super(sexp)
|
744
|
+
a = __process_arglist(sexp)
|
745
|
+
s = a.join(", ")
|
746
|
+
return "super" + "(#{s})"
|
747
|
+
end
|
748
|
+
|
749
|
+
## Sexp -> String
|
750
|
+
def process_true(sexp)
|
751
|
+
return "true"
|
752
|
+
end
|
753
|
+
|
754
|
+
## Sexp -> String
|
755
|
+
def process_until(sexp)
|
756
|
+
raise ArgumentError if @level > 0
|
757
|
+
c = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
758
|
+
s = __with_depth(sexp[1.i32])
|
759
|
+
if sexp.fetch(2.i32)
|
760
|
+
return "until" + " " + c + "\n" + s + __tabs + "end"
|
761
|
+
else
|
762
|
+
return "begin" + "\n" + s + __tabs + "end" + " " + "until" + " " + c
|
763
|
+
end
|
764
|
+
end
|
765
|
+
|
766
|
+
## Sexp -> String
|
767
|
+
def process_while(sexp)
|
768
|
+
raise ArgumentError if @level > 0
|
769
|
+
c = __with_level_s(2.i32) { process(sexp.fetch_as_sexp(0.i32)) }
|
770
|
+
s = __with_depth(sexp[1.i32])
|
771
|
+
if sexp.fetch(2.i32)
|
772
|
+
return "while" + " " + c + "\n" + s + __tabs + "end"
|
773
|
+
else
|
774
|
+
return "begin" + "\n" + s + __tabs + "end" + " " + "while" + " " + c
|
775
|
+
end
|
776
|
+
end
|
777
|
+
|
778
|
+
## Sexp -> String
|
779
|
+
def process_yield(sexp)
|
780
|
+
a = __process_arglist(sexp)
|
781
|
+
if a.empty?
|
782
|
+
return "yield"
|
783
|
+
end
|
784
|
+
s = a.join(", ")
|
785
|
+
return "yield" + "(#{s})"
|
786
|
+
end
|
787
|
+
end
|
788
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module MetaRuby
|
2
|
+
## <>
|
3
|
+
## @__comments: Array<String>?
|
4
|
+
class Sexp < ::Sexp
|
5
|
+
## Symbol -> void
|
6
|
+
## Symbol, Array<java.lang.Object?> -> void
|
7
|
+
def initialize(type, args = nix)
|
8
|
+
if args.nix?
|
9
|
+
super(type)
|
10
|
+
else
|
11
|
+
super(type, args)
|
12
|
+
end
|
13
|
+
@__comments = nil
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
# ===
|
18
|
+
|
19
|
+
## int32 -> String
|
20
|
+
## Fixnum -> String
|
21
|
+
def fetch_as_s(i)
|
22
|
+
o = fetch(i)
|
23
|
+
case o
|
24
|
+
when String
|
25
|
+
return o
|
26
|
+
when Symbol
|
27
|
+
return o.to_s
|
28
|
+
else
|
29
|
+
raise TypeError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
## int32 -> Sexp
|
34
|
+
## Fixnum -> Sexp
|
35
|
+
def fetch_as_sexp(i)
|
36
|
+
return fetch(i).is_a!(Sexp)
|
37
|
+
end
|
38
|
+
|
39
|
+
## -> Array<String>?
|
40
|
+
def comments
|
41
|
+
return @__comments
|
42
|
+
end
|
43
|
+
|
44
|
+
## Array<String>? -> void
|
45
|
+
def comments=(o)
|
46
|
+
@__comments = o
|
47
|
+
return
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "meta-ruby"
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meta-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- InfraRuby Vision
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: infraruby-shim
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: s-expression
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: fab
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: infraruby-task
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Ruby metaprogramming with s-expressions
|
84
|
+
email: rubygems@infraruby.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- Gemfile
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- bin/meta-hello
|
94
|
+
- lib/meta-ruby.rb
|
95
|
+
- meta-ruby.gemspec
|
96
|
+
- pool/meta-ruby.cut
|
97
|
+
- pool/meta-ruby.jar
|
98
|
+
- ruby/MetaRuby.rb
|
99
|
+
- ruby/MetaRuby/Directory.rb
|
100
|
+
- ruby/MetaRuby/Generator.rb
|
101
|
+
- ruby/MetaRuby/Processor.rb
|
102
|
+
- ruby/MetaRuby/Processor/SourceProcessor.rb
|
103
|
+
- ruby/MetaRuby/Sexp.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: http://infraruby.com/
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Ruby metaprogramming with s-expressions
|
129
|
+
test_files: []
|