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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +201 -0
  3. data/README.md +73 -0
  4. data/gob-lang.gemspec +2 -1
  5. data/lib/gob/cli/base.rb +2 -2
  6. data/lib/gob/cli/commands/fmt.rb +2 -2
  7. data/lib/gob/cli/completion.rb +104 -0
  8. data/lib/gob/cli/errors.rb +0 -2
  9. data/lib/gob/cli.rb +21 -3
  10. data/lib/gob/go/printer.rb +215 -72
  11. data/lib/gob/transpilation/context.rb +102 -0
  12. data/lib/gob/transpilation/runner.rb +1500 -0
  13. data/lib/gob/validation/rules/array_length.rb +45 -0
  14. data/lib/gob/validation/rules/attribute_type.rb +30 -0
  15. data/lib/gob/validation/rules/block_delimiters.rb +43 -0
  16. data/lib/gob/validation/rules/collection_literal.rb +107 -0
  17. data/lib/gob/validation/rules/config.yml +58 -4
  18. data/lib/gob/validation/rules/cop.rb +72 -0
  19. data/lib/gob/validation/rules/defer_statement.rb +33 -0
  20. data/lib/gob/validation/rules/error_check.rb +33 -0
  21. data/lib/gob/validation/rules/go_block.rb +58 -0
  22. data/lib/gob/validation/rules/indentation.rb +118 -0
  23. data/lib/gob/validation/rules/invoked_lambda.rb +48 -0
  24. data/lib/gob/validation/rules/lambda_signature.rb +58 -0
  25. data/lib/gob/validation/rules/multiline.rb +99 -0
  26. data/lib/gob/validation/rules/operator_assignment.rb +44 -0
  27. data/lib/gob/validation/rules/pointer_type.rb +36 -0
  28. data/lib/gob/validation/rules/quote_marks.rb +33 -0
  29. data/lib/gob/validation/rules/redundant_lambda_parentheses.rb +47 -0
  30. data/lib/gob/validation/rules/select_block.rb +85 -0
  31. data/lib/gob/validation/rules/signature_comment.rb +43 -0
  32. data/lib/gob/validation/rules/this_receiver.rb +38 -0
  33. data/lib/gob/validation/rules/trailing_whitespace.rb +38 -0
  34. data/lib/gob/validation/rules/variable_declaration.rb +32 -0
  35. data/lib/gob/validation/rules/word_array.rb +33 -0
  36. data/lib/gob/validation/rules/zero_based_range_loop.rb +63 -0
  37. data/lib/gob/validation/rules.rb +85 -11
  38. data/lib/gob/version.rb +1 -1
  39. data/lib/gob.rb +10 -12
  40. metadata +41 -9
  41. data/lib/gob/go/dsl.rb +0 -196
  42. data/lib/gob/matcher.rb +0 -34
  43. data/lib/gob/transpilation/rules/core.rb +0 -1184
  44. data/lib/gob/transpiler.rb +0 -815
  45. data/lib/gob/validation/rules/class_rules.rb +0 -78
  46. data/lib/gob/validation/rules/function_rules.rb +0 -33
  47. data/lib/gob/validation/rules/module_rules.rb +0 -78
  48. data/lib/gob/validation/rules/program_rules.rb +0 -72
@@ -18,51 +18,113 @@ module Gob
18
18
 
19
19
  def emit(node, out, indent: 0)
