gob-lang 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +201 -0
- data/README.md +73 -0
- data/gob-lang.gemspec +2 -1
- data/lib/gob/cli/base.rb +2 -2
- data/lib/gob/cli/commands/fmt.rb +2 -2
- data/lib/gob/cli/completion.rb +104 -0
- data/lib/gob/cli/errors.rb +0 -2
- data/lib/gob/cli.rb +21 -3
- data/lib/gob/go/printer.rb +215 -72
- data/lib/gob/transpilation/context.rb +102 -0
- data/lib/gob/transpilation/runner.rb +1500 -0
- data/lib/gob/validation/rules/array_length.rb +45 -0
- data/lib/gob/validation/rules/attribute_type.rb +30 -0
- data/lib/gob/validation/rules/block_delimiters.rb +43 -0
- data/lib/gob/validation/rules/collection_literal.rb +107 -0
- data/lib/gob/validation/rules/config.yml +58 -4
- data/lib/gob/validation/rules/cop.rb +72 -0
- data/lib/gob/validation/rules/defer_statement.rb +33 -0
- data/lib/gob/validation/rules/error_check.rb +33 -0
- data/lib/gob/validation/rules/go_block.rb +58 -0
- data/lib/gob/validation/rules/indentation.rb +118 -0
- data/lib/gob/validation/rules/invoked_lambda.rb +48 -0
- data/lib/gob/validation/rules/lambda_signature.rb +58 -0
- data/lib/gob/validation/rules/multiline.rb +99 -0
- data/lib/gob/validation/rules/operator_assignment.rb +44 -0
- data/lib/gob/validation/rules/pointer_type.rb +36 -0
- data/lib/gob/validation/rules/quote_marks.rb +33 -0
- data/lib/gob/validation/rules/redundant_lambda_parentheses.rb +47 -0
- data/lib/gob/validation/rules/select_block.rb +85 -0
- data/lib/gob/validation/rules/signature_comment.rb +43 -0
- data/lib/gob/validation/rules/this_receiver.rb +38 -0
- data/lib/gob/validation/rules/trailing_whitespace.rb +38 -0
- data/lib/gob/validation/rules/variable_declaration.rb +32 -0
- data/lib/gob/validation/rules/word_array.rb +33 -0
- data/lib/gob/validation/rules/zero_based_range_loop.rb +63 -0
- data/lib/gob/validation/rules.rb +85 -11
- data/lib/gob/version.rb +1 -1
- data/lib/gob.rb +10 -12
- metadata +41 -9
- data/lib/gob/go/dsl.rb +0 -196
- data/lib/gob/matcher.rb +0 -34
- data/lib/gob/transpilation/rules/core.rb +0 -1184
- data/lib/gob/transpiler.rb +0 -815
- data/lib/gob/validation/rules/class_rules.rb +0 -78
- data/lib/gob/validation/rules/function_rules.rb +0 -33
- data/lib/gob/validation/rules/module_rules.rb +0 -78
- data/lib/gob/validation/rules/program_rules.rb +0 -72
|
@@ -0,0 +1,1500 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
#
|
|
3
|
+
# runner.rb
|
|
4
|
+
# Copyright (C) 2026 nathan <nathan@elitedesk>
|
|
5
|
+
#
|
|
6
|
+
# Distributed under terms of the MIT license.
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
module Gob
|
|
10
|
+
module Transpilation
|
|
11
|
+
class Runner
|
|
12
|
+
module DefinitionNodes
|
|
13
|
+
def parse_signature_comment(text)
|
|
14
|
+
case text.split(/\s*->\s*/, 2)
|
|
15
|
+
in [/\A#\s*\z/, ret] unless ret.empty?
|
|
16
|
+
{ params: [], returns: ret.delete_prefix("(").delete_suffix(")").split(/\s*,\s*/) }
|
|
17
|
+
in [/\A#\s*\(([^)]*)\)\z/, ret] unless ret.empty?
|
|
18
|
+
{ params: $1.split(/\s*,\s*/), returns: ret.delete_prefix("(").delete_suffix(")").split(/\s*,\s*/) }
|
|
19
|
+
in [/\A#\s*(.+)\z/, ret] unless ret.empty?
|
|
20
|
+
{ params: $1.split(/\s*,\s*/), returns: ret.delete_prefix("(").delete_suffix(")").split(/\s*,\s*/) }
|
|
21
|
+
in [/\A#\s*\(([^)]*)\)\s*\z/]
|
|
22
|
+
{ params: $1.split(/\s*,\s*/), returns: [] }
|
|
23
|
+
else
|
|
24
|
+
{ params: [], returns: [] }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# var colour = Struct.new(R: byte)
|
|
29
|
+
# type colour struct { R byte }
|
|
30
|
+
# ---
|
|
31
|
+
def TypeDecl(name, fields)
|
|
32
|
+
SyntaxTree::Go::GenDecl.new(
|
|
33
|
+
tok: :type,
|
|
34
|
+
specs: [
|
|
35
|
+
SyntaxTree::Go::TypeSpec.new(
|
|
36
|
+
name: Ident(name),
|
|
37
|
+
type: SyntaxTree::Go::StructType.new(
|
|
38
|
+
fields: SyntaxTree::Go::FieldList.new(list: fields)
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
]
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Title: string, # `json:"title"`
|
|
46
|
+
# Title string `json:"title"`
|
|
47
|
+
# ---
|
|
48
|
+
def Fields(assocs)
|
|
49
|
+
Array(assocs).filter_map do |assoc|
|
|
50
|
+
case assoc
|
|
51
|
+
in SyntaxTree::Assoc[
|
|
52
|
+
key: SyntaxTree::Label[value: label],
|
|
53
|
+
value: type,
|
|
54
|
+
comments: [SyntaxTree::Comment[value: tag], *]
|
|
55
|
+
]
|
|
56
|
+
SyntaxTree::Go::Field.new(
|
|
57
|
+
names: [ Ident(label.delete_suffix(":")) ],
|
|
58
|
+
type: Statements(type),
|
|
59
|
+
tag: BasicLit(:STRING, tag.sub(/\A#\s*/, ""))
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
in SyntaxTree::Assoc[ key: SyntaxTree::Label[value: label], value: type ]
|
|
63
|
+
SyntaxTree::Go::Field.new(
|
|
64
|
+
names: [ Ident(label.delete_suffix(":")) ],
|
|
65
|
+
type: Statements(type)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# attr_accessor :A, int
|
|
69
|
+
# A int
|
|
70
|
+
# ---
|
|
71
|
+
in SyntaxTree::Command[
|
|
72
|
+
message: SyntaxTree::Ident[value: "attr_accessor"],
|
|
73
|
+
arguments: SyntaxTree::Args[ parts: [ { value: { value: label } }, type ] ]
|
|
74
|
+
]
|
|
75
|
+
SyntaxTree::Go::Field.new(names: [ Ident(label) ], type: Statements(type))
|
|
76
|
+
|
|
77
|
+
else
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# def MarshalText # -> ([]byte, error)
|
|
84
|
+
# func (self colour) MarshalText() ([]byte, error)
|
|
85
|
+
# ---
|
|
86
|
+
def Methods(name, body)
|
|
87
|
+
Array(body).filter_map do |node|
|
|
88
|
+
case node
|
|
89
|
+
in SyntaxTree::DefNode[
|
|
90
|
+
name: { value: method },
|
|
91
|
+
params: params,
|
|
92
|
+
bodystmt: SyntaxTree::BodyStmt[ statements: SyntaxTree::Statements[ body: statements ] ]
|
|
93
|
+
]
|
|
94
|
+
Function(
|
|
95
|
+
name: method,
|
|
96
|
+
params: params,
|
|
97
|
+
body: descend(statements).then { |children| children.tree },
|
|
98
|
+
receiver: SyntaxTree::Go::FieldList.new(
|
|
99
|
+
list: [ SyntaxTree::Go::Field.new(names: [ Ident("self") ], type: Ident(name)) ]
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
else
|
|
104
|
+
nil
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def Function(name:, params:, body:, receiver: nil)
|
|
110
|
+
case params
|
|
111
|
+
in { contents: SyntaxTree::Params[requireds: args], comments: [SyntaxTree::Comment[value: types], *] }
|
|
112
|
+
input, output = parse_signature_comment(types).values_at(:params, :returns)
|
|
113
|
+
params = args.map { |arg|
|
|
114
|
+
SyntaxTree::Go::Field.new(
|
|
115
|
+
names: [ SyntaxTree::Go::Ident.new(name: arg.value) ],
|
|
116
|
+
type: SyntaxTree::Go::Ident.new(name: input.shift)
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
results = output.map { |result|
|
|
120
|
+
SyntaxTree::Go::Field.new(
|
|
121
|
+
names: [ SyntaxTree::Go::Ident.new(name: result) ],
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
in { contents: SyntaxTree::Params[requireds: args] }
|
|
126
|
+
params = args.map { |arg|
|
|
127
|
+
SyntaxTree::Go::Field.new(
|
|
128
|
+
names: [ SyntaxTree::Go::Ident.new(name: arg.value) ],
|
|
129
|
+
type: SyntaxTree::Go::Ident.new(name: input.shift)
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
results = []
|
|
133
|
+
|
|
134
|
+
in { comments: [SyntaxTree::Comment[value: types], *] }
|
|
135
|
+
_, output = parse_signature_comment(types).values_at(:params, :returns)
|
|
136
|
+
params = []
|
|
137
|
+
results = output.map { |result|
|
|
138
|
+
SyntaxTree::Go::Field.new(
|
|
139
|
+
names: [ SyntaxTree::Go::Ident.new(name: result) ],
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
else
|
|
144
|
+
params = []
|
|
145
|
+
results = []
|
|
146
|
+
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
SyntaxTree::Go::FuncDecl.new(
|
|
150
|
+
recv: receiver,
|
|
151
|
+
name: SyntaxTree::Go::Ident.new(name: name),
|
|
152
|
+
type: SyntaxTree::Go::FuncType.new(
|
|
153
|
+
type_params: nil,
|
|
154
|
+
params: SyntaxTree::Go::FieldList.new(list: params),
|
|
155
|
+
results: SyntaxTree::Go::FieldList.new(list: results),
|
|
156
|
+
),
|
|
157
|
+
body: SyntaxTree::Go::BlockStmt.new(list: body)
|
|
158
|
+
)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
module LambdaNodes
|
|
163
|
+
# lambda { |w, r| ... }
|
|
164
|
+
# func(w http.ResponseWriter, r *http.Request) { ... }
|
|
165
|
+
# ---
|
|
166
|
+
def Lambda(block)
|
|
167
|
+
case block
|
|
168
|
+
in SyntaxTree::BlockNode[
|
|
169
|
+
block_var: block_var,
|
|
170
|
+
bodystmt: SyntaxTree::BodyStmt[ statements: SyntaxTree::Statements[ body: body ] ]
|
|
171
|
+
]
|
|
172
|
+
FuncLit(block_var, body)
|
|
173
|
+
|
|
174
|
+
in SyntaxTree::BlockNode[
|
|
175
|
+
block_var: block_var,
|
|
176
|
+
bodystmt: SyntaxTree::Statements[ body: body ]
|
|
177
|
+
]
|
|
178
|
+
FuncLit(block_var, body)
|
|
179
|
+
|
|
180
|
+
else
|
|
181
|
+
nil
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# |tag| # string -> bool
|
|
186
|
+
# (tag string) bool
|
|
187
|
+
# ---
|
|
188
|
+
def FuncLit(block_var, body)
|
|
189
|
+
case block_var
|
|
190
|
+
in SyntaxTree::BlockVar[
|
|
191
|
+
params: SyntaxTree::Params[requireds: args],
|
|
192
|
+
comments: [SyntaxTree::Comment[value: types], *]
|
|
193
|
+
]
|
|
194
|
+
input, output = parse_signature_comment(types).values_at(:params, :returns)
|
|
195
|
+
|
|
196
|
+
in SyntaxTree::BlockVar[ params: SyntaxTree::Params[requireds: args] ]
|
|
197
|
+
input, output = [], []
|
|
198
|
+
|
|
199
|
+
else
|
|
200
|
+
args, input, output = [], [], []
|
|
201
|
+
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
SyntaxTree::Go::FuncLit.new(
|
|
205
|
+
type: SyntaxTree::Go::FuncType.new(
|
|
206
|
+
type_params: nil,
|
|
207
|
+
params: SyntaxTree::Go::FieldList.new(
|
|
208
|
+
list: args.map { |arg|
|
|
209
|
+
SyntaxTree::Go::Field.new(names: [ Ident(arg.value) ], type: Ident(input.shift))
|
|
210
|
+
}
|
|
211
|
+
),
|
|
212
|
+
results: SyntaxTree::Go::FieldList.new(
|
|
213
|
+
list: output.map { |result| SyntaxTree::Go::Field.new(names: [ Ident(result) ]) }
|
|
214
|
+
)
|
|
215
|
+
),
|
|
216
|
+
body: descend(body).then { |children| Block(body: children.tree) }
|
|
217
|
+
)
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
module BlockNodes
|
|
222
|
+
def Block(body:)
|
|
223
|
+
SyntaxTree::Go::BlockStmt.new(list: body)
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
module LogicNodes
|
|
228
|
+
# if err != nil
|
|
229
|
+
# if err
|
|
230
|
+
# ---
|
|
231
|
+
def Truthy(node)
|
|
232
|
+
case node
|
|
233
|
+
in SyntaxTree::Go::Ident[name: "err"]
|
|
234
|
+
Binary(node, :!=, Ident(:nil))
|
|
235
|
+
|
|
236
|
+
else
|
|
237
|
+
node
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# unless err != nil
|
|
242
|
+
# if err == nil
|
|
243
|
+
# ---
|
|
244
|
+
def Not(node)
|
|
245
|
+
case node
|
|
246
|
+
in SyntaxTree::Go::BinaryExpr[x: x, op: :!=, y: y]
|
|
247
|
+
Binary(x, :==, y)
|
|
248
|
+
|
|
249
|
+
in SyntaxTree::Go::BinaryExpr[x: x, op: :==, y: y]
|
|
250
|
+
Binary(x, :!=, y)
|
|
251
|
+
|
|
252
|
+
else
|
|
253
|
+
SyntaxTree::Go::UnaryExpr.new(op: :!, x: node)
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# if err = f()
|
|
258
|
+
# if err := f(); err != nil
|
|
259
|
+
# ---
|
|
260
|
+
def Guard(node)
|
|
261
|
+
case node
|
|
262
|
+
in SyntaxTree::Paren[contents: SyntaxTree::Statements[body: [statement]]]
|
|
263
|
+
Guard(statement)
|
|
264
|
+
|
|
265
|
+
in SyntaxTree::Assign | SyntaxTree::MAssign
|
|
266
|
+
Statements(node).then { |init| [init, Truthy(init.lhs.last)] }
|
|
267
|
+
|
|
268
|
+
else
|
|
269
|
+
[nil, Truthy(LeftHand(node))]
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def Body(ctx, statements)
|
|
274
|
+
ctx.descend(statements.body).then { |children| Block(body: children.tree) }
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# if cond ... end
|
|
278
|
+
# if cond { ... }
|
|
279
|
+
# ---
|
|
280
|
+
def If(ctx, predicate, statements, consequent)
|
|
281
|
+
init, cond = Guard(predicate)
|
|
282
|
+
|
|
283
|
+
SyntaxTree::Go::IfStmt.new(
|
|
284
|
+
init: init,
|
|
285
|
+
cond: cond,
|
|
286
|
+
body: Body(ctx, statements),
|
|
287
|
+
else_: Logic(ctx, consequent)
|
|
288
|
+
)
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# unless cond ... end
|
|
292
|
+
# if !cond { ... }
|
|
293
|
+
# ---
|
|
294
|
+
def Unless(ctx, predicate, statements, consequent)
|
|
295
|
+
init, cond = Guard(predicate)
|
|
296
|
+
|
|
297
|
+
SyntaxTree::Go::IfStmt.new(
|
|
298
|
+
init: init,
|
|
299
|
+
cond: Not(cond),
|
|
300
|
+
body: Body(ctx, statements),
|
|
301
|
+
else_: Logic(ctx, consequent)
|
|
302
|
+
)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# while cond ... end
|
|
306
|
+
# for cond { ... }
|
|
307
|
+
# ---
|
|
308
|
+
def While(ctx, predicate, statements)
|
|
309
|
+
init, cond = Guard(predicate)
|
|
310
|
+
|
|
311
|
+
SyntaxTree::Go::ForStmt.new(
|
|
312
|
+
init: init,
|
|
313
|
+
cond: cond,
|
|
314
|
+
post: nil,
|
|
315
|
+
body: Body(ctx, statements)
|
|
316
|
+
)
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# until cond ... end
|
|
320
|
+
# for !cond { ... }
|
|
321
|
+
# ---
|
|
322
|
+
def Until(ctx, predicate, statements)
|
|
323
|
+
init, cond = Guard(predicate)
|
|
324
|
+
|
|
325
|
+
SyntaxTree::Go::ForStmt.new(
|
|
326
|
+
init: init,
|
|
327
|
+
cond: Not(cond),
|
|
328
|
+
post: nil,
|
|
329
|
+
body: Body(ctx, statements)
|
|
330
|
+
)
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# loop do ... end
|
|
334
|
+
# for { ... }
|
|
335
|
+
# ---
|
|
336
|
+
def Loop(ctx, statements)
|
|
337
|
+
SyntaxTree::Go::ForStmt.new(
|
|
338
|
+
init: nil,
|
|
339
|
+
cond: nil,
|
|
340
|
+
post: nil,
|
|
341
|
+
body: Body(ctx, statements)
|
|
342
|
+
)
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# collection.each do |key, value| ... end
|
|
346
|
+
# for key, value := range collection { ... }
|
|
347
|
+
# ---
|
|
348
|
+
# when metrics.KindUint64
|
|
349
|
+
# case metrics.KindUint64:
|
|
350
|
+
# ---
|
|
351
|
+
def Cases(ctx, node)
|
|
352
|
+
case node
|
|
353
|
+
in SyntaxTree::When[
|
|
354
|
+
arguments: SyntaxTree::Args[ parts: parts ],
|
|
355
|
+
statements: SyntaxTree::Statements[ body: body ],
|
|
356
|
+
consequent: consequent
|
|
357
|
+
]
|
|
358
|
+
[
|
|
359
|
+
SyntaxTree::Go::CaseClause.new(
|
|
360
|
+
list: parts.map { |part| Statements(part) },
|
|
361
|
+
body: ctx.descend(body).then { |children| children.tree }
|
|
362
|
+
),
|
|
363
|
+
*Cases(ctx, consequent)
|
|
364
|
+
]
|
|
365
|
+
|
|
366
|
+
# else
|
|
367
|
+
# default:
|
|
368
|
+
# ---
|
|
369
|
+
in SyntaxTree::Else[ statements: SyntaxTree::Statements[ body: body ] ]
|
|
370
|
+
[
|
|
371
|
+
SyntaxTree::Go::CaseClause.new(
|
|
372
|
+
list: [],
|
|
373
|
+
body: ctx.descend(body).then { |children| children.tree }
|
|
374
|
+
)
|
|
375
|
+
]
|
|
376
|
+
|
|
377
|
+
else
|
|
378
|
+
[]
|
|
379
|
+
end
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# when msg = ~fast
|
|
383
|
+
# case msg := <-fast:
|
|
384
|
+
# ---
|
|
385
|
+
def Comm(node)
|
|
386
|
+
case node
|
|
387
|
+
in SyntaxTree::Assign | SyntaxTree::MAssign
|
|
388
|
+
Statements(node)
|
|
389
|
+
|
|
390
|
+
else
|
|
391
|
+
SyntaxTree::Go::ExprStmt.new(x: Statements(node))
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
def Comms(ctx, node)
|
|
396
|
+
case node
|
|
397
|
+
in SyntaxTree::When[
|
|
398
|
+
arguments: SyntaxTree::Args[ parts: [ comm ] ],
|
|
399
|
+
statements: SyntaxTree::Statements[ body: body ],
|
|
400
|
+
consequent: consequent
|
|
401
|
+
]
|
|
402
|
+
[
|
|
403
|
+
SyntaxTree::Go::CommClause.new(
|
|
404
|
+
comm: Comm(comm),
|
|
405
|
+
body: ctx.descend(body).then { |children| children.tree }
|
|
406
|
+
),
|
|
407
|
+
*Comms(ctx, consequent)
|
|
408
|
+
]
|
|
409
|
+
|
|
410
|
+
# else
|
|
411
|
+
# default:
|
|
412
|
+
# ---
|
|
413
|
+
in SyntaxTree::Else[ statements: SyntaxTree::Statements[ body: body ] ]
|
|
414
|
+
[
|
|
415
|
+
SyntaxTree::Go::CommClause.new(
|
|
416
|
+
comm: nil,
|
|
417
|
+
body: ctx.descend(body).then { |children| children.tree }
|
|
418
|
+
)
|
|
419
|
+
]
|
|
420
|
+
|
|
421
|
+
else
|
|
422
|
+
[]
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
# case ... end
|
|
427
|
+
# select { ... }
|
|
428
|
+
# ---
|
|
429
|
+
def Select(ctx, consequent)
|
|
430
|
+
SyntaxTree::Go::SelectStmt.new(
|
|
431
|
+
body: SyntaxTree::Go::BlockStmt.new(list: Comms(ctx, consequent))
|
|
432
|
+
)
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
# case s.Value.Kind() ... end
|
|
436
|
+
# switch s.Value.Kind() { ... }
|
|
437
|
+
# ---
|
|
438
|
+
def Switch(ctx, value, consequent)
|
|
439
|
+
SyntaxTree::Go::SwitchStmt.new(
|
|
440
|
+
tag: Statements(value),
|
|
441
|
+
body: SyntaxTree::Go::BlockStmt.new(list: Cases(ctx, consequent))
|
|
442
|
+
)
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# case d = decl.(type) ... end
|
|
446
|
+
# switch d := decl.(type) { ... }
|
|
447
|
+
# ---
|
|
448
|
+
def TypeSwitch(ctx, name, value, consequent)
|
|
449
|
+
SyntaxTree::Go::TypeSwitchStmt.new(
|
|
450
|
+
assign: SyntaxTree::Go::AssignStmt.new(
|
|
451
|
+
lhs: [Ident(name)],
|
|
452
|
+
tok: ":=",
|
|
453
|
+
rhs: [SyntaxTree::Go::TypeAssertExpr.new(x: Statements(value), type: nil)]
|
|
454
|
+
),
|
|
455
|
+
body: SyntaxTree::Go::BlockStmt.new(list: Cases(ctx, consequent))
|
|
456
|
+
)
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
def Each(ctx, collection, requireds, statements)
|
|
460
|
+
key, value = requireds.map { |required| Ident(required.value) }
|
|
461
|
+
|
|
462
|
+
SyntaxTree::Go::RangeStmt.new(
|
|
463
|
+
key: key,
|
|
464
|
+
value: value,
|
|
465
|
+
tok: ":=",
|
|
466
|
+
x: LeftHand(collection),
|
|
467
|
+
body: Body(ctx, statements)
|
|
468
|
+
)
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
def Logic(ctx, node)
|
|
472
|
+
case node
|
|
473
|
+
in SyntaxTree::IfNode[predicate: predicate, statements: statements, consequent: consequent]
|
|
474
|
+
If(ctx, predicate, statements, consequent)
|
|
475
|
+
|
|
476
|
+
in SyntaxTree::Elsif[predicate: predicate, statements: statements, consequent: consequent]
|
|
477
|
+
If(ctx, predicate, statements, consequent)
|
|
478
|
+
|
|
479
|
+
in SyntaxTree::UnlessNode[predicate: predicate, statements: statements, consequent: consequent]
|
|
480
|
+
Unless(ctx, predicate, statements, consequent)
|
|
481
|
+
|
|
482
|
+
in SyntaxTree::Else[statements: statements]
|
|
483
|
+
Body(ctx, statements)
|
|
484
|
+
|
|
485
|
+
in SyntaxTree::WhileNode[predicate: predicate, statements: statements]
|
|
486
|
+
While(ctx, predicate, statements)
|
|
487
|
+
|
|
488
|
+
in SyntaxTree::UntilNode[predicate: predicate, statements: statements]
|
|
489
|
+
Until(ctx, predicate, statements)
|
|
490
|
+
|
|
491
|
+
in SyntaxTree::MethodAddBlock[
|
|
492
|
+
call: SyntaxTree::CallNode[
|
|
493
|
+
message: SyntaxTree::Ident[value: "loop"]
|
|
494
|
+
],
|
|
495
|
+
block: SyntaxTree::BlockNode[
|
|
496
|
+
bodystmt: SyntaxTree::BodyStmt[statements: statements]
|
|
497
|
+
]
|
|
498
|
+
]
|
|
499
|
+
Loop(ctx, statements)
|
|
500
|
+
|
|
501
|
+
in SyntaxTree::MethodAddBlock[
|
|
502
|
+
call: SyntaxTree::CallNode[
|
|
503
|
+
receiver: collection,
|
|
504
|
+
message: SyntaxTree::Ident[value: "each"]
|
|
505
|
+
],
|
|
506
|
+
block: SyntaxTree::BlockNode[
|
|
507
|
+
block_var: SyntaxTree::BlockVar[
|
|
508
|
+
params: SyntaxTree::Params[requireds: requireds]
|
|
509
|
+
],
|
|
510
|
+
bodystmt: SyntaxTree::BodyStmt[statements: statements]
|
|
511
|
+
]
|
|
512
|
+
]
|
|
513
|
+
Each(ctx, collection, requireds, statements)
|
|
514
|
+
|
|
515
|
+
# 3.times do |i| ... end
|
|
516
|
+
# for i := range 3 { ... }
|
|
517
|
+
# ---
|
|
518
|
+
in SyntaxTree::MethodAddBlock[
|
|
519
|
+
call: SyntaxTree::CallNode[
|
|
520
|
+
receiver: count,
|
|
521
|
+
message: SyntaxTree::Ident[value: "times"]
|
|
522
|
+
],
|
|
523
|
+
block: SyntaxTree::BlockNode[
|
|
524
|
+
block_var: SyntaxTree::BlockVar[
|
|
525
|
+
params: SyntaxTree::Params[requireds: requireds]
|
|
526
|
+
],
|
|
527
|
+
bodystmt: SyntaxTree::BodyStmt[statements: statements]
|
|
528
|
+
]
|
|
529
|
+
]
|
|
530
|
+
Each(ctx, count, requireds, statements)
|
|
531
|
+
|
|
532
|
+
# 100.times do ... end
|
|
533
|
+
# for range 100 { ... }
|
|
534
|
+
# ---
|
|
535
|
+
in SyntaxTree::MethodAddBlock[
|
|
536
|
+
call: SyntaxTree::CallNode[
|
|
537
|
+
receiver: count,
|
|
538
|
+
message: SyntaxTree::Ident[value: "times"]
|
|
539
|
+
],
|
|
540
|
+
block: SyntaxTree::BlockNode[
|
|
541
|
+
bodystmt: SyntaxTree::BodyStmt[statements: statements]
|
|
542
|
+
]
|
|
543
|
+
]
|
|
544
|
+
Each(ctx, count, [], statements)
|
|
545
|
+
|
|
546
|
+
# case ... end
|
|
547
|
+
# select { ... }
|
|
548
|
+
# ---
|
|
549
|
+
in SyntaxTree::Case[ value: nil, consequent: consequent ]
|
|
550
|
+
Select(ctx, consequent)
|
|
551
|
+
|
|
552
|
+
# case d = decl.(type) ... end
|
|
553
|
+
# switch d := decl.(type) { ... }
|
|
554
|
+
# ---
|
|
555
|
+
in SyntaxTree::Case[
|
|
556
|
+
value: SyntaxTree::Assign[
|
|
557
|
+
target: SyntaxTree::VarField[ value: { value: name } ],
|
|
558
|
+
value: SyntaxTree::CallNode[
|
|
559
|
+
receiver: receiver,
|
|
560
|
+
message: :call,
|
|
561
|
+
arguments: SyntaxTree::ArgParen[ arguments: SyntaxTree::Args[
|
|
562
|
+
parts: [ SyntaxTree::VCall[value: SyntaxTree::Ident[value: "type"]] ]
|
|
563
|
+
] ]
|
|
564
|
+
]
|
|
565
|
+
],
|
|
566
|
+
consequent: consequent
|
|
567
|
+
]
|
|
568
|
+
TypeSwitch(ctx, name, receiver, consequent)
|
|
569
|
+
|
|
570
|
+
# case s.Value.Kind() ... end
|
|
571
|
+
# switch s.Value.Kind() { ... }
|
|
572
|
+
# ---
|
|
573
|
+
in SyntaxTree::Case[ value: value, consequent: consequent ]
|
|
574
|
+
Switch(ctx, value, consequent)
|
|
575
|
+
|
|
576
|
+
else
|
|
577
|
+
nil
|
|
578
|
+
end
|
|
579
|
+
end
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
module VariableNodes
|
|
583
|
+
def Variable(node)
|
|
584
|
+
case node
|
|
585
|
+
in SyntaxTree::VarField[value: value]
|
|
586
|
+
Variable(value)
|
|
587
|
+
|
|
588
|
+
in SyntaxTree::VarRef[value: value]
|
|
589
|
+
Variable(value)
|
|
590
|
+
|
|
591
|
+
in SyntaxTree::VCall[value: value]
|
|
592
|
+
Variable(value)
|
|
593
|
+
|
|
594
|
+
in SyntaxTree::IVar[value: name]
|
|
595
|
+
SyntaxTree::Go::UnaryExpr.new(
|
|
596
|
+
op: :&,
|
|
597
|
+
x: SyntaxTree::Go::Ident.new(name: name.delete_prefix("@"))
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
in SyntaxTree::CVar[value: name]
|
|
601
|
+
SyntaxTree::Go::StarExpr.new(
|
|
602
|
+
x: SyntaxTree::Go::Ident.new(name: name.delete_prefix("@@"))
|
|
603
|
+
)
|
|
604
|
+
|
|
605
|
+
# STATUS_ACTIVE
|
|
606
|
+
# statusActive
|
|
607
|
+
# ---
|
|
608
|
+
in SyntaxTree::Const[value: name] if name == name.upcase
|
|
609
|
+
Constant(name)
|
|
610
|
+
|
|
611
|
+
in { value: String => name }
|
|
612
|
+
SyntaxTree::Go::Ident.new(name: name)
|
|
613
|
+
|
|
614
|
+
else
|
|
615
|
+
nil
|
|
616
|
+
end
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
# STATUS_ACTIVE
|
|
620
|
+
# statusActive
|
|
621
|
+
# ---
|
|
622
|
+
def Constant(name)
|
|
623
|
+
head, *tail = name.downcase.split("_")
|
|
624
|
+
|
|
625
|
+
Ident(head + tail.map(&:capitalize).join)
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
def Ident(name) = SyntaxTree::Go::Ident.new(name: name.to_s)
|
|
629
|
+
def BasicLit(kind, value) = SyntaxTree::Go::BasicLit.new(kind: kind.to_sym, value: value.to_s)
|
|
630
|
+
def CharLiteral(value) = BasicLit(:CHAR, "'#{value}'")
|
|
631
|
+
def StringLiteral(value) = BasicLit(:STRING, "\"#{value}\"")
|
|
632
|
+
def RawStringLiteral(value) = BasicLit(:STRING, "`#{value}`")
|
|
633
|
+
def IntLiteral(value) = BasicLit(:INT, value)
|
|
634
|
+
def FloatLiteral(value) = BasicLit(:FLOAT, value)
|
|
635
|
+
|
|
636
|
+
def Receive(x) = SyntaxTree::Go::UnaryExpr.new(op: :"<-", x: x)
|
|
637
|
+
|
|
638
|
+
def Binary(left, operator, right, location: SyntaxTree::Location.default)
|
|
639
|
+
SyntaxTree::Go::BinaryExpr.new(x: left, op: operator, y: right, location: location)
|
|
640
|
+
end
|
|
641
|
+
|
|
642
|
+
def Call(node)
|
|
643
|
+
case node
|
|
644
|
+
|
|
645
|
+
# h.Value().(Hash[string, int])
|
|
646
|
+
# h.Value().(map[string]int)
|
|
647
|
+
# ---
|
|
648
|
+
in SyntaxTree::CallNode[
|
|
649
|
+
receiver: receiver,
|
|
650
|
+
operator: SyntaxTree::Period[value: "."],
|
|
651
|
+
message: :call,
|
|
652
|
+
arguments: SyntaxTree::ArgParen[ arguments: SyntaxTree::Args[ parts: [ type ] ] ]
|
|
653
|
+
]
|
|
654
|
+
SyntaxTree::Go::TypeAssertExpr.new(x: Statements(receiver), type: Statements(type))
|
|
655
|
+
|
|
656
|
+
# article.new(Title: "Learning Go")
|
|
657
|
+
# article{Title: "Learning Go"}
|
|
658
|
+
# ---
|
|
659
|
+
in SyntaxTree::CallNode[
|
|
660
|
+
operator: SyntaxTree::Period[value: "."],
|
|
661
|
+
message: SyntaxTree::Ident[value: "new"]
|
|
662
|
+
]
|
|
663
|
+
Statements(node)
|
|
664
|
+
|
|
665
|
+
in SyntaxTree::CallNode[
|
|
666
|
+
receiver: receiver,
|
|
667
|
+
operator: SyntaxTree::Period[value: "."],
|
|
668
|
+
message: { value: message },
|
|
669
|
+
arguments: SyntaxTree::ArgParen[arguments: arguments]
|
|
670
|
+
]
|
|
671
|
+
SyntaxTree::Go::CallExpr.new(
|
|
672
|
+
fun: SyntaxTree::Go::SelectorExpr.new(
|
|
673
|
+
x: LeftHand(receiver),
|
|
674
|
+
sel: Ident(message)
|
|
675
|
+
),
|
|
676
|
+
args: Array(arguments&.parts).map { |argument| Statements(argument) }
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
in SyntaxTree::CallNode[
|
|
680
|
+
receiver: nil,
|
|
681
|
+
operator: nil,
|
|
682
|
+
message: SyntaxTree::Ident[value: message],
|
|
683
|
+
arguments: SyntaxTree::ArgParen[arguments: arguments]
|
|
684
|
+
]
|
|
685
|
+
SyntaxTree::Go::CallExpr.new(
|
|
686
|
+
fun: Ident(message),
|
|
687
|
+
args: Array(arguments&.parts).map { |argument| Statements(argument) }
|
|
688
|
+
)
|
|
689
|
+
|
|
690
|
+
in SyntaxTree::CallNode[
|
|
691
|
+
receiver: receiver,
|
|
692
|
+
operator: SyntaxTree::Period[value: "."],
|
|
693
|
+
message: { value: message },
|
|
694
|
+
arguments: nil
|
|
695
|
+
]
|
|
696
|
+
case receiver
|
|
697
|
+
# @priv.PublicKey
|
|
698
|
+
# &priv.PublicKey
|
|
699
|
+
# ---
|
|
700
|
+
in SyntaxTree::VarRef[value: SyntaxTree::IVar[value: name]]
|
|
701
|
+
SyntaxTree::Go::UnaryExpr.new(
|
|
702
|
+
op: :&,
|
|
703
|
+
x: SyntaxTree::Go::SelectorExpr.new(
|
|
704
|
+
x: Ident(name.delete_prefix("@")),
|
|
705
|
+
sel: Ident(message)
|
|
706
|
+
)
|
|
707
|
+
)
|
|
708
|
+
|
|
709
|
+
# @@ast.Ident
|
|
710
|
+
# *ast.Ident
|
|
711
|
+
# ---
|
|
712
|
+
in SyntaxTree::VarRef[value: SyntaxTree::CVar[value: name]]
|
|
713
|
+
SyntaxTree::Go::StarExpr.new(
|
|
714
|
+
x: SyntaxTree::Go::SelectorExpr.new(
|
|
715
|
+
x: Ident(name.delete_prefix("@@")),
|
|
716
|
+
sel: Ident(message)
|
|
717
|
+
)
|
|
718
|
+
)
|
|
719
|
+
|
|
720
|
+
else
|
|
721
|
+
SyntaxTree::Go::SelectorExpr.new(x: LeftHand(receiver), sel: Ident(message))
|
|
722
|
+
end
|
|
723
|
+
|
|
724
|
+
else
|
|
725
|
+
nil
|
|
726
|
+
end
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
end
|
|
730
|
+
|
|
731
|
+
module StatementNodes
|
|
732
|
+
def Package(name:, declarations:)
|
|
733
|
+
SyntaxTree::Go::File.new(
|
|
734
|
+
name: SyntaxTree::Go::Ident.new(name: name.to_s),
|
|
735
|
+
decls: declarations
|
|
736
|
+
)
|
|
737
|
+
end
|
|
738
|
+
|
|
739
|
+
def LeftHand(node)
|
|
740
|
+
case node
|
|
741
|
+
in SyntaxTree::CallNode
|
|
742
|
+
Call(node)
|
|
743
|
+
|
|
744
|
+
in SyntaxTree::ARefField[ collection: collection, index: SyntaxTree::Args[ parts: [ index ] ] ]
|
|
745
|
+
SyntaxTree::Go::IndexExpr.new(x: Statements(collection), index: Statements(index))
|
|
746
|
+
|
|
747
|
+
in SyntaxTree::MLHS[parts: parts]
|
|
748
|
+
parts.map { |part| LeftHand(part) }
|
|
749
|
+
|
|
750
|
+
in SyntaxTree::MRHS[parts: parts]
|
|
751
|
+
parts.map { |part| LeftHand(part) }
|
|
752
|
+
|
|
753
|
+
in SyntaxTree::Int[value: text]
|
|
754
|
+
IntLiteral(text)
|
|
755
|
+
|
|
756
|
+
in SyntaxTree::FloatLiteral[value: text]
|
|
757
|
+
FloatLiteral(text)
|
|
758
|
+
|
|
759
|
+
in SyntaxTree::Binary[left: left, operator: operator, right: right] => binary
|
|
760
|
+
Binary(Statements(left), operator, Statements(right), location: binary.location)
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
in SyntaxTree::VarField[value: value]
|
|
765
|
+
LeftHand(value)
|
|
766
|
+
|
|
767
|
+
in SyntaxTree::VCall[value: value]
|
|
768
|
+
LeftHand(value)
|
|
769
|
+
|
|
770
|
+
in SyntaxTree::Field[parent: parent, operator: SyntaxTree::Period[value: "."], name: { value: name }]
|
|
771
|
+
SyntaxTree::Go::SelectorExpr.new(x: LeftHand(parent), sel: Ident(name))
|
|
772
|
+
|
|
773
|
+
in SyntaxTree::StringLiteral[parts: [SyntaxTree::TStringContent[value: text]]]
|
|
774
|
+
StringLiteral(text)
|
|
775
|
+
|
|
776
|
+
in SyntaxTree::StringLiteral[parts: []]
|
|
777
|
+
StringLiteral("")
|
|
778
|
+
|
|
779
|
+
in SyntaxTree::XStringLiteral[parts: [SyntaxTree::TStringContent[value: text]]]
|
|
780
|
+
RawStringLiteral(text)
|
|
781
|
+
|
|
782
|
+
else
|
|
783
|
+
Variable(node)
|
|
784
|
+
end
|
|
785
|
+
end
|
|
786
|
+
|
|
787
|
+
def RightHand(node) = Statements(node)
|
|
788
|
+
|
|
789
|
+
# city = "Wellington"
|
|
790
|
+
# city := "Wellington"
|
|
791
|
+
# ---
|
|
792
|
+
# x, y = 10, 20.5
|
|
793
|
+
# x, y = 10, 20.5
|
|
794
|
+
# ---
|
|
795
|
+
def Assignment(ctx, target, value)
|
|
796
|
+
lhs = Array(LeftHand(target))
|
|
797
|
+
names = lhs.grep(SyntaxTree::Go::Ident).map(&:name) - ["_"]
|
|
798
|
+
|
|
799
|
+
SyntaxTree::Go::AssignStmt.new(
|
|
800
|
+
lhs: lhs,
|
|
801
|
+
tok_pos: nil,
|
|
802
|
+
tok: ctx.declare(names).any? ? ":=" : "=",
|
|
803
|
+
rhs: Array(RightHand(value))
|
|
804
|
+
)
|
|
805
|
+
end
|
|
806
|
+
|
|
807
|
+
# var :name, string, "Nathan"
|
|
808
|
+
# var name string = "Nathan"
|
|
809
|
+
# ---
|
|
810
|
+
# const :greeting, "hi"
|
|
811
|
+
# const greeting = "hi"
|
|
812
|
+
# ---
|
|
813
|
+
def Declaration(tok, variable, type, value)
|
|
814
|
+
declare([variable])
|
|
815
|
+
|
|
816
|
+
SyntaxTree::Go::DeclStmt.new(
|
|
817
|
+
decl: SyntaxTree::Go::GenDecl.new(
|
|
818
|
+
tok: tok,
|
|
819
|
+
specs: [
|
|
820
|
+
SyntaxTree::Go::ValueSpec.new(
|
|
821
|
+
names: [Ident(variable)],
|
|
822
|
+
type: type && Statements(type),
|
|
823
|
+
values: value ? [Statements(value)] : []
|
|
824
|
+
)
|
|
825
|
+
]
|
|
826
|
+
)
|
|
827
|
+
)
|
|
828
|
+
end
|
|
829
|
+
|
|
830
|
+
# runtime: runRuntime
|
|
831
|
+
# "runtime": runRuntime
|
|
832
|
+
# ---
|
|
833
|
+
def Entry(type, entry)
|
|
834
|
+
case [type, entry]
|
|
835
|
+
in [SyntaxTree::Go::MapType, SyntaxTree::Go::KeyValueExpr[key: SyntaxTree::Go::Ident[name: name], value: value]]
|
|
836
|
+
SyntaxTree::Go::KeyValueExpr.new(key: StringLiteral(name), value: value)
|
|
837
|
+
|
|
838
|
+
else
|
|
839
|
+
entry
|
|
840
|
+
end
|
|
841
|
+
end
|
|
842
|
+
|
|
843
|
+
def Command(name, args)
|
|
844
|
+
case [name, args]
|
|
845
|
+
in ["require", [SyntaxTree::StringLiteral[parts: [SyntaxTree::TStringContent[value: path]]]]]
|
|
846
|
+
ImportSpec(path)
|
|
847
|
+
|
|
848
|
+
# require "encoding/json/v2", json
|
|
849
|
+
# json "encoding/json/v2"
|
|
850
|
+
# ---
|
|
851
|
+
in ["require", [SyntaxTree::StringLiteral[parts: [SyntaxTree::TStringContent[value: path]]], name]]
|
|
852
|
+
ImportSpec(path, name: Statements(name))
|
|
853
|
+
|
|
854
|
+
# defer h.Delete()
|
|
855
|
+
# defer h.Delete()
|
|
856
|
+
# ---
|
|
857
|
+
in ["defer", [call]]
|
|
858
|
+
SyntaxTree::Go::DeferStmt.new(call: Statements(call))
|
|
859
|
+
|
|
860
|
+
# var demos = Hash[string, Proc].new(...)
|
|
861
|
+
# var demos = map[string]func(){...}
|
|
862
|
+
# ---
|
|
863
|
+
in ["var" | "const" => tok, [SyntaxTree::Assign[target: target, value: value]]]
|
|
864
|
+
SyntaxTree::Go::GenDecl.new(
|
|
865
|
+
tok: tok.to_sym,
|
|
866
|
+
specs: [ SyntaxTree::Go::ValueSpec.new( names: [LeftHand(target)], values: [RightHand(value)] ) ]
|
|
867
|
+
)
|
|
868
|
+
|
|
869
|
+
# var mixed Array[byte, 16]
|
|
870
|
+
# var mixed [16]byte
|
|
871
|
+
# ---
|
|
872
|
+
in ["var", [SyntaxTree::Command[
|
|
873
|
+
message: SyntaxTree::Ident[value: variable],
|
|
874
|
+
arguments: SyntaxTree::Args[ parts: [ type ] ]
|
|
875
|
+
]]]
|
|
876
|
+
Declaration(:var, variable, type, nil)
|
|
877
|
+
|
|
878
|
+
# var :a, int
|
|
879
|
+
# var a int
|
|
880
|
+
# ---
|
|
881
|
+
in ["var", [SyntaxTree::SymbolLiteral[value: SyntaxTree::Ident[value: variable]], type]]
|
|
882
|
+
Declaration(:var, variable, type, nil)
|
|
883
|
+
|
|
884
|
+
# var :name, string, "Nathan"
|
|
885
|
+
# var name string = "Nathan"
|
|
886
|
+
# ---
|
|
887
|
+
in ["var", [SyntaxTree::SymbolLiteral[value: SyntaxTree::Ident[value: variable]], type, value]]
|
|
888
|
+
Declaration(:var, variable, type, value)
|
|
889
|
+
|
|
890
|
+
# const :greeting, "hi"
|
|
891
|
+
# const greeting = "hi"
|
|
892
|
+
# ---
|
|
893
|
+
in ["const", [SyntaxTree::SymbolLiteral[value: SyntaxTree::Ident[value: variable]], value]]
|
|
894
|
+
Declaration(:const, variable, nil, value)
|
|
895
|
+
|
|
896
|
+
else
|
|
897
|
+
nil
|
|
898
|
+
end
|
|
899
|
+
end
|
|
900
|
+
|
|
901
|
+
def Statements(node)
|
|
902
|
+
case node
|
|
903
|
+
in SyntaxTree::Assign[
|
|
904
|
+
target: SyntaxTree::VarField[value: SyntaxTree::Ident[value: "err"]],
|
|
905
|
+
value: SyntaxTree::CallNode[
|
|
906
|
+
receiver: nil,
|
|
907
|
+
operator: nil,
|
|
908
|
+
message: SyntaxTree::Ident[value: "getSomeValue"],
|
|
909
|
+
arguments: SyntaxTree::ArgParen[arguments: nil]
|
|
910
|
+
]
|
|
911
|
+
]
|
|
912
|
+
nil
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
# Hash[string, bool]
|
|
916
|
+
# map[string]bool
|
|
917
|
+
# ---
|
|
918
|
+
in SyntaxTree::ARef[
|
|
919
|
+
collection: SyntaxTree::VarRef[value: SyntaxTree::Const[value: "Hash"]],
|
|
920
|
+
index: SyntaxTree::Args[ parts: [ key, value ] ]
|
|
921
|
+
]
|
|
922
|
+
SyntaxTree::Go::MapType.new(
|
|
923
|
+
key: Statements(key),
|
|
924
|
+
value: Statements(value)
|
|
925
|
+
)
|
|
926
|
+
|
|
927
|
+
# Chan[int]
|
|
928
|
+
# chan int
|
|
929
|
+
# ---
|
|
930
|
+
in SyntaxTree::ARef[
|
|
931
|
+
collection: SyntaxTree::VarRef[ value: SyntaxTree::Const[ value: "Chan" ] ],
|
|
932
|
+
index: SyntaxTree::Args[ parts: [ type ] ]
|
|
933
|
+
]
|
|
934
|
+
SyntaxTree::Go::ChanType.new(dir: 3, value: Statements(type))
|
|
935
|
+
|
|
936
|
+
# Array[string]
|
|
937
|
+
# []string
|
|
938
|
+
# ---
|
|
939
|
+
in SyntaxTree::ARef[
|
|
940
|
+
collection: SyntaxTree::VarRef[ value: SyntaxTree::Const[ value: "Array" ] ],
|
|
941
|
+
index: SyntaxTree::Args[ parts: [ type ] ]
|
|
942
|
+
]
|
|
943
|
+
SyntaxTree::Go::ArrayType.new( len: nil, elt: Statements(type) )
|
|
944
|
+
|
|
945
|
+
# Array[string, 5]
|
|
946
|
+
# [5]string
|
|
947
|
+
# ---
|
|
948
|
+
in SyntaxTree::ARef[
|
|
949
|
+
collection: SyntaxTree::VarRef[ value: SyntaxTree::Const[ value: "Array" ] ],
|
|
950
|
+
index: SyntaxTree::Args[ parts: [ type, size ] ]
|
|
951
|
+
]
|
|
952
|
+
SyntaxTree::Go::ArrayType.new( len: Statements(size), elt: Statements(type) )
|
|
953
|
+
|
|
954
|
+
# Proc
|
|
955
|
+
# func()
|
|
956
|
+
# ---
|
|
957
|
+
in SyntaxTree::VarRef[ value: SyntaxTree::Const[ value: "Proc" ] ]
|
|
958
|
+
SyntaxTree::Go::FuncType.new(
|
|
959
|
+
params: SyntaxTree::Go::FieldList.new(list: []),
|
|
960
|
+
results: nil
|
|
961
|
+
)
|
|
962
|
+
|
|
963
|
+
# Array[byte].new("hello")
|
|
964
|
+
# []byte("hello")
|
|
965
|
+
# ---
|
|
966
|
+
in SyntaxTree::CallNode[
|
|
967
|
+
receiver: SyntaxTree::ARef[
|
|
968
|
+
collection: SyntaxTree::VarRef[ value: SyntaxTree::Const[value: "Array"] ],
|
|
969
|
+
index: SyntaxTree::Args[
|
|
970
|
+
parts: [ SyntaxTree::VCall[value: SyntaxTree::Ident[value: "byte" | "rune"]] ]
|
|
971
|
+
]
|
|
972
|
+
] => receiver,
|
|
973
|
+
operator: SyntaxTree::Period[value: "."],
|
|
974
|
+
message: SyntaxTree::Ident[value: "new"],
|
|
975
|
+
arguments: SyntaxTree::ArgParen[
|
|
976
|
+
arguments: SyntaxTree::Args[ parts: [ SyntaxTree::StringLiteral | SyntaxTree::XStringLiteral => text ] ]
|
|
977
|
+
]
|
|
978
|
+
]
|
|
979
|
+
SyntaxTree::Go::CallExpr.new(
|
|
980
|
+
fun: Statements(receiver),
|
|
981
|
+
args: [ Statements(text) ]
|
|
982
|
+
)
|
|
983
|
+
|
|
984
|
+
# @url.URL.new(Scheme: "https")
|
|
985
|
+
# &url.URL{Scheme: "https"}
|
|
986
|
+
# ---
|
|
987
|
+
in SyntaxTree::CallNode[
|
|
988
|
+
receiver: SyntaxTree::CallNode[
|
|
989
|
+
receiver: SyntaxTree::VarRef[ value: SyntaxTree::IVar[value: name] ],
|
|
990
|
+
operator: SyntaxTree::Period[value: "."],
|
|
991
|
+
message: { value: selector }
|
|
992
|
+
],
|
|
993
|
+
operator: SyntaxTree::Period[value: "."],
|
|
994
|
+
message: SyntaxTree::Ident[value: "new"],
|
|
995
|
+
arguments: arguments
|
|
996
|
+
]
|
|
997
|
+
SyntaxTree::Go::UnaryExpr.new(
|
|
998
|
+
op: :&,
|
|
999
|
+
x: SyntaxTree::Go::CompositeLit.new(
|
|
1000
|
+
type: SyntaxTree::Go::SelectorExpr.new(
|
|
1001
|
+
x: Ident(name.delete_prefix("@")),
|
|
1002
|
+
sel: Ident(selector)
|
|
1003
|
+
),
|
|
1004
|
+
elts: Array(arguments&.arguments&.parts).flat_map { |item| Statements(item) },
|
|
1005
|
+
location: arguments&.location || SyntaxTree::Location.default
|
|
1006
|
+
)
|
|
1007
|
+
)
|
|
1008
|
+
|
|
1009
|
+
# Array[string].new("go", "stdlib")
|
|
1010
|
+
# Hash[string, int].new("answer" => 42)
|
|
1011
|
+
# article.new(Title: "Learning Go")
|
|
1012
|
+
# []string{"go", "stdlib"}
|
|
1013
|
+
# map[string]int{"answer": 42}
|
|
1014
|
+
# article{Title: "Learning Go"}
|
|
1015
|
+
# ---
|
|
1016
|
+
in SyntaxTree::CallNode[
|
|
1017
|
+
receiver: receiver,
|
|
1018
|
+
operator: SyntaxTree::Period[value: "."],
|
|
1019
|
+
message: SyntaxTree::Ident[value: "new"],
|
|
1020
|
+
arguments: arguments
|
|
1021
|
+
]
|
|
1022
|
+
Statements(receiver).then do |type|
|
|
1023
|
+
SyntaxTree::Go::CompositeLit.new(
|
|
1024
|
+
type: type,
|
|
1025
|
+
elts: Array(arguments&.arguments&.parts).flat_map { |item|
|
|
1026
|
+
Array(Statements(item)).map { |entry| Entry(type, entry) }
|
|
1027
|
+
},
|
|
1028
|
+
location: arguments&.location || SyntaxTree::Location.default
|
|
1029
|
+
)
|
|
1030
|
+
end
|
|
1031
|
+
|
|
1032
|
+
# Title: "Learning Go"
|
|
1033
|
+
# Title: "Learning Go"
|
|
1034
|
+
# ---
|
|
1035
|
+
in SyntaxTree::BareAssocHash[ assocs: assocs ]
|
|
1036
|
+
assocs.map { |assoc| Statements(assoc) }
|
|
1037
|
+
|
|
1038
|
+
in SyntaxTree::Assoc[
|
|
1039
|
+
key: SyntaxTree::Label[value: label],
|
|
1040
|
+
value: value
|
|
1041
|
+
]
|
|
1042
|
+
SyntaxTree::Go::KeyValueExpr.new(
|
|
1043
|
+
key: Ident(label.delete_suffix(":")),
|
|
1044
|
+
value: Statements(value)
|
|
1045
|
+
)
|
|
1046
|
+
|
|
1047
|
+
# "q": ["go & rust"]
|
|
1048
|
+
# "q": {"go & rust"}
|
|
1049
|
+
# ---
|
|
1050
|
+
in SyntaxTree::Assoc[
|
|
1051
|
+
key: SyntaxTree::DynaSymbol[parts: [SyntaxTree::TStringContent[value: text]]],
|
|
1052
|
+
value: value
|
|
1053
|
+
]
|
|
1054
|
+
SyntaxTree::Go::KeyValueExpr.new(
|
|
1055
|
+
key: StringLiteral(text),
|
|
1056
|
+
value: Statements(value)
|
|
1057
|
+
)
|
|
1058
|
+
|
|
1059
|
+
in SyntaxTree::Assoc[ key: key, value: value ]
|
|
1060
|
+
SyntaxTree::Go::KeyValueExpr.new(
|
|
1061
|
+
key: Statements(key),
|
|
1062
|
+
value: Statements(value)
|
|
1063
|
+
)
|
|
1064
|
+
|
|
1065
|
+
# { Name: "session", Value: "abc123" }
|
|
1066
|
+
# {Name: "session", Value: "abc123"}
|
|
1067
|
+
# ---
|
|
1068
|
+
in SyntaxTree::HashLiteral[ assocs: assocs ] => hash
|
|
1069
|
+
SyntaxTree::Go::CompositeLit.new(
|
|
1070
|
+
type: nil,
|
|
1071
|
+
elts: assocs.map { |assoc| Statements(assoc) },
|
|
1072
|
+
location: hash.location
|
|
1073
|
+
)
|
|
1074
|
+
|
|
1075
|
+
# ["go & rust"]
|
|
1076
|
+
# {"go & rust"}
|
|
1077
|
+
# ---
|
|
1078
|
+
in SyntaxTree::ArrayLiteral[ contents: SyntaxTree::Args[ parts: parts ] ] => array
|
|
1079
|
+
SyntaxTree::Go::CompositeLit.new(
|
|
1080
|
+
type: nil,
|
|
1081
|
+
elts: parts.map { |part| Statements(part) },
|
|
1082
|
+
location: array.location
|
|
1083
|
+
)
|
|
1084
|
+
|
|
1085
|
+
# digest[]
|
|
1086
|
+
# digest[:]
|
|
1087
|
+
# ---
|
|
1088
|
+
in SyntaxTree::ARef[ collection: collection, index: nil ]
|
|
1089
|
+
SyntaxTree::Go::SliceExpr.new(x: Statements(collection), low: nil, high: nil)
|
|
1090
|
+
|
|
1091
|
+
# snippet[...400]
|
|
1092
|
+
# snippet[:400]
|
|
1093
|
+
# ---
|
|
1094
|
+
in SyntaxTree::ARef[
|
|
1095
|
+
collection: collection,
|
|
1096
|
+
index: SyntaxTree::Args[ parts: [ SyntaxTree::RangeNode[ left: low, right: high ] ] ]
|
|
1097
|
+
]
|
|
1098
|
+
SyntaxTree::Go::SliceExpr.new(
|
|
1099
|
+
x: Statements(collection),
|
|
1100
|
+
low: low && Statements(low),
|
|
1101
|
+
high: high && Statements(high)
|
|
1102
|
+
)
|
|
1103
|
+
|
|
1104
|
+
# demos[os.Args[1]]
|
|
1105
|
+
# demos[os.Args[1]]
|
|
1106
|
+
# ---
|
|
1107
|
+
in SyntaxTree::ARef[ collection: collection, index: SyntaxTree::Args[ parts: [ index ] ] ]
|
|
1108
|
+
SyntaxTree::Go::IndexExpr.new(
|
|
1109
|
+
x: Statements(collection),
|
|
1110
|
+
index: Statements(index)
|
|
1111
|
+
)
|
|
1112
|
+
|
|
1113
|
+
#in SyntaxTree::ARef[collection: collection, index: SyntaxTree::Args[parts: [index]]]
|
|
1114
|
+
# SyntaxTree::Go::IndexExpr.new(
|
|
1115
|
+
# x: LeftHand(collection),
|
|
1116
|
+
# index: LeftHand(index)
|
|
1117
|
+
# )
|
|
1118
|
+
|
|
1119
|
+
#in SyntaxTree::ARefField[collection: collection, index: SyntaxTree::Args[parts: [index]]]
|
|
1120
|
+
# SyntaxTree::Go::IndexExpr.new(
|
|
1121
|
+
# x: LeftHand(collection),
|
|
1122
|
+
# index: LeftHand(index)
|
|
1123
|
+
# )
|
|
1124
|
+
|
|
1125
|
+
#in SyntaxTree::Go::ReturnStmt
|
|
1126
|
+
# SyntaxTree::Go::ReturnStmt.new(return_: nil, results: nil)
|
|
1127
|
+
|
|
1128
|
+
#in SyntaxTree::Go::IncDecStmt
|
|
1129
|
+
# SyntaxTree::Go::IncDecStmt.new(x: nil, tok_pos: nil, tok: nil)
|
|
1130
|
+
|
|
1131
|
+
#in SyntaxTree::Go::ExprStmt
|
|
1132
|
+
# SyntaxTree::Go::ExprStmt.new(x: nil)
|
|
1133
|
+
|
|
1134
|
+
#in SyntaxTree::Go::IndexExpr
|
|
1135
|
+
# SyntaxTree::Go::IndexStmt.new(x: nil)
|
|
1136
|
+
|
|
1137
|
+
#in SyntaxTree::Go::SliceExpr
|
|
1138
|
+
# SyntaxTree::Go::SliceStmt.new(x: nil)
|
|
1139
|
+
|
|
1140
|
+
#in SyntaxTree::Go::SelectorExpr
|
|
1141
|
+
# SyntaxTree::Go::SelectorExpr.new(x: nil)
|
|
1142
|
+
|
|
1143
|
+
#in SyntaxTree::Go::ArrayType
|
|
1144
|
+
# SyntaxTree::Go::ArrayType.new(x: nil)
|
|
1145
|
+
|
|
1146
|
+
#in SyntaxTree::Go::MapType
|
|
1147
|
+
# SyntaxTree::Go::MapType.new(x: nil)
|
|
1148
|
+
|
|
1149
|
+
#in SyntaxTree::Go::StarExpr
|
|
1150
|
+
# SyntaxTree::Go::StarExpr.new(x: nil)
|
|
1151
|
+
|
|
1152
|
+
#in SyntaxTree::Go::UnaryExpr
|
|
1153
|
+
# SyntaxTree::Go::UnaryExpr.new(x: nil)
|
|
1154
|
+
|
|
1155
|
+
#in SyntaxTree::Go::KeyValueExpr
|
|
1156
|
+
# SyntaxTree::Go::KeyValueExpr.new(x: nil)
|
|
1157
|
+
|
|
1158
|
+
#in SyntaxTree::Go::CompositeLit
|
|
1159
|
+
# SyntaxTree::Go::CompositeLit.new(x: nil)
|
|
1160
|
+
|
|
1161
|
+
in SyntaxTree::Command[
|
|
1162
|
+
message: SyntaxTree::Ident[value: name],
|
|
1163
|
+
arguments: SyntaxTree::Args[parts: args]
|
|
1164
|
+
]
|
|
1165
|
+
Command(name, args)
|
|
1166
|
+
|
|
1167
|
+
in SyntaxTree::CallNode
|
|
1168
|
+
Call(node)
|
|
1169
|
+
|
|
1170
|
+
in SyntaxTree::Assign[target: target, value: value]
|
|
1171
|
+
SyntaxTree::Go::AssignStmt.new(
|
|
1172
|
+
lhs: Array(LeftHand(target)),
|
|
1173
|
+
tok_pos: nil,
|
|
1174
|
+
tok: ":=",
|
|
1175
|
+
rhs: Array(RightHand(value))
|
|
1176
|
+
)
|
|
1177
|
+
|
|
1178
|
+
in SyntaxTree::MAssign[
|
|
1179
|
+
target: SyntaxTree::MLHS[
|
|
1180
|
+
parts: [
|
|
1181
|
+
SyntaxTree::VarField[value: SyntaxTree::Ident[value: "_"]],
|
|
1182
|
+
SyntaxTree::VarField[value: SyntaxTree::Ident[value: "err"]]
|
|
1183
|
+
]
|
|
1184
|
+
],
|
|
1185
|
+
value: SyntaxTree::CallNode[
|
|
1186
|
+
receiver: nil,
|
|
1187
|
+
operator: nil,
|
|
1188
|
+
message: SyntaxTree::Ident[value: "getSomeValue"],
|
|
1189
|
+
arguments: SyntaxTree::ArgParen[arguments: nil]
|
|
1190
|
+
]
|
|
1191
|
+
]
|
|
1192
|
+
nil
|
|
1193
|
+
|
|
1194
|
+
in SyntaxTree::MAssign[target: target, value: value]
|
|
1195
|
+
SyntaxTree::Go::AssignStmt.new(
|
|
1196
|
+
lhs: Array(LeftHand(target)),
|
|
1197
|
+
tok_pos: nil,
|
|
1198
|
+
tok: ":=",
|
|
1199
|
+
rhs: Array(RightHand(value))
|
|
1200
|
+
)
|
|
1201
|
+
|
|
1202
|
+
in SyntaxTree::MethodAddBlock[
|
|
1203
|
+
call: SyntaxTree::CallNode[
|
|
1204
|
+
receiver: SyntaxTree::Paren[
|
|
1205
|
+
contents: SyntaxTree::Statements[
|
|
1206
|
+
body: [
|
|
1207
|
+
SyntaxTree::RangeNode[
|
|
1208
|
+
left: SyntaxTree::Int[value: "1"],
|
|
1209
|
+
operator: SyntaxTree::Op[value: ".."],
|
|
1210
|
+
right: SyntaxTree::Int[value: "5"]
|
|
1211
|
+
]
|
|
1212
|
+
]
|
|
1213
|
+
]
|
|
1214
|
+
],
|
|
1215
|
+
operator: SyntaxTree::Period[value: "."],
|
|
1216
|
+
message: SyntaxTree::Ident[value: "each"]
|
|
1217
|
+
],
|
|
1218
|
+
block: SyntaxTree::BlockNode[
|
|
1219
|
+
block_var: SyntaxTree::BlockVar[
|
|
1220
|
+
params: SyntaxTree::Params[
|
|
1221
|
+
requireds: [SyntaxTree::Ident[value: "i"]]
|
|
1222
|
+
]
|
|
1223
|
+
],
|
|
1224
|
+
bodystmt: SyntaxTree::BodyStmt[
|
|
1225
|
+
statements: SyntaxTree::Statements[
|
|
1226
|
+
body: [SyntaxTree::VarRef[value: SyntaxTree::Ident[value: "i"]]]
|
|
1227
|
+
]
|
|
1228
|
+
]
|
|
1229
|
+
]
|
|
1230
|
+
]
|
|
1231
|
+
nil
|
|
1232
|
+
|
|
1233
|
+
# lambda { |w, r| ... }
|
|
1234
|
+
# func(w http.ResponseWriter, r *http.Request) { ... }
|
|
1235
|
+
# ---
|
|
1236
|
+
in SyntaxTree::MethodAddBlock[
|
|
1237
|
+
call: SyntaxTree::CallNode[ message: SyntaxTree::Ident[value: "lambda"] ],
|
|
1238
|
+
block: block
|
|
1239
|
+
]
|
|
1240
|
+
Lambda(block)
|
|
1241
|
+
|
|
1242
|
+
# ch << "ping"
|
|
1243
|
+
# ch <- "ping"
|
|
1244
|
+
# ---
|
|
1245
|
+
in SyntaxTree::Binary[ left: left, operator: :<<, right: right ]
|
|
1246
|
+
SyntaxTree::Go::SendStmt.new(chan: Statements(left), value: Statements(right))
|
|
1247
|
+
|
|
1248
|
+
# break
|
|
1249
|
+
# break
|
|
1250
|
+
# ---
|
|
1251
|
+
in SyntaxTree::Break
|
|
1252
|
+
SyntaxTree::Go::BranchStmt.new(tok: :break)
|
|
1253
|
+
|
|
1254
|
+
# next
|
|
1255
|
+
# continue
|
|
1256
|
+
# ---
|
|
1257
|
+
in SyntaxTree::Next
|
|
1258
|
+
SyntaxTree::Go::BranchStmt.new(tok: :continue)
|
|
1259
|
+
|
|
1260
|
+
# return "discarded", "kept"
|
|
1261
|
+
# return "discarded", "kept"
|
|
1262
|
+
# ---
|
|
1263
|
+
in SyntaxTree::ReturnNode[arguments: SyntaxTree::Args[ parts: parts ]]
|
|
1264
|
+
SyntaxTree::Go::ReturnStmt.new(results: parts.map { |part| Statements(part) })
|
|
1265
|
+
|
|
1266
|
+
# return
|
|
1267
|
+
# return
|
|
1268
|
+
# ---
|
|
1269
|
+
in SyntaxTree::ReturnNode
|
|
1270
|
+
SyntaxTree::Go::ReturnStmt.new(results: [])
|
|
1271
|
+
|
|
1272
|
+
# ~time.After(50 * time.Millisecond)
|
|
1273
|
+
# <-time.After(50 * time.Millisecond)
|
|
1274
|
+
# ---
|
|
1275
|
+
in SyntaxTree::Unary[ operator: "~", statement: statement ]
|
|
1276
|
+
Receive(Statements(statement))
|
|
1277
|
+
|
|
1278
|
+
# -12345
|
|
1279
|
+
# -12345
|
|
1280
|
+
# ---
|
|
1281
|
+
in SyntaxTree::Unary[ operator: operator, statement: statement ]
|
|
1282
|
+
SyntaxTree::Go::UnaryExpr.new(op: operator.to_sym, x: Statements(statement))
|
|
1283
|
+
|
|
1284
|
+
# :int32
|
|
1285
|
+
# int32
|
|
1286
|
+
# ---
|
|
1287
|
+
in SyntaxTree::SymbolLiteral[value: SyntaxTree::Ident[value: name]]
|
|
1288
|
+
Ident(name)
|
|
1289
|
+
|
|
1290
|
+
in SyntaxTree::VoidStmt
|
|
1291
|
+
nil
|
|
1292
|
+
else
|
|
1293
|
+
LeftHand(node)
|
|
1294
|
+
end
|
|
1295
|
+
end
|
|
1296
|
+
|
|
1297
|
+
def ImportSpec(path, name: nil)
|
|
1298
|
+
SyntaxTree::Go::ImportSpec.new(
|
|
1299
|
+
doc: nil,
|
|
1300
|
+
name: name,
|
|
1301
|
+
path: StringLiteral(path),
|
|
1302
|
+
comment: nil,
|
|
1303
|
+
end_pos: nil
|
|
1304
|
+
)
|
|
1305
|
+
end
|
|
1306
|
+
|
|
1307
|
+
end
|
|
1308
|
+
|
|
1309
|
+
def call(node)
|
|
1310
|
+
root_context = Context.new(node.statements.body)
|
|
1311
|
+
|
|
1312
|
+
root_context.match do
|
|
1313
|
+
before do |ctx|
|
|
1314
|
+
end
|
|
1315
|
+
|
|
1316
|
+
iterate do |ctx, node|
|
|
1317
|
+
case node
|
|
1318
|
+
in SyntaxTree::ModuleDeclaration[
|
|
1319
|
+
constant: SyntaxTree::ConstRef[
|
|
1320
|
+
constant: SyntaxTree::Const[value: "Main"]
|
|
1321
|
+
],
|
|
1322
|
+
bodystmt: SyntaxTree::BodyStmt[
|
|
1323
|
+
statements: SyntaxTree::Statements[body: body]
|
|
1324
|
+
]
|
|
1325
|
+
]
|
|
1326
|
+
Hoist.new(body)
|
|
1327
|
+
|
|
1328
|
+
# var colour = Struct.new(R: byte) do def M ... end end
|
|
1329
|
+
# type colour struct { R byte }
|
|
1330
|
+
# func (self colour) M() { ... }
|
|
1331
|
+
# ---
|
|
1332
|
+
in SyntaxTree::Command[
|
|
1333
|
+
message: SyntaxTree::Ident[value: "var"],
|
|
1334
|
+
arguments: SyntaxTree::Args[ parts: [ SyntaxTree::Assign[
|
|
1335
|
+
target: SyntaxTree::VarField[ value: { value: name } ],
|
|
1336
|
+
value: SyntaxTree::CallNode[
|
|
1337
|
+
receiver: SyntaxTree::VarRef[ value: SyntaxTree::Const[value: "Struct"] ],
|
|
1338
|
+
message: SyntaxTree::Ident[value: "new"],
|
|
1339
|
+
arguments: SyntaxTree::ArgParen[ arguments: SyntaxTree::Args[
|
|
1340
|
+
parts: [ SyntaxTree::BareAssocHash[ assocs: assocs ] ]
|
|
1341
|
+
] ]
|
|
1342
|
+
]
|
|
1343
|
+
] ] ],
|
|
1344
|
+
block: SyntaxTree::BlockNode[
|
|
1345
|
+
bodystmt: SyntaxTree::BodyStmt[ statements: SyntaxTree::Statements[ body: methods ] ]
|
|
1346
|
+
]
|
|
1347
|
+
]
|
|
1348
|
+
[ TypeDecl(name, Fields(assocs)), *Methods(name, methods) ]
|
|
1349
|
+
|
|
1350
|
+
# var person = Struct.new(Name: string, Age: int)
|
|
1351
|
+
# type person struct { Name string; Age int }
|
|
1352
|
+
# ---
|
|
1353
|
+
in SyntaxTree::Command[
|
|
1354
|
+
message: SyntaxTree::Ident[value: "var"],
|
|
1355
|
+
arguments: SyntaxTree::Args[ parts: [ SyntaxTree::Assign[
|
|
1356
|
+
target: SyntaxTree::VarField[ value: { value: name } ],
|
|
1357
|
+
value: SyntaxTree::CallNode[
|
|
1358
|
+
receiver: SyntaxTree::VarRef[ value: SyntaxTree::Const[value: "Struct"] ],
|
|
1359
|
+
message: SyntaxTree::Ident[value: "new"],
|
|
1360
|
+
arguments: SyntaxTree::ArgParen[ arguments: SyntaxTree::Args[
|
|
1361
|
+
parts: [ SyntaxTree::BareAssocHash[ assocs: assocs ] ]
|
|
1362
|
+
] ]
|
|
1363
|
+
]
|
|
1364
|
+
] ] ]
|
|
1365
|
+
]
|
|
1366
|
+
TypeDecl(name, Fields(assocs))
|
|
1367
|
+
|
|
1368
|
+
# class Args
|
|
1369
|
+
# type Args struct { ... }
|
|
1370
|
+
# ---
|
|
1371
|
+
in SyntaxTree::ClassDeclaration[
|
|
1372
|
+
constant: SyntaxTree::ConstRef[ constant: SyntaxTree::Const[value: name] ],
|
|
1373
|
+
bodystmt: SyntaxTree::BodyStmt[ statements: SyntaxTree::Statements[ body: body ] ]
|
|
1374
|
+
]
|
|
1375
|
+
[ TypeDecl(name, Fields(body)), *Methods(name, body) ]
|
|
1376
|
+
|
|
1377
|
+
in SyntaxTree::DefNode[
|
|
1378
|
+
name: { value: name },
|
|
1379
|
+
params: params,
|
|
1380
|
+
bodystmt: SyntaxTree::BodyStmt[
|
|
1381
|
+
statements: SyntaxTree::Statements[body: body]
|
|
1382
|
+
]
|
|
1383
|
+
]
|
|
1384
|
+
ctx.descend(body).then do |children|
|
|
1385
|
+
Function(
|
|
1386
|
+
name: name,
|
|
1387
|
+
params: params,
|
|
1388
|
+
body: children.tree
|
|
1389
|
+
)
|
|
1390
|
+
end
|
|
1391
|
+
|
|
1392
|
+
in SyntaxTree::Begin[
|
|
1393
|
+
bodystmt: SyntaxTree::BodyStmt[
|
|
1394
|
+
statements: SyntaxTree::Statements[body: body]
|
|
1395
|
+
]
|
|
1396
|
+
]
|
|
1397
|
+
ctx.descend(body, inherit: false).then do |children|
|
|
1398
|
+
Block(body: children.tree)
|
|
1399
|
+
end
|
|
1400
|
+
|
|
1401
|
+
in SyntaxTree::IfNode | SyntaxTree::UnlessNode | SyntaxTree::WhileNode | SyntaxTree::UntilNode |
|
|
1402
|
+
SyntaxTree::Case |
|
|
1403
|
+
SyntaxTree::MethodAddBlock[call: SyntaxTree::CallNode[ message: SyntaxTree::Ident[value: "times"] ]] |
|
|
1404
|
+
SyntaxTree::MethodAddBlock[call: SyntaxTree::CallNode[ message: SyntaxTree::Ident[value: "loop"] ]] |
|
|
1405
|
+
SyntaxTree::MethodAddBlock[call: SyntaxTree::CallNode[ message: SyntaxTree::Ident[value: "each"] ]] => logic_node
|
|
1406
|
+
Logic(ctx, logic_node)
|
|
1407
|
+
|
|
1408
|
+
# go lambda { ... }.()
|
|
1409
|
+
# go func() { ... }()
|
|
1410
|
+
# ---
|
|
1411
|
+
in SyntaxTree::Command[
|
|
1412
|
+
message: SyntaxTree::Ident[value: "go"],
|
|
1413
|
+
arguments: SyntaxTree::Args[ parts: [
|
|
1414
|
+
SyntaxTree::CallNode[
|
|
1415
|
+
receiver: SyntaxTree::MethodAddBlock[
|
|
1416
|
+
call: SyntaxTree::CallNode[ message: SyntaxTree::Ident[value: "lambda"] ],
|
|
1417
|
+
block: block
|
|
1418
|
+
],
|
|
1419
|
+
operator: SyntaxTree::Period[value: "."],
|
|
1420
|
+
message: :call,
|
|
1421
|
+
arguments: SyntaxTree::ArgParen[ arguments: arguments ]
|
|
1422
|
+
]
|
|
1423
|
+
] ]
|
|
1424
|
+
]
|
|
1425
|
+
SyntaxTree::Go::GoStmt.new(
|
|
1426
|
+
call: SyntaxTree::Go::CallExpr.new(
|
|
1427
|
+
fun: Lambda(block),
|
|
1428
|
+
args: Array(arguments&.parts).map { |argument| Statements(argument) }
|
|
1429
|
+
)
|
|
1430
|
+
)
|
|
1431
|
+
|
|
1432
|
+
in SyntaxTree::CallNode
|
|
1433
|
+
SyntaxTree::Go::ExprStmt.new(x: Statements(node))
|
|
1434
|
+
|
|
1435
|
+
in SyntaxTree::Comment[value: value]
|
|
1436
|
+
SyntaxTree::Go::Comment.new(text: value.sub(/\A#/, "//"))
|
|
1437
|
+
|
|
1438
|
+
# PI = 3.14159
|
|
1439
|
+
# const pi = 3.14159
|
|
1440
|
+
# ---
|
|
1441
|
+
in SyntaxTree::Assign[
|
|
1442
|
+
target: SyntaxTree::VarField[value: SyntaxTree::Const[value: name]],
|
|
1443
|
+
value: value
|
|
1444
|
+
] if ctx.equal?(root_context)
|
|
1445
|
+
SyntaxTree::Go::GenDecl.new(
|
|
1446
|
+
tok: :const,
|
|
1447
|
+
specs: [ SyntaxTree::Go::ValueSpec.new( names: [Constant(name)], values: [RightHand(value)] ) ]
|
|
1448
|
+
)
|
|
1449
|
+
|
|
1450
|
+
# packageLevel = "I live at package scope"
|
|
1451
|
+
# var packageLevel = "I live at package scope"
|
|
1452
|
+
# ---
|
|
1453
|
+
in SyntaxTree::Assign[target: target, value: value] if ctx.equal?(root_context)
|
|
1454
|
+
SyntaxTree::Go::GenDecl.new(
|
|
1455
|
+
tok: :var,
|
|
1456
|
+
specs: [ SyntaxTree::Go::ValueSpec.new( names: [LeftHand(target)], values: [RightHand(value)] ) ]
|
|
1457
|
+
)
|
|
1458
|
+
|
|
1459
|
+
in SyntaxTree::Assign[target: target, value: value]
|
|
1460
|
+
Assignment(ctx, target, value)
|
|
1461
|
+
|
|
1462
|
+
in SyntaxTree::MAssign[target: target, value: value]
|
|
1463
|
+
Assignment(ctx, target, value)
|
|
1464
|
+
|
|
1465
|
+
else
|
|
1466
|
+
Statements(node)
|
|
1467
|
+
end
|
|
1468
|
+
end
|
|
1469
|
+
|
|
1470
|
+
after do |ctx|
|
|
1471
|
+
if ctx.equal?(root_context)
|
|
1472
|
+
# a declaration may arrive with its methods alongside it
|
|
1473
|
+
ctx.tree.replace(ctx.tree.flatten)
|
|
1474
|
+
|
|
1475
|
+
ctx.tree.unshift(SyntaxTree::Go::GenDecl.new(
|
|
1476
|
+
tok: :import,
|
|
1477
|
+
specs: ctx.tree.select { |node| node.is_a?(SyntaxTree::Go::ImportSpec) }
|
|
1478
|
+
))
|
|
1479
|
+
ctx.tree.reject! { |node| node.is_a?(SyntaxTree::Go::ImportSpec) }
|
|
1480
|
+
file = Package(name: "main", declarations: ctx.tree.dup)
|
|
1481
|
+
ctx.tree.replace([file])
|
|
1482
|
+
end
|
|
1483
|
+
end
|
|
1484
|
+
end
|
|
1485
|
+
|
|
1486
|
+
root_context.tree.first
|
|
1487
|
+
end
|
|
1488
|
+
end
|
|
1489
|
+
|
|
1490
|
+
class Context
|
|
1491
|
+
include Runner::DefinitionNodes
|
|
1492
|
+
include Runner::StatementNodes
|
|
1493
|
+
|
|
1494
|
+
include Runner::LogicNodes
|
|
1495
|
+
include Runner::LambdaNodes
|
|
1496
|
+
include Runner::BlockNodes
|
|
1497
|
+
include Runner::VariableNodes
|
|
1498
|
+
end
|
|
1499
|
+
end
|
|
1500
|
+
end
|