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.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +17 -0
  3. data/exe/gob +8 -0
  4. data/exe/stree-go +12 -0
  5. data/ext/gipper/gipper.go +172 -0
  6. data/ext/gipper/libgipper.h +95 -0
  7. data/ext/gipper/libgipper.so +0 -0
  8. data/ext/goformatter/format/LICENSE +27 -0
  9. data/ext/goformatter/format/format.go +132 -0
  10. data/ext/goformatter/format/internal.go +174 -0
  11. data/ext/goformatter/go.mod +3 -0
  12. data/ext/goformatter/libgoformatter.h +95 -0
  13. data/ext/goformatter/libgoformatter.so +0 -0
  14. data/ext/goformatter/main.go +34 -0
  15. data/gob-lang.gemspec +57 -0
  16. data/lib/gipper.rb +24 -0
  17. data/lib/go_formatter.rb +33 -0
  18. data/lib/gob/cli/base.rb +53 -0
  19. data/lib/gob/cli/commands/build.rb +29 -0
  20. data/lib/gob/cli/commands/fmt.rb +31 -0
  21. data/lib/gob/cli/commands/run.rb +21 -0
  22. data/lib/gob/cli/commands/transpile.rb +20 -0
  23. data/lib/gob/cli/commands/version.rb +19 -0
  24. data/lib/gob/cli/commands/vet.rb +33 -0
  25. data/lib/gob/cli/errors.rb +50 -0
  26. data/lib/gob/cli.rb +53 -0
  27. data/lib/gob/go/dsl.rb +196 -0
  28. data/lib/gob/go/printer.rb +412 -0
  29. data/lib/gob/go/runner.rb +42 -0
  30. data/lib/gob/matcher.rb +34 -0
  31. data/lib/gob/transpilation/rules/core.rb +1184 -0
  32. data/lib/gob/transpiler.rb +815 -0
  33. data/lib/gob/validation/offence.rb +24 -0
  34. data/lib/gob/validation/rules/class_rules.rb +78 -0
  35. data/lib/gob/validation/rules/config.yml +16 -0
  36. data/lib/gob/validation/rules/cop.rb +15 -0
  37. data/lib/gob/validation/rules/function_rules.rb +33 -0
  38. data/lib/gob/validation/rules/module_rules.rb +78 -0
  39. data/lib/gob/validation/rules/program_rules.rb +72 -0
  40. data/lib/gob/validation/rules.rb +93 -0
  41. data/lib/gob/version.rb +5 -0
  42. data/lib/gob.rb +41 -0
  43. data/lib/syntax_tree/go/format.rb +176 -0
  44. data/lib/syntax_tree/go/json_visitor.rb +34 -0
  45. data/lib/syntax_tree/go/match_visitor.rb +103 -0
  46. data/lib/syntax_tree/go/materializer.rb +92 -0
  47. data/lib/syntax_tree/go/node.rb +50 -0
  48. data/lib/syntax_tree/go/nodes.rb +2147 -0
  49. data/lib/syntax_tree/go/pretty_print_visitor.rb +76 -0
  50. data/lib/syntax_tree/go/schema.json +1455 -0
  51. data/lib/syntax_tree/go/visitor.rb +71 -0
  52. data/lib/syntax_tree/go.rb +55 -0
  53. data/lib/thor/help_header.rb +23 -0
  54. metadata +292 -0