20
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"
21
+ when SyntaxTree::Go::File
22
+ emit_file(node, out)
23
+ when SyntaxTree::Go::GenDecl
24
+ emit_gen_decl(node, out, indent: indent)
25
+ when SyntaxTree::Go::ImportSpec
26
+ emit_import_spec(node, out)
27
+ when SyntaxTree::Go::FuncDecl
28
+ emit_func_decl(node, out, indent: indent)
29
+ when SyntaxTree::Go::FuncType
30
+ emit_func_type(node, out)
31
+ when SyntaxTree::Go::FieldList
32
+ emit_field_list(node, out)
33
+ when SyntaxTree::Go::Field
34
+ emit_field(node, out)
35
+ when SyntaxTree::Go::TypeSpec
36
+ emit_type_spec(node, out, indent: indent)
37
+ when SyntaxTree::Go::ValueSpec
38
+ emit_value_spec(node, out)
39
+ when SyntaxTree::Go::StructType
40
+ emit_struct_type(node, out, indent: indent)
41
+ when SyntaxTree::Go::BlockStmt
42
+ emit_block_stmt(node, out, indent: indent)
43
+ when SyntaxTree::Go::ExprStmt
44
+ emit(node.x, out, indent: indent)
45
+ when SyntaxTree::Go::ReturnStmt
46
+ emit_return_stmt(node, out, indent: indent)
47
+ when SyntaxTree::Go::AssignStmt
48
+ emit_assign_stmt(node, out, indent: indent)
49
+ when SyntaxTree::Go::DeclStmt
50
+ emit(node.decl, out, indent: indent)
51
+ when SyntaxTree::Go::CallExpr
52
+ emit_call_expr(node, out, indent: indent)
53
+ when SyntaxTree::Go::SelectorExpr
54
+ emit_selector_expr(node, out)
55
+ when SyntaxTree::Go::Ident
56
+ out << node.name.to_s
57
+ when SyntaxTree::Go::BasicLit
58
+ out << node.value.to_s
59
+ when SyntaxTree::Go::BinaryExpr
60
+ emit_binary_expr(node, out, indent: indent)
61
+ when SyntaxTree::Go::UnaryExpr
62
+ emit_unary_expr(node, out)
63
+ when SyntaxTree::Go::ParenExpr
64
+ emit_paren_expr(node, out)
65
+ when SyntaxTree::Go::CompositeLit
66
+ emit_composite_lit(node, out, indent: indent)
67
+ when SyntaxTree::Go::KeyValueExpr
68
+ emit_key_value_expr(node, out, indent: indent)
69
+ when SyntaxTree::Go::IndexExpr
70
+ emit_index_expr(node, out)
71
+ when SyntaxTree::Go::SliceExpr
72
+ emit_slice_expr(node, out)
73
+ when SyntaxTree::Go::StarExpr
74
+ out << "*"
75
+ emit(node.x, out)
76
+ when SyntaxTree::Go::ArrayType
77
+ emit_array_type(node, out)
78
+ when SyntaxTree::Go::MapType
79
+ emit_map_type(node, out)
80
+ when SyntaxTree::Go::ChanType
81
+ emit_chan_type(node, out)
82
+ when SyntaxTree::Go::FuncLit
83
+ emit_func_lit(node, out, indent: indent)
84
+ when SyntaxTree::Go::TypeAssertExpr
85
+ emit_type_assert(node, out)
86
+ when SyntaxTree::Go::IfStmt
87
+ emit_if_stmt(node, out, indent: indent)
88
+ when SyntaxTree::Go::ForStmt
89
+ emit_for_stmt(node, out, indent: indent)
90
+ when SyntaxTree::Go::RangeStmt
91
+ emit_range_stmt(node, out, indent: indent)
92
+ when SyntaxTree::Go::SwitchStmt
93
+ emit_switch_stmt(node, out, indent: indent)
94
+ when SyntaxTree::Go::TypeSwitchStmt
95
+ emit_type_switch_stmt(node, out, indent: indent)
96
+ when SyntaxTree::Go::SelectStmt
97
+ emit_select_stmt(node, out, indent: indent)
98
+ when SyntaxTree::Go::CaseClause
99
+ emit_case_clause(node, out, indent: indent)
100
+ when SyntaxTree::Go::CommClause
101
+ emit_comm_clause(node, out, indent: indent)
102
+ when SyntaxTree::Go::BranchStmt
103
+ emit_branch_stmt(node, out, indent: indent)
104
+ when SyntaxTree::Go::IncDecStmt
105
+ emit(node.x, out)
106
+ out << node.tok.to_s
107
+ when SyntaxTree::Go::DeferStmt
108
+ out << "defer "
109
+ emit(node.call, out)
110
+ when SyntaxTree::Go::GoStmt
111
+ out << "go "
112
+ emit(node.call, out)
113
+ when SyntaxTree::Go::SendStmt
114
+ emit(node.chan, out)
115
+ out << " <- "
116
+ emit(node.value, out)
117
+ when SyntaxTree::Go::LabeledStmt
118
+ out << node.label.name.to_s << ":\n"
119
+ emit(node.stmt, out, indent: indent)
120
+ when SyntaxTree::Go::EmptyStmt
121
+ nil
122
+ when SyntaxTree::Go::CommentGroup
123
+ emit_comment_group(node, out, indent: indent)
124
+ when SyntaxTree::Go::Comment
125
+ emit_comment(node, out, indent: indent)
126
+ when nil
127
+ out << "nil"
66
128
  else
67
129
  out << "/* unsupported: #{node.class} */"
68
130
  end
@@ -115,14 +177,20 @@ module Gob
115
177
  end
116
178
 
117
179
  def emit_value_spec(node, out)
118
- node.names.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
180
+ node.names.each_with_index do |n, i|
181
+ out << ", " if i > 0
182
+ emit(n, out)
183
+ end
119
184
  if node.type
