gob-lang 1.0.0-aarch64-linux
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/README.md +17 -0
- data/exe/gob +8 -0
- data/exe/stree-go +12 -0
- data/ext/gipper/gipper.go +172 -0
- data/ext/gipper/libgipper.h +95 -0
- data/ext/gipper/libgipper.so +0 -0
- data/ext/goformatter/format/LICENSE +27 -0
- data/ext/goformatter/format/format.go +132 -0
- data/ext/goformatter/format/internal.go +174 -0
- data/ext/goformatter/go.mod +3 -0
- data/ext/goformatter/libgoformatter.h +95 -0
- data/ext/goformatter/libgoformatter.so +0 -0
- data/ext/goformatter/main.go +34 -0
- data/gob-lang.gemspec +57 -0
- data/lib/gipper.rb +24 -0
- data/lib/go_formatter.rb +33 -0
- data/lib/gob/cli/base.rb +53 -0
- data/lib/gob/cli/commands/build.rb +29 -0
- data/lib/gob/cli/commands/fmt.rb +31 -0
- data/lib/gob/cli/commands/run.rb +21 -0
- data/lib/gob/cli/commands/transpile.rb +20 -0
- data/lib/gob/cli/commands/version.rb +19 -0
- data/lib/gob/cli/commands/vet.rb +33 -0
- data/lib/gob/cli/errors.rb +50 -0
- data/lib/gob/cli.rb +53 -0
- data/lib/gob/go/dsl.rb +196 -0
- data/lib/gob/go/printer.rb +412 -0
- data/lib/gob/go/runner.rb +42 -0
- data/lib/gob/matcher.rb +34 -0
- data/lib/gob/transpilation/rules/core.rb +1184 -0
- data/lib/gob/transpiler.rb +815 -0
- data/lib/gob/validation/offence.rb +24 -0
- data/lib/gob/validation/rules/class_rules.rb +78 -0
- data/lib/gob/validation/rules/config.yml +16 -0
- data/lib/gob/validation/rules/cop.rb +15 -0
- data/lib/gob/validation/rules/function_rules.rb +33 -0
- data/lib/gob/validation/rules/module_rules.rb +78 -0
- data/lib/gob/validation/rules/program_rules.rb +72 -0
- data/lib/gob/validation/rules.rb +93 -0
- data/lib/gob/version.rb +5 -0
- data/lib/gob.rb +41 -0
- data/lib/syntax_tree/go/format.rb +176 -0
- data/lib/syntax_tree/go/json_visitor.rb +34 -0
- data/lib/syntax_tree/go/match_visitor.rb +103 -0
- data/lib/syntax_tree/go/materializer.rb +92 -0
- data/lib/syntax_tree/go/node.rb +50 -0
- data/lib/syntax_tree/go/nodes.rb +2147 -0
- data/lib/syntax_tree/go/pretty_print_visitor.rb +76 -0
- data/lib/syntax_tree/go/schema.json +1455 -0
- data/lib/syntax_tree/go/visitor.rb +71 -0
- data/lib/syntax_tree/go.rb +55 -0
- data/lib/thor/help_header.rb +23 -0
- metadata +292 -0
|
@@ -0,0 +1,1184 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Transpilation
|
|
5
|
+
module Rules
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def const_go_name(name)
|
|
9
|
+
s = name.to_s
|
|
10
|
+
return s unless s.match?(/\A[A-Z0-9_]+\z/)
|
|
11
|
+
parts = s.split("_").reject(&:empty?)
|
|
12
|
+
return s.downcase if parts.length <= 1
|
|
13
|
+
parts.first.downcase + parts.drop(1).map { |p| p.capitalize }.join
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def register(matcher)
|
|
17
|
+
matcher.rule(:program) do |node|
|
|
18
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Program)
|
|
19
|
+
|
|
20
|
+
body = visit_all(node.statements.body)
|
|
21
|
+
# Flatten: imports collected, funcs/decls collected
|
|
22
|
+
decls = []
|
|
23
|
+
imports = []
|
|
24
|
+
package = "main"
|
|
25
|
+
|
|
26
|
+
body.flatten.each do |item|
|
|
27
|
+
case item
|
|
28
|
+
when SyntaxTree::Go::GenDecl
|
|
29
|
+
if item.tok.to_s == "import"
|
|
30
|
+
imports.concat(item.specs)
|
|
31
|
+
else
|
|
32
|
+
decls << item
|
|
33
|
+
end
|
|
34
|
+
when SyntaxTree::Go::FuncDecl, SyntaxTree::Go::DeclStmt
|
|
35
|
+
decls << item
|
|
36
|
+
when Hash
|
|
37
|
+
# metadata from module/import handling
|
|
38
|
+
package = item[:package] if item[:package]
|
|
39
|
+
decls.concat(Array(item[:decls]))
|
|
40
|
+
imports.concat(Array(item[:imports]))
|
|
41
|
+
when SyntaxTree::Go::Node
|
|
42
|
+
decls << item
|
|
43
|
+
when nil
|
|
44
|
+
# skip
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
merged_decls = []
|
|
49
|
+
unless imports.empty?
|
|
50
|
+
merged_decls << SyntaxTree::Go::GenDecl.new(
|
|
51
|
+
doc: nil, tok_pos: nil, tok: "import", lparen: nil,
|
|
52
|
+
specs: imports, rparen: nil
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
merged_decls.concat(decls)
|
|
56
|
+
|
|
57
|
+
SyntaxTree::Go::File.new(
|
|
58
|
+
doc: nil,
|
|
59
|
+
package: nil,
|
|
60
|
+
name: ident(package),
|
|
61
|
+
decls: merged_decls,
|
|
62
|
+
file_start: nil,
|
|
63
|
+
file_end: nil,
|
|
64
|
+
scope: nil,
|
|
65
|
+
imports: imports,
|
|
66
|
+
unresolved: [],
|
|
67
|
+
comments: [],
|
|
68
|
+
go_version: nil
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
matcher.rule(:comment) do |node|
|
|
73
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Comment)
|
|
74
|
+
|
|
75
|
+
Matcher::DROP
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
matcher.rule(:void_stmt) do |node|
|
|
79
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::VoidStmt)
|
|
80
|
+
|
|
81
|
+
Matcher::DROP
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
matcher.rule(:begin_block) do |node|
|
|
85
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Begin)
|
|
86
|
+
bs = node.bodystmt
|
|
87
|
+
if bs.is_a?(SyntaxTree::BodyStmt)
|
|
88
|
+
next Matcher::MISS if bs.rescue_clause || bs.else_clause || bs.ensure_clause
|
|
89
|
+
end
|
|
90
|
+
stmts = scoped { visit_all(stmt_body(bs)).map { |s| stmtify(s) } }
|
|
91
|
+
block_stmt(list: stmts)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
matcher.rule(:require) do |node|
|
|
95
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Command)
|
|
96
|
+
next Matcher::MISS unless node.message.is_a?(SyntaxTree::Ident) && node.message.value == "require"
|
|
97
|
+
|
|
98
|
+
arg = node.arguments.parts.first
|
|
99
|
+
path = string_value(arg)
|
|
100
|
+
next Matcher::MISS unless path
|
|
101
|
+
|
|
102
|
+
gen_decl(tok: "import", specs: [import_spec(path: path)])
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
matcher.rule(:command) do |node|
|
|
106
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Command)
|
|
107
|
+
|
|
108
|
+
name = node.message.is_a?(SyntaxTree::Ident) ? node.message.value : node.message.to_s
|
|
109
|
+
next Matcher::DROP if %w[private public protected module_function].include?(name)
|
|
110
|
+
next Matcher::MISS if %w[define_const define_var const var].include?(name)
|
|
111
|
+
|
|
112
|
+
args = visit_args(node.arguments)
|
|
113
|
+
call_expr(fun: ident(name), args: args)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
matcher.rule(:define_const) do |node|
|
|
117
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Command)
|
|
118
|
+
next Matcher::MISS unless node.message.is_a?(SyntaxTree::Ident) && %w[define_const const].include?(node.message.value)
|
|
119
|
+
|
|
120
|
+
parts = node.arguments.is_a?(SyntaxTree::Args) ? node.arguments.parts : Array(node.arguments)
|
|
121
|
+
name_node = parts[0]
|
|
122
|
+
value_node = parts[1]
|
|
123
|
+
next Matcher::MISS unless name_node && value_node
|
|
124
|
+
|
|
125
|
+
name = case name_node
|
|
126
|
+
when SyntaxTree::SymbolLiteral then name_node.value.value.to_s
|
|
127
|
+
when SyntaxTree::Ident then name_node.value
|
|
128
|
+
when SyntaxTree::StringLiteral then string_value(name_node)
|
|
129
|
+
else name_node.to_s
|
|
130
|
+
end
|
|
131
|
+
go_name = Gob::Transpilation::Rules.const_go_name(name)
|
|
132
|
+
val = visit(value_node)
|
|
133
|
+
decl = SyntaxTree::Go::GenDecl.new(
|
|
134
|
+
doc: nil, tok_pos: nil, tok: "const", lparen: nil,
|
|
135
|
+
specs: [SyntaxTree::Go::ValueSpec.new(doc: nil, names: [ident(go_name)], type: nil, values: [val], comment: nil)],
|
|
136
|
+
rparen: nil
|
|
137
|
+
)
|
|
138
|
+
decl.instance_variable_set(:@location, node.location) rescue nil
|
|
139
|
+
decl
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
matcher.rule(:define_var) do |node|
|
|
143
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Command)
|
|
144
|
+
next Matcher::MISS unless node.message.is_a?(SyntaxTree::Ident) && %w[define_var var].include?(node.message.value)
|
|
145
|
+
|
|
146
|
+
parts = node.arguments.is_a?(SyntaxTree::Args) ? node.arguments.parts : Array(node.arguments)
|
|
147
|
+
name_node = parts[0]
|
|
148
|
+
type_node = parts[1]
|
|
149
|
+
value_node = parts[2]
|
|
150
|
+
next Matcher::MISS unless name_node && type_node
|
|
151
|
+
|
|
152
|
+
name = case name_node
|
|
153
|
+
when SyntaxTree::SymbolLiteral then name_node.value.value.to_s
|
|
154
|
+
when SyntaxTree::Ident then name_node.value
|
|
155
|
+
when SyntaxTree::StringLiteral then string_value(name_node)
|
|
156
|
+
else name_node.to_s
|
|
157
|
+
end
|
|
158
|
+
val = value_node ? visit(value_node) : nil
|
|
159
|
+
spec = SyntaxTree::Go::ValueSpec.new(
|
|
160
|
+
doc: nil, names: [ident(name)], type: type_from_node(type_node), values: val ? [val] : [], comment: nil
|
|
161
|
+
)
|
|
162
|
+
decl = SyntaxTree::Go::GenDecl.new(doc: nil, tok_pos: nil, tok: "var", lparen: nil, specs: [spec], rparen: nil)
|
|
163
|
+
decl.instance_variable_set(:@location, node.location) rescue nil
|
|
164
|
+
decl
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
matcher.rule(:define_const_call) do |node|
|
|
168
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::CallNode)
|
|
169
|
+
next Matcher::MISS unless node.receiver.nil?
|
|
170
|
+
msg = node.message
|
|
171
|
+
name_str = msg.is_a?(SyntaxTree::Ident) ? msg.value : msg.to_s
|
|
172
|
+
next Matcher::MISS unless %w[define_const const].include?(name_str)
|
|
173
|
+
|
|
174
|
+
parts = if node.arguments.is_a?(SyntaxTree::ArgParen)
|
|
175
|
+
node.arguments.arguments.is_a?(SyntaxTree::Args) ? node.arguments.arguments.parts : Array(node.arguments.arguments)
|
|
176
|
+
elsif node.arguments.is_a?(SyntaxTree::Args)
|
|
177
|
+
node.arguments.parts
|
|
178
|
+
else
|
|
179
|
+
Array(node.arguments)
|
|
180
|
+
end
|
|
181
|
+
name_node = parts[0]
|
|
182
|
+
value_node = parts[1]
|
|
183
|
+
next Matcher::MISS unless name_node && value_node
|
|
184
|
+
|
|
185
|
+
name = case name_node
|
|
186
|
+
when SyntaxTree::SymbolLiteral then name_node.value.value.to_s
|
|
187
|
+
when SyntaxTree::Ident then name_node.value
|
|
188
|
+
when SyntaxTree::StringLiteral then string_value(name_node)
|
|
189
|
+
else name_node.to_s
|
|
190
|
+
end
|
|
191
|
+
go_name = Gob::Transpilation::Rules.const_go_name(name)
|
|
192
|
+
val = visit(value_node)
|
|
193
|
+
decl = SyntaxTree::Go::GenDecl.new(
|
|
194
|
+
doc: nil, tok_pos: nil, tok: "const", lparen: nil,
|
|
195
|
+
specs: [SyntaxTree::Go::ValueSpec.new(doc: nil, names: [ident(go_name)], type: nil, values: [val], comment: nil)],
|
|
196
|
+
rparen: nil
|
|
197
|
+
)
|
|
198
|
+
decl.instance_variable_set(:@location, node.location) rescue nil
|
|
199
|
+
decl
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
matcher.rule(:define_var_call) do |node|
|
|
203
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::CallNode)
|
|
204
|
+
next Matcher::MISS unless node.receiver.nil?
|
|
205
|
+
msg = node.message
|
|
206
|
+
name_str = msg.is_a?(SyntaxTree::Ident) ? msg.value : msg.to_s
|
|
207
|
+
next Matcher::MISS unless %w[define_var var].include?(name_str)
|
|
208
|
+
|
|
209
|
+
parts = if node.arguments.is_a?(SyntaxTree::ArgParen)
|
|
210
|
+
node.arguments.arguments.is_a?(SyntaxTree::Args) ? node.arguments.arguments.parts : Array(node.arguments.arguments)
|
|
211
|
+
elsif node.arguments.is_a?(SyntaxTree::Args)
|
|
212
|
+
node.arguments.parts
|
|
213
|
+
else
|
|
214
|
+
Array(node.arguments)
|
|
215
|
+
end
|
|
216
|
+
name_node = parts[0]
|
|
217
|
+
type_node = parts[1]
|
|
218
|
+
value_node = parts[2]
|
|
219
|
+
next Matcher::MISS unless name_node && type_node
|
|
220
|
+
|
|
221
|
+
name = case name_node
|
|
222
|
+
when SyntaxTree::SymbolLiteral then name_node.value.value.to_s
|
|
223
|
+
when SyntaxTree::Ident then name_node.value
|
|
224
|
+
when SyntaxTree::StringLiteral then string_value(name_node)
|
|
225
|
+
else name_node.to_s
|
|
226
|
+
end
|
|
227
|
+
val = value_node ? visit(value_node) : nil
|
|
228
|
+
spec = SyntaxTree::Go::ValueSpec.new(
|
|
229
|
+
doc: nil, names: [ident(name)], type: type_from_node(type_node), values: val ? [val] : [], comment: nil
|
|
230
|
+
)
|
|
231
|
+
decl = SyntaxTree::Go::GenDecl.new(doc: nil, tok_pos: nil, tok: "var", lparen: nil, specs: [spec], rparen: nil)
|
|
232
|
+
decl.instance_variable_set(:@location, node.location) rescue nil
|
|
233
|
+
decl
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
matcher.rule(:class_decl) do |node|
|
|
237
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ClassDeclaration)
|
|
238
|
+
|
|
239
|
+
class_name = const_name(node.constant)
|
|
240
|
+
body = stmt_body(node.bodystmt)
|
|
241
|
+
fields = []
|
|
242
|
+
decls = []
|
|
243
|
+
|
|
244
|
+
body.each do |stmt|
|
|
245
|
+
# attr_accessor :Name, :type # `tag`
|
|
246
|
+
if stmt.is_a?(SyntaxTree::Command) && stmt.message.is_a?(SyntaxTree::Ident) && stmt.message.value == "attr_accessor"
|
|
247
|
+
parts = stmt.arguments.is_a?(SyntaxTree::Args) ? stmt.arguments.parts : Array(stmt.arguments)
|
|
248
|
+
name_node = parts[0]
|
|
249
|
+
type_node = parts[1]
|
|
250
|
+
next unless name_node
|
|
251
|
+
|
|
252
|
+
field_name = case name_node
|
|
253
|
+
when SyntaxTree::SymbolLiteral then name_node.value.value.to_s
|
|
254
|
+
when SyntaxTree::Ident then name_node.value
|
|
255
|
+
when SyntaxTree::StringLiteral then string_value(name_node)
|
|
256
|
+
else name_node.to_s
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# tag from trailing comment
|
|
260
|
+
tag = nil
|
|
261
|
+
if stmt.respond_to?(:comments)
|
|
262
|
+
stmt.comments.each do |c|
|
|
263
|
+
t = c.value.to_s.strip.sub(/^#\s*/, "")
|
|
264
|
+
tag = t if t.start_with?("`") && t.end_with?("`")
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
# also check inline comments on parts
|
|
268
|
+
if !tag
|
|
269
|
+
Array(parts).each do |p|
|
|
270
|
+
next unless p.respond_to?(:comments)
|
|
271
|
+
p.comments.each do |c|
|
|
272
|
+
t = c.value.to_s.strip.sub(/^#\s*/, "")
|
|
273
|
+
tag = t if t.start_with?("`") && t.end_with?("`")
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
tag_lit = tag ? SyntaxTree::Go::BasicLit.new(kind: "STRING", value: tag) : nil
|
|
279
|
+
if type_node.nil?
|
|
280
|
+
# embedding: attr_accessor :User
|
|
281
|
+
fields << SyntaxTree::Go::Field.new(
|
|
282
|
+
doc: nil, names: [], type: ident(field_name), tag: tag_lit, comment: nil
|
|
283
|
+
)
|
|
284
|
+
else
|
|
285
|
+
fields << SyntaxTree::Go::Field.new(
|
|
286
|
+
doc: nil, names: [ident(field_name)], type: type_from_node(type_node), tag: tag_lit, comment: nil
|
|
287
|
+
)
|
|
288
|
+
end
|
|
289
|
+
next
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
if stmt.is_a?(SyntaxTree::DefNode)
|
|
293
|
+
method_name = stmt.name.value
|
|
294
|
+
param_names = param_name_list(stmt.params)
|
|
295
|
+
sig = signature_for_def(stmt)
|
|
296
|
+
|
|
297
|
+
if method_name == "initialize"
|
|
298
|
+
# constructor: func NewClassName(...) *ClassName
|
|
299
|
+
body_stmts = with_class_self(class_name) do
|
|
300
|
+
scoped(param_names) do
|
|
301
|
+
visit_all(stmt_body(stmt.bodystmt)).map { |s| stmtify(s) }
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
# replace self = &ClassName{}? We'll build it: self := &ClassName{} at start, return self at end
|
|
305
|
+
init_self = SyntaxTree::Go::AssignStmt.new(
|
|
306
|
+
lhs: [ident("self")], tok: ":=", rhs: [
|
|
307
|
+
SyntaxTree::Go::UnaryExpr.new(op: "&", x: SyntaxTree::Go::CompositeLit.new(
|
|
308
|
+
type: ident(class_name), lbrace: nil, elts: [], rbrace: nil, incomplete: false
|
|
309
|
+
))
|
|
310
|
+
]
|
|
311
|
+
)
|
|
312
|
+
ret_self = SyntaxTree::Go::ReturnStmt.new(return_: nil, results: [ident("self")])
|
|
313
|
+
ctor_body = [init_self] + body_stmts + [ret_self]
|
|
314
|
+
params = param_names.each_with_index.map do |n, i|
|
|
315
|
+
t = sig && sig[:params][i] ? parse_go_type(sig[:params][i]) : nil
|
|
316
|
+
SyntaxTree::Go::Field.new(names: [ident(n)], type: t)
|
|
317
|
+
end
|
|
318
|
+
decls << func_decl(
|
|
319
|
+
name: "New#{class_name}",
|
|
320
|
+
params: params,
|
|
321
|
+
body: ctor_body,
|
|
322
|
+
results: field_list(list: [SyntaxTree::Go::Field.new(names: [], type: SyntaxTree::Go::StarExpr.new(star: nil, x: ident(class_name)))])
|
|
323
|
+
)
|
|
324
|
+
next
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
body_stmts = with_class_self(class_name) do
|
|
328
|
+
scoped(param_names) do
|
|
329
|
+
visit_all(stmt_body(stmt.bodystmt)).map { |s| stmtify(s) }
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
params = param_names.each_with_index.map do |n, i|
|
|
333
|
+
t = sig && sig[:params][i] ? parse_go_type(sig[:params][i]) : nil
|
|
334
|
+
SyntaxTree::Go::Field.new(names: [ident(n)], type: t)
|
|
335
|
+
end
|
|
336
|
+
results = if sig && sig[:returns] && !sig[:returns].empty?
|
|
337
|
+
field_list(list: sig[:returns].map { |r| SyntaxTree::Go::Field.new(names: [], type: parse_go_type(r)) })
|
|
338
|
+
else
|
|
339
|
+
nil
|
|
340
|
+
end
|
|
341
|
+
recv = field_list(list: [SyntaxTree::Go::Field.new(names: [ident("self")], type: SyntaxTree::Go::StarExpr.new(star: nil, x: ident(class_name)))])
|
|
342
|
+
decls << func_decl(name: method_name, params: params, body: body_stmts, results: results, recv: recv)
|
|
343
|
+
next
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# other statements: define_const/define_var handled by normal rules if visited
|
|
347
|
+
visited = visit(stmt)
|
|
348
|
+
next if visited.nil?
|
|
349
|
+
Array(visited).each do |v|
|
|
350
|
+
decls << v if v
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
struct_type = SyntaxTree::Go::StructType.new(struct: nil, fields: field_list(list: fields), incomplete: false)
|
|
355
|
+
type_spec = SyntaxTree::Go::TypeSpec.new(doc: nil, name: ident(class_name), type_params: nil, assign: nil, type: struct_type, comment: nil)
|
|
356
|
+
type_decl = SyntaxTree::Go::GenDecl.new(doc: nil, tok_pos: nil, tok: "type", lparen: nil, specs: [type_spec], rparen: nil)
|
|
357
|
+
|
|
358
|
+
[type_decl] + decls
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
matcher.rule(:case_node) do |node|
|
|
362
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Case)
|
|
363
|
+
|
|
364
|
+
build_case = lambda do |when_node|
|
|
365
|
+
clauses = []
|
|
366
|
+
cur = when_node
|
|
367
|
+
while cur
|
|
368
|
+
if cur.is_a?(SyntaxTree::When)
|
|
369
|
+
args = cur.arguments.is_a?(SyntaxTree::Args) ? cur.arguments.parts : Array(cur.arguments)
|
|
370
|
+
list = visit_all(args)
|
|
371
|
+
body = visit_all(stmt_body(cur.statements)).map { |s| stmtify(s) }
|
|
372
|
+
clauses << SyntaxTree::Go::CaseClause.new(case_: nil, list: list, colon: nil, body: body)
|
|
373
|
+
cur = cur.consequent
|
|
374
|
+
elsif cur.is_a?(SyntaxTree::Else)
|
|
375
|
+
body = visit_all(stmt_body(cur.statements)).map { |s| stmtify(s) }
|
|
376
|
+
clauses << SyntaxTree::Go::CaseClause.new(case_: nil, list: [], colon: nil, body: body)
|
|
377
|
+
cur = nil
|
|
378
|
+
else
|
|
379
|
+
cur = nil
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
clauses
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
clauses = build_case.call(node.consequent)
|
|
386
|
+
body = SyntaxTree::Go::BlockStmt.new(lbrace: nil, list: clauses, rbrace: nil)
|
|
387
|
+
|
|
388
|
+
if node.value.is_a?(SyntaxTree::Assign)
|
|
389
|
+
# type switch: case d = decl
|
|
390
|
+
asn = node.value
|
|
391
|
+
target = assign_target_name(asn.target, self)
|
|
392
|
+
target_ident = target.is_a?(String) ? ident(target) : (target.is_a?(SyntaxTree::Go::Node) ? target : visit(target))
|
|
393
|
+
rhs = visit(asn.value)
|
|
394
|
+
type_assert = SyntaxTree::Go::TypeAssertExpr.new(x: rhs, lparen: nil, type: nil, rparen: nil)
|
|
395
|
+
assign = SyntaxTree::Go::AssignStmt.new(lhs: [target_ident], tok_pos: nil, tok: ":=", rhs: [type_assert])
|
|
396
|
+
next SyntaxTree::Go::TypeSwitchStmt.new(switch: nil, init: nil, assign: assign, body: body)
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
tag = node.value ? visit(node.value) : nil
|
|
400
|
+
SyntaxTree::Go::SwitchStmt.new(switch: nil, init: nil, tag: tag, body: body)
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
matcher.rule(:while_node) do |node|
|
|
404
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::WhileNode)
|
|
405
|
+
|
|
406
|
+
cond = visit(node.predicate)
|
|
407
|
+
body = visit_all(stmt_body(node.statements)).map { |s| stmtify(s) }
|
|
408
|
+
SyntaxTree::Go::ForStmt.new(for_: nil, init: nil, cond: cond, post: nil, body: block_stmt(list: body))
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
matcher.rule(:until_node) do |node|
|
|
412
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::UntilNode)
|
|
413
|
+
|
|
414
|
+
cond = visit(node.predicate)
|
|
415
|
+
cond = SyntaxTree::Go::UnaryExpr.new(op_pos: nil, op: "!".to_sym, x: SyntaxTree::Go::ParenExpr.new(lparen: nil, x: cond, rparen: nil))
|
|
416
|
+
body = visit_all(stmt_body(node.statements)).map { |s| stmtify(s) }
|
|
417
|
+
SyntaxTree::Go::ForStmt.new(for_: nil, init: nil, cond: cond, post: nil, body: block_stmt(list: body))
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
matcher.rule(:break_node) do |node|
|
|
421
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Break)
|
|
422
|
+
|
|
423
|
+
SyntaxTree::Go::BranchStmt.new(tok_pos: nil, tok: "break", label: nil)
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
matcher.rule(:next_node) do |node|
|
|
427
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Next)
|
|
428
|
+
|
|
429
|
+
SyntaxTree::Go::BranchStmt.new(tok_pos: nil, tok: "continue", label: nil)
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
matcher.rule(:yield_node) do |node|
|
|
433
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::YieldNode)
|
|
434
|
+
|
|
435
|
+
args = node.arguments ? visit_args(node.arguments) : []
|
|
436
|
+
call_expr(fun: ident("yield"), args: args)
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
matcher.rule(:module_main) do |node|
|
|
440
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ModuleDeclaration)
|
|
441
|
+
|
|
442
|
+
name = const_name(node.constant)
|
|
443
|
+
body_nodes = visit_all(stmt_body(node.bodystmt))
|
|
444
|
+
|
|
445
|
+
decls = []
|
|
446
|
+
imports = []
|
|
447
|
+
body_nodes.flatten.each do |item|
|
|
448
|
+
case item
|
|
449
|
+
when SyntaxTree::Go::GenDecl
|
|
450
|
+
if item.tok.to_s == "import"
|
|
451
|
+
imports.concat(item.specs)
|
|
452
|
+
else
|
|
453
|
+
decls << item
|
|
454
|
+
end
|
|
455
|
+
when SyntaxTree::Go::Node
|
|
456
|
+
decls << item
|
|
457
|
+
when Hash
|
|
458
|
+
decls.concat(Array(item[:decls]))
|
|
459
|
+
imports.concat(Array(item[:imports]))
|
|
460
|
+
end
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
{ package: name.downcase, decls: decls, imports: imports }
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
matcher.rule(:def_node) do |node|
|
|
467
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::DefNode)
|
|
468
|
+
|
|
469
|
+
name = node.name.value
|
|
470
|
+
param_names = param_name_list(node.params)
|
|
471
|
+
sig = signature_for_def(node)
|
|
472
|
+
|
|
473
|
+
body_stmts = scoped(param_names) do
|
|
474
|
+
visit_all(stmt_body(node.bodystmt)).map { |s| stmtify(s) }
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
params = param_names.each_with_index.map do |n, i|
|
|
478
|
+
t = sig && sig[:params][i] ? parse_go_type(sig[:params][i]) : nil
|
|
479
|
+
SyntaxTree::Go::Field.new(names: [ident(n)], type: t)
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
results = if sig && sig[:returns] && !sig[:returns].empty?
|
|
483
|
+
field_list(list: sig[:returns].map { |r| SyntaxTree::Go::Field.new(names: [], type: parse_go_type(r)) })
|
|
484
|
+
else
|
|
485
|
+
nil
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
func_decl(name: name, params: params, body: body_stmts, results: results)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
matcher.rule(:call_node) do |node|
|
|
492
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::CallNode)
|
|
493
|
+
|
|
494
|
+
msg = node.message
|
|
495
|
+
msg_name = if msg.is_a?(SyntaxTree::Const) || msg.is_a?(SyntaxTree::Ident)
|
|
496
|
+
msg.value
|
|
497
|
+
elsif msg.respond_to?(:value)
|
|
498
|
+
msg.value
|
|
499
|
+
else
|
|
500
|
+
msg.to_s
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
# make(Type, ...) / new(Type) - type arg as first argument
|
|
504
|
+
if node.receiver.nil? && %w[make new].include?(msg_name)
|
|
505
|
+
raw_args = if node.arguments.is_a?(SyntaxTree::ArgParen)
|
|
506
|
+
node.arguments.arguments.is_a?(SyntaxTree::Args) ? node.arguments.arguments.parts : Array(node.arguments.arguments)
|
|
507
|
+
elsif node.arguments.is_a?(SyntaxTree::Args)
|
|
508
|
+
node.arguments.parts
|
|
509
|
+
else
|
|
510
|
+
Array(node.arguments)
|
|
511
|
+
end
|
|
512
|
+
if raw_args[0]
|
|
513
|
+
type_arg = type_from_node(raw_args[0])
|
|
514
|
+
rest = raw_args[1..].map { |a| visit(a) }
|
|
515
|
+
next SyntaxTree::Go::CallExpr.new(fun: ident(msg_name), lparen: nil, args: [type_arg] + rest, ellipsis: nil, rparen: nil)
|
|
516
|
+
end
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
# addr(x) => &x
|
|
520
|
+
if node.receiver.nil? && msg_name == "addr"
|
|
521
|
+
args = visit_args(node.arguments)
|
|
522
|
+
if args.length == 1
|
|
523
|
+
next SyntaxTree::Go::UnaryExpr.new(op_pos: nil, op: "&", x: args.first)
|
|
524
|
+
end
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
# slice(x, low, high) => x[low:high] (nil/omitted for missing)
|
|
528
|
+
if node.receiver.nil? && msg_name == "slice"
|
|
529
|
+
raw = if node.arguments.is_a?(SyntaxTree::ArgParen)
|
|
530
|
+
node.arguments.arguments.is_a?(SyntaxTree::Args) ? node.arguments.arguments.parts : Array(node.arguments.arguments)
|
|
531
|
+
elsif node.arguments.is_a?(SyntaxTree::Args)
|
|
532
|
+
node.arguments.parts
|
|
533
|
+
else
|
|
534
|
+
Array(node.arguments)
|
|
535
|
+
end
|
|
536
|
+
if raw[0]
|
|
537
|
+
x = visit(raw[0])
|
|
538
|
+
low = raw[1] ? visit(raw[1]) : nil
|
|
539
|
+
high = raw[2] ? visit(raw[2]) : nil
|
|
540
|
+
low = nil if low.is_a?(SyntaxTree::Go::Ident) && low.name == "nil"
|
|
541
|
+
high = nil if high.is_a?(SyntaxTree::Go::Ident) && high.name == "nil"
|
|
542
|
+
next SyntaxTree::Go::SliceExpr.new(x: x, lbrack: nil, low: low, high: high, max: nil, slice3: false, rbrack: nil)
|
|
543
|
+
end
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
# Type.new(...) where receiver is Array[...] or Hash[...] type syntax
|
|
547
|
+
if msg_name == "new" && node.receiver
|
|
548
|
+
recv_node = node.receiver
|
|
549
|
+
is_type_syntax = false
|
|
550
|
+
if recv_node.is_a?(SyntaxTree::ARef) && recv_node.collection.is_a?(SyntaxTree::VarRef) && recv_node.collection.value.is_a?(SyntaxTree::Const) && %w[Array Hash].include?(recv_node.collection.value.value)
|
|
551
|
+
is_type_syntax = true
|
|
552
|
+
elsif recv_node.is_a?(SyntaxTree::ARef) && recv_node.collection.is_a?(SyntaxTree::VarRef) && recv_node.collection.value.is_a?(SyntaxTree::Ident) && %w[Array Hash].include?(recv_node.collection.value.value)
|
|
553
|
+
is_type_syntax = true
|
|
554
|
+
elsif recv_node.is_a?(SyntaxTree::CallNode) && recv_node.message.is_a?(SyntaxTree::Ident) && recv_node.message.value == "[]" && recv_node.receiver.is_a?(SyntaxTree::VarRef) && recv_node.receiver.value.is_a?(SyntaxTree::Const) && %w[Array Hash].include?(recv_node.receiver.value.value)
|
|
555
|
+
is_type_syntax = true
|
|
556
|
+
elsif recv_node.is_a?(SyntaxTree::CallNode) && recv_node.message.is_a?(SyntaxTree::Ident) && recv_node.message.value == "[]" && recv_node.receiver.is_a?(SyntaxTree::VarRef) && recv_node.receiver.value.is_a?(SyntaxTree::Ident) && %w[Array Hash].include?(recv_node.receiver.value.value)
|
|
557
|
+
is_type_syntax = true
|
|
558
|
+
end
|
|
559
|
+
if is_type_syntax
|
|
560
|
+
type = type_from_node(recv_node)
|
|
561
|
+
args = visit_args(node.arguments)
|
|
562
|
+
if type.is_a?(SyntaxTree::Go::MapType)
|
|
563
|
+
elts = if args.length == 1 && args.first.is_a?(SyntaxTree::Go::CompositeLit) && args.first.type.is_a?(SyntaxTree::Go::MapType)
|
|
564
|
+
args.first.elts
|
|
565
|
+
else
|
|
566
|
+
args
|
|
567
|
+
end
|
|
568
|
+
next SyntaxTree::Go::CompositeLit.new(type: type, lbrace: nil, elts: elts, rbrace: nil, incomplete: false)
|
|
569
|
+
end
|
|
570
|
+
if type.is_a?(SyntaxTree::Go::ArrayType)
|
|
571
|
+
elt = type.elt
|
|
572
|
+
is_byte_rune = elt.is_a?(SyntaxTree::Go::Ident) && %w[byte rune].include?(elt.name)
|
|
573
|
+
if is_byte_rune
|
|
574
|
+
next SyntaxTree::Go::CallExpr.new(fun: type, lparen: nil, args: args, ellipsis: nil, rparen: nil)
|
|
575
|
+
end
|
|
576
|
+
elts = if args.length == 1 && args.first.is_a?(SyntaxTree::Go::CompositeLit) && args.first.type.is_a?(SyntaxTree::Go::MapType)
|
|
577
|
+
args.first.elts
|
|
578
|
+
else
|
|
579
|
+
args
|
|
580
|
+
end
|
|
581
|
+
next SyntaxTree::Go::CompositeLit.new(type: type, lbrace: nil, elts: elts, rbrace: nil, incomplete: false)
|
|
582
|
+
end
|
|
583
|
+
end
|
|
584
|
+
end
|
|
585
|
+
|
|
586
|
+
receiver = visit(node.receiver)
|
|
587
|
+
next Matcher::MISS if receiver.nil? && %w[define_const define_var const var].include?(msg_name.to_s)
|
|
588
|
+
|
|
589
|
+
# @User.new(...) => &User{...}
|
|
590
|
+
if receiver.is_a?(SyntaxTree::Go::UnaryExpr) && receiver.op.to_s == "&" && receiver.x.is_a?(SyntaxTree::Go::Ident) && msg_name == "new"
|
|
591
|
+
args = visit_args(node.arguments)
|
|
592
|
+
if args.length == 1 && args.first.is_a?(SyntaxTree::Go::CompositeLit) && args.first.type.is_a?(SyntaxTree::Go::MapType)
|
|
593
|
+
elts = args.first.elts
|
|
594
|
+
else
|
|
595
|
+
elts = args
|
|
596
|
+
end
|
|
597
|
+
next SyntaxTree::Go::UnaryExpr.new(
|
|
598
|
+
op_pos: nil, op: "&".to_sym,
|
|
599
|
+
x: SyntaxTree::Go::CompositeLit.new(type: receiver.x, lbrace: nil, elts: elts, rbrace: nil, incomplete: false)
|
|
600
|
+
)
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
# Type.new(...) => Type{...}, or typed map/slice literal/conversion
|
|
604
|
+
if msg_name == "new" && (receiver.is_a?(SyntaxTree::Go::Ident) || receiver.is_a?(SyntaxTree::Go::SelectorExpr) || receiver.is_a?(SyntaxTree::Go::ArrayType) || receiver.is_a?(SyntaxTree::Go::MapType))
|
|
605
|
+
args = visit_args(node.arguments)
|
|
606
|
+
if receiver.is_a?(SyntaxTree::Go::MapType)
|
|
607
|
+
elts = if args.length == 1 && args.first.is_a?(SyntaxTree::Go::CompositeLit) && args.first.type.is_a?(SyntaxTree::Go::MapType)
|
|
608
|
+
args.first.elts
|
|
609
|
+
else
|
|
610
|
+
args
|
|
611
|
+
end
|
|
612
|
+
next SyntaxTree::Go::CompositeLit.new(type: receiver, lbrace: nil, elts: elts, rbrace: nil, incomplete: false)
|
|
613
|
+
end
|
|
614
|
+
if receiver.is_a?(SyntaxTree::Go::ArrayType)
|
|
615
|
+
elt = receiver.elt
|
|
616
|
+
is_byte_rune = elt.is_a?(SyntaxTree::Go::Ident) && %w[byte rune].include?(elt.name)
|
|
617
|
+
if is_byte_rune
|
|
618
|
+
next SyntaxTree::Go::CallExpr.new(fun: receiver, lparen: nil, args: args, ellipsis: nil, rparen: nil)
|
|
619
|
+
end
|
|
620
|
+
elts = if args.length == 1 && args.first.is_a?(SyntaxTree::Go::CompositeLit) && args.first.type.is_a?(SyntaxTree::Go::MapType)
|
|
621
|
+
args.first.elts
|
|
622
|
+
else
|
|
623
|
+
args
|
|
624
|
+
end
|
|
625
|
+
next SyntaxTree::Go::CompositeLit.new(type: receiver, lbrace: nil, elts: elts, rbrace: nil, incomplete: false)
|
|
626
|
+
end
|
|
627
|
+
if args.length == 1 && args.first.is_a?(SyntaxTree::Go::CompositeLit) && args.first.type.is_a?(SyntaxTree::Go::MapType)
|
|
628
|
+
elts = args.first.elts
|
|
629
|
+
else
|
|
630
|
+
elts = args
|
|
631
|
+
end
|
|
632
|
+
next SyntaxTree::Go::CompositeLit.new(type: receiver, lbrace: nil, elts: elts, rbrace: nil, incomplete: false)
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
# @@pkg.Type => *pkg.Type (pointer type expression)
|
|
636
|
+
if receiver.is_a?(SyntaxTree::Go::StarExpr) && receiver.x.is_a?(SyntaxTree::Go::Ident) && node.arguments.nil?
|
|
637
|
+
next SyntaxTree::Go::StarExpr.new(star: nil, x: SyntaxTree::Go::SelectorExpr.new(x: receiver.x, sel: ident(msg_name)))
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
# No-arg, no-parens call is a selector/property access in Go golden
|
|
641
|
+
if node.arguments.nil?
|
|
642
|
+
if receiver
|
|
643
|
+
next selector_expr(x: receiver, sel: msg_name)
|
|
644
|
+
else
|
|
645
|
+
next ident(msg_name)
|
|
646
|
+
end
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
args = visit_args(node.arguments)
|
|
650
|
+
fun = if receiver
|
|
651
|
+
selector_expr(x: receiver, sel: msg_name)
|
|
652
|
+
else
|
|
653
|
+
ident(msg_name)
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
call_expr(fun: fun, args: args)
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
matcher.rule(:vcall) do |node|
|
|
660
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::VCall)
|
|
661
|
+
|
|
662
|
+
name = node.value.value
|
|
663
|
+
next Matcher::DROP if %w[private public protected module_function].include?(name)
|
|
664
|
+
|
|
665
|
+
ident(name)
|
|
666
|
+
end
|
|
667
|
+
|
|
668
|
+
matcher.rule(:var_ref_ident) do |node|
|
|
669
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::VarRef)
|
|
670
|
+
next Matcher::MISS unless node.value.is_a?(SyntaxTree::Ident)
|
|
671
|
+
|
|
672
|
+
ident(node.value.value)
|
|
673
|
+
end
|
|
674
|
+
|
|
675
|
+
matcher.rule(:var_ref_ivar) do |node|
|
|
676
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::VarRef)
|
|
677
|
+
next Matcher::MISS unless node.value.is_a?(SyntaxTree::IVar)
|
|
678
|
+
|
|
679
|
+
name = node.value.value.delete_prefix("@")
|
|
680
|
+
SyntaxTree::Go::UnaryExpr.new(op_pos: nil, op: "&".to_sym, x: ident(name))
|
|
681
|
+
end
|
|
682
|
+
|
|
683
|
+
matcher.rule(:var_ref_cvar) do |node|
|
|
684
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::VarRef)
|
|
685
|
+
next Matcher::MISS unless node.value.is_a?(SyntaxTree::CVar)
|
|
686
|
+
|
|
687
|
+
name = node.value.value.delete_prefix("@@")
|
|
688
|
+
SyntaxTree::Go::StarExpr.new(star: nil, x: ident(name))
|
|
689
|
+
end
|
|
690
|
+
|
|
691
|
+
matcher.rule(:label) do |node|
|
|
692
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Label)
|
|
693
|
+
|
|
694
|
+
ident(node.value.to_s.chomp(":"))
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
matcher.rule(:dyna_symbol) do |node|
|
|
698
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::DynaSymbol)
|
|
699
|
+
|
|
700
|
+
s = node.parts.map { |p| p.is_a?(SyntaxTree::TStringContent) ? p.value : visit(p).to_s }.join
|
|
701
|
+
SyntaxTree::Go::BasicLit.new(kind: "STRING", value: "\"#{s}\"")
|
|
702
|
+
end
|
|
703
|
+
|
|
704
|
+
matcher.rule(:ivar) do |node|
|
|
705
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::IVar)
|
|
706
|
+
|
|
707
|
+
name = node.value.delete_prefix("@")
|
|
708
|
+
SyntaxTree::Go::UnaryExpr.new(op_pos: nil, op: "&".to_sym, x: ident(name))
|
|
709
|
+
end
|
|
710
|
+
|
|
711
|
+
matcher.rule(:cvar) do |node|
|
|
712
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::CVar)
|
|
713
|
+
|
|
714
|
+
name = node.value.delete_prefix("@@")
|
|
715
|
+
SyntaxTree::Go::StarExpr.new(star: nil, x: ident(name))
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
matcher.rule(:field_node) do |node|
|
|
719
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Field)
|
|
720
|
+
|
|
721
|
+
receiver = visit(node.parent)
|
|
722
|
+
name = node.name.is_a?(SyntaxTree::Ident) ? node.name.value : node.name.to_s
|
|
723
|
+
selector_expr(x: receiver, sel: name)
|
|
724
|
+
end
|
|
725
|
+
|
|
726
|
+
matcher.rule(:aref) do |node|
|
|
727
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ARef)
|
|
728
|
+
|
|
729
|
+
collection = visit(node.collection)
|
|
730
|
+
index_node = node.index
|
|
731
|
+
args = if index_node.is_a?(SyntaxTree::Args)
|
|
732
|
+
visit_all(index_node.parts)
|
|
733
|
+
elsif index_node
|
|
734
|
+
visit_all([index_node])
|
|
735
|
+
else
|
|
736
|
+
[]
|
|
737
|
+
end
|
|
738
|
+
SyntaxTree::Go::IndexExpr.new(x: collection, lbrack: nil, index: args.first, rbrack: nil)
|
|
739
|
+
end
|
|
740
|
+
|
|
741
|
+
matcher.rule(:aref_field) do |node|
|
|
742
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ARefField)
|
|
743
|
+
|
|
744
|
+
collection = visit(node.collection)
|
|
745
|
+
index_node = node.index
|
|
746
|
+
args = if index_node.is_a?(SyntaxTree::Args)
|
|
747
|
+
visit_all(index_node.parts)
|
|
748
|
+
elsif index_node
|
|
749
|
+
visit_all([index_node])
|
|
750
|
+
else
|
|
751
|
+
[]
|
|
752
|
+
end
|
|
753
|
+
SyntaxTree::Go::IndexExpr.new(x: collection, lbrack: nil, index: args.first, rbrack: nil)
|
|
754
|
+
end
|
|
755
|
+
|
|
756
|
+
matcher.rule(:string_literal) do |node|
|
|
757
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::StringLiteral)
|
|
758
|
+
|
|
759
|
+
value = node.parts.map { |p| p.is_a?(SyntaxTree::TStringContent) ? p.value : "" }.join
|
|
760
|
+
str_lit(value)
|
|
761
|
+
end
|
|
762
|
+
|
|
763
|
+
matcher.rule(:int) do |node|
|
|
764
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Int)
|
|
765
|
+
|
|
766
|
+
int_lit(node.value)
|
|
767
|
+
end
|
|
768
|
+
|
|
769
|
+
matcher.rule(:float) do |node|
|
|
770
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::FloatLiteral)
|
|
771
|
+
|
|
772
|
+
float_lit(node.value)
|
|
773
|
+
end
|
|
774
|
+
|
|
775
|
+
matcher.rule(:bool_nil) do |node|
|
|
776
|
+
case node
|
|
777
|
+
when SyntaxTree::VarRef
|
|
778
|
+
case node.value
|
|
779
|
+
when SyntaxTree::Kw
|
|
780
|
+
case node.value.value
|
|
781
|
+
when "true", "false" then ident(node.value.value)
|
|
782
|
+
when "nil" then ident("nil")
|
|
783
|
+
when "self" then ident("self")
|
|
784
|
+
else next Matcher::MISS
|
|
785
|
+
end
|
|
786
|
+
else
|
|
787
|
+
next Matcher::MISS
|
|
788
|
+
end
|
|
789
|
+
else
|
|
790
|
+
next Matcher::MISS
|
|
791
|
+
end
|
|
792
|
+
end
|
|
793
|
+
|
|
794
|
+
matcher.rule(:op_assign) do |node|
|
|
795
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::OpAssign)
|
|
796
|
+
|
|
797
|
+
t = assign_target_name(node.target, self)
|
|
798
|
+
lhs = t.is_a?(String) ? ident(t) : (t.is_a?(SyntaxTree::Go::Node) ? t : visit(t))
|
|
799
|
+
rhs = visit(node.value)
|
|
800
|
+
op = node.operator.value
|
|
801
|
+
op = "+=" if op == "+="
|
|
802
|
+
SyntaxTree::Go::AssignStmt.new(lhs: [lhs], tok_pos: nil, tok: op, rhs: [rhs])
|
|
803
|
+
end
|
|
804
|
+
|
|
805
|
+
matcher.rule(:assign) do |node|
|
|
806
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Assign)
|
|
807
|
+
|
|
808
|
+
t = node.target
|
|
809
|
+
if (t.is_a?(SyntaxTree::VarField) && t.value.is_a?(SyntaxTree::Const)) ||
|
|
810
|
+
t.is_a?(SyntaxTree::ConstRef)
|
|
811
|
+
name = assign_target_name(t, self)
|
|
812
|
+
val = visit(node.value)
|
|
813
|
+
decl = SyntaxTree::Go::GenDecl.new(
|
|
814
|
+
doc: nil, tok_pos: nil, tok: "const", lparen: nil,
|
|
815
|
+
specs: [SyntaxTree::Go::ValueSpec.new(doc: nil, names: [ident(Gob::Transpilation::Rules.const_go_name(name.to_s))], type: nil, values: [val], comment: nil)],
|
|
816
|
+
rparen: nil
|
|
817
|
+
)
|
|
818
|
+
decl.instance_variable_set(:@location, node.location) rescue nil
|
|
819
|
+
next decl
|
|
820
|
+
end
|
|
821
|
+
|
|
822
|
+
targets = assign_targets(node.target, self)
|
|
823
|
+
value = visit(node.value)
|
|
824
|
+
build_assign(targets, [value], self)
|
|
825
|
+
end
|
|
826
|
+
|
|
827
|
+
matcher.rule(:massign) do |node|
|
|
828
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::MAssign)
|
|
829
|
+
|
|
830
|
+
targets = node.target.parts.map { |p| assign_target_name(p, self) }
|
|
831
|
+
values = if node.value.is_a?(SyntaxTree::MRHS)
|
|
832
|
+
node.value.parts.map { |p| visit(p) }
|
|
833
|
+
else
|
|
834
|
+
[visit(node.value)]
|
|
835
|
+
end
|
|
836
|
+
build_assign(targets, values, self)
|
|
837
|
+
end
|
|
838
|
+
|
|
839
|
+
matcher.rule(:return) do |node|
|
|
840
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ReturnNode)
|
|
841
|
+
|
|
842
|
+
args = node.arguments
|
|
843
|
+
parts = case args
|
|
844
|
+
when SyntaxTree::Args then args.parts
|
|
845
|
+
when SyntaxTree::ArgParen then args.arguments.parts
|
|
846
|
+
else Array(args)
|
|
847
|
+
end
|
|
848
|
+
results = visit_all(parts)
|
|
849
|
+
SyntaxTree::Go::ReturnStmt.new(return_: nil, results: results)
|
|
850
|
+
end
|
|
851
|
+
|
|
852
|
+
matcher.rule(:binary) do |node|
|
|
853
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Binary)
|
|
854
|
+
|
|
855
|
+
left = visit(node.left)
|
|
856
|
+
right = visit(node.right)
|
|
857
|
+
op = node.operator.is_a?(Symbol) ? node.operator.to_s : node.operator.to_s
|
|
858
|
+
SyntaxTree::Go::BinaryExpr.new(x: left, op_pos: nil, op: op.to_sym, y: right)
|
|
859
|
+
end
|
|
860
|
+
|
|
861
|
+
matcher.rule(:paren) do |node|
|
|
862
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Paren)
|
|
863
|
+
|
|
864
|
+
inner = node.contents
|
|
865
|
+
if inner.is_a?(SyntaxTree::Params)
|
|
866
|
+
next Matcher::MISS
|
|
867
|
+
end
|
|
868
|
+
visited = visit(inner)
|
|
869
|
+
SyntaxTree::Go::ParenExpr.new(lparen: nil, x: visited, rparen: nil)
|
|
870
|
+
end
|
|
871
|
+
|
|
872
|
+
matcher.rule(:array_literal) do |node|
|
|
873
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ArrayLiteral)
|
|
874
|
+
|
|
875
|
+
elts = node.contents ? visit_all(array_elements(node.contents)) : []
|
|
876
|
+
SyntaxTree::Go::CompositeLit.new(
|
|
877
|
+
type: SyntaxTree::Go::ArrayType.new(lbrack: nil, len: nil, elt: ident("any")),
|
|
878
|
+
lbrace: nil,
|
|
879
|
+
elts: elts,
|
|
880
|
+
rbrace: nil,
|
|
881
|
+
incomplete: false
|
|
882
|
+
)
|
|
883
|
+
end
|
|
884
|
+
|
|
885
|
+
matcher.rule(:const_ref) do |node|
|
|
886
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ConstRef)
|
|
887
|
+
|
|
888
|
+
ident(Gob::Transpilation::Rules.const_go_name(node.constant.value))
|
|
889
|
+
end
|
|
890
|
+
|
|
891
|
+
matcher.rule(:class_declaration) do |node|
|
|
892
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ClassDeclaration)
|
|
893
|
+
|
|
894
|
+
name = node.constant.constant.value
|
|
895
|
+
body_nodes = stmt_body(node.bodystmt)
|
|
896
|
+
fields = []
|
|
897
|
+
methods = []
|
|
898
|
+
|
|
899
|
+
body_nodes.each do |child|
|
|
900
|
+
case child
|
|
901
|
+
when SyntaxTree::Command
|
|
902
|
+
if child.message.is_a?(SyntaxTree::Ident) && child.message.value == "attr_accessor"
|
|
903
|
+
child.arguments.parts.each do |arg|
|
|
904
|
+
fname = case arg
|
|
905
|
+
when SyntaxTree::SymbolLiteral then arg.value.value
|
|
906
|
+
else arg.to_s
|
|
907
|
+
end
|
|
908
|
+
fields << SyntaxTree::Go::Field.new(names: [ident(fname)], type: ident("any"))
|
|
909
|
+
end
|
|
910
|
+
else
|
|
911
|
+
visited = visit(child)
|
|
912
|
+
methods.concat(Array(visited)) if visited
|
|
913
|
+
end
|
|
914
|
+
when SyntaxTree::DefNode
|
|
915
|
+
visited = visit(child)
|
|
916
|
+
# method inside class -> method with receiver? check golden
|
|
917
|
+
if visited.is_a?(SyntaxTree::Go::FuncDecl)
|
|
918
|
+
# convert to method: add receiver
|
|
919
|
+
recv = SyntaxTree::Go::FieldList.new(
|
|
920
|
+
opening: nil,
|
|
921
|
+
list: [SyntaxTree::Go::Field.new(names: [ident("self")], type: SyntaxTree::Go::StarExpr.new(star: nil, x: ident(name)))],
|
|
922
|
+
closing: nil
|
|
923
|
+
)
|
|
924
|
+
visited = SyntaxTree::Go::FuncDecl.new(
|
|
925
|
+
doc: visited.doc, recv: recv, name: visited.name, type: visited.type, body: visited.body
|
|
926
|
+
)
|
|
927
|
+
end
|
|
928
|
+
methods << visited if visited
|
|
929
|
+
when SyntaxTree::VoidStmt, SyntaxTree::Comment
|
|
930
|
+
next
|
|
931
|
+
else
|
|
932
|
+
visited = visit(child)
|
|
933
|
+
methods.concat(Array(visited)) if visited
|
|
934
|
+
end
|
|
935
|
+
end
|
|
936
|
+
|
|
937
|
+
struct_type = SyntaxTree::Go::StructType.new(
|
|
938
|
+
struct: nil,
|
|
939
|
+
fields: SyntaxTree::Go::FieldList.new(opening: nil, list: fields, closing: nil),
|
|
940
|
+
incomplete: false
|
|
941
|
+
)
|
|
942
|
+
type_spec = SyntaxTree::Go::TypeSpec.new(doc: nil, name: ident(name), type_params: nil, assign: nil, type: struct_type, comment: nil)
|
|
943
|
+
type_decl = SyntaxTree::Go::GenDecl.new(doc: nil, tok_pos: nil, tok: "type", lparen: nil, specs: [type_spec], rparen: nil)
|
|
944
|
+
|
|
945
|
+
result = [type_decl]
|
|
946
|
+
result.concat(methods)
|
|
947
|
+
result
|
|
948
|
+
end
|
|
949
|
+
|
|
950
|
+
matcher.rule(:symbol_literal) do |node|
|
|
951
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::SymbolLiteral)
|
|
952
|
+
|
|
953
|
+
val = node.value
|
|
954
|
+
name = val.is_a?(SyntaxTree::Ident) ? val.value : val.to_s
|
|
955
|
+
str_lit(name)
|
|
956
|
+
end
|
|
957
|
+
|
|
958
|
+
matcher.rule(:qwords) do |node|
|
|
959
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::QWords)
|
|
960
|
+
|
|
961
|
+
elts = node.elements.map do |e|
|
|
962
|
+
case e
|
|
963
|
+
when SyntaxTree::TStringContent then str_lit(e.value)
|
|
964
|
+
else visit(e)
|
|
965
|
+
end
|
|
966
|
+
end
|
|
967
|
+
SyntaxTree::Go::CompositeLit.new(
|
|
968
|
+
type: SyntaxTree::Go::ArrayType.new(lbrack: nil, len: nil, elt: ident("any")),
|
|
969
|
+
lbrace: nil, elts: elts, rbrace: nil, incomplete: false
|
|
970
|
+
)
|
|
971
|
+
end
|
|
972
|
+
|
|
973
|
+
matcher.rule(:statements_node) do |node|
|
|
974
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Statements)
|
|
975
|
+
|
|
976
|
+
visit_all(node.body)
|
|
977
|
+
end
|
|
978
|
+
|
|
979
|
+
matcher.rule(:range_node) do |node|
|
|
980
|
+
is_range = defined?(SyntaxTree::RangeNode) && node.is_a?(SyntaxTree::RangeNode)
|
|
981
|
+
is_range ||= defined?(SyntaxTree::Dot2) && node.is_a?(SyntaxTree::Dot2)
|
|
982
|
+
is_range ||= defined?(SyntaxTree::Dot3) && node.is_a?(SyntaxTree::Dot3)
|
|
983
|
+
next Matcher::MISS unless is_range
|
|
984
|
+
|
|
985
|
+
left = visit(node.left)
|
|
986
|
+
right = visit(node.right)
|
|
987
|
+
SyntaxTree::Go::CompositeLit.new(
|
|
988
|
+
type: SyntaxTree::Go::ArrayType.new(lbrack: nil, len: nil, elt: ident("int")),
|
|
989
|
+
lbrace: nil, elts: [left, right], rbrace: nil, incomplete: false
|
|
990
|
+
)
|
|
991
|
+
end
|
|
992
|
+
|
|
993
|
+
matcher.rule(:const_path_ref) do |node|
|
|
994
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ConstPathRef)
|
|
995
|
+
|
|
996
|
+
left = visit(node.parent)
|
|
997
|
+
right = ident(node.constant.value)
|
|
998
|
+
selector_expr(x: left, sel: right)
|
|
999
|
+
end
|
|
1000
|
+
|
|
1001
|
+
matcher.rule(:var_ref_const) do |node|
|
|
1002
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::VarRef)
|
|
1003
|
+
next Matcher::MISS unless node.value.is_a?(SyntaxTree::Const)
|
|
1004
|
+
|
|
1005
|
+
ident(Gob::Transpilation::Rules.const_go_name(node.value.value))
|
|
1006
|
+
end
|
|
1007
|
+
|
|
1008
|
+
matcher.rule(:unary) do |node|
|
|
1009
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Unary)
|
|
1010
|
+
|
|
1011
|
+
op = node.operator.to_s
|
|
1012
|
+
stmt = visit(node.statement)
|
|
1013
|
+
SyntaxTree::Go::UnaryExpr.new(op_pos: nil, op: op.to_sym, x: stmt)
|
|
1014
|
+
end
|
|
1015
|
+
|
|
1016
|
+
matcher.rule(:aref) do |node|
|
|
1017
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::ARef)
|
|
1018
|
+
|
|
1019
|
+
collection = visit(node.collection)
|
|
1020
|
+
index = node.index
|
|
1021
|
+
idx_nodes = case index
|
|
1022
|
+
when SyntaxTree::Args then visit_all(index.parts)
|
|
1023
|
+
else visit_all(Array(index))
|
|
1024
|
+
end
|
|
1025
|
+
SyntaxTree::Go::IndexExpr.new(x: collection, lbrack: nil, index: idx_nodes.first, rbrack: nil)
|
|
1026
|
+
end
|
|
1027
|
+
|
|
1028
|
+
matcher.rule(:hash_literal) do |node|
|
|
1029
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::HashLiteral)
|
|
1030
|
+
|
|
1031
|
+
assocs = node.assocs.map do |assoc|
|
|
1032
|
+
case assoc
|
|
1033
|
+
when SyntaxTree::Assoc
|
|
1034
|
+
key = visit(assoc.key)
|
|
1035
|
+
val = visit(assoc.value)
|
|
1036
|
+
SyntaxTree::Go::KeyValueExpr.new(key: key, colon: nil, value: val)
|
|
1037
|
+
else
|
|
1038
|
+
visit(assoc)
|
|
1039
|
+
end
|
|
1040
|
+
end
|
|
1041
|
+
SyntaxTree::Go::CompositeLit.new(
|
|
1042
|
+
type: SyntaxTree::Go::MapType.new(map: nil, key: ident("any"), value: ident("any")),
|
|
1043
|
+
lbrace: nil, elts: assocs, rbrace: nil, incomplete: false
|
|
1044
|
+
)
|
|
1045
|
+
end
|
|
1046
|
+
|
|
1047
|
+
matcher.rule(:if_node) do |node|
|
|
1048
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::IfNode)
|
|
1049
|
+
|
|
1050
|
+
cond = visit(node.predicate)
|
|
1051
|
+
consequent = visit_all(node.statements.body).map { |s| stmtify(s) }
|
|
1052
|
+
else_node = nil
|
|
1053
|
+
if node.consequent
|
|
1054
|
+
if node.consequent.is_a?(SyntaxTree::Elsif)
|
|
1055
|
+
else_node = visit(node.consequent)
|
|
1056
|
+
elsif node.consequent.is_a?(SyntaxTree::Else)
|
|
1057
|
+
else_stmts = visit_all(node.consequent.statements.body).map { |s| stmtify(s) }
|
|
1058
|
+
else_node = block_stmt(list: else_stmts)
|
|
1059
|
+
end
|
|
1060
|
+
end
|
|
1061
|
+
SyntaxTree::Go::IfStmt.new(if_: nil, init: nil, cond: cond, body: block_stmt(list: consequent), else_: else_node)
|
|
1062
|
+
end
|
|
1063
|
+
|
|
1064
|
+
matcher.rule(:elsif) do |node|
|
|
1065
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Elsif)
|
|
1066
|
+
|
|
1067
|
+
cond = visit(node.predicate)
|
|
1068
|
+
consequent = visit_all(node.statements.body).map { |s| stmtify(s) }
|
|
1069
|
+
else_node = nil
|
|
1070
|
+
if node.consequent
|
|
1071
|
+
if node.consequent.is_a?(SyntaxTree::Elsif)
|
|
1072
|
+
else_node = visit(node.consequent)
|
|
1073
|
+
elsif node.consequent.is_a?(SyntaxTree::Else)
|
|
1074
|
+
else_stmts = visit_all(node.consequent.statements.body).map { |s| stmtify(s) }
|
|
1075
|
+
else_node = block_stmt(list: else_stmts)
|
|
1076
|
+
end
|
|
1077
|
+
end
|
|
1078
|
+
SyntaxTree::Go::IfStmt.new(if_: nil, init: nil, cond: cond, body: block_stmt(list: consequent), else_: else_node)
|
|
1079
|
+
end
|
|
1080
|
+
|
|
1081
|
+
matcher.rule(:unless_node) do |node|
|
|
1082
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::UnlessNode)
|
|
1083
|
+
|
|
1084
|
+
cond = visit(node.predicate)
|
|
1085
|
+
cond = SyntaxTree::Go::UnaryExpr.new(op_pos: nil, op: "!".to_sym, x: SyntaxTree::Go::ParenExpr.new(lparen: nil, x: cond, rparen: nil))
|
|
1086
|
+
consequent = visit_all(node.statements.body).map { |s| stmtify(s) }
|
|
1087
|
+
else_node = nil
|
|
1088
|
+
if node.consequent
|
|
1089
|
+
if node.consequent.is_a?(SyntaxTree::Else)
|
|
1090
|
+
else_stmts = visit_all(node.consequent.statements.body).map { |s| stmtify(s) }
|
|
1091
|
+
else_node = block_stmt(list: else_stmts)
|
|
1092
|
+
end
|
|
1093
|
+
end
|
|
1094
|
+
SyntaxTree::Go::IfStmt.new(if_: nil, init: nil, cond: cond, body: block_stmt(list: consequent), else_: else_node)
|
|
1095
|
+
end
|
|
1096
|
+
|
|
1097
|
+
matcher.rule(:bare_assoc_hash) do |node|
|
|
1098
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::BareAssocHash)
|
|
1099
|
+
|
|
1100
|
+
assocs = node.assocs.map do |assoc|
|
|
1101
|
+
case assoc
|
|
1102
|
+
when SyntaxTree::Assoc
|
|
1103
|
+
SyntaxTree::Go::KeyValueExpr.new(key: visit(assoc.key), colon: nil, value: visit(assoc.value))
|
|
1104
|
+
else
|
|
1105
|
+
visit(assoc)
|
|
1106
|
+
end
|
|
1107
|
+
end
|
|
1108
|
+
assocs
|
|
1109
|
+
end
|
|
1110
|
+
|
|
1111
|
+
matcher.rule(:assoc_new) do |node|
|
|
1112
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::Assoc)
|
|
1113
|
+
|
|
1114
|
+
SyntaxTree::Go::KeyValueExpr.new(key: visit(node.key), colon: nil, value: visit(node.value))
|
|
1115
|
+
end
|
|
1116
|
+
|
|
1117
|
+
matcher.rule(:method_add_block) do |node|
|
|
1118
|
+
next Matcher::MISS unless node.is_a?(SyntaxTree::MethodAddBlock)
|
|
1119
|
+
|
|
1120
|
+
call = node.call
|
|
1121
|
+
block = node.block
|
|
1122
|
+
|
|
1123
|
+
# defer { ... } / go { ... }
|
|
1124
|
+
if call.is_a?(SyntaxTree::CallNode) && call.receiver.nil? && %w[defer go].include?(call.message.value)
|
|
1125
|
+
stmts = visit_all(stmt_body(block.bodystmt)).map { |s| stmtify(s) }
|
|
1126
|
+
fn = SyntaxTree::Go::FuncLit.new(
|
|
1127
|
+
type: SyntaxTree::Go::FuncType.new(func: nil, type_params: nil, params: field_list, results: nil),
|
|
1128
|
+
body: block_stmt(list: stmts)
|
|
1129
|
+
)
|
|
1130
|
+
call_expr_node = SyntaxTree::Go::CallExpr.new(fun: fn, lparen: nil, args: [], ellipsis: nil, rparen: nil)
|
|
1131
|
+
next(call.message.value == "defer" ? SyntaxTree::Go::DeferStmt.new(defer: nil, call: call_expr_node) : SyntaxTree::Go::GoStmt.new(go: nil, call: call_expr_node))
|
|
1132
|
+
end
|
|
1133
|
+
|
|
1134
|
+
# lambda { |...| ... }
|
|
1135
|
+
if call.is_a?(SyntaxTree::CallNode) && call.receiver.nil? && call.message.value == "lambda"
|
|
1136
|
+
params = block_params(block)
|
|
1137
|
+
has_yield = contains_yield?(block.bodystmt)
|
|
1138
|
+
params = params.map { |p| p == "yield_" ? "yield" : p }
|
|
1139
|
+
if has_yield && !params.include?("yield")
|
|
1140
|
+
params << "yield"
|
|
1141
|
+
end
|
|
1142
|
+
stmts = scoped(params) { visit_all(stmt_body(block.bodystmt)).map { |s| stmtify(s) } }
|
|
1143
|
+
fields = params.map { |n| SyntaxTree::Go::Field.new(names: [ident(n)], type: n == "yield" ? ident("any") : nil) }
|
|
1144
|
+
next SyntaxTree::Go::FuncLit.new(
|
|
1145
|
+
type: SyntaxTree::Go::FuncType.new(func: nil, type_params: nil, params: field_list(list: fields), results: nil),
|
|
1146
|
+
body: block_stmt(list: stmts)
|
|
1147
|
+
)
|
|
1148
|
+
end
|
|
1149
|
+
|
|
1150
|
+
# select { |c| ... }
|
|
1151
|
+
if call.is_a?(SyntaxTree::CallNode) && call.receiver.nil? && call.message.value == "select"
|
|
1152
|
+
next build_select(node, self)
|
|
1153
|
+
end
|
|
1154
|
+
|
|
1155
|
+
# receiver.each { |...| ... } / receiver.times { |...| ... }
|
|
1156
|
+
if call.is_a?(SyntaxTree::CallNode) && %w[each times].include?(call.message.value)
|
|
1157
|
+
next build_each(node, self)
|
|
1158
|
+
end
|
|
1159
|
+
|
|
1160
|
+
# loop { ... }
|
|
1161
|
+
if call.is_a?(SyntaxTree::CallNode) && call.receiver.nil? && call.message.value == "loop"
|
|
1162
|
+
stmts = scoped { visit_all(stmt_body(block.bodystmt)).map { |s| stmtify(s) } }
|
|
1163
|
+
next SyntaxTree::Go::ForStmt.new(for_: nil, init: nil, cond: nil, post: nil, body: block_stmt(list: stmts))
|
|
1164
|
+
end
|
|
1165
|
+
|
|
1166
|
+
# Generic: call with block -> call_expr + no special handling for now
|
|
1167
|
+
visited_call = visit(call)
|
|
1168
|
+
stmts = visit_all(stmt_body(block.bodystmt)).map { |s| stmtify(s) }
|
|
1169
|
+
if visited_call.is_a?(SyntaxTree::Go::CallExpr)
|
|
1170
|
+
fn = SyntaxTree::Go::FuncLit.new(
|
|
1171
|
+
type: SyntaxTree::Go::FuncType.new(func: nil, type_params: nil, params: field_list, results: nil),
|
|
1172
|
+
body: block_stmt(list: stmts)
|
|
1173
|
+
)
|
|
1174
|
+
visited_call.args << fn
|
|
1175
|
+
next visited_call
|
|
1176
|
+
end
|
|
1177
|
+
Matcher::MISS
|
|
1178
|
+
end
|
|
1179
|
+
end
|
|
1180
|
+
|
|
1181
|
+
# Helpers available in rule context via transpiler instance_exec
|
|
1182
|
+
end
|
|
1183
|
+
end
|
|
1184
|
+
end
|