bonescript-compiler 0.5.3 → 0.5.5
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.
- package/LICENSE +21 -21
- package/dist/algorithm_catalog.js +166 -166
- package/dist/cli.d.ts +1 -2
- package/dist/cli.js +617 -75
- package/dist/cli.js.map +1 -1
- package/dist/emit_capability.d.ts +0 -13
- package/dist/emit_capability.js +134 -296
- package/dist/emit_capability.js.map +1 -1
- package/dist/emit_composition.js +3 -37
- package/dist/emit_composition.js.map +1 -1
- package/dist/emit_deploy.js +167 -165
- package/dist/emit_deploy.js.map +1 -1
- package/dist/emit_events.d.ts +0 -1
- package/dist/emit_events.js +275 -325
- package/dist/emit_events.js.map +1 -1
- package/dist/emit_extras.js +5 -3
- package/dist/emit_extras.js.map +1 -1
- package/dist/emit_full.js +112 -272
- package/dist/emit_full.js.map +1 -1
- package/dist/emit_maintenance.js +249 -249
- package/dist/emit_nakama.d.ts +23 -0
- package/dist/emit_nakama.js +510 -0
- package/dist/emit_nakama.js.map +1 -0
- package/dist/emit_runtime.d.ts +11 -17
- package/dist/emit_runtime.js +688 -29
- package/dist/emit_runtime.js.map +1 -1
- package/dist/emit_sourcemap.js +66 -66
- package/dist/emit_tests.js +12 -47
- package/dist/emit_tests.js.map +1 -1
- package/dist/emit_websocket.js +3 -0
- package/dist/emit_websocket.js.map +1 -1
- package/dist/emitter.js +49 -94
- package/dist/emitter.js.map +1 -1
- package/dist/extension_manager.d.ts +2 -2
- package/dist/extension_manager.js +20 -9
- package/dist/extension_manager.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/ir.d.ts +0 -4
- package/dist/lowering.d.ts +14 -5
- package/dist/lowering.js +417 -66
- package/dist/lowering.js.map +1 -1
- package/dist/module_loader.d.ts +2 -2
- package/dist/module_loader.js +23 -20
- package/dist/module_loader.js.map +1 -1
- package/dist/optimizer.js +3 -6
- package/dist/optimizer.js.map +1 -1
- package/dist/scaffold.d.ts +2 -2
- package/dist/scaffold.js +319 -315
- package/dist/scaffold.js.map +1 -1
- package/dist/solver.js +1 -1
- package/dist/solver.js.map +1 -1
- package/dist/source_map.js.map +1 -0
- package/dist/test.js.map +1 -0
- package/dist/test_typechecker.d.ts +5 -0
- package/dist/test_typechecker.js +126 -0
- package/dist/test_typechecker.js.map +1 -0
- package/dist/typechecker.d.ts +0 -7
- package/dist/typechecker.js +16 -103
- package/dist/typechecker.js.map +1 -1
- package/dist/verifier.d.ts +1 -5
- package/dist/verifier.js +38 -142
- package/dist/verifier.js.map +1 -1
- package/package.json +53 -62
- package/src/algorithm_catalog.ts +345 -345
- package/src/ast.d.ts +244 -0
- package/src/ast.ts +334 -334
- package/src/cli.ts +704 -98
- package/src/emit_batch.ts +140 -140
- package/src/emit_capability.ts +436 -613
- package/src/emit_composition.ts +196 -229
- package/src/emit_deploy.ts +190 -187
- package/src/emit_events.ts +307 -362
- package/src/emit_extras.ts +240 -237
- package/src/emit_full.ts +309 -472
- package/src/emit_maintenance.ts +459 -459
- package/src/emit_nakama.ts +576 -0
- package/src/emit_runtime.ts +730 -17
- package/src/emit_sourcemap.ts +140 -140
- package/src/emit_tests.ts +205 -243
- package/src/emit_websocket.ts +229 -226
- package/src/emitter.ts +578 -626
- package/src/extension_manager.ts +187 -177
- package/src/formatter.ts +297 -297
- package/src/index.ts +90 -88
- package/src/ir.ts +215 -216
- package/src/lexer.d.ts +195 -0
- package/src/lexer.ts +630 -630
- package/src/lowering.ts +556 -168
- package/src/module_loader.ts +114 -112
- package/src/optimizer.ts +196 -199
- package/src/parse_decls.d.ts +13 -0
- package/src/parse_decls.ts +409 -409
- package/src/parse_decls2.d.ts +13 -0
- package/src/parse_decls2.ts +244 -244
- package/src/parse_expr.d.ts +7 -0
- package/src/parse_expr.ts +197 -197
- package/src/parse_types.d.ts +6 -0
- package/src/parse_types.ts +54 -54
- package/src/parser.d.ts +10 -0
- package/src/parser.ts +1 -1
- package/src/parser_base.d.ts +19 -0
- package/src/parser_base.ts +57 -57
- package/src/parser_recovery.ts +153 -153
- package/src/scaffold.ts +375 -371
- package/src/solver.ts +330 -330
- package/src/typechecker.d.ts +52 -0
- package/src/typechecker.ts +591 -700
- package/src/types.d.ts +38 -0
- package/src/types.ts +122 -122
- package/src/verifier.ts +49 -154
- package/README.md +0 -382
- package/dist/commands/check.d.ts +0 -5
- package/dist/commands/check.js +0 -34
- package/dist/commands/check.js.map +0 -1
- package/dist/commands/compile.d.ts +0 -5
- package/dist/commands/compile.js +0 -215
- package/dist/commands/compile.js.map +0 -1
- package/dist/commands/debug.d.ts +0 -5
- package/dist/commands/debug.js +0 -59
- package/dist/commands/debug.js.map +0 -1
- package/dist/commands/diff.d.ts +0 -5
- package/dist/commands/diff.js +0 -123
- package/dist/commands/diff.js.map +0 -1
- package/dist/commands/fmt.d.ts +0 -5
- package/dist/commands/fmt.js +0 -49
- package/dist/commands/fmt.js.map +0 -1
- package/dist/commands/init.d.ts +0 -5
- package/dist/commands/init.js +0 -96
- package/dist/commands/init.js.map +0 -1
- package/dist/commands/ir.d.ts +0 -5
- package/dist/commands/ir.js +0 -27
- package/dist/commands/ir.js.map +0 -1
- package/dist/commands/lex.d.ts +0 -5
- package/dist/commands/lex.js +0 -21
- package/dist/commands/lex.js.map +0 -1
- package/dist/commands/parse.d.ts +0 -5
- package/dist/commands/parse.js +0 -30
- package/dist/commands/parse.js.map +0 -1
- package/dist/commands/test.d.ts +0 -5
- package/dist/commands/test.js +0 -61
- package/dist/commands/test.js.map +0 -1
- package/dist/commands/verify_determinism.d.ts +0 -5
- package/dist/commands/verify_determinism.js +0 -64
- package/dist/commands/verify_determinism.js.map +0 -1
- package/dist/commands/watch.d.ts +0 -5
- package/dist/commands/watch.js +0 -50
- package/dist/commands/watch.js.map +0 -1
- package/dist/emit_auth.d.ts +0 -18
- package/dist/emit_auth.js +0 -507
- package/dist/emit_auth.js.map +0 -1
- package/dist/emit_database.d.ts +0 -7
- package/dist/emit_database.js +0 -72
- package/dist/emit_database.js.map +0 -1
- package/dist/emit_index.d.ts +0 -6
- package/dist/emit_index.js +0 -202
- package/dist/emit_index.js.map +0 -1
- package/dist/emit_models.d.ts +0 -12
- package/dist/emit_models.js +0 -171
- package/dist/emit_models.js.map +0 -1
- package/dist/emit_openapi.d.ts +0 -9
- package/dist/emit_openapi.js +0 -306
- package/dist/emit_openapi.js.map +0 -1
- package/dist/emit_package.d.ts +0 -7
- package/dist/emit_package.js +0 -68
- package/dist/emit_package.js.map +0 -1
- package/dist/emit_router.d.ts +0 -12
- package/dist/emit_router.js +0 -389
- package/dist/emit_router.js.map +0 -1
- package/dist/lowering_channels.d.ts +0 -11
- package/dist/lowering_channels.js +0 -103
- package/dist/lowering_channels.js.map +0 -1
- package/dist/lowering_entities.d.ts +0 -11
- package/dist/lowering_entities.js +0 -232
- package/dist/lowering_entities.js.map +0 -1
- package/dist/lowering_helpers.d.ts +0 -13
- package/dist/lowering_helpers.js +0 -76
- package/dist/lowering_helpers.js.map +0 -1
- package/src/commands/check.ts +0 -33
- package/src/commands/compile.ts +0 -191
- package/src/commands/debug.ts +0 -33
- package/src/commands/diff.ts +0 -105
- package/src/commands/fmt.ts +0 -22
- package/src/commands/init.ts +0 -72
- package/src/commands/ir.ts +0 -23
- package/src/commands/lex.ts +0 -17
- package/src/commands/parse.ts +0 -24
- package/src/commands/test.ts +0 -36
- package/src/commands/verify_determinism.ts +0 -66
- package/src/commands/watch.ts +0 -25
- package/src/emit_auth.ts +0 -513
- package/src/emit_database.ts +0 -72
- package/src/emit_index.ts +0 -210
- package/src/emit_models.ts +0 -176
- package/src/emit_openapi.ts +0 -315
- package/src/emit_package.ts +0 -66
- package/src/emit_router.ts +0 -408
- package/src/lowering_channels.ts +0 -108
- package/src/lowering_entities.ts +0 -258
- package/src/lowering_helpers.ts +0 -75
package/src/emitter.ts
CHANGED
|
@@ -1,626 +1,578 @@
|
|
|
1
|
-
|
|
2
|
-
* BoneScript Code Emitter — Stage 6 of the compilation pipeline.
|
|
3
|
-
* Implements spec/09_CODEGEN.md.
|
|
4
|
-
*
|
|
5
|
-
* Generates target code from the IR. Every IR node maps to code.
|
|
6
|
-
* No orphan logic. No hidden behavior. Deterministic formatting.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import * as IR from "./ir";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
lines
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
line +=
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
lines.push(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
lines.push(
|
|
184
|
-
lines.push(
|
|
185
|
-
lines.push(`
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
lines.push(
|
|
264
|
-
lines.push(
|
|
265
|
-
lines.push(
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
lines
|
|
291
|
-
lines.push(
|
|
292
|
-
lines.push(
|
|
293
|
-
lines.push(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
lines.push(
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
lines.push(`
|
|
343
|
-
lines.push(``);
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
lines.push(`
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
lines.push(
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
lines.push(`
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
//
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
lines.push(`
|
|
470
|
-
lines.push(
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
const
|
|
479
|
-
lines.push(
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
lines.push(`
|
|
497
|
-
lines.push(
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
lines.push(
|
|
501
|
-
lines.push(`
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
lines.push(`
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
lines.push(
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
lines.push(`# Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
580
|
-
lines.push(`# Source: ${system.source_hash}`);
|
|
581
|
-
lines.push(``);
|
|
582
|
-
lines.push(`infrastructure:`);
|
|
583
|
-
|
|
584
|
-
// Data stores
|
|
585
|
-
lines.push(` datastores:`);
|
|
586
|
-
for (const mod of system.modules) {
|
|
587
|
-
if (mod.kind === "data_store") {
|
|
588
|
-
lines.push(` - name: ${toSnakeCase(mod.name)}`);
|
|
589
|
-
lines.push(` engine: ${mod.config["engine"] || "postgresql"}`);
|
|
590
|
-
lines.push(` replicas: ${mod.config["replicas"] || 1}`);
|
|
591
|
-
if (mod.config["retention_ms"]) lines.push(` retention_ms: ${mod.config["retention_ms"]}`);
|
|
592
|
-
if (mod.config["partition_key"]) lines.push(` partition_key: ${mod.config["partition_key"]}`);
|
|
593
|
-
lines.push(``);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
// Gateway
|
|
598
|
-
lines.push(` gateway:`);
|
|
599
|
-
const gw = system.modules.find(m => m.kind === "gateway");
|
|
600
|
-
if (gw) {
|
|
601
|
-
for (const [key, val] of Object.entries(gw.config)) {
|
|
602
|
-
lines.push(` ${key}: ${val}`);
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
lines.push(``);
|
|
606
|
-
|
|
607
|
-
// Events
|
|
608
|
-
if (system.events.length > 0) {
|
|
609
|
-
lines.push(` events:`);
|
|
610
|
-
for (const ev of system.events) {
|
|
611
|
-
lines.push(` - name: ${ev.name}`);
|
|
612
|
-
lines.push(` delivery: ${ev.delivery}`);
|
|
613
|
-
lines.push(` ordering: ${ev.ordering}`);
|
|
614
|
-
if (ev.ttl_ms) lines.push(` ttl_ms: ${ev.ttl_ms}`);
|
|
615
|
-
lines.push(``);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
return {
|
|
620
|
-
path: `config/infrastructure.yaml`,
|
|
621
|
-
content: lines.join("\n"),
|
|
622
|
-
language: "yaml",
|
|
623
|
-
source_module: "config",
|
|
624
|
-
};
|
|
625
|
-
}
|
|
626
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Code Emitter — Stage 6 of the compilation pipeline.
|
|
3
|
+
* Implements spec/09_CODEGEN.md.
|
|
4
|
+
*
|
|
5
|
+
* Generates target code from the IR. Every IR node maps to code.
|
|
6
|
+
* No orphan logic. No hidden behavior. Deterministic formatting.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as IR from "./ir";
|
|
10
|
+
|
|
11
|
+
export interface EmittedFile {
|
|
12
|
+
path: string;
|
|
13
|
+
content: string;
|
|
14
|
+
language: "typescript" | "sql" | "yaml" | "json";
|
|
15
|
+
source_module: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ─── Type Mapping ────────────────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
const TS_TYPE_MAP: Record<string, string> = {
|
|
21
|
+
string: "string",
|
|
22
|
+
uint: "number",
|
|
23
|
+
int: "number",
|
|
24
|
+
float: "number",
|
|
25
|
+
bool: "boolean",
|
|
26
|
+
timestamp: "Date",
|
|
27
|
+
uuid: "string",
|
|
28
|
+
bytes: "Buffer",
|
|
29
|
+
json: "unknown",
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const SQL_TYPE_MAP: Record<string, string> = {
|
|
33
|
+
string: "VARCHAR",
|
|
34
|
+
uint: "BIGINT",
|
|
35
|
+
int: "BIGINT",
|
|
36
|
+
float: "DOUBLE PRECISION",
|
|
37
|
+
bool: "BOOLEAN",
|
|
38
|
+
timestamp: "TIMESTAMPTZ",
|
|
39
|
+
uuid: "UUID",
|
|
40
|
+
bytes: "BYTEA",
|
|
41
|
+
json: "JSONB",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function toTsType(irType: string): string {
|
|
45
|
+
if (TS_TYPE_MAP[irType]) return TS_TYPE_MAP[irType];
|
|
46
|
+
const listMatch = irType.match(/^list<(.+)>$/);
|
|
47
|
+
if (listMatch) return `${toTsType(listMatch[1])}[]`;
|
|
48
|
+
const setMatch = irType.match(/^set<(.+)>$/);
|
|
49
|
+
if (setMatch) return `Set<${toTsType(setMatch[1])}>`;
|
|
50
|
+
const mapMatch = irType.match(/^map<(.+),\s*(.+)>$/);
|
|
51
|
+
if (mapMatch) return `Map<${toTsType(mapMatch[1])}, ${toTsType(mapMatch[2])}>`;
|
|
52
|
+
const optMatch = irType.match(/^optional<(.+)>$/);
|
|
53
|
+
if (optMatch) return `${toTsType(optMatch[1])} | null`;
|
|
54
|
+
// Entity reference or unknown
|
|
55
|
+
return irType;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function toSqlType(irType: string): string {
|
|
59
|
+
if (SQL_TYPE_MAP[irType]) return SQL_TYPE_MAP[irType];
|
|
60
|
+
if (irType.startsWith("list<") || irType.startsWith("set<") || irType.startsWith("map<")) return "JSONB";
|
|
61
|
+
if (irType.startsWith("optional<")) return toSqlType(irType.slice(9, -1));
|
|
62
|
+
return "JSONB";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function toSnakeCase(s: string): string {
|
|
66
|
+
return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ─── Emitter ─────────────────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
export class Emitter {
|
|
72
|
+
emit(system: IR.IRSystem): EmittedFile[] {
|
|
73
|
+
const files: EmittedFile[] = [];
|
|
74
|
+
|
|
75
|
+
// 1. Schema files (SQL)
|
|
76
|
+
for (const mod of system.modules) {
|
|
77
|
+
if (mod.kind === "data_store" || mod.kind === "api_service") {
|
|
78
|
+
for (const model of mod.models) {
|
|
79
|
+
files.push(this.emitSchema(model, mod, system));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 2. Type definition files (TypeScript)
|
|
85
|
+
files.push(this.emitSharedTypes(system));
|
|
86
|
+
|
|
87
|
+
// 3. Event types (TypeScript)
|
|
88
|
+
if (system.events.length > 0) {
|
|
89
|
+
files.push(this.emitEventTypes(system));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 4. Service files (TypeScript)
|
|
93
|
+
for (const mod of system.modules) {
|
|
94
|
+
if (mod.kind === "api_service") {
|
|
95
|
+
files.push(this.emitService(mod, system));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 5. State machine files (TypeScript)
|
|
100
|
+
for (const mod of system.modules) {
|
|
101
|
+
for (const sm of mod.state_machines) {
|
|
102
|
+
files.push(this.emitStateMachine(sm, mod, system));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 6. Config files (YAML)
|
|
107
|
+
files.push(this.emitServiceConfig(system));
|
|
108
|
+
|
|
109
|
+
// 7. Infrastructure config (YAML)
|
|
110
|
+
files.push(this.emitInfraConfig(system));
|
|
111
|
+
|
|
112
|
+
return files;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ─── SQL Schema ────────────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
private emitSchema(model: IR.IRModel, mod: IR.IRModule, system: IR.IRSystem): EmittedFile {
|
|
118
|
+
const tableName = toSnakeCase(model.name) + "s";
|
|
119
|
+
const lines: string[] = [];
|
|
120
|
+
|
|
121
|
+
lines.push(`-- Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
122
|
+
lines.push(`-- Source: ${system.source_hash}`);
|
|
123
|
+
lines.push(`-- Module: ${mod.name}`);
|
|
124
|
+
lines.push(``);
|
|
125
|
+
lines.push(`CREATE TABLE IF NOT EXISTS ${tableName} (`);
|
|
126
|
+
|
|
127
|
+
const fieldLines: string[] = [];
|
|
128
|
+
for (const field of model.fields) {
|
|
129
|
+
let line = ` ${field.name} ${toSqlType(field.type)}`;
|
|
130
|
+
if (!field.nullable) line += " NOT NULL";
|
|
131
|
+
if (field.default_value) {
|
|
132
|
+
if (field.default_value === "gen_random_uuid()") line += " DEFAULT gen_random_uuid()";
|
|
133
|
+
else if (field.default_value === "now()") line += " DEFAULT NOW()";
|
|
134
|
+
else line += ` DEFAULT ${field.default_value}`;
|
|
135
|
+
} else if (field.name === "created_at" || field.name === "updated_at") {
|
|
136
|
+
// Always add DEFAULT NOW() for timestamp audit fields
|
|
137
|
+
line += " DEFAULT NOW()";
|
|
138
|
+
} else if (field.name === "id" && field.type === "uuid") {
|
|
139
|
+
// Always add DEFAULT gen_random_uuid() for uuid primary keys
|
|
140
|
+
line += " DEFAULT gen_random_uuid()";
|
|
141
|
+
}
|
|
142
|
+
if (field.name === model.primary_key) line += " PRIMARY KEY";
|
|
143
|
+
fieldLines.push(line);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Add unique constraints
|
|
147
|
+
for (const c of model.constraints) {
|
|
148
|
+
if (c.kind === "unique") {
|
|
149
|
+
fieldLines.push(` CONSTRAINT ${tableName}_${c.target}_unique UNIQUE (${c.target})`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Add foreign key constraints from relations
|
|
154
|
+
for (const rel of mod.relations || []) {
|
|
155
|
+
if (rel.kind === "belongs_to") {
|
|
156
|
+
// belongs_to: FK is on this table
|
|
157
|
+
fieldLines.push(` CONSTRAINT fk_${tableName}_${rel.foreign_key} FOREIGN KEY (${rel.foreign_key}) REFERENCES ${rel.to_table}(id) ON DELETE CASCADE`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
lines.push(fieldLines.join(",\n"));
|
|
162
|
+
lines.push(`);`);
|
|
163
|
+
lines.push(``);
|
|
164
|
+
|
|
165
|
+
// Indexes
|
|
166
|
+
for (const idx of model.indexes) {
|
|
167
|
+
const idxName = `idx_${tableName}_${idx.fields.join("_")}`;
|
|
168
|
+
const unique = idx.unique ? "UNIQUE " : "";
|
|
169
|
+
lines.push(`CREATE ${unique}INDEX IF NOT EXISTS ${idxName} ON ${tableName} (${idx.fields.join(", ")});`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// FK indexes for belongs_to relations
|
|
173
|
+
for (const rel of mod.relations || []) {
|
|
174
|
+
if (rel.kind === "belongs_to") {
|
|
175
|
+
lines.push(`CREATE INDEX IF NOT EXISTS idx_${tableName}_${rel.foreign_key} ON ${tableName} (${rel.foreign_key});`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Junction tables for many_to_many
|
|
180
|
+
for (const rel of mod.relations || []) {
|
|
181
|
+
if (rel.kind === "many_to_many" && rel.junction_table) {
|
|
182
|
+
lines.push(``);
|
|
183
|
+
lines.push(`CREATE TABLE IF NOT EXISTS ${rel.junction_table} (`);
|
|
184
|
+
lines.push(` ${rel.from_table.slice(0, -1)}_id UUID NOT NULL REFERENCES ${rel.from_table}(id) ON DELETE CASCADE,`);
|
|
185
|
+
lines.push(` ${rel.to_table.slice(0, -1)}_id UUID NOT NULL REFERENCES ${rel.to_table}(id) ON DELETE CASCADE,`);
|
|
186
|
+
lines.push(` created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),`);
|
|
187
|
+
lines.push(` PRIMARY KEY (${rel.from_table.slice(0, -1)}_id, ${rel.to_table.slice(0, -1)}_id)`);
|
|
188
|
+
lines.push(`);`);
|
|
189
|
+
lines.push(`CREATE INDEX IF NOT EXISTS idx_${rel.junction_table}_${rel.from_table.slice(0, -1)} ON ${rel.junction_table} (${rel.from_table.slice(0, -1)}_id);`);
|
|
190
|
+
lines.push(`CREATE INDEX IF NOT EXISTS idx_${rel.junction_table}_${rel.to_table.slice(0, -1)} ON ${rel.junction_table} (${rel.to_table.slice(0, -1)}_id);`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Updated_at trigger
|
|
195
|
+
if (model.fields.some(f => f.name === "updated_at")) {
|
|
196
|
+
lines.push(``);
|
|
197
|
+
lines.push(`CREATE OR REPLACE FUNCTION update_${tableName}_updated_at()`);
|
|
198
|
+
lines.push(`RETURNS TRIGGER AS $$`);
|
|
199
|
+
lines.push(`BEGIN`);
|
|
200
|
+
lines.push(` NEW.updated_at = NOW();`);
|
|
201
|
+
lines.push(` RETURN NEW;`);
|
|
202
|
+
lines.push(`END;`);
|
|
203
|
+
lines.push(`$$ LANGUAGE plpgsql;`);
|
|
204
|
+
lines.push(``);
|
|
205
|
+
lines.push(`CREATE TRIGGER trg_${tableName}_updated_at`);
|
|
206
|
+
lines.push(` BEFORE UPDATE ON ${tableName}`);
|
|
207
|
+
lines.push(` FOR EACH ROW`);
|
|
208
|
+
lines.push(` EXECUTE FUNCTION update_${tableName}_updated_at();`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
lines.push(``);
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
path: `schema/${toSnakeCase(model.name)}.sql`,
|
|
215
|
+
content: lines.join("\n"),
|
|
216
|
+
language: "sql",
|
|
217
|
+
source_module: mod.id,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ─── Shared Types ──────────────────────────────────────────────────────────
|
|
222
|
+
|
|
223
|
+
private emitSharedTypes(system: IR.IRSystem): EmittedFile {
|
|
224
|
+
const lines: string[] = [];
|
|
225
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
226
|
+
lines.push(`// Source: ${system.source_hash}`);
|
|
227
|
+
lines.push(``);
|
|
228
|
+
|
|
229
|
+
// Track exported interface names to avoid duplicate exports (e.g. ValidationError
|
|
230
|
+
// defined in multiple model files would collide on export * re-exports).
|
|
231
|
+
const exportedNames = new Set<string>();
|
|
232
|
+
|
|
233
|
+
for (const mod of system.modules) {
|
|
234
|
+
for (const model of mod.models) {
|
|
235
|
+
// If the name was already exported by a previous module, qualify it with
|
|
236
|
+
// the module name so every export remains unique.
|
|
237
|
+
let ifaceName = model.name;
|
|
238
|
+
if (exportedNames.has(ifaceName)) {
|
|
239
|
+
ifaceName = `${mod.name}${model.name}`;
|
|
240
|
+
}
|
|
241
|
+
exportedNames.add(ifaceName);
|
|
242
|
+
|
|
243
|
+
lines.push(`export interface ${ifaceName} {`);
|
|
244
|
+
for (const field of model.fields) {
|
|
245
|
+
const nullable = field.nullable ? " | null" : "";
|
|
246
|
+
lines.push(` ${field.name}: ${toTsType(field.type)}${nullable};`);
|
|
247
|
+
}
|
|
248
|
+
lines.push(`}`);
|
|
249
|
+
lines.push(``);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Common types
|
|
254
|
+
lines.push(`export interface ServiceError {`);
|
|
255
|
+
lines.push(` code: string;`);
|
|
256
|
+
lines.push(` message: string;`);
|
|
257
|
+
lines.push(` details?: unknown;`);
|
|
258
|
+
lines.push(`}`);
|
|
259
|
+
lines.push(``);
|
|
260
|
+
lines.push(`export type Result<T, E = ServiceError> =`);
|
|
261
|
+
lines.push(` | { ok: true; value: T }`);
|
|
262
|
+
lines.push(` | { ok: false; error: E };`);
|
|
263
|
+
lines.push(``);
|
|
264
|
+
lines.push(`export interface RequestContext {`);
|
|
265
|
+
lines.push(` authenticated: boolean;`);
|
|
266
|
+
lines.push(` actor_id: string | null;`);
|
|
267
|
+
lines.push(` trace_id: string;`);
|
|
268
|
+
lines.push(` correlation_id: string;`);
|
|
269
|
+
lines.push(`}`);
|
|
270
|
+
lines.push(``);
|
|
271
|
+
lines.push(`export interface PaginatedResult<T> {`);
|
|
272
|
+
lines.push(` items: T[];`);
|
|
273
|
+
lines.push(` total: number;`);
|
|
274
|
+
lines.push(` page: number;`);
|
|
275
|
+
lines.push(` page_size: number;`);
|
|
276
|
+
lines.push(`}`);
|
|
277
|
+
lines.push(``);
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
path: `types/models.ts`,
|
|
281
|
+
content: lines.join("\n"),
|
|
282
|
+
language: "typescript",
|
|
283
|
+
source_module: "shared",
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ─── Event Types ───────────────────────────────────────────────────────────
|
|
288
|
+
|
|
289
|
+
private emitEventTypes(system: IR.IRSystem): EmittedFile {
|
|
290
|
+
const lines: string[] = [];
|
|
291
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
292
|
+
lines.push(`// Source: ${system.source_hash}`);
|
|
293
|
+
lines.push(``);
|
|
294
|
+
|
|
295
|
+
for (const ev of system.events) {
|
|
296
|
+
lines.push(`export interface ${ev.name}Event {`);
|
|
297
|
+
lines.push(` type: "${ev.name}";`);
|
|
298
|
+
lines.push(` payload: {`);
|
|
299
|
+
for (const field of ev.payload) {
|
|
300
|
+
const nullable = field.nullable ? " | null" : "";
|
|
301
|
+
lines.push(` ${field.name}: ${toTsType(field.type)}${nullable};`);
|
|
302
|
+
}
|
|
303
|
+
lines.push(` };`);
|
|
304
|
+
lines.push(` metadata: {`);
|
|
305
|
+
lines.push(` source: string;`);
|
|
306
|
+
lines.push(` timestamp: Date;`);
|
|
307
|
+
lines.push(` correlation_id: string;`);
|
|
308
|
+
lines.push(` causation_id: string;`);
|
|
309
|
+
lines.push(` };`);
|
|
310
|
+
lines.push(`}`);
|
|
311
|
+
lines.push(``);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Union type of all events
|
|
315
|
+
const eventNames = system.events.map(e => `${e.name}Event`);
|
|
316
|
+
lines.push(`export type SystemEvent = ${eventNames.join(" | ")};`);
|
|
317
|
+
lines.push(``);
|
|
318
|
+
|
|
319
|
+
// Event bus interface
|
|
320
|
+
lines.push(`export interface EventBus {`);
|
|
321
|
+
lines.push(` publish(event: SystemEvent): Promise<void>;`);
|
|
322
|
+
lines.push(` subscribe(type: string, handler: (event: SystemEvent) => Promise<void>): void;`);
|
|
323
|
+
lines.push(`}`);
|
|
324
|
+
lines.push(``);
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
path: `types/events.ts`,
|
|
328
|
+
content: lines.join("\n"),
|
|
329
|
+
language: "typescript",
|
|
330
|
+
source_module: "shared",
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// ─── Service Implementation ────────────────────────────────────────────────
|
|
335
|
+
|
|
336
|
+
private emitService(mod: IR.IRModule, system: IR.IRSystem): EmittedFile {
|
|
337
|
+
const lines: string[] = [];
|
|
338
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
339
|
+
lines.push(`// Source: ${system.source_hash}`);
|
|
340
|
+
lines.push(`// Module: ${mod.name} (${mod.kind})`);
|
|
341
|
+
lines.push(``);
|
|
342
|
+
lines.push(`import { Result, RequestContext, ServiceError, PaginatedResult } from "../types/models";`);
|
|
343
|
+
lines.push(``);
|
|
344
|
+
|
|
345
|
+
for (const iface of mod.interfaces) {
|
|
346
|
+
// Interface definition
|
|
347
|
+
lines.push(`export interface ${iface.name} {`);
|
|
348
|
+
for (const method of iface.methods) {
|
|
349
|
+
const params = method.input.map(f => `${f.name}: ${toTsType(f.type)}`).join(", ");
|
|
350
|
+
const ctxParam = method.authenticated ? "ctx: RequestContext" : "";
|
|
351
|
+
const allParams = [ctxParam, params].filter(Boolean).join(", ");
|
|
352
|
+
lines.push(` ${method.name}(${allParams}): Promise<Result<${toTsType(method.output)}>>;`);
|
|
353
|
+
}
|
|
354
|
+
lines.push(`}`);
|
|
355
|
+
lines.push(``);
|
|
356
|
+
|
|
357
|
+
// Implementation class
|
|
358
|
+
lines.push(`export class ${mod.name} implements ${iface.name} {`);
|
|
359
|
+
for (const method of iface.methods) {
|
|
360
|
+
lines.push(this.emitMethod(method));
|
|
361
|
+
}
|
|
362
|
+
lines.push(`}`);
|
|
363
|
+
lines.push(``);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return {
|
|
367
|
+
path: `services/${toSnakeCase(mod.name)}.ts`,
|
|
368
|
+
content: lines.join("\n"),
|
|
369
|
+
language: "typescript",
|
|
370
|
+
source_module: mod.id,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private emitMethod(method: IR.IRMethod): string {
|
|
375
|
+
const lines: string[] = [];
|
|
376
|
+
const params = method.input.map(f => `${f.name}: ${toTsType(f.type)}`).join(", ");
|
|
377
|
+
const ctxParam = method.authenticated ? "ctx: RequestContext" : "";
|
|
378
|
+
const allParams = [ctxParam, params].filter(Boolean).join(", ");
|
|
379
|
+
|
|
380
|
+
lines.push(` async ${method.name}(${allParams}): Promise<Result<${toTsType(method.output)}>> {`);
|
|
381
|
+
|
|
382
|
+
// Auth check
|
|
383
|
+
if (method.authenticated) {
|
|
384
|
+
lines.push(` // [Guard] Authentication required`);
|
|
385
|
+
lines.push(` if (!ctx.authenticated) {`);
|
|
386
|
+
lines.push(` return { ok: false, error: { code: "UNAUTHORIZED", message: "Authentication required" } };`);
|
|
387
|
+
lines.push(` }`);
|
|
388
|
+
lines.push(``);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Preconditions
|
|
392
|
+
if (method.preconditions.length > 0) {
|
|
393
|
+
lines.push(` // [Preconditions]`);
|
|
394
|
+
for (const pre of method.preconditions) {
|
|
395
|
+
lines.push(` // CHECK: ${pre.description}`);
|
|
396
|
+
}
|
|
397
|
+
lines.push(``);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Effects
|
|
401
|
+
if (method.effects.length > 0) {
|
|
402
|
+
lines.push(` // [Effects] Applied in declaration order (deterministic)`);
|
|
403
|
+
for (const eff of method.effects) {
|
|
404
|
+
const opSymbol = eff.op === "assign" ? "=" : eff.op === "add" ? "+=" : "-=";
|
|
405
|
+
lines.push(` // EFFECT: ${eff.target} ${opSymbol} ${eff.value}`);
|
|
406
|
+
}
|
|
407
|
+
lines.push(``);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Emissions
|
|
411
|
+
if (method.emissions.length > 0) {
|
|
412
|
+
lines.push(` // [Events]`);
|
|
413
|
+
for (const ev of method.emissions) {
|
|
414
|
+
lines.push(` // EMIT: ${ev}`);
|
|
415
|
+
}
|
|
416
|
+
lines.push(``);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
lines.push(` // TODO: Implementation`);
|
|
420
|
+
lines.push(` throw new Error("Not implemented: ${method.name}");`);
|
|
421
|
+
lines.push(` }`);
|
|
422
|
+
lines.push(``);
|
|
423
|
+
|
|
424
|
+
return lines.join("\n");
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// ─── State Machine ─────────────────────────────────────────────────────────
|
|
428
|
+
|
|
429
|
+
private emitStateMachine(sm: IR.IRStateMachine, mod: IR.IRModule, system: IR.IRSystem): EmittedFile {
|
|
430
|
+
const lines: string[] = [];
|
|
431
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
432
|
+
lines.push(`// Source: ${system.source_hash}`);
|
|
433
|
+
lines.push(`// Entity: ${sm.entity}`);
|
|
434
|
+
lines.push(``);
|
|
435
|
+
|
|
436
|
+
// State type
|
|
437
|
+
const stateUnion = sm.states.map(s => `"${s}"`).join(" | ");
|
|
438
|
+
lines.push(`export type ${sm.entity}State = ${stateUnion};`);
|
|
439
|
+
lines.push(``);
|
|
440
|
+
|
|
441
|
+
// Transition table
|
|
442
|
+
lines.push(`export const ${sm.entity.toUpperCase()}_TRANSITIONS: Record<${sm.entity}State, Record<string, ${sm.entity}State>> = {`);
|
|
443
|
+
for (const state of sm.states) {
|
|
444
|
+
const transitions = sm.transitions.filter(t => t.from === state);
|
|
445
|
+
const entries = transitions.map(t => `"${t.trigger}": "${t.to}"`).join(", ");
|
|
446
|
+
lines.push(` "${state}": { ${entries} },`);
|
|
447
|
+
}
|
|
448
|
+
lines.push(`};`);
|
|
449
|
+
lines.push(``);
|
|
450
|
+
|
|
451
|
+
// Transition function
|
|
452
|
+
lines.push(`export interface TransitionError {`);
|
|
453
|
+
lines.push(` current: ${sm.entity}State;`);
|
|
454
|
+
lines.push(` trigger: string;`);
|
|
455
|
+
lines.push(` message: string;`);
|
|
456
|
+
lines.push(`}`);
|
|
457
|
+
lines.push(``);
|
|
458
|
+
lines.push(`export function transition${sm.entity}(`);
|
|
459
|
+
lines.push(` current: ${sm.entity}State,`);
|
|
460
|
+
lines.push(` trigger: string`);
|
|
461
|
+
lines.push(`): { ok: true; state: ${sm.entity}State } | { ok: false; error: TransitionError } {`);
|
|
462
|
+
lines.push(` const next = ${sm.entity.toUpperCase()}_TRANSITIONS[current]?.[trigger];`);
|
|
463
|
+
lines.push(` if (!next) {`);
|
|
464
|
+
lines.push(` return {`);
|
|
465
|
+
lines.push(` ok: false,`);
|
|
466
|
+
lines.push(` error: {`);
|
|
467
|
+
lines.push(` current,`);
|
|
468
|
+
lines.push(` trigger,`);
|
|
469
|
+
lines.push(` message: \`Invalid transition: \${current} --[\${trigger}]--> ?\``);
|
|
470
|
+
lines.push(` }`);
|
|
471
|
+
lines.push(` };`);
|
|
472
|
+
lines.push(` }`);
|
|
473
|
+
lines.push(` return { ok: true, state: next };`);
|
|
474
|
+
lines.push(`}`);
|
|
475
|
+
lines.push(``);
|
|
476
|
+
|
|
477
|
+
// Initial state
|
|
478
|
+
lines.push(`export const ${sm.entity.toUpperCase()}_INITIAL: ${sm.entity}State = "${sm.initial}";`);
|
|
479
|
+
lines.push(``);
|
|
480
|
+
|
|
481
|
+
return {
|
|
482
|
+
path: `services/state_machines/${toSnakeCase(sm.entity)}_states.ts`,
|
|
483
|
+
content: lines.join("\n"),
|
|
484
|
+
language: "typescript",
|
|
485
|
+
source_module: mod.id,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// ─── Service Config (YAML) ─────────────────────────────────────────────────
|
|
490
|
+
|
|
491
|
+
private emitServiceConfig(system: IR.IRSystem): EmittedFile {
|
|
492
|
+
const lines: string[] = [];
|
|
493
|
+
lines.push(`# Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
494
|
+
lines.push(`# Source: ${system.source_hash}`);
|
|
495
|
+
lines.push(``);
|
|
496
|
+
lines.push(`system:`);
|
|
497
|
+
lines.push(` name: ${system.name}`);
|
|
498
|
+
lines.push(` version: ${system.version}`);
|
|
499
|
+
lines.push(` domain: ${system.domain || "generic"}`);
|
|
500
|
+
lines.push(``);
|
|
501
|
+
lines.push(`services:`);
|
|
502
|
+
|
|
503
|
+
for (const mod of system.modules) {
|
|
504
|
+
if (mod.kind === "api_service" || mod.kind === "realtime_service") {
|
|
505
|
+
lines.push(` - name: ${toSnakeCase(mod.name)}`);
|
|
506
|
+
lines.push(` kind: ${mod.kind}`);
|
|
507
|
+
lines.push(` dependencies:`);
|
|
508
|
+
for (const dep of mod.dependencies) {
|
|
509
|
+
const depMod = system.modules.find(m => m.id === dep);
|
|
510
|
+
if (depMod) lines.push(` - ${toSnakeCase(depMod.name)}`);
|
|
511
|
+
}
|
|
512
|
+
for (const [key, val] of Object.entries(mod.config)) {
|
|
513
|
+
lines.push(` ${key}: ${val}`);
|
|
514
|
+
}
|
|
515
|
+
lines.push(``);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
return {
|
|
520
|
+
path: `config/services.yaml`,
|
|
521
|
+
content: lines.join("\n"),
|
|
522
|
+
language: "yaml",
|
|
523
|
+
source_module: "config",
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// ─── Infrastructure Config (YAML) ──────────────────────────────────────────
|
|
528
|
+
|
|
529
|
+
private emitInfraConfig(system: IR.IRSystem): EmittedFile {
|
|
530
|
+
const lines: string[] = [];
|
|
531
|
+
lines.push(`# Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
532
|
+
lines.push(`# Source: ${system.source_hash}`);
|
|
533
|
+
lines.push(``);
|
|
534
|
+
lines.push(`infrastructure:`);
|
|
535
|
+
|
|
536
|
+
// Data stores
|
|
537
|
+
lines.push(` datastores:`);
|
|
538
|
+
for (const mod of system.modules) {
|
|
539
|
+
if (mod.kind === "data_store") {
|
|
540
|
+
lines.push(` - name: ${toSnakeCase(mod.name)}`);
|
|
541
|
+
lines.push(` engine: ${mod.config["engine"] || "postgresql"}`);
|
|
542
|
+
lines.push(` replicas: ${mod.config["replicas"] || 1}`);
|
|
543
|
+
if (mod.config["retention_ms"]) lines.push(` retention_ms: ${mod.config["retention_ms"]}`);
|
|
544
|
+
if (mod.config["partition_key"]) lines.push(` partition_key: ${mod.config["partition_key"]}`);
|
|
545
|
+
lines.push(``);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Gateway
|
|
550
|
+
lines.push(` gateway:`);
|
|
551
|
+
const gw = system.modules.find(m => m.kind === "gateway");
|
|
552
|
+
if (gw) {
|
|
553
|
+
for (const [key, val] of Object.entries(gw.config)) {
|
|
554
|
+
lines.push(` ${key}: ${val}`);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
lines.push(``);
|
|
558
|
+
|
|
559
|
+
// Events
|
|
560
|
+
if (system.events.length > 0) {
|
|
561
|
+
lines.push(` events:`);
|
|
562
|
+
for (const ev of system.events) {
|
|
563
|
+
lines.push(` - name: ${ev.name}`);
|
|
564
|
+
lines.push(` delivery: ${ev.delivery}`);
|
|
565
|
+
lines.push(` ordering: ${ev.ordering}`);
|
|
566
|
+
if (ev.ttl_ms) lines.push(` ttl_ms: ${ev.ttl_ms}`);
|
|
567
|
+
lines.push(``);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return {
|
|
572
|
+
path: `config/infrastructure.yaml`,
|
|
573
|
+
content: lines.join("\n"),
|
|
574
|
+
language: "yaml",
|
|
575
|
+
source_module: "config",
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
}
|