120
185
  out << " "
121
186
  emit(node.type, out)
122
187
  end
123
188
  if node.values && !node.values.empty?
124
189
  out << " = "
125
- node.values.each_with_index { |v, i| out << ", " if i > 0; emit(v, out) }
190
+ node.values.each_with_index do |v, i|
191
+ out << ", " if i > 0
192
+ emit(v, out)
193
+ end
126
194
  end
127
195
  end
128
196
 
@@ -215,21 +283,25 @@ module Gob
215
283
  end
216
284
 
217
285
  def emit_assign_stmt(node, out, indent: 0)
218
- node.lhs.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
286
+ node.lhs.each_with_index do |n, i|
287
+ out << ", " if i > 0
288
+ emit(n, out)
289
+ end
219
290
  out << " #{node.tok} "
220
- node.rhs.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
291
+ node.rhs.each_with_index do |n, i|
292
+ out << ", " if i > 0
293
+ emit(n, out)
294
+ end
221
295
  end
222
296
 
223
- def emit_call_expr(node, out)
297
+ def emit_call_expr(node, out, indent: 0)
224
298
  emit(node.fun, out)
225
299
  out << "("
226
300
  node.args.each_with_index do |arg, i|
227
301
  out << ", " if i > 0
228
- emit(arg, out)
229
- end
230
- if node.ellipsis
231
- out << "..."
302
+ emit(arg, out, indent: indent)
232
303
  end
304
+ out << "..." if node.ellipsis
233
305
  out << ")"
234
306
  end
235
307
 
@@ -239,10 +311,17 @@ module Gob
239
311
  emit(node.sel, out)
240
312
  end
241
313
 
242
- def emit_binary_expr(node, out)
243
- emit(node.x, out)
244
- out << " " << node.op.to_s << " "
245
- emit(node.y, out)
314
+ def emit_binary_expr(node, out, indent: 0)
315
+ emit(node.x, out, indent: indent)
316
+ out << " " << node.op.to_s
317
+
318
+ if node.location.start_line != node.location.end_line
319
+ out << "\n" << tabs(indent + 1)
320
+ else
321
+ out << " "
322
+ end
323
+
324
+ emit(node.y, out, indent: indent)
246
325
  end
247
326
 
248
327
  def emit_unary_expr(node, out)
@@ -256,20 +335,31 @@ module Gob
256
335
  out << ")"
257
336
  end
258
337
 
259
- def emit_composite_lit(node, out)
338
+ def emit_composite_lit(node, out, indent: 0)
260
339
  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)
340
+
341
+ if node.location.start_line != node.location.end_line && !node.elts.empty?
342
+ out << "{\n"
343
+ node.elts.each do |elt|
344
+ out << tabs(indent + 1)
345
+ emit(elt, out, indent: indent + 1)
346
+ out << ",\n"
347
+ end
348
+ out << tabs(indent) << "}"
349
+ else
350
+ out << "{"
351
+ node.elts.each_with_index do |elt, i|
352
+ out << ", " if i > 0
353
+ emit(elt, out, indent: indent)
354
+ end
355
+ out << "}"
265
356
  end
266
- out << "}"
267
357
  end
268
358
 
269
- def emit_key_value_expr(node, out)
359
+ def emit_key_value_expr(node, out, indent: 0)
270
360
  emit(node.key, out)
271
361
  out << ": "
272
- emit(node.value, out)
362
+ emit(node.value, out, indent: indent)
273
363
  end
274
364
 
275
365
  def emit_index_expr(node, out)
@@ -279,6 +369,19 @@ module Gob
279
369
  out << "]"
280
370
  end
281
371
 
372
+ def emit_slice_expr(node, out)
373
+ emit(node.x, out)
374
+ out << "["
375
+ emit(node.low, out) if node.low
376
+ out << ":"
377
+ emit(node.high, out) if node.high
378
+ if node.slice3
379
+ out << ":"
380
+ emit(node.max, out) if node.max
381
+ end
382
+ out << "]"
383
+ end
384
+
282
385
  def emit_array_type(node, out)
283
386
  out << "["
284
387
  emit(node.len, out) if node.len
@@ -302,11 +405,7 @@ module Gob
302
405
  def emit_type_assert(node, out)
303
406
  emit(node.x, out)
304
407
  out << ".("
305
- if node.type
306
- emit(node.type, out)
307
- else
308
- out << "type"
309
- end
408
+ node.type ? emit(node.type, out) : out << "type"
310
409
  out << ")"
311
410
  end
312
411
 