data/lib/gob/go/dsl.rb ADDED
@@ -0,0 +1,196 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "syntax_tree/go"
5
+
6
+ module Gob
7
+ module Go
8
+ module DSL
9
+ RUBY_KEYWORDS = %w[
10
+ __FILE__ __LINE__ alias and begin BEGIN break case class def defined?
11
+ do else elsif end END ensure false for if in module next nil not or
12
+ redo rescue retry return self super then true undef unless until when
13
+ while yield
14
+ ].to_set
15
+
16
+ SCHEMA_PATH = ::File.expand_path("../../syntax_tree/go/schema.json", __dir__)
17
+
18
+ module_function
19
+
20
+ def schema
21
+ @schema ||= JSON.parse(::File.read(SCHEMA_PATH))
22
+ end
23
+
24
+ def snake(name)
25
+ name.gsub(/([a-z0-9])([A-Z])/, "\\1_\\2").downcase
26
+ end
27
+
28
+ def fname(name)
29
+ n = snake(name)
30
+ RUBY_KEYWORDS.include?(n) ? "#{n}_" : n
31
+ end
32
+
33
+ def build_node(type, kwargs)
34
+ klass = SyntaxTree::Go.const_get(type)
35
+ node_def = schema.find { |n| n["name"] == type }
36
+ args = {}
37
+ node_def["fields"].each do |f|
38
+ key = fname(f["name"]).to_sym
39
+ args[key] = kwargs[key] if kwargs.key?(key)
40
+ end
41
+ klass.new(**args)
42
+ end
43
+
44
+ # Generic node builder: node("FuncDecl", name: ..., type: ..., body: ...)
45
+ def node(type, **kwargs)
46
+ build_node(type, kwargs)
47
+ end
48
+
49
+ # --- Convenience constructors ---
50
+
51
+ def ident(name, name_pos: nil)
52
+ SyntaxTree::Go::Ident.new(name_pos: name_pos, name: name.to_s, obj: nil)
53
+ end
54
+
55
+ def basic_lit(kind:, value:, value_pos: nil, value_end: nil)
56
+ SyntaxTree::Go::BasicLit.new(value_pos: value_pos, value_end: value_end, kind: kind.to_s, value: value.to_s)
57
+ end
58
+
59
+ def str_lit(value)
60
+ # value is raw string content (as in source), not interpreted
61
+ basic_lit(kind: "STRING", value: "\"#{value}\"")
62
+ end
63
+
64
+ def int_lit(value)
65
+ basic_lit(kind: "INT", value: value.to_s)
66
+ end
67
+
68
+ def float_lit(value)
69
+ basic_lit(kind: "FLOAT", value: value.to_s)
70
+ end
71
+
72
+ def field_list(list: [], opening: nil, closing: nil)
73
+ SyntaxTree::Go::FieldList.new(opening: opening, list: list, closing: closing)
74
+ end
75
+
76
+ def block_stmt(list: [], lbrace: nil, rbrace: nil)
77
+ SyntaxTree::Go::BlockStmt.new(lbrace: lbrace, list: merge_if_init_stmts(Array(list)), rbrace: rbrace)
78
+ end
79
+
80
+ def merge_if_init_stmts(stmts)
81
+ out = []
82
+ i = 0
83
+ while i < stmts.length
84
+ s = stmts[i]
85
+ n = stmts[i + 1]
86
+ if n.is_a?(SyntaxTree::Go::IfStmt) && n.init.nil? && should_merge_if_init?(s, n.cond)
87
+ init = if s.is_a?(SyntaxTree::Go::AssignStmt)
88
+ s
89
+ elsif s.is_a?(SyntaxTree::Go::DeclStmt) && s.decl.is_a?(SyntaxTree::Go::GenDecl) && s.decl.tok.to_s == "var" && s.decl.specs.length == 1
90
+ spec = s.decl.specs.first
91
+ SyntaxTree::Go::AssignStmt.new(lhs: spec.names.map { |nm| SyntaxTree::Go::Ident.new(name: nm.name, name_pos: nil, obj: nil) }, tok: ":=", rhs: spec.values)
92
+ end
93
+ if init
94
+ out << SyntaxTree::Go::IfStmt.new(if_: nil, init: init, cond: n.cond, body: n.body, else_: n.else_)
95
+ i += 2
96
+ next
97
+ end
98
+ end
99
+ out << s
100
+ i += 1
101
+ end
102
+ out
103
+ end
104
+
105
+ def should_merge_if_init?(s, cond)
106
+ names = []
107
+ if s.is_a?(SyntaxTree::Go::AssignStmt) && s.lhs.all? { |l| l.is_a?(SyntaxTree::Go::Ident) }
108
+ names = s.lhs.map(&:name)
109
+ elsif s.is_a?(SyntaxTree::Go::DeclStmt) && s.decl.is_a?(SyntaxTree::Go::GenDecl) && s.decl.tok.to_s == "var" && s.decl.specs.length == 1
110
+ spec = s.decl.specs.first
111
+ return false unless spec.names.length >= 1 && spec.values && !spec.values.empty?
112
+ names = spec.names.map(&:name)
113
+ else
114
+ return false
115
+ end
116
+ return false unless names.any? { |name| go_cond_uses_ident?(cond, name) }
117
+ # conservative: merge common err/ok patterns
118
+ names.any? { |n| %w[err ok].include?(n) }
119
+ end
120
+
121
+ def go_cond_uses_ident?(node, name)
122
+ return false unless node
123
+ case node
124
+ when SyntaxTree::Go::Ident
125
+ node.name == name
126
+ when SyntaxTree::Go::BinaryExpr
127
+ go_cond_uses_ident?(node.x, name) || go_cond_uses_ident?(node.y, name)
128
+ when SyntaxTree::Go::UnaryExpr
129
+ go_cond_uses_ident?(node.x, name)
130
+ when SyntaxTree::Go::ParenExpr
131
+ go_cond_uses_ident?(node.x, name)
132
+ when SyntaxTree::Go::CallExpr
133
+ go_cond_uses_ident?(node.fun, name) || Array(node.args).any? { |a| go_cond_uses_ident?(a, name) }
134
+ else
135
+ false
136
+ end
137
+ end
138
+
139
+ def expr_stmt(x:)
140
+ SyntaxTree::Go::ExprStmt.new(x: x)
141
+ end
142
+
143
+ def return_stmt(results: [], return_: nil)
144
+ SyntaxTree::Go::ReturnStmt.new(return_: return_, results: Array(results))
145
+ end
146
+
147
+ def call_expr(fun:, args: [], lparen: nil, ellipsis: nil, rparen: nil)
148
+ SyntaxTree::Go::CallExpr.new(fun: fun, lparen: lparen, args: Array(args), ellipsis: ellipsis, rparen: rparen)
149
+ end
150
+
151
+ def selector_expr(x:, sel:)
152
+ sel_node = sel.is_a?(String) || sel.is_a?(Symbol) ? ident(sel) : sel
153
+ SyntaxTree::Go::SelectorExpr.new(x: x, sel: sel_node)
154
+ end
155
+
156
+ def func_type(params: nil, results: nil, func: nil, type_params: nil)
157
+ params_node =
158
+ case params
159
+ when Array then field_list(list: params)
160
+ when nil then field_list
161
+ else params
162
+ end
163
+ SyntaxTree::Go::FuncType.new(func: func, type_params: type_params, params: params_node, results: results)
164
+ end
165
+
166
+ def func_decl(name:, body: nil, params: nil, results: nil, recv: nil, doc: nil, type_params: nil)
167
+ name_node = name.is_a?(String) || name.is_a?(Symbol) ? ident(name) : name
168
+ body_node = body.is_a?(Array) ? block_stmt(list: body) : body
169
+ SyntaxTree::Go::FuncDecl.new(
170
+ doc: doc, recv: recv, name: name_node,
171
+ type: func_type(params: params, results: results, type_params: type_params),
172
+ body: body_node
173
+ )
174
+ end
175
+
176
+ def gen_decl(tok:, specs: [], doc: nil, lparen: nil, rparen: nil, tok_pos: nil)
177
+ SyntaxTree::Go::GenDecl.new(doc: doc, tok_pos: tok_pos, tok: tok.to_s, lparen: lparen, specs: Array(specs), rparen: rparen)
178
+ end
179
+
180
+ def import_spec(path:, name: nil, doc: nil, comment: nil, end_pos: nil)
181
+ path_node = path.is_a?(String) ? str_lit(path) : path
182
+ name_node = name.is_a?(String) || name.is_a?(Symbol) ? ident(name) : name
183
+ SyntaxTree::Go::ImportSpec.new(doc: doc, name: name_node, path: path_node, comment: comment, end_pos: end_pos)
184
+ end
185
+
186
+ def file_node(name:, decls: [], doc: nil, package: nil, imports: [], comments: [])
187
+ name_node = name.is_a?(String) || name.is_a?(Symbol) ? ident(name) : name
188
+ SyntaxTree::Go::File.new(
189
+ doc: doc, package: package, name: name_node, decls: Array(decls),
190
+ file_start: nil, file_end: nil, scope: nil, imports: Array(imports),
191
+ unresolved: [], comments: Array(comments), go_version: nil
192
+ )
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,412 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gob
4
+ module Go
5
+ class Printer
6
+ def self.print(node)
7
+ new.print(node)
8
+ end
9
+
10
+ def print(node)
11
+ out = +""
12
+ emit(node, out)
13
+ out << "\n" unless out.end_with?("\n")
14
+ out
15
+ end
16
+
17
+ private
18
+
19
+ def emit(node, out, indent: 0)
20
+ case node
21
+ when SyntaxTree::Go::File then emit_file(node, out)
22
+ when SyntaxTree::Go::GenDecl then emit_gen_decl(node, out, indent: indent)
23
+ when SyntaxTree::Go::ImportSpec then emit_import_spec(node, out)
24
+ when SyntaxTree::Go::FuncDecl then emit_func_decl(node, out, indent: indent)
25
+ when SyntaxTree::Go::FuncType then emit_func_type(node, out)
26
+ when SyntaxTree::Go::FieldList then emit_field_list(node, out)
27
+ when SyntaxTree::Go::Field then emit_field(node, out)
28
+ when SyntaxTree::Go::TypeSpec then emit_type_spec(node, out, indent: indent)
29
+ when SyntaxTree::Go::ValueSpec then emit_value_spec(node, out)
30
+ when SyntaxTree::Go::StructType then emit_struct_type(node, out, indent: indent)
31
+ when SyntaxTree::Go::BlockStmt then emit_block_stmt(node, out, indent: indent)
32
+ when SyntaxTree::Go::ExprStmt then emit(node.x, out, indent: indent)
33
+ when SyntaxTree::Go::ReturnStmt then emit_return_stmt(node, out, indent: indent)
34
+ when SyntaxTree::Go::AssignStmt then emit_assign_stmt(node, out, indent: indent)
35
+ when SyntaxTree::Go::DeclStmt then emit(node.decl, out, indent: indent)
36
+ when SyntaxTree::Go::CallExpr then emit_call_expr(node, out)
37
+ when SyntaxTree::Go::SelectorExpr then emit_selector_expr(node, out)
38
+ when SyntaxTree::Go::Ident then out << node.name.to_s
39
+ when SyntaxTree::Go::BasicLit then out << node.value.to_s
40
+ when SyntaxTree::Go::BinaryExpr then emit_binary_expr(node, out)
41
+ when SyntaxTree::Go::UnaryExpr then emit_unary_expr(node, out)
42
+ when SyntaxTree::Go::ParenExpr then emit_paren_expr(node, out)
43
+ when SyntaxTree::Go::CompositeLit then emit_composite_lit(node, out)
44
+ when SyntaxTree::Go::KeyValueExpr then emit_key_value_expr(node, out)
45
+ when SyntaxTree::Go::IndexExpr then emit_index_expr(node, out)
46
+ when SyntaxTree::Go::StarExpr then out << "*"; emit(node.x, out)
47
+ when SyntaxTree::Go::ArrayType then emit_array_type(node, out)
48
+ when SyntaxTree::Go::MapType then emit_map_type(node, out)
49
+ when SyntaxTree::Go::FuncLit then emit_func_lit(node, out, indent: indent)
50
+ when SyntaxTree::Go::TypeAssertExpr then emit_type_assert(node, out)
51
+ when SyntaxTree::Go::IfStmt then emit_if_stmt(node, out, indent: indent)
52
+ when SyntaxTree::Go::ForStmt then emit_for_stmt(node, out, indent: indent)
53
+ when SyntaxTree::Go::RangeStmt then emit_range_stmt(node, out, indent: indent)
54
+ when SyntaxTree::Go::SwitchStmt then emit_switch_stmt(node, out, indent: indent)
55
+ when SyntaxTree::Go::CaseClause then emit_case_clause(node, out, indent: indent)
56
+ when SyntaxTree::Go::BranchStmt then emit_branch_stmt(node, out, indent: indent)
57
+ when SyntaxTree::Go::IncDecStmt then emit(node.x, out); out << node.tok.to_s
58
+ when SyntaxTree::Go::DeferStmt then out << "defer "; emit(node.call, out)
59
+ when SyntaxTree::Go::GoStmt then out << "go "; emit(node.call, out)
60
+ when SyntaxTree::Go::SendStmt then emit(node.chan, out); out << " <- "; emit(node.value, out)
61
+ when SyntaxTree::Go::LabeledStmt then out << node.label.name.to_s << ":\n"; emit(node.stmt, out, indent: indent)
62
+ when SyntaxTree::Go::EmptyStmt then nil
63
+ when SyntaxTree::Go::CommentGroup then emit_comment_group(node, out, indent: indent)
64
+ when SyntaxTree::Go::Comment then emit_comment(node, out, indent: indent)
65
+ when nil then out << "nil"
66
+ else
67
+ out << "/* unsupported: #{node.class} */"
68
+ end
69
+ end
70
+
71
+ def tabs(indent)
72
+ "\t" * indent
73
+ end
74
+
75
+ def emit_file(node, out)
76
+ out << "package "
77
+ emit(node.name, out)
78
+ out << "\n"
79
+
80
+ node.decls.each do |decl|
81
+ out << "\n"
82
+ emit(decl, out, indent: 0)
83
+ out << "\n" unless out.end_with?("\n")
84
+ end
85
+ end
86
+
87
+ def emit_gen_decl(node, out, indent: 0)
88
+ out << tabs(indent) << node.tok.to_s
89
+ if node.specs.length == 1 && node.lparen.nil?
90
+ out << " "
91
+ emit(node.specs.first, out)
92
+ else
93
+ out << " (\n"
94
+ node.specs.each do |spec|
95
+ out << tabs(indent + 1)
96
+ emit(spec, out)
97
+ out << "\n"
98
+ end
99
+ out << tabs(indent) << ")"
100
+ end
101
+ end
102
+
103
+ def emit_import_spec(node, out)
104
+ if node.name
105
+ emit(node.name, out)
106
+ out << " "
107
+ end
108
+ emit(node.path, out)
109
+ end
110
+
111
+ def emit_type_spec(node, out, indent: 0)
112
+ emit(node.name, out)
113
+ out << " "
114
+ emit(node.type, out, indent: indent)
115
+ end
116
+
117
+ def emit_value_spec(node, out)
118
+ node.names.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
119
+ if node.type
120
+ out << " "
121
+ emit(node.type, out)
122
+ end
123
+ if node.values && !node.values.empty?
124
+ out << " = "
125
+ node.values.each_with_index { |v, i| out << ", " if i > 0; emit(v, out) }
126
+ end
127
+ end
128
+
129
+ def emit_struct_type(node, out, indent: 0)
130
+ out << "struct {"
131
+ fields = node.fields&.list || []
132
+ if fields.empty?
133
+ out << "}"
134
+ return
135
+ end
136
+ out << "\n"
137
+ fields.each do |f|
138
+ out << tabs(indent + 1)
139
+ emit_field(f, out)
140
+ out << "\n"
141
+ end
142
+ out << tabs(indent) << "}"
143
+ end
144
+
145
+ def emit_func_decl(node, out, indent: 0)
146
+ out << tabs(indent) << "func "
147
+ if node.recv
148
+ emit(node.recv, out)
149
+ out << " "
150
+ end
151
+ emit(node.name, out)
152
+ emit(node.type.params, out) if node.type&.params
153
+ if node.type&.results
154
+ out << " "
155
+ emit(node.type.results, out)
156
+ end
157
+ if node.body
158
+ out << " "
159
+ emit(node.body, out, indent: indent)
160
+ end
161
+ end
162
+
163
+ def emit_func_type(node, out)
164
+ out << "func"
165
+ emit(node.params, out) if node.params
166
+ if node.results
167
+ out << " "
168
+ emit(node.results, out)
169
+ end
170
+ end
171
+
172
+ def emit_field_list(node, out)
173
+ out << "("
174
+ node.list.each_with_index do |field, i|
175
+ out << ", " if i > 0
176
+ emit_field(field, out)
177
+ end
178
+ out << ")"
179
+ end
180
+
181
+ def emit_field(node, out)
182
+ if node.names && !node.names.empty?
183
+ node.names.each_with_index do |name, i|
184
+ out << ", " if i > 0
185
+ emit(name, out)
186
+ end
187
+ out << " " if node.type
188
+ end
189
+ emit(node.type, out) if node.type
190
+ if node.tag
191
+ out << " "
192
+ emit(node.tag, out)
193
+ end
194
+ end
195
+
196
+ def emit_block_stmt(node, out, indent: 0)
197
+ out << "{\n"
198
+ node.list.each do |stmt|
199
+ out << tabs(indent + 1)
200
+ emit(stmt, out, indent: indent + 1)
201
+ out << "\n" unless out.end_with?("\n")
202
+ end
203
+ out << tabs(indent) << "}"
204
+ end
205
+
206
+ def emit_return_stmt(node, out, indent: 0)
207
+ out << "return"
208
+ unless node.results.empty?
209
+ out << " "
210
+ node.results.each_with_index do |r, i|
211
+ out << ", " if i > 0
212
+ emit(r, out)
213
+ end
214
+ end
215
+ end
216
+
217
+ def emit_assign_stmt(node, out, indent: 0)
218
+ node.lhs.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
219
+ out << " #{node.tok} "
220
+ node.rhs.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
221
+ end
222
+
223
+ def emit_call_expr(node, out)
224
+ emit(node.fun, out)
225
+ out << "("
226
+ node.args.each_with_index do |arg, i|
227
+ out << ", " if i > 0
228
+ emit(arg, out)
229
+ end
230
+ if node.ellipsis
231
+ out << "..."
232
+ end
233
+ out << ")"
234
+ end
235
+
236
+ def emit_selector_expr(node, out)
237
+ emit(node.x, out)
238
+ out << "."
239
+ emit(node.sel, out)
240
+ end
241
+
242
+ def emit_binary_expr(node, out)
243
+ emit(node.x, out)
244
+ out << " " << node.op.to_s << " "
245
+ emit(node.y, out)
246
+ end
247
+
248
+ def emit_unary_expr(node, out)
249
+ out << node.op.to_s
250
+ emit(node.x, out)
251
+ end
252
+
253
+ def emit_paren_expr(node, out)
254
+ out << "("
255
+ emit(node.x, out)
256
+ out << ")"
257
+ end
258
+
259
+ def emit_composite_lit(node, out)
260
+ emit(node.type, out) if node.type
261
+ out << "{"
262
+ node.elts.each_with_index do |elt, i|
263
+ out << ", " if i > 0
264
+ emit(elt, out)
265
+ end
266
+ out << "}"
267
+ end
268
+
269
+ def emit_key_value_expr(node, out)
270
+ emit(node.key, out)
271
+ out << ": "
272
+ emit(node.value, out)
273
+ end
274
+
275
+ def emit_index_expr(node, out)
276
+ emit(node.x, out)
277
+ out << "["
278
+ emit(node.index, out)
279
+ out << "]"
280
+ end
281
+
282
+ def emit_array_type(node, out)
283
+ out << "["
284
+ emit(node.len, out) if node.len
285
+ out << "]"
286
+ emit(node.elt, out)
287
+ end
288
+
289
+ def emit_map_type(node, out)
290
+ out << "map["
291
+ emit(node.key, out)
292
+ out << "]"
293
+ emit(node.value, out)
294
+ end
295
+
296
+ def emit_func_lit(node, out, indent: 0)
297
+ emit(node.type, out)
298
+ out << " "
299
+ emit(node.body, out, indent: indent)
300
+ end
301
+
302
+ def emit_type_assert(node, out)
303
+ emit(node.x, out)
304
+ out << ".("
305
+ if node.type
306
+ emit(node.type, out)
307
+ else
308
+ out << "type"
309
+ end
310
+ out << ")"
311
+ end
312
+
313
+ def emit_if_stmt(node, out, indent: 0)
314
+ out << "if "
315
+ if node.init
316
+ emit(node.init, out)
317
+ out << "; "
318
+ end
319
+ emit(node.cond, out)
320
+ out << " "
321
+ emit(node.body, out, indent: indent)
322
+ if node.else_
323
+ out << " else "
324
+ if node.else_.is_a?(SyntaxTree::Go::IfStmt)
325
+ emit(node.else_, out, indent: indent)
326
+ else
327
+ emit(node.else_, out, indent: indent)
328
+ end
329
+ end
330
+ end
331
+
332
+ def emit_for_stmt(node, out, indent: 0)
333
+ out << "for "
334
+ if node.init || node.post
335
+ emit(node.init, out) if node.init
336
+ out << "; "
337
+ emit(node.cond, out) if node.cond
338
+ out << "; "
339
+ emit(node.post, out) if node.post
340
+ out << " "
341
+ elsif node.cond
342
+ emit(node.cond, out)
343
+ out << " "
344
+ end
345
+ emit(node.body, out, indent: indent)
346
+ end
347
+
348
+ def emit_range_stmt(node, out, indent: 0)
349
+ out << "for "
350
+ if node.key
351
+ emit(node.key, out)
352
+ if node.value
353
+ out << ", "
354
+ emit(node.value, out)
355
+ end
356
+ out << " #{node.tok} "
357
+ end
358
+ out << "range "
359
+ emit(node.x, out)
360
+ out << " "
361
+ emit(node.body, out, indent: indent)
362
+ end
363
+
364
+ def emit_switch_stmt(node, out, indent: 0)
365
+ out << "switch "
366
+ if node.init
367
+ emit(node.init, out)
368
+ out << "; "
369
+ end
370
+ if node.tag
371
+ emit(node.tag, out)
372
+ out << " "
373
+ end
374
+ emit(node.body, out, indent: indent)
375
+ end
376
+
377
+ def emit_case_clause(node, out, indent: 0)
378
+ if node.list.empty?
379
+ out << tabs(indent) << "default:\n"
380
+ else
381
+ out << tabs(indent) << "case "
382
+ node.list.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
383
+ out << ":\n"
384
+ end
385
+ node.body.each do |stmt|
386
+ out << tabs(indent + 1)
387
+ emit(stmt, out, indent: indent + 1)
388
+ out << "\n" unless out.end_with?("\n")
389
+ end
390
+ end
391
+
392
+ def emit_branch_stmt(node, out, indent: 0)
393
+ out << node.tok.to_s
394
+ if node.label
395
+ out << " "
396
+ emit(node.label, out)
397
+ end
398
+ end
399
+
400
+ def emit_comment(node, out, indent: 0)
401
+ out << tabs(indent) << node.text.to_s
402
+ end
403
+
404
+ def emit_comment_group(node, out, indent: 0)
405
+ node.list.each do |c|
406
+ emit(c, out, indent: indent)
407
+ out << "\n" unless out.end_with?("\n")
408
+ end
409
+ end
410
+ end
411
+ end
412
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "open3"
5
+ require "tmpdir"
6
+
7
+ module Gob
8
+ module Go
9
+ module Runner
10
+ module_function
11
+
12
+ def run(source, args: [])
13
+ with_source(source) do |path|
14
+ capture("go", "run", path, *args)
15
+ end
16
+ end
17
+
18
+ def build(source, output:)
19
+ FileUtils.mkdir_p(File.dirname(output))
20
+
21
+ with_source(source) do |path|
22
+ capture("go", "build", "-o", File.expand_path(output), path)
23
+ end
24
+ end
25
+
26
+ def with_source(source)
27
+ Dir.mktmpdir("gob-") do |directory|
28
+ path = File.join(directory, "main.go")
29
+ File.write(path, source)
30
+ yield path
31
+ end
32
+ end
33
+
34
+ def capture(*command)
35
+ stdout, stderr, status = Open3.capture3(*command)
36
+ $stdout.write(stdout)
37
+ $stderr.write(stderr)
38
+ status.success?
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gob
4
+ class Matcher
5
+ MISS = :__gob_miss__
6
+ DROP = :__gob_drop__
7
+
8
+ Rule = Struct.new(:name, :block)
9
+
10
+ def initialize
11
+ @rules = []
12
+ end
13
+
14
+ def rule(name, &block)
15
+ @rules << Rule.new(name, block)
16
+ self
17
+ end
18
+
19
+ def match(node, context)
20
+ @rules.each do |rule|
21
+ result = context.instance_exec(node, &rule.block)
22
+ next if result == MISS
23
+
24
+ return nil if result == DROP
25
+ return result
26
+ rescue NoMatchingPatternError
27
+ next
28
+ end
29
+ MISS
30
+ end
31
+
32
+ attr_reader :rules
33
+ end
34
+ end