@@ -374,12 +473,56 @@ module Gob
374
473
  emit(node.body, out, indent: indent)
375
474
  end
376
475
 
476
+ def emit_type_switch_stmt(node, out, indent: 0)
477
+ out << "switch "
478
+ emit(node.init, out) if node.init
479
+ out << "; " if node.init
480
+ emit(node.assign, out)
481
+ out << " "
482
+ emit(node.body, out, indent: indent)
483
+ end
484
+
485
+ def emit_select_stmt(node, out, indent: 0)
486
+ out << "select "
487
+ emit(node.body, out, indent: indent)
488
+ end
489
+
490
+ def emit_comm_clause(node, out, indent: 0)
491
+ if node.comm
492
+ out << tabs(indent) << "case "
493
+ emit(node.comm, out)
494
+ out << ":\n"
495
+ else
496
+ out << tabs(indent) << "default:\n"
497
+ end
498
+ node.body.each do |stmt|
499
+ out << tabs(indent + 1)
500
+ emit(stmt, out, indent: indent + 1)
501
+ out << "\n" unless out.end_with?("\n")
502
+ end
503
+ end
504
+
505
+ def emit_chan_type(node, out)
506
+ case node.dir
507
+ when 1
508
+ out << "chan<- "
509
+ when 2
510
+ out << "<-chan "
511
+ else
512
+ out << "chan "
513
+ end
514
+ emit(node.value, out)
515
+ end
516
+
377
517
  def emit_case_clause(node, out, indent: 0)
378
518
  if node.list.empty?
379
519
  out << tabs(indent) << "default:\n"
380
520
  else
381
521
  out << tabs(indent) << "case "
382
- node.list.each_with_index { |n, i| out << ", " if i > 0; emit(n, out) }
522
+ node.list.each_with_index do |n, i|
523
+ out << ", " if i > 0
524
+ emit(n, out)
525
+ end
383
526
  out << ":\n"
384
527
  end
385
528
  node.body.each do |stmt|
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # context.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
+ Hoist = Struct.new(:nodes)
12
+
13
+ class Context
14
+ attr_reader :body, :children, :tree, :state
15
+
16
+ protected
17
+
18
+ attr_reader :before_callback, :after_callback, :iterate_callback
19
+
20
+ def inherit_callbacks_from(context)
21
+ @before_callback = context.before_callback
22
+ @after_callback = context.after_callback
23
+ @iterate_callback = context.iterate_callback
24
+ end
25
+
26
+ public
27
+
28
+ def initialize(body, state: nil)
29
+ @body = body
30
+ @state = state || { declared: [] }
31
+ @children = []
32
+ @tree = []
33
+ end
34
+
35
+ # A nested scope sees what was declared above it, and nothing it declares
36
+ # escapes back out. `inherit: false` opens a fresh declaration scope.
37
+ def descend(body, inherit: true)
38
+ child_state =
39
+ if inherit
40
+ { **state, declared: state.fetch(:declared).dup }
41
+ end
42
+
43
+ child_context = self.class.new(body, state: child_state)
44
+ child_context.inherit_callbacks_from(self)
45
+
46
+ @children << child_context
47
+
48
+ child_context
49
+ end
50
+
51
+ # Records `names` and answers with the ones that were not already in scope.
52
+ def declare(names)
53
+ state.fetch(:declared).then do |declared|
54
+ names.reject { |name| declared.include?(name) }.tap { |fresh| declared.concat(fresh) }
55
+ end
56
+ end
57
+
58
+ def match(&block)
59
+ instance_exec(self, &block) if block
60
+
61
+ before_callback&.call(self)
62
+
63
+ Array(body).each do |node|
64
+ iterate_callback && instance_exec(self, node, &iterate_callback).then do |result|
65
+ case result
66
+ when Hoist
67
+ result.nodes.each do |hoisted_node|
68
+ hoisted = iterate_callback && instance_exec(self, hoisted_node, &iterate_callback)
69
+
70
+ if hoisted
71
+ @tree << hoisted
72
+ end
73
+ end
74
+ when nil
75
+ # dropped
76
+ else
77
+ @tree << result
78
+ end
79
+ end
80
+ end
81
+
82
+ children.each(&:match)
83
+
84
+ after_callback&.call(self)
85
+
86
+ self
87
+ end
88
+
89
+ def before(&block)
90
+ @before_callback = block
91
+ end
92
+
93
+ def after(&block)
94
+ @after_callback = block
95
+ end
96
+
97
+ def iterate(&block)
98
+ @iterate_callback = block
99
+ end
100
+ end
101
+ end
102
+ end