eny-ai 1.0.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.
@@ -0,0 +1,2476 @@
1
+ import {
2
+ __esm,
3
+ __export
4
+ } from "./chunk-PNKVD2UK.js";
5
+
6
+ // src/lexer/tokenize.ts
7
+ function isWhitespace(ch) {
8
+ return ch === " " || ch === " " || ch === "\r";
9
+ }
10
+ function isNewline(ch) {
11
+ return ch === "\n";
12
+ }
13
+ function isDigit(ch) {
14
+ return /[0-9]/.test(ch);
15
+ }
16
+ function isIdentifierStart(ch) {
17
+ return /[A-Za-z_\p{L}]/u.test(ch);
18
+ }
19
+ function isIdentifierPart(ch) {
20
+ return /[A-Za-z0-9_\p{L}]/u.test(ch);
21
+ }
22
+ function getCodePoint(str, pos) {
23
+ const code = str.codePointAt(pos);
24
+ if (code === void 0) return { char: "", len: 0 };
25
+ const char = String.fromCodePoint(code);
26
+ const len = code > 65535 ? 2 : 1;
27
+ return { char, len };
28
+ }
29
+ function tokenize(input) {
30
+ const tokens = [];
31
+ const len = input.length;
32
+ let i = 0;
33
+ while (i < len) {
34
+ const { char: ch, len: charLen } = getCodePoint(input, i);
35
+ if (charLen === 0) {
36
+ i++;
37
+ continue;
38
+ }
39
+ if (isWhitespace(ch)) {
40
+ i += charLen;
41
+ continue;
42
+ }
43
+ if (isNewline(ch)) {
44
+ tokens.push({ type: "NEWLINE" /* NEWLINE */, value: "\n", pos: i });
45
+ i += charLen;
46
+ continue;
47
+ }
48
+ if (SYMBOLS.has(ch)) {
49
+ tokens.push({ type: "SYMBOL" /* SYMBOL */, value: ch, pos: i });
50
+ i += charLen;
51
+ continue;
52
+ }
53
+ if (ch === "\u2192" || ch === "-" && input[i + 1] === ">") {
54
+ if (ch === "\u2192") {
55
+ tokens.push({ type: "ARROW" /* ARROW */, value: "\u2192", pos: i });
56
+ i++;
57
+ } else {
58
+ tokens.push({ type: "ARROW" /* ARROW */, value: "->", pos: i });
59
+ i += 2;
60
+ }
61
+ continue;
62
+ }
63
+ if (ch === "{") {
64
+ tokens.push({ type: "LBRACE" /* LBRACE */, value: "{", pos: i });
65
+ i++;
66
+ continue;
67
+ }
68
+ if (ch === "}") {
69
+ tokens.push({ type: "RBRACE" /* RBRACE */, value: "}", pos: i });
70
+ i++;
71
+ continue;
72
+ }
73
+ if (ch === "(") {
74
+ tokens.push({ type: "LPAREN" /* LPAREN */, value: "(", pos: i });
75
+ i++;
76
+ continue;
77
+ }
78
+ if (ch === ")") {
79
+ tokens.push({ type: "RPAREN" /* RPAREN */, value: ")", pos: i });
80
+ i++;
81
+ continue;
82
+ }
83
+ if (ch === "[") {
84
+ tokens.push({ type: "LBRACKET" /* LBRACKET */, value: "[", pos: i });
85
+ i++;
86
+ continue;
87
+ }
88
+ if (ch === "]") {
89
+ tokens.push({ type: "RBRACKET" /* RBRACKET */, value: "]", pos: i });
90
+ i++;
91
+ continue;
92
+ }
93
+ if (ch === ":") {
94
+ tokens.push({ type: "COLON" /* COLON */, value: ":", pos: i });
95
+ i++;
96
+ continue;
97
+ }
98
+ if (ch === ",") {
99
+ tokens.push({ type: "COMMA" /* COMMA */, value: ",", pos: i });
100
+ i++;
101
+ continue;
102
+ }
103
+ if (ch === "=" && input[i + 1] === "=" || ch === "!" && input[i + 1] === "=" || ch === ">" && input[i + 1] === "=" || ch === "<" && input[i + 1] === "=") {
104
+ tokens.push({ type: "COMPARATOR" /* COMPARATOR */, value: input.substr(i, 2), pos: i });
105
+ i += 2;
106
+ continue;
107
+ }
108
+ if (ch === ">" || ch === "<") {
109
+ tokens.push({ type: "COMPARATOR" /* COMPARATOR */, value: ch, pos: i });
110
+ i++;
111
+ continue;
112
+ }
113
+ if (ch === "\u2260") {
114
+ tokens.push({ type: "COMPARATOR" /* COMPARATOR */, value: "\u2260", pos: i });
115
+ i++;
116
+ continue;
117
+ }
118
+ if (OPERATORS.has(ch) && !SYMBOLS.has(ch)) {
119
+ tokens.push({ type: "OPERATOR" /* OPERATOR */, value: ch, pos: i });
120
+ i++;
121
+ continue;
122
+ }
123
+ if (ch === '"') {
124
+ let j = i + 1;
125
+ let out = "";
126
+ while (j < len) {
127
+ if (input[j] === "\\" && j + 1 < len) {
128
+ out += input[j + 1];
129
+ j += 2;
130
+ continue;
131
+ }
132
+ if (input[j] === '"') {
133
+ break;
134
+ }
135
+ out += input[j];
136
+ j++;
137
+ }
138
+ tokens.push({ type: "STRING" /* STRING */, value: out, pos: i });
139
+ i = j + 1;
140
+ continue;
141
+ }
142
+ if (isDigit(ch)) {
143
+ let j = i;
144
+ let val = "";
145
+ while (j < len && isDigit(input[j])) {
146
+ val += input[j];
147
+ j++;
148
+ }
149
+ tokens.push({ type: "NUMBER" /* NUMBER */, value: val, pos: i });
150
+ i = j;
151
+ continue;
152
+ }
153
+ if (isIdentifierStart(ch)) {
154
+ let j = i;
155
+ let val = "";
156
+ while (j < len && isIdentifierPart(input[j])) {
157
+ val += input[j];
158
+ j++;
159
+ }
160
+ tokens.push({ type: "IDENTIFIER" /* IDENTIFIER */, value: val, pos: i });
161
+ i = j;
162
+ continue;
163
+ }
164
+ tokens.push({ type: "IDENTIFIER" /* IDENTIFIER */, value: ch, pos: i });
165
+ i += charLen;
166
+ }
167
+ tokens.push({ type: "EOF" /* EOF */, value: "", pos: i });
168
+ return tokens;
169
+ }
170
+ var TokenType, SYMBOLS_STATE, SYMBOLS_EXEC, SYMBOLS_STRUCT, SYMBOLS_TIME, SYMBOLS_ENERGY, SYMBOLS_MEMORY, SYMBOLS_UI, SYMBOLS_SECURITY, SYMBOLS_EVOLVE, SYMBOLS_LOGIC, SYMBOLS, SYMBOL_NAMES, OPERATORS;
171
+ var init_tokenize = __esm({
172
+ "src/lexer/tokenize.ts"() {
173
+ "use strict";
174
+ TokenType = /* @__PURE__ */ ((TokenType2) => {
175
+ TokenType2["SYMBOL"] = "SYMBOL";
176
+ TokenType2["IDENTIFIER"] = "IDENTIFIER";
177
+ TokenType2["STRING"] = "STRING";
178
+ TokenType2["NUMBER"] = "NUMBER";
179
+ TokenType2["ARROW"] = "ARROW";
180
+ TokenType2["LBRACE"] = "LBRACE";
181
+ TokenType2["RBRACE"] = "RBRACE";
182
+ TokenType2["COLON"] = "COLON";
183
+ TokenType2["COMMA"] = "COMMA";
184
+ TokenType2["LPAREN"] = "LPAREN";
185
+ TokenType2["RPAREN"] = "RPAREN";
186
+ TokenType2["LBRACKET"] = "LBRACKET";
187
+ TokenType2["RBRACKET"] = "RBRACKET";
188
+ TokenType2["NEWLINE"] = "NEWLINE";
189
+ TokenType2["COMPARATOR"] = "COMPARATOR";
190
+ TokenType2["OPERATOR"] = "OPERATOR";
191
+ TokenType2["EOF"] = "EOF";
192
+ return TokenType2;
193
+ })(TokenType || {});
194
+ SYMBOLS_STATE = /* @__PURE__ */ new Set([
195
+ "\u03A3",
196
+ // STATE - Estado global
197
+ "\u03C3",
198
+ // SUBSTATE - Estado local
199
+ "\xD8",
200
+ // VOID - Não-existência
201
+ "\u{1F512}",
202
+ // LOCK - Imutável
203
+ "\u{1F513}"
204
+ // UNLOCK - Mutável
205
+ ]);
206
+ SYMBOLS_EXEC = /* @__PURE__ */ new Set([
207
+ "\u0394",
208
+ // ACTION - Ação/função
209
+ "\u0192",
210
+ // FUNCTION - Capacidade lógica
211
+ "\u21C9",
212
+ // PARALLEL - Execução paralela
213
+ "\u25B3",
214
+ // DEPEND - Dependência
215
+ "\u25CF",
216
+ // CORE - Núcleo
217
+ "\u23F3"
218
+ // DELAY - Espera temporal
219
+ ]);
220
+ SYMBOLS_STRUCT = /* @__PURE__ */ new Set([
221
+ "\u03A9",
222
+ // OBJECT - Objeto
223
+ "\u25A0",
224
+ // OBJECT ALT - Entidade concreta
225
+ "\u25CB",
226
+ // CLASS - Classe/molde
227
+ "\u2B12",
228
+ // INHERIT - Herança
229
+ "\u2283",
230
+ // CONTAINS - Composição
231
+ "\u2254",
232
+ // ASSIGN - Atribuição
233
+ "\u29C9"
234
+ // MODULE - Módulo
235
+ ]);
236
+ SYMBOLS_TIME = /* @__PURE__ */ new Set([
237
+ "\u26A1",
238
+ // CAUSE - Causa/origem
239
+ "\u2726",
240
+ // CONSEQUENCE - Resultado
241
+ "\u21BB",
242
+ // LOOP - Repetição
243
+ "\u25CC",
244
+ // EVENT - Evento
245
+ "\u21C4",
246
+ // SYNC - Sincronização
247
+ "\u231B"
248
+ // TIMEOUT - Limite temporal
249
+ ]);
250
+ SYMBOLS_ENERGY = /* @__PURE__ */ new Set([
251
+ "\u03A8",
252
+ // ENERGY - Energia vital
253
+ "\u23EC",
254
+ // THROTTLE - Limitar
255
+ "\u23EB",
256
+ // BOOST - Acelerar
257
+ "\u20B5"
258
+ // COST - Custo
259
+ ]);
260
+ SYMBOLS_MEMORY = /* @__PURE__ */ new Set([
261
+ "\u{1D4DC}",
262
+ // MEMORY - Memória
263
+ "\u267E",
264
+ // PERSIST - Persistência
265
+ "\u2302"
266
+ // ROOT - Raiz do sistema
267
+ ]);
268
+ SYMBOLS_UI = /* @__PURE__ */ new Set([
269
+ "\u03A6",
270
+ // FORM - Form/UI
271
+ "\u0398",
272
+ // ANIMATION - Animação
273
+ "\u2610",
274
+ // INTERFACE - Interface
275
+ "\u{1F441}",
276
+ // VISUAL - Percepção visual
277
+ "\u2328",
278
+ // INPUT - Entrada
279
+ "\u{1F9ED}"
280
+ // POINTER - Ponteiro/direção
281
+ ]);
282
+ SYMBOLS_SECURITY = /* @__PURE__ */ new Set([
283
+ "\u{1F6E1}",
284
+ // SECURITY - Segurança
285
+ "\u{1F511}",
286
+ // KEY - Chave/autorização
287
+ "\u26A0",
288
+ // RISK - Risco
289
+ "\u26D4",
290
+ // BLOCK - Bloqueio
291
+ "\u{1F9EA}"
292
+ // SANDBOX - Isolamento
293
+ ]);
294
+ SYMBOLS_EVOLVE = /* @__PURE__ */ new Set([
295
+ "\u2301",
296
+ // EVOLVE BLOCK - Bloco de evolução
297
+ "\u27F3",
298
+ // EVOLVE - Evoluir
299
+ "\u2714",
300
+ // VERIFY - Verificar
301
+ "\u2713",
302
+ // VALID - Válido
303
+ "\u2716",
304
+ // INVALID - Inválido
305
+ "\u{1F4DC}"
306
+ // LOG - Registro
307
+ ]);
308
+ SYMBOLS_LOGIC = /* @__PURE__ */ new Set([
309
+ "\u039B",
310
+ // LOGIC - Lógica condicional
311
+ "\u2297"
312
+ // PARALLEL BLOCK - Bloco paralelo
313
+ ]);
314
+ SYMBOLS = /* @__PURE__ */ new Set([
315
+ ...SYMBOLS_STATE,
316
+ ...SYMBOLS_EXEC,
317
+ ...SYMBOLS_STRUCT,
318
+ ...SYMBOLS_TIME,
319
+ ...SYMBOLS_ENERGY,
320
+ ...SYMBOLS_MEMORY,
321
+ ...SYMBOLS_UI,
322
+ ...SYMBOLS_SECURITY,
323
+ ...SYMBOLS_EVOLVE,
324
+ ...SYMBOLS_LOGIC
325
+ ]);
326
+ SYMBOL_NAMES = {
327
+ "\u03A3": "STATE",
328
+ "\u03C3": "SUBSTATE",
329
+ "\xD8": "VOID",
330
+ "\u{1F512}": "LOCK",
331
+ "\u{1F513}": "UNLOCK",
332
+ "\u0394": "ACTION",
333
+ "\u0192": "FUNCTION",
334
+ "\u21C9": "PARALLEL",
335
+ "\u25B3": "DEPEND",
336
+ "\u25CF": "CORE",
337
+ "\u23F3": "DELAY",
338
+ "\u03A9": "OBJECT",
339
+ "\u25A0": "OBJECT_ALT",
340
+ "\u25CB": "CLASS",
341
+ "\u2B12": "INHERIT",
342
+ "\u2283": "CONTAINS",
343
+ "\u2254": "ASSIGN",
344
+ "\u29C9": "MODULE",
345
+ "\u26A1": "CAUSE",
346
+ "\u2726": "CONSEQUENCE",
347
+ "\u21BB": "LOOP",
348
+ "\u25CC": "EVENT",
349
+ "\u21C4": "SYNC",
350
+ "\u231B": "TIMEOUT",
351
+ "\u03A8": "ENERGY",
352
+ "\u23EC": "THROTTLE",
353
+ "\u23EB": "BOOST",
354
+ "\u20B5": "COST",
355
+ "\u{1D4DC}": "MEMORY",
356
+ "\u267E": "PERSIST",
357
+ "\u2302": "ROOT",
358
+ "\u03A6": "FORM",
359
+ "\u0398": "ANIMATION",
360
+ "\u2610": "INTERFACE",
361
+ "\u{1F441}": "VISUAL",
362
+ "\u2328": "INPUT",
363
+ "\u{1F9ED}": "POINTER",
364
+ "\u{1F6E1}": "SECURITY",
365
+ "\u{1F511}": "KEY",
366
+ "\u26A0": "RISK",
367
+ "\u26D4": "BLOCK",
368
+ "\u{1F9EA}": "SANDBOX",
369
+ "\u2301": "EVOLVE_BLOCK",
370
+ "\u27F3": "EVOLVE",
371
+ "\u2714": "VERIFY",
372
+ "\u2713": "VALID",
373
+ "\u2716": "INVALID",
374
+ "\u{1F4DC}": "LOG",
375
+ "\u039B": "LOGIC",
376
+ "\u2297": "PARALLEL_BLOCK"
377
+ };
378
+ OPERATORS = /* @__PURE__ */ new Set(["+", "\u2212", "-", "\u2254"]);
379
+ }
380
+ });
381
+
382
+ // src/parser/ebnfParser.ts
383
+ function peek(cx) {
384
+ return cx.tokens[cx.i];
385
+ }
386
+ function next(cx) {
387
+ return cx.tokens[cx.i++];
388
+ }
389
+ function eat(cx, type, value) {
390
+ const t = peek(cx);
391
+ if (!t) throw new Error(`Unexpected EOF, expected ${type}`);
392
+ if (t.type !== type) throw new Error(`Expected token ${type} but got ${t.type} ('${t.value}') at ${t.pos}`);
393
+ if (value !== void 0 && t.value !== value) throw new Error(`Expected token value ${value} but got '${t.value}' at ${t.pos}`);
394
+ return next(cx);
395
+ }
396
+ function skipNewlines(cx) {
397
+ while (peek(cx) && peek(cx).type === "NEWLINE" /* NEWLINE */) next(cx);
398
+ }
399
+ function parseTokens(tokens, options) {
400
+ const cx = { tokens, i: 0 };
401
+ const ast = {
402
+ raw: tokens.map((t) => t.value).join(" "),
403
+ // Core collections
404
+ functions: [],
405
+ ias: [],
406
+ nodes: [],
407
+ modules: [],
408
+ objects: [],
409
+ forms: [],
410
+ animations: [],
411
+ logic: [],
412
+ parallels: [],
413
+ build: null,
414
+ evolve: null,
415
+ targets: [],
416
+ version: null,
417
+ logs: [],
418
+ // Extended collections from full alphabet
419
+ substates: [],
420
+ classes: [],
421
+ loops: [],
422
+ events: [],
423
+ syncs: [],
424
+ timeouts: [],
425
+ delays: [],
426
+ securities: [],
427
+ keys: [],
428
+ sandboxes: [],
429
+ causes: [],
430
+ consequences: [],
431
+ memories: [],
432
+ persists: [],
433
+ roots: [],
434
+ interfaces: [],
435
+ visuals: [],
436
+ inputs: [],
437
+ throttles: [],
438
+ boosts: [],
439
+ costs: [],
440
+ locks: [],
441
+ unlocks: [],
442
+ voids: [],
443
+ inherits: [],
444
+ contains: [],
445
+ verifies: [],
446
+ logNodes: [],
447
+ database: null,
448
+ kernel: null,
449
+ training: null
450
+ };
451
+ const onLog = (e) => {
452
+ const entry = { timestamp: (/* @__PURE__ */ new Date()).toISOString(), ...e };
453
+ ast.logs.push(entry);
454
+ if (options?.onLog) options.onLog(entry);
455
+ };
456
+ skipNewlines(cx);
457
+ while (peek(cx) && peek(cx).type !== "EOF" /* EOF */) {
458
+ const t = peek(cx);
459
+ if (t.type === "SYMBOL" /* SYMBOL */) {
460
+ const sym = t.value;
461
+ next(cx);
462
+ skipNewlines(cx);
463
+ while (peek(cx) && peek(cx).type === "NEWLINE" /* NEWLINE */) next(cx);
464
+ const ident = peek(cx);
465
+ if (!ident) break;
466
+ const kw = ident.type === "IDENTIFIER" /* IDENTIFIER */ ? ident.value.toUpperCase() : void 0;
467
+ if (sym === "\u03A3" && kw === "SYSTEM") {
468
+ next(cx);
469
+ skipNewlines(cx);
470
+ const nameTok = eat(cx, "STRING" /* STRING */);
471
+ ast.system = nameTok.value;
472
+ onLog({ level: "info", event: "parse.system", detail: { name: ast.system } });
473
+ skipNewlines(cx);
474
+ continue;
475
+ }
476
+ if (sym === "\u03A3" && kw === "TARGET") {
477
+ next(cx);
478
+ skipNewlines(cx);
479
+ const targets = [];
480
+ while (peek(cx) && peek(cx).type !== "NEWLINE" /* NEWLINE */ && peek(cx).type !== "EOF" /* EOF */) {
481
+ const v = peek(cx);
482
+ if (v.type === "IDENTIFIER" /* IDENTIFIER */ || v.type === "STRING" /* STRING */) {
483
+ targets.push(v.value);
484
+ next(cx);
485
+ } else if (v.type === "COMMA" /* COMMA */) {
486
+ next(cx);
487
+ } else {
488
+ break;
489
+ }
490
+ }
491
+ ast.targets = [...ast.targets || [], ...targets];
492
+ onLog({ level: "info", event: "parse.target", detail: { targets } });
493
+ skipNewlines(cx);
494
+ continue;
495
+ }
496
+ if (sym === "\u03A3" && kw === "MODE") {
497
+ next(cx);
498
+ skipNewlines(cx);
499
+ const v = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
500
+ ast.mode = v.value;
501
+ onLog({ level: "info", event: "parse.mode", detail: { mode: ast.mode } });
502
+ skipNewlines(cx);
503
+ continue;
504
+ }
505
+ if (sym === "\u03A3" && kw === "UI") {
506
+ next(cx);
507
+ skipNewlines(cx);
508
+ const v = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
509
+ ast.ui = v.value;
510
+ onLog({ level: "info", event: "parse.ui", detail: { ui: ast.ui } });
511
+ skipNewlines(cx);
512
+ continue;
513
+ }
514
+ if (sym === "\u03A3" && kw === "MODULE") {
515
+ next(cx);
516
+ skipNewlines(cx);
517
+ const nameTok = eat(cx, "STRING" /* STRING */);
518
+ const module = { type: "module", name: nameTok.value };
519
+ ast.modules.push(module);
520
+ ast.nodes.push(module);
521
+ onLog({ level: "info", event: "parse.module", detail: module });
522
+ skipNewlines(cx);
523
+ continue;
524
+ }
525
+ if (sym === "\u03A3" && kw === "BUILD") {
526
+ next(cx);
527
+ skipNewlines(cx);
528
+ eat(cx, "LBRACE" /* LBRACE */);
529
+ const targets = [];
530
+ skipNewlines(cx);
531
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
532
+ const key = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
533
+ eat(cx, "COLON" /* COLON */);
534
+ const val = peek(cx);
535
+ if (val.type === "IDENTIFIER" /* IDENTIFIER */ || val.type === "STRING" /* STRING */) {
536
+ targets.push(val.value);
537
+ next(cx);
538
+ } else {
539
+ throw new Error("Expected identifier or string for build target");
540
+ }
541
+ skipNewlines(cx);
542
+ if (peek(cx) && peek(cx).type === "COMMA" /* COMMA */) next(cx);
543
+ skipNewlines(cx);
544
+ }
545
+ eat(cx, "RBRACE" /* RBRACE */);
546
+ const b = { type: "build", targets };
547
+ ast.build = b;
548
+ ast.nodes.push(b);
549
+ onLog({ level: "info", event: "parse.build", detail: b });
550
+ skipNewlines(cx);
551
+ continue;
552
+ }
553
+ if (sym === "\u03A3" && kw === "EVOLVE") {
554
+ next(cx);
555
+ skipNewlines(cx);
556
+ const v = peek(cx);
557
+ if (v && (v.type === "IDENTIFIER" /* IDENTIFIER */ || v.type === "STRING" /* STRING */)) {
558
+ const ev = { type: "evolve", steps: [next(cx).value] };
559
+ ast.evolve = ev;
560
+ onLog({ level: "info", event: "parse.evolve", detail: ev });
561
+ }
562
+ skipNewlines(cx);
563
+ continue;
564
+ }
565
+ if (sym === "\u03A3" && kw === "DATABASE") {
566
+ next(cx);
567
+ skipNewlines(cx);
568
+ const v = peek(cx);
569
+ if (v && v.type === "IDENTIFIER" /* IDENTIFIER */) {
570
+ const mode = next(cx).value;
571
+ const db = { type: "database", mode };
572
+ ast.database = db;
573
+ ast.nodes.push(db);
574
+ onLog({ level: "info", event: "parse.database", detail: db });
575
+ }
576
+ skipNewlines(cx);
577
+ continue;
578
+ }
579
+ if (sym === "\u03A3" && kw === "KERNEL") {
580
+ next(cx);
581
+ skipNewlines(cx);
582
+ const hooks = [];
583
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
584
+ next(cx);
585
+ skipNewlines(cx);
586
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
587
+ const p = peek(cx);
588
+ if (p.type === "IDENTIFIER" /* IDENTIFIER */) {
589
+ const hookName = next(cx).value;
590
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
591
+ hooks.push(hookName + " " + next(cx).value);
592
+ } else {
593
+ hooks.push(hookName);
594
+ }
595
+ } else {
596
+ next(cx);
597
+ }
598
+ skipNewlines(cx);
599
+ }
600
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
601
+ }
602
+ const kernel = { type: "kernel", hooks };
603
+ ast.kernel = kernel;
604
+ ast.nodes.push(kernel);
605
+ onLog({ level: "info", event: "parse.kernel", detail: kernel });
606
+ skipNewlines(cx);
607
+ continue;
608
+ }
609
+ if (sym === "\u03A3" && kw === "TRAINING") {
610
+ next(cx);
611
+ skipNewlines(cx);
612
+ const v = peek(cx);
613
+ if (v && (v.type === "IDENTIFIER" /* IDENTIFIER */ || v.type === "STRING" /* STRING */)) {
614
+ const training = { type: "training", topic: next(cx).value };
615
+ ast.training = training;
616
+ ast.nodes.push(training);
617
+ onLog({ level: "info", event: "parse.training", detail: training });
618
+ }
619
+ skipNewlines(cx);
620
+ continue;
621
+ }
622
+ if (sym === "\u03A3" && kw === "LEVEL") {
623
+ next(cx);
624
+ skipNewlines(cx);
625
+ const v = peek(cx);
626
+ if (v && v.type === "NUMBER" /* NUMBER */) {
627
+ if (ast.training) {
628
+ ast.training.level = parseInt(next(cx).value, 10);
629
+ } else {
630
+ next(cx);
631
+ }
632
+ }
633
+ skipNewlines(cx);
634
+ continue;
635
+ }
636
+ if (sym === "\u0394") {
637
+ let nameTok = null;
638
+ if (kw === "FUNCTION") {
639
+ next(cx);
640
+ skipNewlines(cx);
641
+ const tname = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
642
+ nameTok = tname;
643
+ } else {
644
+ if (ident.type === "IDENTIFIER" /* IDENTIFIER */) {
645
+ nameTok = next(cx);
646
+ }
647
+ }
648
+ if (!nameTok) {
649
+ onLog({ level: "warn", event: "parse.function.missing.name" });
650
+ skipNewlines(cx);
651
+ continue;
652
+ }
653
+ skipNewlines(cx);
654
+ let args = [];
655
+ if (peek(cx) && peek(cx).type === "LPAREN" /* LPAREN */) {
656
+ next(cx);
657
+ skipNewlines(cx);
658
+ while (peek(cx) && peek(cx).type !== "RPAREN" /* RPAREN */) {
659
+ const arg = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
660
+ args.push(arg.value);
661
+ if (peek(cx) && peek(cx).type === "COMMA" /* COMMA */) next(cx);
662
+ skipNewlines(cx);
663
+ }
664
+ eat(cx, "RPAREN" /* RPAREN */);
665
+ }
666
+ const steps = [];
667
+ skipNewlines(cx);
668
+ while (peek(cx) && peek(cx).type === "ARROW" /* ARROW */) {
669
+ next(cx);
670
+ const bodyTok = peek(cx);
671
+ if (bodyTok && (bodyTok.type === "IDENTIFIER" /* IDENTIFIER */ || bodyTok.type === "STRING" /* STRING */)) {
672
+ steps.push({ raw: bodyTok.value });
673
+ next(cx);
674
+ }
675
+ skipNewlines(cx);
676
+ }
677
+ onLog({ level: "info", event: "parse.function.start", detail: { name: nameTok.value, args } });
678
+ const fn = { type: "function", name: nameTok.value, args, steps };
679
+ ast.functions.push(fn);
680
+ ast.nodes.push(fn);
681
+ onLog({ level: "info", event: "parse.function.end", detail: fn });
682
+ continue;
683
+ }
684
+ if (sym === "\u03A8") {
685
+ if (kw === "IA") {
686
+ next(cx);
687
+ skipNewlines(cx);
688
+ const nameTok = peek(cx);
689
+ let name = "ia";
690
+ if (nameTok && (nameTok.type === "STRING" /* STRING */ || nameTok.type === "IDENTIFIER" /* IDENTIFIER */)) {
691
+ name = next(cx).value;
692
+ }
693
+ const body = [];
694
+ skipNewlines(cx);
695
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
696
+ next(cx);
697
+ skipNewlines(cx);
698
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
699
+ const cap = peek(cx);
700
+ if (cap.type === "IDENTIFIER" /* IDENTIFIER */ || cap.type === "STRING" /* STRING */) {
701
+ body.push(cap.value);
702
+ next(cx);
703
+ } else {
704
+ next(cx);
705
+ }
706
+ skipNewlines(cx);
707
+ }
708
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
709
+ } else {
710
+ while (peek(cx) && peek(cx).type !== "SYMBOL" /* SYMBOL */ && peek(cx).type !== "EOF" /* EOF */ && peek(cx).type !== "NEWLINE" /* NEWLINE */) {
711
+ const cap = peek(cx);
712
+ if (cap.type === "IDENTIFIER" /* IDENTIFIER */ || cap.type === "STRING" /* STRING */) {
713
+ body.push(cap.value);
714
+ next(cx);
715
+ } else break;
716
+ skipNewlines(cx);
717
+ }
718
+ }
719
+ const ia = { type: "ia", name, body };
720
+ ast.ias.push(ia);
721
+ ast.nodes.push(ia);
722
+ onLog({ level: "info", event: "parse.ia", detail: ia });
723
+ continue;
724
+ }
725
+ skipNewlines(cx);
726
+ continue;
727
+ }
728
+ if (sym === "\u03A9") {
729
+ skipNewlines(cx);
730
+ const nameTok = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
731
+ const name = nameTok.value;
732
+ const props = {};
733
+ skipNewlines(cx);
734
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
735
+ next(cx);
736
+ skipNewlines(cx);
737
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
738
+ const p = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
739
+ skipNewlines(cx);
740
+ if (peek(cx) && peek(cx).type === "COLON" /* COLON */) {
741
+ next(cx);
742
+ skipNewlines(cx);
743
+ const val = peek(cx);
744
+ if (val && (val.type === "STRING" /* STRING */ || val.type === "IDENTIFIER" /* IDENTIFIER */ || val.type === "NUMBER" /* NUMBER */)) {
745
+ props[p.value] = val.value;
746
+ next(cx);
747
+ } else {
748
+ props[p.value] = null;
749
+ }
750
+ } else {
751
+ props[p.value] = null;
752
+ }
753
+ skipNewlines(cx);
754
+ }
755
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
756
+ }
757
+ const obj = { type: "object", name, props };
758
+ ast.objects.push(obj);
759
+ ast.nodes.push(obj);
760
+ onLog({ level: "info", event: "parse.object", detail: obj });
761
+ continue;
762
+ }
763
+ if (sym === "\u03A6") {
764
+ skipNewlines(cx);
765
+ let name = "form";
766
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ && cx.tokens[cx.i + 1] && cx.tokens[cx.i + 1].type !== "LBRACE" /* LBRACE */) {
767
+ name = next(cx).value;
768
+ }
769
+ const props = {};
770
+ skipNewlines(cx);
771
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
772
+ next(cx);
773
+ skipNewlines(cx);
774
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
775
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
776
+ const key = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
777
+ eat(cx, "COLON" /* COLON */);
778
+ const val = peek(cx);
779
+ if (val && (val.type === "STRING" /* STRING */ || val.type === "IDENTIFIER" /* IDENTIFIER */ || val.type === "NUMBER" /* NUMBER */)) {
780
+ props[key.value] = val.value;
781
+ next(cx);
782
+ } else {
783
+ props[key.value] = true;
784
+ }
785
+ } else {
786
+ next(cx);
787
+ }
788
+ skipNewlines(cx);
789
+ }
790
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
791
+ }
792
+ const form = { type: "form", name, props };
793
+ ast.forms.push(form);
794
+ ast.nodes.push(form);
795
+ onLog({ level: "info", event: "parse.form", detail: form });
796
+ continue;
797
+ }
798
+ if (sym === "\u0398") {
799
+ skipNewlines(cx);
800
+ const maybeAnim = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
801
+ if (maybeAnim.value.toLowerCase() !== "animation") {
802
+ onLog({ level: "warn", event: "parse.animation.missing.keyword" });
803
+ continue;
804
+ }
805
+ skipNewlines(cx);
806
+ const nameTok = peek(cx);
807
+ let name = "animation";
808
+ if (nameTok && (nameTok.type === "STRING" /* STRING */ || nameTok.type === "IDENTIFIER" /* IDENTIFIER */)) {
809
+ name = next(cx).value;
810
+ }
811
+ const props = {};
812
+ skipNewlines(cx);
813
+ while (peek(cx) && peek(cx).type !== "SYMBOL" /* SYMBOL */ && peek(cx).type !== "EOF" /* EOF */) {
814
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
815
+ const key = next(cx).value;
816
+ if (peek(cx) && peek(cx).type === "COLON" /* COLON */) {
817
+ next(cx);
818
+ const val = peek(cx);
819
+ if (val && (val.type === "STRING" /* STRING */ || val.type === "NUMBER" /* NUMBER */ || val.type === "IDENTIFIER" /* IDENTIFIER */)) {
820
+ props[key] = val.value;
821
+ next(cx);
822
+ }
823
+ } else {
824
+ props[key] = true;
825
+ }
826
+ } else {
827
+ break;
828
+ }
829
+ skipNewlines(cx);
830
+ }
831
+ const anim = { type: "animation", name, props };
832
+ ast.animations.push(anim);
833
+ ast.nodes.push(anim);
834
+ onLog({ level: "info", event: "parse.animation", detail: anim });
835
+ continue;
836
+ }
837
+ if (sym === "\u039B") {
838
+ skipNewlines(cx);
839
+ const ifTok = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
840
+ if (ifTok.value.toLowerCase() !== "if") {
841
+ onLog({ level: "warn", event: "parse.logic.missing.if" });
842
+ continue;
843
+ }
844
+ const leftTok = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
845
+ let leftName = leftTok.value;
846
+ while (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ && peek(cx).value === ".") {
847
+ next(cx);
848
+ const p = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
849
+ leftName += "." + p.value;
850
+ }
851
+ const comp = eat(cx, "COMPARATOR" /* COMPARATOR */);
852
+ const right = peek(cx);
853
+ if (!right || !(right.type === "STRING" /* STRING */ || right.type === "NUMBER" /* NUMBER */ || right.type === "IDENTIFIER" /* IDENTIFIER */)) throw new Error("Invalid logic right-hand side");
854
+ next(cx);
855
+ const rule = `${leftName} ${comp.value} ${right.value}`;
856
+ const trueFlow = [];
857
+ const falseFlow = [];
858
+ skipNewlines(cx);
859
+ while (peek(cx) && peek(cx).type === "ARROW" /* ARROW */) {
860
+ next(cx);
861
+ const step = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
862
+ trueFlow.push(step.value);
863
+ skipNewlines(cx);
864
+ }
865
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ && peek(cx).value.toLowerCase() === "else") {
866
+ next(cx);
867
+ skipNewlines(cx);
868
+ while (peek(cx) && peek(cx).type === "ARROW" /* ARROW */) {
869
+ next(cx);
870
+ const step = eat(cx, "IDENTIFIER" /* IDENTIFIER */);
871
+ falseFlow.push(step.value);
872
+ skipNewlines(cx);
873
+ }
874
+ }
875
+ const logic = { type: "logic", rule, trueFlow, falseFlow };
876
+ ast.logic.push(logic);
877
+ ast.nodes.push(logic);
878
+ onLog({ level: "info", event: "parse.logic", detail: logic });
879
+ continue;
880
+ }
881
+ if (sym === "\u2297") {
882
+ skipNewlines(cx);
883
+ const items = [];
884
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
885
+ next(cx);
886
+ skipNewlines(cx);
887
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
888
+ const parts = [];
889
+ while (peek(cx) && peek(cx).type !== "NEWLINE" /* NEWLINE */ && peek(cx).type !== "RBRACE" /* RBRACE */) {
890
+ const p = peek(cx);
891
+ if (p.type === "IDENTIFIER" /* IDENTIFIER */ || p.type === "STRING" /* STRING */ || p.type === "NUMBER" /* NUMBER */) {
892
+ parts.push(next(cx).value);
893
+ } else {
894
+ next(cx);
895
+ }
896
+ }
897
+ if (parts.length > 0) items.push(parts.join(" "));
898
+ if (peek(cx) && peek(cx).type === "NEWLINE" /* NEWLINE */) next(cx);
899
+ skipNewlines(cx);
900
+ }
901
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
902
+ }
903
+ const par = { type: "parallel", items };
904
+ ast.parallels.push(par);
905
+ ast.nodes.push(par);
906
+ onLog({ level: "info", event: "parse.parallel", detail: par });
907
+ continue;
908
+ }
909
+ if (sym === "\u2301" && kw === "EVOLVE") {
910
+ next(cx);
911
+ skipNewlines(cx);
912
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
913
+ next(cx);
914
+ skipNewlines(cx);
915
+ const steps = [];
916
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
917
+ const p = peek(cx);
918
+ if (p.type === "IDENTIFIER" /* IDENTIFIER */ || p.type === "STRING" /* STRING */) {
919
+ steps.push(p.value);
920
+ next(cx);
921
+ } else {
922
+ next(cx);
923
+ }
924
+ skipNewlines(cx);
925
+ }
926
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
927
+ const ev = { type: "evolve", steps };
928
+ ast.evolve = ev;
929
+ ast.nodes.push(ev);
930
+ onLog({ level: "info", event: "parse.evolve", detail: ev });
931
+ continue;
932
+ }
933
+ }
934
+ if (sym === "\u03C3") {
935
+ skipNewlines(cx);
936
+ let name = "substate";
937
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
938
+ name = next(cx).value;
939
+ }
940
+ const props = {};
941
+ skipNewlines(cx);
942
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
943
+ next(cx);
944
+ skipNewlines(cx);
945
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
946
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
947
+ const key = next(cx).value;
948
+ if (peek(cx) && peek(cx).type === "COLON" /* COLON */) {
949
+ next(cx);
950
+ const val = peek(cx);
951
+ if (val && (val.type === "STRING" /* STRING */ || val.type === "IDENTIFIER" /* IDENTIFIER */ || val.type === "NUMBER" /* NUMBER */)) {
952
+ props[key] = next(cx).value;
953
+ }
954
+ } else {
955
+ props[key] = true;
956
+ }
957
+ } else {
958
+ next(cx);
959
+ }
960
+ skipNewlines(cx);
961
+ }
962
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
963
+ }
964
+ const substate = { type: "substate", name, props };
965
+ ast.substates.push(substate);
966
+ ast.nodes.push(substate);
967
+ onLog({ level: "info", event: "parse.substate", detail: substate });
968
+ continue;
969
+ }
970
+ if (sym === "\u25CB") {
971
+ skipNewlines(cx);
972
+ let name = "class";
973
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
974
+ name = next(cx).value;
975
+ }
976
+ let extendsClass;
977
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ && peek(cx).value.toLowerCase() === "extends") {
978
+ next(cx);
979
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
980
+ extendsClass = next(cx).value;
981
+ }
982
+ }
983
+ const props = {};
984
+ const methods = [];
985
+ skipNewlines(cx);
986
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
987
+ next(cx);
988
+ skipNewlines(cx);
989
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
990
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
991
+ const key = next(cx).value;
992
+ if (peek(cx) && peek(cx).type === "LPAREN" /* LPAREN */) {
993
+ methods.push(key);
994
+ next(cx);
995
+ while (peek(cx) && peek(cx).type !== "RPAREN" /* RPAREN */) next(cx);
996
+ if (peek(cx)) next(cx);
997
+ } else if (peek(cx) && peek(cx).type === "COLON" /* COLON */) {
998
+ next(cx);
999
+ const val = peek(cx);
1000
+ if (val && (val.type === "STRING" /* STRING */ || val.type === "IDENTIFIER" /* IDENTIFIER */ || val.type === "NUMBER" /* NUMBER */)) {
1001
+ props[key] = next(cx).value;
1002
+ }
1003
+ } else {
1004
+ props[key] = true;
1005
+ }
1006
+ } else {
1007
+ next(cx);
1008
+ }
1009
+ skipNewlines(cx);
1010
+ }
1011
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1012
+ }
1013
+ const classNode = { type: "class", name, extends: extendsClass, props, methods };
1014
+ ast.classes.push(classNode);
1015
+ ast.nodes.push(classNode);
1016
+ onLog({ level: "info", event: "parse.class", detail: classNode });
1017
+ continue;
1018
+ }
1019
+ if (sym === "\u21BB") {
1020
+ skipNewlines(cx);
1021
+ let count;
1022
+ let condition;
1023
+ const body = [];
1024
+ if (peek(cx) && peek(cx).type === "NUMBER" /* NUMBER */) {
1025
+ count = parseInt(next(cx).value, 10);
1026
+ } else if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1027
+ condition = next(cx).value;
1028
+ }
1029
+ skipNewlines(cx);
1030
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1031
+ next(cx);
1032
+ skipNewlines(cx);
1033
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1034
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1035
+ body.push(next(cx).value);
1036
+ } else {
1037
+ next(cx);
1038
+ }
1039
+ skipNewlines(cx);
1040
+ }
1041
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1042
+ }
1043
+ const loop = { type: "loop", count, condition, body };
1044
+ ast.loops.push(loop);
1045
+ ast.nodes.push(loop);
1046
+ onLog({ level: "info", event: "parse.loop", detail: loop });
1047
+ continue;
1048
+ }
1049
+ if (sym === "\u25CC") {
1050
+ skipNewlines(cx);
1051
+ let name = "event";
1052
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1053
+ name = next(cx).value;
1054
+ }
1055
+ const handler = [];
1056
+ skipNewlines(cx);
1057
+ while (peek(cx) && peek(cx).type === "ARROW" /* ARROW */) {
1058
+ next(cx);
1059
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1060
+ handler.push(next(cx).value);
1061
+ }
1062
+ skipNewlines(cx);
1063
+ }
1064
+ const event = { type: "event", name, handler };
1065
+ ast.events.push(event);
1066
+ ast.nodes.push(event);
1067
+ onLog({ level: "info", event: "parse.event", detail: event });
1068
+ continue;
1069
+ }
1070
+ if (sym === "\u21C4") {
1071
+ skipNewlines(cx);
1072
+ const sources = [];
1073
+ while (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1074
+ sources.push(next(cx).value);
1075
+ if (peek(cx) && peek(cx).type === "COMMA" /* COMMA */) next(cx);
1076
+ skipNewlines(cx);
1077
+ }
1078
+ const sync = { type: "sync", sources };
1079
+ ast.syncs.push(sync);
1080
+ ast.nodes.push(sync);
1081
+ onLog({ level: "info", event: "parse.sync", detail: sync });
1082
+ continue;
1083
+ }
1084
+ if (sym === "\u231B") {
1085
+ skipNewlines(cx);
1086
+ let duration = 0;
1087
+ let unit = "ms";
1088
+ if (peek(cx) && peek(cx).type === "NUMBER" /* NUMBER */) {
1089
+ duration = parseInt(next(cx).value, 10);
1090
+ }
1091
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1092
+ const u = peek(cx).value.toLowerCase();
1093
+ if (u === "ms" || u === "s" || u === "m") {
1094
+ unit = u;
1095
+ next(cx);
1096
+ }
1097
+ }
1098
+ const timeout = { type: "timeout", duration, unit };
1099
+ ast.timeouts.push(timeout);
1100
+ ast.nodes.push(timeout);
1101
+ onLog({ level: "info", event: "parse.timeout", detail: timeout });
1102
+ continue;
1103
+ }
1104
+ if (sym === "\u23F3") {
1105
+ skipNewlines(cx);
1106
+ let duration = 0;
1107
+ let unit = "ms";
1108
+ if (peek(cx) && peek(cx).type === "NUMBER" /* NUMBER */) {
1109
+ duration = parseInt(next(cx).value, 10);
1110
+ }
1111
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1112
+ const u = peek(cx).value.toLowerCase();
1113
+ if (u === "ms" || u === "s" || u === "m") {
1114
+ unit = u;
1115
+ next(cx);
1116
+ }
1117
+ }
1118
+ const delay = { type: "delay", duration, unit };
1119
+ ast.delays.push(delay);
1120
+ ast.nodes.push(delay);
1121
+ onLog({ level: "info", event: "parse.delay", detail: delay });
1122
+ continue;
1123
+ }
1124
+ if (sym === "\u{1F6E1}") {
1125
+ skipNewlines(cx);
1126
+ let level;
1127
+ const rules = [];
1128
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1129
+ const lv = peek(cx).value.toLowerCase();
1130
+ if (["low", "medium", "high", "critical"].includes(lv)) {
1131
+ level = lv;
1132
+ next(cx);
1133
+ }
1134
+ }
1135
+ skipNewlines(cx);
1136
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1137
+ next(cx);
1138
+ skipNewlines(cx);
1139
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1140
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1141
+ rules.push(next(cx).value);
1142
+ } else {
1143
+ next(cx);
1144
+ }
1145
+ skipNewlines(cx);
1146
+ }
1147
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1148
+ }
1149
+ const security = { type: "security", level, rules };
1150
+ ast.securities.push(security);
1151
+ ast.nodes.push(security);
1152
+ onLog({ level: "info", event: "parse.security", detail: security });
1153
+ continue;
1154
+ }
1155
+ if (sym === "\u{1F511}") {
1156
+ skipNewlines(cx);
1157
+ let name = "key";
1158
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1159
+ name = next(cx).value;
1160
+ }
1161
+ const permissions = [];
1162
+ skipNewlines(cx);
1163
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1164
+ next(cx);
1165
+ skipNewlines(cx);
1166
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1167
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1168
+ permissions.push(next(cx).value);
1169
+ } else {
1170
+ next(cx);
1171
+ }
1172
+ skipNewlines(cx);
1173
+ }
1174
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1175
+ }
1176
+ const key = { type: "key", name, permissions };
1177
+ ast.keys.push(key);
1178
+ ast.nodes.push(key);
1179
+ onLog({ level: "info", event: "parse.key", detail: key });
1180
+ continue;
1181
+ }
1182
+ if (sym === "\u{1F9EA}") {
1183
+ skipNewlines(cx);
1184
+ let name = "sandbox";
1185
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1186
+ name = next(cx).value;
1187
+ }
1188
+ const body = [];
1189
+ skipNewlines(cx);
1190
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1191
+ next(cx);
1192
+ skipNewlines(cx);
1193
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1194
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1195
+ body.push(next(cx).value);
1196
+ } else {
1197
+ next(cx);
1198
+ }
1199
+ skipNewlines(cx);
1200
+ }
1201
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1202
+ }
1203
+ const sandbox = { type: "sandbox", name, body };
1204
+ ast.sandboxes.push(sandbox);
1205
+ ast.nodes.push(sandbox);
1206
+ onLog({ level: "info", event: "parse.sandbox", detail: sandbox });
1207
+ continue;
1208
+ }
1209
+ if (sym === "\u26A1") {
1210
+ skipNewlines(cx);
1211
+ let trigger = "";
1212
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1213
+ trigger = next(cx).value;
1214
+ }
1215
+ const effects = [];
1216
+ skipNewlines(cx);
1217
+ while (peek(cx) && peek(cx).type === "ARROW" /* ARROW */) {
1218
+ next(cx);
1219
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1220
+ effects.push(next(cx).value);
1221
+ }
1222
+ skipNewlines(cx);
1223
+ }
1224
+ const cause = { type: "cause", trigger, effects };
1225
+ ast.causes.push(cause);
1226
+ ast.nodes.push(cause);
1227
+ onLog({ level: "info", event: "parse.cause", detail: cause });
1228
+ continue;
1229
+ }
1230
+ if (sym === "\u2726") {
1231
+ skipNewlines(cx);
1232
+ let source = "";
1233
+ let result = "";
1234
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1235
+ source = next(cx).value;
1236
+ }
1237
+ skipNewlines(cx);
1238
+ if (peek(cx) && peek(cx).type === "ARROW" /* ARROW */) {
1239
+ next(cx);
1240
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1241
+ result = next(cx).value;
1242
+ }
1243
+ }
1244
+ const consequence = { type: "consequence", source, result };
1245
+ ast.consequences.push(consequence);
1246
+ ast.nodes.push(consequence);
1247
+ onLog({ level: "info", event: "parse.consequence", detail: consequence });
1248
+ continue;
1249
+ }
1250
+ if (sym === "\u{1D4DC}") {
1251
+ skipNewlines(cx);
1252
+ let name = "memory";
1253
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1254
+ name = next(cx).value;
1255
+ }
1256
+ let size;
1257
+ let persist = false;
1258
+ skipNewlines(cx);
1259
+ if (peek(cx) && peek(cx).type === "NUMBER" /* NUMBER */) {
1260
+ size = parseInt(next(cx).value, 10);
1261
+ }
1262
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ && peek(cx).value.toLowerCase() === "persist") {
1263
+ persist = true;
1264
+ next(cx);
1265
+ }
1266
+ const memory = { type: "memory", name, size, persist };
1267
+ ast.memories.push(memory);
1268
+ ast.nodes.push(memory);
1269
+ onLog({ level: "info", event: "parse.memory", detail: memory });
1270
+ continue;
1271
+ }
1272
+ if (sym === "\u267E") {
1273
+ skipNewlines(cx);
1274
+ let target = "";
1275
+ let storage;
1276
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1277
+ target = next(cx).value;
1278
+ }
1279
+ skipNewlines(cx);
1280
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1281
+ const s = peek(cx).value.toLowerCase();
1282
+ if (["local", "session", "db", "file"].includes(s)) {
1283
+ storage = s;
1284
+ next(cx);
1285
+ }
1286
+ }
1287
+ const persist = { type: "persist", target, storage };
1288
+ ast.persists.push(persist);
1289
+ ast.nodes.push(persist);
1290
+ onLog({ level: "info", event: "parse.persist", detail: persist });
1291
+ continue;
1292
+ }
1293
+ if (sym === "\u2302") {
1294
+ skipNewlines(cx);
1295
+ let name = "root";
1296
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1297
+ name = next(cx).value;
1298
+ }
1299
+ const children = [];
1300
+ skipNewlines(cx);
1301
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1302
+ next(cx);
1303
+ skipNewlines(cx);
1304
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1305
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1306
+ children.push(next(cx).value);
1307
+ } else {
1308
+ next(cx);
1309
+ }
1310
+ skipNewlines(cx);
1311
+ }
1312
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1313
+ }
1314
+ const root = { type: "root", name, children };
1315
+ ast.roots.push(root);
1316
+ ast.nodes.push(root);
1317
+ onLog({ level: "info", event: "parse.root", detail: root });
1318
+ continue;
1319
+ }
1320
+ if (sym === "\u2610") {
1321
+ skipNewlines(cx);
1322
+ let name = "interface";
1323
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1324
+ name = next(cx).value;
1325
+ }
1326
+ const methods = [];
1327
+ skipNewlines(cx);
1328
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1329
+ next(cx);
1330
+ skipNewlines(cx);
1331
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1332
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1333
+ methods.push(next(cx).value);
1334
+ } else {
1335
+ next(cx);
1336
+ }
1337
+ skipNewlines(cx);
1338
+ }
1339
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1340
+ }
1341
+ const iface = { type: "interface", name, methods };
1342
+ ast.interfaces.push(iface);
1343
+ ast.nodes.push(iface);
1344
+ onLog({ level: "info", event: "parse.interface", detail: iface });
1345
+ continue;
1346
+ }
1347
+ if (sym === "\u{1F441}") {
1348
+ skipNewlines(cx);
1349
+ let name = "visual";
1350
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1351
+ name = next(cx).value;
1352
+ }
1353
+ const props = {};
1354
+ skipNewlines(cx);
1355
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1356
+ next(cx);
1357
+ skipNewlines(cx);
1358
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1359
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1360
+ const key = next(cx).value;
1361
+ if (peek(cx) && peek(cx).type === "COLON" /* COLON */) {
1362
+ next(cx);
1363
+ const val = peek(cx);
1364
+ if (val && (val.type === "STRING" /* STRING */ || val.type === "IDENTIFIER" /* IDENTIFIER */ || val.type === "NUMBER" /* NUMBER */)) {
1365
+ props[key] = next(cx).value;
1366
+ }
1367
+ } else {
1368
+ props[key] = true;
1369
+ }
1370
+ } else {
1371
+ next(cx);
1372
+ }
1373
+ skipNewlines(cx);
1374
+ }
1375
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1376
+ }
1377
+ const visual = { type: "visual", name, props };
1378
+ ast.visuals.push(visual);
1379
+ ast.nodes.push(visual);
1380
+ onLog({ level: "info", event: "parse.visual", detail: visual });
1381
+ continue;
1382
+ }
1383
+ if (sym === "\u2328") {
1384
+ skipNewlines(cx);
1385
+ let name = "input";
1386
+ let inputType = "text";
1387
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1388
+ name = next(cx).value;
1389
+ }
1390
+ skipNewlines(cx);
1391
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1392
+ const t2 = peek(cx).value.toLowerCase();
1393
+ if (["text", "number", "file", "event"].includes(t2)) {
1394
+ inputType = t2;
1395
+ next(cx);
1396
+ }
1397
+ }
1398
+ const input = { type: "input", name, inputType };
1399
+ ast.inputs.push(input);
1400
+ ast.nodes.push(input);
1401
+ onLog({ level: "info", event: "parse.input", detail: input });
1402
+ continue;
1403
+ }
1404
+ if (sym === "\u23EC") {
1405
+ skipNewlines(cx);
1406
+ let target = "";
1407
+ let limit = 0;
1408
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1409
+ target = next(cx).value;
1410
+ }
1411
+ skipNewlines(cx);
1412
+ if (peek(cx) && peek(cx).type === "NUMBER" /* NUMBER */) {
1413
+ limit = parseInt(next(cx).value, 10);
1414
+ }
1415
+ const throttle = { type: "throttle", target, limit };
1416
+ ast.throttles.push(throttle);
1417
+ ast.nodes.push(throttle);
1418
+ onLog({ level: "info", event: "parse.throttle", detail: throttle });
1419
+ continue;
1420
+ }
1421
+ if (sym === "\u23EB") {
1422
+ skipNewlines(cx);
1423
+ let target = "";
1424
+ let factor;
1425
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1426
+ target = next(cx).value;
1427
+ }
1428
+ skipNewlines(cx);
1429
+ if (peek(cx) && peek(cx).type === "NUMBER" /* NUMBER */) {
1430
+ factor = parseFloat(next(cx).value);
1431
+ }
1432
+ const boost = { type: "boost", target, factor };
1433
+ ast.boosts.push(boost);
1434
+ ast.nodes.push(boost);
1435
+ onLog({ level: "info", event: "parse.boost", detail: boost });
1436
+ continue;
1437
+ }
1438
+ if (sym === "\u20B5") {
1439
+ skipNewlines(cx);
1440
+ let operation = "";
1441
+ let value = 0;
1442
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1443
+ operation = next(cx).value;
1444
+ }
1445
+ skipNewlines(cx);
1446
+ if (peek(cx) && peek(cx).type === "NUMBER" /* NUMBER */) {
1447
+ value = parseFloat(next(cx).value);
1448
+ }
1449
+ const cost = { type: "cost", operation, value };
1450
+ ast.costs.push(cost);
1451
+ ast.nodes.push(cost);
1452
+ onLog({ level: "info", event: "parse.cost", detail: cost });
1453
+ continue;
1454
+ }
1455
+ if (sym === "\u{1F512}") {
1456
+ skipNewlines(cx);
1457
+ let target = "";
1458
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1459
+ target = next(cx).value;
1460
+ }
1461
+ const lock = { type: "lock", target };
1462
+ ast.locks.push(lock);
1463
+ ast.nodes.push(lock);
1464
+ onLog({ level: "info", event: "parse.lock", detail: lock });
1465
+ continue;
1466
+ }
1467
+ if (sym === "\u{1F513}") {
1468
+ skipNewlines(cx);
1469
+ let target = "";
1470
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1471
+ target = next(cx).value;
1472
+ }
1473
+ const unlock = { type: "unlock", target };
1474
+ ast.unlocks.push(unlock);
1475
+ ast.nodes.push(unlock);
1476
+ onLog({ level: "info", event: "parse.unlock", detail: unlock });
1477
+ continue;
1478
+ }
1479
+ if (sym === "\xD8") {
1480
+ skipNewlines(cx);
1481
+ let target;
1482
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1483
+ target = next(cx).value;
1484
+ }
1485
+ const voidNode = { type: "void", target };
1486
+ ast.voids.push(voidNode);
1487
+ ast.nodes.push(voidNode);
1488
+ onLog({ level: "info", event: "parse.void", detail: voidNode });
1489
+ continue;
1490
+ }
1491
+ if (sym === "\u2B12") {
1492
+ skipNewlines(cx);
1493
+ let child = "";
1494
+ let parent = "";
1495
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1496
+ child = next(cx).value;
1497
+ }
1498
+ skipNewlines(cx);
1499
+ if (peek(cx) && peek(cx).type === "ARROW" /* ARROW */) {
1500
+ next(cx);
1501
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1502
+ parent = next(cx).value;
1503
+ }
1504
+ }
1505
+ const inherit = { type: "inherit", child, parent };
1506
+ ast.inherits.push(inherit);
1507
+ ast.nodes.push(inherit);
1508
+ onLog({ level: "info", event: "parse.inherit", detail: inherit });
1509
+ continue;
1510
+ }
1511
+ if (sym === "\u2283") {
1512
+ skipNewlines(cx);
1513
+ let container = "";
1514
+ const items = [];
1515
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1516
+ container = next(cx).value;
1517
+ }
1518
+ skipNewlines(cx);
1519
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1520
+ next(cx);
1521
+ skipNewlines(cx);
1522
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1523
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1524
+ items.push(next(cx).value);
1525
+ } else {
1526
+ next(cx);
1527
+ }
1528
+ skipNewlines(cx);
1529
+ }
1530
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1531
+ }
1532
+ const contains = { type: "contains", container, items };
1533
+ ast.contains.push(contains);
1534
+ ast.nodes.push(contains);
1535
+ onLog({ level: "info", event: "parse.contains", detail: contains });
1536
+ continue;
1537
+ }
1538
+ if (sym === "\u2714") {
1539
+ skipNewlines(cx);
1540
+ let target = "";
1541
+ const rules = [];
1542
+ if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1543
+ target = next(cx).value;
1544
+ }
1545
+ skipNewlines(cx);
1546
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1547
+ next(cx);
1548
+ skipNewlines(cx);
1549
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1550
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1551
+ rules.push(next(cx).value);
1552
+ } else {
1553
+ next(cx);
1554
+ }
1555
+ skipNewlines(cx);
1556
+ }
1557
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1558
+ }
1559
+ const verify = { type: "verify", target, rules };
1560
+ ast.verifies.push(verify);
1561
+ ast.nodes.push(verify);
1562
+ onLog({ level: "info", event: "parse.verify", detail: verify });
1563
+ continue;
1564
+ }
1565
+ if (sym === "\u{1F4DC}") {
1566
+ skipNewlines(cx);
1567
+ let message = "";
1568
+ let level = "info";
1569
+ if (peek(cx) && (peek(cx).type === "STRING" /* STRING */ || peek(cx).type === "IDENTIFIER" /* IDENTIFIER */)) {
1570
+ message = next(cx).value;
1571
+ }
1572
+ skipNewlines(cx);
1573
+ if (peek(cx) && peek(cx).type === "IDENTIFIER" /* IDENTIFIER */) {
1574
+ const lv = peek(cx).value.toLowerCase();
1575
+ if (["debug", "info", "warn", "error"].includes(lv)) {
1576
+ level = lv;
1577
+ next(cx);
1578
+ }
1579
+ }
1580
+ const logNode = { type: "log", message, level };
1581
+ ast.logNodes.push(logNode);
1582
+ ast.nodes.push(logNode);
1583
+ onLog({ level: "info", event: "parse.log", detail: logNode });
1584
+ continue;
1585
+ }
1586
+ if (sym === "\u27F3") {
1587
+ skipNewlines(cx);
1588
+ const steps = [];
1589
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1590
+ next(cx);
1591
+ skipNewlines(cx);
1592
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1593
+ if (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */) {
1594
+ steps.push(next(cx).value);
1595
+ } else {
1596
+ next(cx);
1597
+ }
1598
+ skipNewlines(cx);
1599
+ }
1600
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1601
+ } else if (peek(cx) && (peek(cx).type === "IDENTIFIER" /* IDENTIFIER */ || peek(cx).type === "STRING" /* STRING */)) {
1602
+ steps.push(next(cx).value);
1603
+ }
1604
+ const ev = { type: "evolve", steps };
1605
+ ast.evolve = ev;
1606
+ ast.nodes.push(ev);
1607
+ onLog({ level: "info", event: "parse.evolve", detail: ev });
1608
+ continue;
1609
+ }
1610
+ if (sym === "\u25A0") {
1611
+ skipNewlines(cx);
1612
+ const nameTok = peek(cx);
1613
+ if (!nameTok || nameTok.type !== "IDENTIFIER" /* IDENTIFIER */) {
1614
+ skipNewlines(cx);
1615
+ continue;
1616
+ }
1617
+ const name = next(cx).value;
1618
+ const props = {};
1619
+ skipNewlines(cx);
1620
+ if (peek(cx) && peek(cx).type === "LBRACE" /* LBRACE */) {
1621
+ next(cx);
1622
+ skipNewlines(cx);
1623
+ while (peek(cx) && peek(cx).type !== "RBRACE" /* RBRACE */) {
1624
+ const p = peek(cx);
1625
+ if (p && p.type === "IDENTIFIER" /* IDENTIFIER */) {
1626
+ const key = next(cx).value;
1627
+ if (peek(cx) && peek(cx).type === "COLON" /* COLON */) {
1628
+ next(cx);
1629
+ const val = peek(cx);
1630
+ if (val && (val.type === "STRING" /* STRING */ || val.type === "IDENTIFIER" /* IDENTIFIER */ || val.type === "NUMBER" /* NUMBER */)) {
1631
+ props[key] = next(cx).value;
1632
+ } else {
1633
+ props[key] = null;
1634
+ }
1635
+ } else {
1636
+ props[key] = null;
1637
+ }
1638
+ } else {
1639
+ next(cx);
1640
+ }
1641
+ skipNewlines(cx);
1642
+ }
1643
+ if (peek(cx) && peek(cx).type === "RBRACE" /* RBRACE */) next(cx);
1644
+ }
1645
+ const obj = { type: "object", name, props };
1646
+ ast.objects.push(obj);
1647
+ ast.nodes.push(obj);
1648
+ onLog({ level: "info", event: "parse.object", detail: obj });
1649
+ continue;
1650
+ }
1651
+ skipNewlines(cx);
1652
+ continue;
1653
+ }
1654
+ if (t.type === "SYMBOL" /* SYMBOL */ && t.value === "\u0394") {
1655
+ next(cx);
1656
+ }
1657
+ if (t.type === "IDENTIFIER" /* IDENTIFIER */ && t.value.toLowerCase() === "\u0394") {
1658
+ next(cx);
1659
+ continue;
1660
+ }
1661
+ if (t.type === "IDENTIFIER" /* IDENTIFIER */) {
1662
+ const val = t.value.toLowerCase();
1663
+ next(cx);
1664
+ skipNewlines(cx);
1665
+ continue;
1666
+ }
1667
+ next(cx);
1668
+ skipNewlines(cx);
1669
+ }
1670
+ return ast;
1671
+ }
1672
+ function parseFromCode(code, options) {
1673
+ const tokens = tokenize(code);
1674
+ return parseTokens(tokens, options);
1675
+ }
1676
+ var init_ebnfParser = __esm({
1677
+ "src/parser/ebnfParser.ts"() {
1678
+ "use strict";
1679
+ init_tokenize();
1680
+ }
1681
+ });
1682
+
1683
+ // src/parser.ts
1684
+ var parser_exports = {};
1685
+ __export(parser_exports, {
1686
+ parse: () => parse,
1687
+ writeWithRetry: () => writeWithRetry
1688
+ });
1689
+ async function writeWithRetry(entry, filePath, attempts = 3, delay = 1e3, onLog) {
1690
+ const fsPromises = await import("fs/promises");
1691
+ let attempt = 0;
1692
+ const tryWrite = async () => {
1693
+ attempt++;
1694
+ try {
1695
+ await fsPromises.appendFile(filePath, JSON.stringify(entry) + "\n", { encoding: "utf8" });
1696
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "logfile.write.success", detail: { file: filePath } });
1697
+ return;
1698
+ } catch (err) {
1699
+ const errEntry = { timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "warn", event: "logfile.write.retry", detail: { attempt, error: String(err) } };
1700
+ if (onLog) onLog(errEntry);
1701
+ if (attempt < attempts) {
1702
+ setTimeout(() => {
1703
+ tryWrite().catch(() => {
1704
+ });
1705
+ }, delay);
1706
+ } else {
1707
+ const fatal = { timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "error", event: "logfile.write.fatal", detail: { attempt, error: String(err) } };
1708
+ if (onLog) onLog(fatal);
1709
+ }
1710
+ }
1711
+ };
1712
+ tryWrite().catch(() => {
1713
+ });
1714
+ }
1715
+ function parse(code, options) {
1716
+ const ast = parseFromCode(code, options);
1717
+ ast.logs = ast.logs || [];
1718
+ return ast;
1719
+ }
1720
+ var init_parser = __esm({
1721
+ "src/parser.ts"() {
1722
+ "use strict";
1723
+ init_ebnfParser();
1724
+ }
1725
+ });
1726
+
1727
+ // src/transpile.ts
1728
+ var transpile_exports = {};
1729
+ __export(transpile_exports, {
1730
+ hasWASMTarget: () => hasWASMTarget,
1731
+ transpileToDTS: () => transpileToDTS,
1732
+ transpileToJS: () => transpileToJS,
1733
+ transpileToWAT: () => transpileToWAT
1734
+ });
1735
+ function transpileToJS(ast, options) {
1736
+ const parts = [];
1737
+ const format = options?.moduleFormat || "esm";
1738
+ const isESM = format === "esm";
1739
+ parts.push("// Generated by enyosx-ai (transpiler)");
1740
+ if (ast.system) {
1741
+ parts.push(isESM ? `export const SYSTEM = ${JSON.stringify(ast.system)};` : `const SYSTEM = ${JSON.stringify(ast.system)};
1742
+ module.exports.SYSTEM = SYSTEM;`);
1743
+ }
1744
+ if (ast.mode) {
1745
+ parts.push(isESM ? `export const MODE = ${JSON.stringify(ast.mode)};` : `const MODE = ${JSON.stringify(ast.mode)};
1746
+ module.exports.MODE = MODE;`);
1747
+ }
1748
+ if (ast.ui) {
1749
+ parts.push(isESM ? `export const UI = ${JSON.stringify(ast.ui)};` : `const UI = ${JSON.stringify(ast.ui)};
1750
+ module.exports.UI = UI;`);
1751
+ }
1752
+ const functionNames = [];
1753
+ if (ast.functions && ast.functions.length) {
1754
+ for (const fn of ast.functions) {
1755
+ const args = (fn.args || []).join(", ");
1756
+ const bodyLines = (fn.steps || []).map((s) => ` // ${s.raw}`);
1757
+ functionNames.push(fn.name);
1758
+ if (isESM) {
1759
+ const fnCode = `export function ${fn.name}(${args}) {
1760
+ ${bodyLines.join("\n")}
1761
+ }`;
1762
+ parts.push(fnCode);
1763
+ } else {
1764
+ const fnCode = `function ${fn.name}(${args}) {
1765
+ ${bodyLines.join("\n")}
1766
+ }
1767
+ module.exports.${fn.name} = ${fn.name};`;
1768
+ parts.push(fnCode);
1769
+ }
1770
+ }
1771
+ }
1772
+ const iaNames = [];
1773
+ if (ast.ias && ast.ias.length) {
1774
+ for (const ia of ast.ias) {
1775
+ const body = ia.body.map((b) => ` // ${b}`).join("\n");
1776
+ const name = ia.name.replace(/[^a-zA-Z0-9_]/g, "_");
1777
+ const constName = `IA_${name}`;
1778
+ iaNames.push(constName);
1779
+ if (isESM) {
1780
+ const code = `export const ${constName} = { name: ${JSON.stringify(ia.name)}, run: () => {
1781
+ ${body}
1782
+ } };`;
1783
+ parts.push(code);
1784
+ } else {
1785
+ const code = `const ${constName} = { name: ${JSON.stringify(ia.name)}, run: () => {
1786
+ ${body}
1787
+ } };
1788
+ module.exports.${constName} = ${constName};`;
1789
+ parts.push(code);
1790
+ }
1791
+ }
1792
+ }
1793
+ const objectNames = [];
1794
+ if (ast.objects && ast.objects.length) {
1795
+ for (const o of ast.objects) {
1796
+ objectNames.push(o.name);
1797
+ parts.push(isESM ? `export const ${o.name} = ${JSON.stringify(o.props, null, 2)};` : `const ${o.name} = ${JSON.stringify(o.props, null, 2)};
1798
+ module.exports.${o.name} = ${o.name};`);
1799
+ }
1800
+ }
1801
+ const formNames = [];
1802
+ if (ast.forms && ast.forms.length) {
1803
+ for (const f of ast.forms) {
1804
+ const constName = `FORM_${f.name}`;
1805
+ formNames.push(constName);
1806
+ parts.push(isESM ? `export const ${constName} = ${JSON.stringify(f.props, null, 2)};` : `const ${constName} = ${JSON.stringify(f.props, null, 2)};
1807
+ module.exports.${constName} = ${constName};`);
1808
+ }
1809
+ }
1810
+ const animNames = [];
1811
+ if (ast.animations && ast.animations.length) {
1812
+ for (const a of ast.animations) {
1813
+ const constName = `ANIMATION_${a.name.replace(/[^a-zA-Z0-9_]/g, "_")}`;
1814
+ animNames.push(constName);
1815
+ parts.push(isESM ? `export const ${constName} = ${JSON.stringify(a.props, null, 2)};` : `const ${constName} = ${JSON.stringify(a.props, null, 2)};
1816
+ module.exports.${constName} = ${constName};`);
1817
+ }
1818
+ }
1819
+ if (ast.build) {
1820
+ parts.push(isESM ? `export const BUILD_TARGETS = ${JSON.stringify(ast.build.targets || [])};` : `const BUILD_TARGETS = ${JSON.stringify(ast.build.targets || [])};
1821
+ module.exports.BUILD_TARGETS = BUILD_TARGETS;`);
1822
+ }
1823
+ if (ast.evolve) {
1824
+ parts.push(isESM ? `export const EVOLVE_STEPS = ${JSON.stringify(ast.evolve.steps || [])};` : `const EVOLVE_STEPS = ${JSON.stringify(ast.evolve.steps || [])};
1825
+ module.exports.EVOLVE_STEPS = EVOLVE_STEPS;`);
1826
+ }
1827
+ if (ast.database) {
1828
+ parts.push(isESM ? `export const DATABASE = ${JSON.stringify(ast.database)};` : `const DATABASE = ${JSON.stringify(ast.database)};
1829
+ module.exports.DATABASE = DATABASE;`);
1830
+ }
1831
+ if (ast.kernel) {
1832
+ parts.push(isESM ? `export const KERNEL = ${JSON.stringify(ast.kernel)};` : `const KERNEL = ${JSON.stringify(ast.kernel)};
1833
+ module.exports.KERNEL = KERNEL;`);
1834
+ }
1835
+ if (ast.training) {
1836
+ parts.push(isESM ? `export const TRAINING = ${JSON.stringify(ast.training)};` : `const TRAINING = ${JSON.stringify(ast.training)};
1837
+ module.exports.TRAINING = TRAINING;`);
1838
+ }
1839
+ if (ast.substates && ast.substates.length) {
1840
+ parts.push(isESM ? `export const SUBSTATES = ${JSON.stringify(ast.substates, null, 2)};` : `const SUBSTATES = ${JSON.stringify(ast.substates, null, 2)};
1841
+ module.exports.SUBSTATES = SUBSTATES;`);
1842
+ }
1843
+ if (ast.classes && ast.classes.length) {
1844
+ for (const c of ast.classes) {
1845
+ const className = c.name.replace(/[^a-zA-Z0-9_]/g, "_");
1846
+ parts.push(isESM ? `export class ${className} {
1847
+ constructor() {
1848
+ Object.assign(this, ${JSON.stringify(c.props)});
1849
+ }
1850
+ ${(c.methods || []).map((m) => ` ${m}() {}`).join("\n")}
1851
+ }` : `class ${className} {
1852
+ constructor() {
1853
+ Object.assign(this, ${JSON.stringify(c.props)});
1854
+ }
1855
+ ${(c.methods || []).map((m) => ` ${m}() {}`).join("\n")}
1856
+ }
1857
+ module.exports.${className} = ${className};`);
1858
+ }
1859
+ }
1860
+ if (ast.loops && ast.loops.length) {
1861
+ parts.push(isESM ? `export const LOOPS = ${JSON.stringify(ast.loops, null, 2)};` : `const LOOPS = ${JSON.stringify(ast.loops, null, 2)};
1862
+ module.exports.LOOPS = LOOPS;`);
1863
+ }
1864
+ if (ast.events && ast.events.length) {
1865
+ for (const e of ast.events) {
1866
+ const eventName = `EVENT_${e.name.replace(/[^a-zA-Z0-9_]/g, "_")}`;
1867
+ parts.push(isESM ? `export const ${eventName} = { name: ${JSON.stringify(e.name)}, handler: ${JSON.stringify(e.handler || [])} };` : `const ${eventName} = { name: ${JSON.stringify(e.name)}, handler: ${JSON.stringify(e.handler || [])} };
1868
+ module.exports.${eventName} = ${eventName};`);
1869
+ }
1870
+ }
1871
+ if (ast.syncs && ast.syncs.length) {
1872
+ parts.push(isESM ? `export const SYNCS = ${JSON.stringify(ast.syncs, null, 2)};` : `const SYNCS = ${JSON.stringify(ast.syncs, null, 2)};
1873
+ module.exports.SYNCS = SYNCS;`);
1874
+ }
1875
+ if (ast.timeouts && ast.timeouts.length) {
1876
+ parts.push(isESM ? `export const TIMEOUTS = ${JSON.stringify(ast.timeouts, null, 2)};` : `const TIMEOUTS = ${JSON.stringify(ast.timeouts, null, 2)};
1877
+ module.exports.TIMEOUTS = TIMEOUTS;`);
1878
+ }
1879
+ if (ast.delays && ast.delays.length) {
1880
+ parts.push(isESM ? `export const DELAYS = ${JSON.stringify(ast.delays, null, 2)};` : `const DELAYS = ${JSON.stringify(ast.delays, null, 2)};
1881
+ module.exports.DELAYS = DELAYS;`);
1882
+ }
1883
+ if (ast.securities && ast.securities.length) {
1884
+ parts.push(isESM ? `export const SECURITY_RULES = ${JSON.stringify(ast.securities, null, 2)};` : `const SECURITY_RULES = ${JSON.stringify(ast.securities, null, 2)};
1885
+ module.exports.SECURITY_RULES = SECURITY_RULES;`);
1886
+ }
1887
+ if (ast.keys && ast.keys.length) {
1888
+ parts.push(isESM ? `export const KEYS = ${JSON.stringify(ast.keys, null, 2)};` : `const KEYS = ${JSON.stringify(ast.keys, null, 2)};
1889
+ module.exports.KEYS = KEYS;`);
1890
+ }
1891
+ if (ast.sandboxes && ast.sandboxes.length) {
1892
+ parts.push(isESM ? `export const SANDBOXES = ${JSON.stringify(ast.sandboxes, null, 2)};` : `const SANDBOXES = ${JSON.stringify(ast.sandboxes, null, 2)};
1893
+ module.exports.SANDBOXES = SANDBOXES;`);
1894
+ }
1895
+ if (ast.causes && ast.causes.length) {
1896
+ parts.push(isESM ? `export const CAUSES = ${JSON.stringify(ast.causes, null, 2)};` : `const CAUSES = ${JSON.stringify(ast.causes, null, 2)};
1897
+ module.exports.CAUSES = CAUSES;`);
1898
+ }
1899
+ if (ast.consequences && ast.consequences.length) {
1900
+ parts.push(isESM ? `export const CONSEQUENCES = ${JSON.stringify(ast.consequences, null, 2)};` : `const CONSEQUENCES = ${JSON.stringify(ast.consequences, null, 2)};
1901
+ module.exports.CONSEQUENCES = CONSEQUENCES;`);
1902
+ }
1903
+ if (ast.memories && ast.memories.length) {
1904
+ parts.push(isESM ? `export const MEMORIES = ${JSON.stringify(ast.memories, null, 2)};` : `const MEMORIES = ${JSON.stringify(ast.memories, null, 2)};
1905
+ module.exports.MEMORIES = MEMORIES;`);
1906
+ }
1907
+ if (ast.persists && ast.persists.length) {
1908
+ parts.push(isESM ? `export const PERSISTS = ${JSON.stringify(ast.persists, null, 2)};` : `const PERSISTS = ${JSON.stringify(ast.persists, null, 2)};
1909
+ module.exports.PERSISTS = PERSISTS;`);
1910
+ }
1911
+ if (ast.interfaces && ast.interfaces.length) {
1912
+ parts.push(isESM ? `export const INTERFACES = ${JSON.stringify(ast.interfaces, null, 2)};` : `const INTERFACES = ${JSON.stringify(ast.interfaces, null, 2)};
1913
+ module.exports.INTERFACES = INTERFACES;`);
1914
+ }
1915
+ if (ast.visuals && ast.visuals.length) {
1916
+ parts.push(isESM ? `export const VISUALS = ${JSON.stringify(ast.visuals, null, 2)};` : `const VISUALS = ${JSON.stringify(ast.visuals, null, 2)};
1917
+ module.exports.VISUALS = VISUALS;`);
1918
+ }
1919
+ if (ast.inputs && ast.inputs.length) {
1920
+ parts.push(isESM ? `export const INPUTS = ${JSON.stringify(ast.inputs, null, 2)};` : `const INPUTS = ${JSON.stringify(ast.inputs, null, 2)};
1921
+ module.exports.INPUTS = INPUTS;`);
1922
+ }
1923
+ if (ast.throttles && ast.throttles.length) {
1924
+ parts.push(isESM ? `export const THROTTLES = ${JSON.stringify(ast.throttles, null, 2)};` : `const THROTTLES = ${JSON.stringify(ast.throttles, null, 2)};
1925
+ module.exports.THROTTLES = THROTTLES;`);
1926
+ }
1927
+ if (ast.boosts && ast.boosts.length) {
1928
+ parts.push(isESM ? `export const BOOSTS = ${JSON.stringify(ast.boosts, null, 2)};` : `const BOOSTS = ${JSON.stringify(ast.boosts, null, 2)};
1929
+ module.exports.BOOSTS = BOOSTS;`);
1930
+ }
1931
+ if (ast.costs && ast.costs.length) {
1932
+ parts.push(isESM ? `export const COSTS = ${JSON.stringify(ast.costs, null, 2)};` : `const COSTS = ${JSON.stringify(ast.costs, null, 2)};
1933
+ module.exports.COSTS = COSTS;`);
1934
+ }
1935
+ if (ast.locks && ast.locks.length) {
1936
+ parts.push(isESM ? `export const LOCKS = ${JSON.stringify(ast.locks, null, 2)};` : `const LOCKS = ${JSON.stringify(ast.locks, null, 2)};
1937
+ module.exports.LOCKS = LOCKS;`);
1938
+ }
1939
+ if (ast.unlocks && ast.unlocks.length) {
1940
+ parts.push(isESM ? `export const UNLOCKS = ${JSON.stringify(ast.unlocks, null, 2)};` : `const UNLOCKS = ${JSON.stringify(ast.unlocks, null, 2)};
1941
+ module.exports.UNLOCKS = UNLOCKS;`);
1942
+ }
1943
+ if (ast.voids && ast.voids.length) {
1944
+ parts.push(isESM ? `export const VOIDS = ${JSON.stringify(ast.voids, null, 2)};` : `const VOIDS = ${JSON.stringify(ast.voids, null, 2)};
1945
+ module.exports.VOIDS = VOIDS;`);
1946
+ }
1947
+ if (ast.inherits && ast.inherits.length) {
1948
+ parts.push(isESM ? `export const INHERITS = ${JSON.stringify(ast.inherits, null, 2)};` : `const INHERITS = ${JSON.stringify(ast.inherits, null, 2)};
1949
+ module.exports.INHERITS = INHERITS;`);
1950
+ }
1951
+ if (ast.contains && ast.contains.length) {
1952
+ parts.push(isESM ? `export const CONTAINS = ${JSON.stringify(ast.contains, null, 2)};` : `const CONTAINS = ${JSON.stringify(ast.contains, null, 2)};
1953
+ module.exports.CONTAINS = CONTAINS;`);
1954
+ }
1955
+ if (ast.verifies && ast.verifies.length) {
1956
+ parts.push(isESM ? `export const VERIFIES = ${JSON.stringify(ast.verifies, null, 2)};` : `const VERIFIES = ${JSON.stringify(ast.verifies, null, 2)};
1957
+ module.exports.VERIFIES = VERIFIES;`);
1958
+ }
1959
+ if (ast.logNodes && ast.logNodes.length) {
1960
+ parts.push(isESM ? `export const LOG_NODES = ${JSON.stringify(ast.logNodes, null, 2)};` : `const LOG_NODES = ${JSON.stringify(ast.logNodes, null, 2)};
1961
+ module.exports.LOG_NODES = LOG_NODES;`);
1962
+ }
1963
+ if (isESM) {
1964
+ parts.push("\nexport default { SYSTEM, MODE, UI };");
1965
+ } else {
1966
+ parts.push("\nmodule.exports.default = { SYSTEM, MODE, UI };");
1967
+ }
1968
+ return parts.join("\n\n");
1969
+ }
1970
+ function transpileToDTS(ast) {
1971
+ const lines = [];
1972
+ lines.push("// Type declarations generated by enyosx-ai");
1973
+ lines.push("");
1974
+ if (ast.system) {
1975
+ lines.push(`export declare const SYSTEM: string;`);
1976
+ }
1977
+ if (ast.mode) {
1978
+ lines.push(`export declare const MODE: string;`);
1979
+ }
1980
+ if (ast.ui) {
1981
+ lines.push(`export declare const UI: string;`);
1982
+ }
1983
+ if (ast.functions && ast.functions.length) {
1984
+ for (const fn of ast.functions) {
1985
+ const args = (fn.args || []).map((a) => `${a}: any`).join(", ");
1986
+ lines.push(`export declare function ${fn.name}(${args}): void;`);
1987
+ }
1988
+ }
1989
+ if (ast.ias && ast.ias.length) {
1990
+ for (const ia of ast.ias) {
1991
+ const name = ia.name.replace(/[^a-zA-Z0-9_]/g, "_");
1992
+ lines.push(`export declare const IA_${name}: { name: string; run: () => void };`);
1993
+ }
1994
+ }
1995
+ if (ast.objects && ast.objects.length) {
1996
+ for (const o of ast.objects) {
1997
+ const propTypes = Object.keys(o.props).map((k) => `${k}: string | null`).join("; ");
1998
+ lines.push(`export declare const ${o.name}: { ${propTypes} };`);
1999
+ }
2000
+ }
2001
+ if (ast.forms && ast.forms.length) {
2002
+ for (const f of ast.forms) {
2003
+ lines.push(`export declare const FORM_${f.name}: Record<string, any>;`);
2004
+ }
2005
+ }
2006
+ if (ast.animations && ast.animations.length) {
2007
+ for (const a of ast.animations) {
2008
+ const constName = `ANIMATION_${a.name.replace(/[^a-zA-Z0-9_]/g, "_")}`;
2009
+ lines.push(`export declare const ${constName}: Record<string, any>;`);
2010
+ }
2011
+ }
2012
+ if (ast.build) {
2013
+ lines.push(`export declare const BUILD_TARGETS: string[];`);
2014
+ }
2015
+ if (ast.evolve) {
2016
+ lines.push(`export declare const EVOLVE_STEPS: string[];`);
2017
+ }
2018
+ lines.push("");
2019
+ lines.push("declare const _default: { SYSTEM: string; MODE: string; UI: string };");
2020
+ lines.push("export default _default;");
2021
+ return lines.join("\n");
2022
+ }
2023
+ function transpileToWAT(ast) {
2024
+ const lines = [];
2025
+ lines.push(";; WebAssembly Text Format generated by enyosx-ai");
2026
+ lines.push(";; This is a stub for future WASM compilation");
2027
+ lines.push("");
2028
+ lines.push("(module");
2029
+ lines.push(` ;; System: ${ast.system || "unnamed"}`);
2030
+ lines.push(` ;; Mode: ${ast.mode || "default"}`);
2031
+ lines.push("");
2032
+ lines.push(" ;; Memory declaration");
2033
+ lines.push(' (memory (export "memory") 1)');
2034
+ lines.push("");
2035
+ if (ast.system) {
2036
+ const bytes = Array.from(new TextEncoder().encode(ast.system)).join(" ");
2037
+ lines.push(` ;; System name data`);
2038
+ lines.push(` (data (i32.const 0) "${ast.system}")`);
2039
+ lines.push("");
2040
+ }
2041
+ if (ast.functions && ast.functions.length) {
2042
+ lines.push(" ;; Function stubs");
2043
+ for (const fn of ast.functions) {
2044
+ const params = (fn.args || []).map((_, i) => `(param $arg${i} i32)`).join(" ");
2045
+ lines.push(` (func (export "${fn.name}") ${params} (result i32)`);
2046
+ lines.push(` ;; Steps: ${(fn.steps || []).map((s) => s.raw).join(" -> ")}`);
2047
+ lines.push(` i32.const 0 ;; placeholder return`);
2048
+ lines.push(` )`);
2049
+ lines.push("");
2050
+ }
2051
+ }
2052
+ if (ast.ias && ast.ias.length) {
2053
+ lines.push(" ;; IA stubs (table references for future indirect calls)");
2054
+ for (const ia of ast.ias) {
2055
+ const name = ia.name.replace(/[^a-zA-Z0-9_]/g, "_");
2056
+ lines.push(` ;; IA: ${ia.name}`);
2057
+ lines.push(` (func (export "ia_${name}") (result i32)`);
2058
+ lines.push(` ;; Capabilities: ${ia.body.join(", ")}`);
2059
+ lines.push(` i32.const 1 ;; stub: IA ready`);
2060
+ lines.push(` )`);
2061
+ lines.push("");
2062
+ }
2063
+ }
2064
+ lines.push(" ;; Main entry point");
2065
+ lines.push(' (func (export "_start")');
2066
+ lines.push(" ;; Initialize system");
2067
+ lines.push(" nop");
2068
+ lines.push(" )");
2069
+ lines.push("");
2070
+ if (ast.build && ast.build.targets) {
2071
+ lines.push(` ;; Build targets: ${ast.build.targets.join(", ")}`);
2072
+ }
2073
+ lines.push(")");
2074
+ lines.push("");
2075
+ lines.push(";; To compile this WAT to WASM:");
2076
+ lines.push(";; wat2wasm output.wat -o output.wasm");
2077
+ lines.push(";; Or use wasm-pack for Rust-based compilation");
2078
+ return lines.join("\n");
2079
+ }
2080
+ function hasWASMTarget(ast) {
2081
+ if (!ast.build || !ast.build.targets) return false;
2082
+ return ast.build.targets.some(
2083
+ (t) => t.toLowerCase() === "wasm" || t.toLowerCase() === "webassembly" || t.toLowerCase() === "wat"
2084
+ );
2085
+ }
2086
+ var init_transpile = __esm({
2087
+ "src/transpile.ts"() {
2088
+ "use strict";
2089
+ }
2090
+ });
2091
+
2092
+ // src/semantic/validate.ts
2093
+ function validateSemantic(ast, options) {
2094
+ const result = validateSemanticFull(ast, options);
2095
+ return result.errors;
2096
+ }
2097
+ function validateSemanticFull(ast, options) {
2098
+ const errors = [];
2099
+ const warnings = [];
2100
+ const pillars = {
2101
+ state: false,
2102
+ action: false,
2103
+ form: false,
2104
+ time: false,
2105
+ energy: false,
2106
+ memory: false,
2107
+ interface: false,
2108
+ security: false,
2109
+ evolve: false
2110
+ };
2111
+ const onLog = (e) => {
2112
+ const entry = { timestamp: (/* @__PURE__ */ new Date()).toISOString(), ...e };
2113
+ if (options?.onLog) options.onLog(entry);
2114
+ };
2115
+ ast.functions = ast.functions || [];
2116
+ ast.modules = ast.modules || [];
2117
+ ast.objects = ast.objects || [];
2118
+ ast.events = ast.events || [];
2119
+ ast.securities = ast.securities || [];
2120
+ ast.memories = ast.memories || [];
2121
+ ast.interfaces = ast.interfaces || [];
2122
+ if (ast.system) {
2123
+ pillars.state = true;
2124
+ } else {
2125
+ warnings.push("No \u03A3 SYSTEM declared - consider adding a system name");
2126
+ }
2127
+ if (ast.substates?.length) {
2128
+ for (const sub of ast.substates) {
2129
+ if (sub.parent && !ast.substates.some((s) => s.name === sub.parent)) {
2130
+ warnings.push(`Substate '${sub.name}' references unknown parent '${sub.parent}'`);
2131
+ }
2132
+ }
2133
+ }
2134
+ if (ast.functions.length > 0) {
2135
+ pillars.action = true;
2136
+ }
2137
+ const fnMap = /* @__PURE__ */ new Map();
2138
+ for (const fn of ast.functions) {
2139
+ fnMap.set(fn.name, (fnMap.get(fn.name) || 0) + 1);
2140
+ }
2141
+ for (const [name, count] of fnMap.entries()) {
2142
+ if (count > 1) {
2143
+ const msg = `Duplicate function name: ${name}`;
2144
+ errors.push(msg);
2145
+ onLog({ level: "error", event: "parse.semantic.error", detail: { error: msg } });
2146
+ }
2147
+ }
2148
+ const knownFns = new Set(ast.functions.map((f) => f.name));
2149
+ for (const fn of ast.functions) {
2150
+ for (const step of fn.steps || []) {
2151
+ const token = step.raw.trim();
2152
+ if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(token) && !knownFns.has(token)) {
2153
+ const msg = `Undefined function reference in '${fn.name}': '${token}'`;
2154
+ errors.push(msg);
2155
+ onLog({ level: "warn", event: "parse.semantic.error", detail: { error: msg } });
2156
+ }
2157
+ }
2158
+ }
2159
+ if (ast.objects?.length || ast.classes?.length) {
2160
+ pillars.form = true;
2161
+ }
2162
+ if (ast.classes?.length) {
2163
+ const classNames = new Set(ast.classes.map((c) => c.name));
2164
+ for (const cls of ast.classes) {
2165
+ if (cls.extends && !classNames.has(cls.extends)) {
2166
+ warnings.push(`Class '${cls.name}' extends unknown class '${cls.extends}'`);
2167
+ }
2168
+ }
2169
+ }
2170
+ if (ast.inherits?.length) {
2171
+ const allNames = /* @__PURE__ */ new Set([
2172
+ ...ast.classes?.map((c) => c.name) || [],
2173
+ ...ast.objects?.map((o) => o.name) || []
2174
+ ]);
2175
+ for (const inh of ast.inherits) {
2176
+ if (!allNames.has(inh.parent)) {
2177
+ warnings.push(`Inherit: '${inh.child}' extends unknown '${inh.parent}'`);
2178
+ }
2179
+ }
2180
+ }
2181
+ if (ast.events?.length || ast.causes?.length || ast.loops?.length) {
2182
+ pillars.time = true;
2183
+ }
2184
+ if (ast.events?.length) {
2185
+ for (const evt of ast.events) {
2186
+ for (const handler of evt.handler || []) {
2187
+ if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(handler) && !knownFns.has(handler)) {
2188
+ warnings.push(`Event '${evt.name}' handler '${handler}' is not a known function`);
2189
+ }
2190
+ }
2191
+ }
2192
+ }
2193
+ if (ast.throttles?.length || ast.boosts?.length || ast.costs?.length) {
2194
+ pillars.energy = true;
2195
+ }
2196
+ if (ast.throttles?.length) {
2197
+ for (const t of ast.throttles) {
2198
+ if (t.limit <= 0) {
2199
+ errors.push(`Throttle '${t.target}' has invalid limit: ${t.limit}`);
2200
+ }
2201
+ }
2202
+ }
2203
+ if (ast.memories?.length || ast.persists?.length || ast.database) {
2204
+ pillars.memory = true;
2205
+ }
2206
+ if (ast.forms?.length || ast.inputs?.length || ast.visuals?.length || ast.interfaces?.length) {
2207
+ pillars.interface = true;
2208
+ }
2209
+ if (ast.securities?.length || ast.keys?.length || ast.sandboxes?.length) {
2210
+ pillars.security = true;
2211
+ }
2212
+ const validSecurityLevels = ["low", "medium", "high", "critical"];
2213
+ if (ast.securities?.length) {
2214
+ for (const sec of ast.securities) {
2215
+ if (sec.level && !validSecurityLevels.includes(sec.level.toLowerCase())) {
2216
+ warnings.push(`Security rule has unknown level '${sec.level}' (use: low, medium, high, critical)`);
2217
+ }
2218
+ }
2219
+ }
2220
+ if (ast.evolve?.steps?.length) {
2221
+ pillars.evolve = true;
2222
+ }
2223
+ const activePillars = Object.values(pillars).filter(Boolean).length;
2224
+ const score = Math.round(activePillars / 9 * 100);
2225
+ ast.errors = errors;
2226
+ if (errors.length > 0) console.debug("[semantic] errors", errors);
2227
+ return { errors, warnings, pillars, score };
2228
+ }
2229
+
2230
+ // src/evolve/stub.ts
2231
+ function applyEvolve(ast, options) {
2232
+ if (!ast.evolve || !ast.evolve.steps || ast.evolve.steps.length === 0) {
2233
+ return { changed: false, appliedSteps: [], skippedSteps: [] };
2234
+ }
2235
+ const onLog = options?.onLog;
2236
+ const steps = ast.evolve.steps;
2237
+ let changed = false;
2238
+ const appliedSteps = [];
2239
+ const skippedSteps = [];
2240
+ for (const step of steps) {
2241
+ const parts = step.trim().split(/\s+/);
2242
+ const command = parts[0]?.toLowerCase();
2243
+ let applied = false;
2244
+ switch (command) {
2245
+ case "rename": {
2246
+ if (parts.length >= 3) {
2247
+ const [, oldName, newName] = parts;
2248
+ for (const fn of ast.functions || []) {
2249
+ if (fn.name === oldName) {
2250
+ fn.name = newName;
2251
+ applied = true;
2252
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.rename", detail: { from: oldName, to: newName } });
2253
+ }
2254
+ }
2255
+ }
2256
+ break;
2257
+ }
2258
+ case "optimize": {
2259
+ if (parts.length >= 2) {
2260
+ const target = parts[1];
2261
+ for (const fn of ast.functions || []) {
2262
+ if (fn.name === target || target === "*") {
2263
+ fn.optimized = true;
2264
+ applied = true;
2265
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.optimize", detail: { target: fn.name } });
2266
+ }
2267
+ }
2268
+ }
2269
+ break;
2270
+ }
2271
+ case "inline": {
2272
+ if (parts.length >= 2) {
2273
+ const target = parts[1];
2274
+ for (const fn of ast.functions || []) {
2275
+ if (fn.name === target) {
2276
+ fn.inline = true;
2277
+ applied = true;
2278
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.inline", detail: { target } });
2279
+ }
2280
+ }
2281
+ }
2282
+ break;
2283
+ }
2284
+ case "memoize": {
2285
+ if (parts.length >= 2) {
2286
+ const target = parts[1];
2287
+ for (const fn of ast.functions || []) {
2288
+ if (fn.name === target) {
2289
+ fn.memoize = true;
2290
+ applied = true;
2291
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.memoize", detail: { target } });
2292
+ }
2293
+ }
2294
+ }
2295
+ break;
2296
+ }
2297
+ case "parallelize": {
2298
+ if (parts.length >= 2) {
2299
+ const target = parts[1];
2300
+ for (const fn of ast.functions || []) {
2301
+ if (fn.name === target) {
2302
+ fn.parallel = true;
2303
+ applied = true;
2304
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.parallelize", detail: { target } });
2305
+ }
2306
+ }
2307
+ }
2308
+ break;
2309
+ }
2310
+ case "deprecate": {
2311
+ if (parts.length >= 2) {
2312
+ const target = parts[1];
2313
+ for (const fn of ast.functions || []) {
2314
+ if (fn.name === target) {
2315
+ fn.deprecated = true;
2316
+ applied = true;
2317
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "warn", event: "evolve.deprecate", detail: { target } });
2318
+ }
2319
+ }
2320
+ }
2321
+ break;
2322
+ }
2323
+ case "secure": {
2324
+ if (parts.length >= 2) {
2325
+ const target = parts[1];
2326
+ for (const fn of ast.functions || []) {
2327
+ if (fn.name === target) {
2328
+ fn.secure = true;
2329
+ applied = true;
2330
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.secure", detail: { target } });
2331
+ }
2332
+ }
2333
+ }
2334
+ break;
2335
+ }
2336
+ case "cache": {
2337
+ if (parts.length >= 2) {
2338
+ const target = parts[1];
2339
+ for (const obj of ast.objects || []) {
2340
+ if (obj.name === target) {
2341
+ obj.cached = true;
2342
+ applied = true;
2343
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.cache", detail: { target } });
2344
+ }
2345
+ }
2346
+ }
2347
+ break;
2348
+ }
2349
+ case "throttle": {
2350
+ if (parts.length >= 3) {
2351
+ const target = parts[1];
2352
+ const limit = parseInt(parts[2], 10);
2353
+ for (const fn of ast.functions || []) {
2354
+ if (fn.name === target) {
2355
+ fn.throttle = limit;
2356
+ applied = true;
2357
+ if (onLog) onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.throttle", detail: { target, limit } });
2358
+ }
2359
+ }
2360
+ }
2361
+ break;
2362
+ }
2363
+ case "observe": {
2364
+ if (parts.length >= 2) {
2365
+ const target = parts[1];
2366
+ for (const fn of ast.functions || []) {
2367
+ if (fn.name === target || target === "*") {
2368
+ fn.observable = true;
2369
+ applied = true;
2370
+ }
2371
+ }
2372
+ for (const obj of ast.objects || []) {
2373
+ if (obj.name === target || target === "*") {
2374
+ obj.observable = true;
2375
+ applied = true;
2376
+ }
2377
+ }
2378
+ if (applied && onLog) {
2379
+ onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.observe", detail: { target } });
2380
+ }
2381
+ }
2382
+ break;
2383
+ }
2384
+ default:
2385
+ if (step.includes("otimizar") || step.includes("optimize")) {
2386
+ for (const fn of ast.functions || []) {
2387
+ fn.optimized = true;
2388
+ }
2389
+ applied = true;
2390
+ } else if (step.includes("adaptar") || step.includes("adapt")) {
2391
+ ast.adaptive = true;
2392
+ applied = true;
2393
+ }
2394
+ break;
2395
+ }
2396
+ if (applied) {
2397
+ changed = true;
2398
+ appliedSteps.push(step);
2399
+ } else {
2400
+ skippedSteps.push(step);
2401
+ }
2402
+ }
2403
+ if (changed && onLog) {
2404
+ onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "evolve.summary", detail: { appliedSteps, skippedSteps } });
2405
+ }
2406
+ return { changed, appliedSteps, skippedSteps };
2407
+ }
2408
+
2409
+ // src/runENY.ts
2410
+ init_parser();
2411
+ init_parser();
2412
+ function runENY(code, options) {
2413
+ const hasSystem = /Σ\s+SYSTEM/i.test(code);
2414
+ if (!hasSystem) {
2415
+ if (options?.onLog) {
2416
+ options.onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "warn", event: "runENY.no-system", detail: { length: code.length } });
2417
+ }
2418
+ if (options?.logFile) {
2419
+ const attempts = options?.logRetryAttempts ?? 3;
2420
+ const delay = options?.logRetryDelayMs ?? 1e3;
2421
+ writeWithRetry({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "warn", event: "runENY.no-system", detail: { length: code.length } }, options.logFile, attempts, delay, options?.onLog);
2422
+ }
2423
+ return { raw: code, isEny: false, logs: [] };
2424
+ }
2425
+ const userOnLog = options?.onLog;
2426
+ let wrappedOnLog = userOnLog;
2427
+ if (options?.logFile) {
2428
+ const attempts = options?.logRetryAttempts ?? 3;
2429
+ const delay = options?.logRetryDelayMs ?? 1e3;
2430
+ wrappedOnLog = (e) => {
2431
+ if (userOnLog) userOnLog(e);
2432
+ writeWithRetry(e, options.logFile, attempts, delay, userOnLog);
2433
+ };
2434
+ }
2435
+ const ast = parse(code, { ...options, onLog: wrappedOnLog });
2436
+ try {
2437
+ const errors = validateSemantic(ast, { onLog: wrappedOnLog });
2438
+ if (errors && errors.length > 0) {
2439
+ if (options?.onLog) options.onLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "warn", event: "parse.semantic.summary", detail: { count: errors.length } });
2440
+ }
2441
+ } catch (err) {
2442
+ if (wrappedOnLog) wrappedOnLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "error", event: "parse.semantic.failed", detail: { error: String(err) } });
2443
+ }
2444
+ if (ast.evolve && ast.evolve.steps && ast.evolve.steps.length > 0) {
2445
+ try {
2446
+ const evolveResult = applyEvolve(ast, { onLog: wrappedOnLog });
2447
+ if (evolveResult.changed && wrappedOnLog) {
2448
+ wrappedOnLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "info", event: "parse.evolve.summary", detail: { changed: true, stepsApplied: ast.evolve.steps.length } });
2449
+ }
2450
+ } catch (err) {
2451
+ if (wrappedOnLog) wrappedOnLog({ timestamp: (/* @__PURE__ */ new Date()).toISOString(), level: "error", event: "parse.evolve.failed", detail: { error: String(err) } });
2452
+ }
2453
+ }
2454
+ ast.isEny = true;
2455
+ return ast;
2456
+ }
2457
+
2458
+ export {
2459
+ TokenType,
2460
+ SYMBOL_NAMES,
2461
+ tokenize,
2462
+ init_tokenize,
2463
+ parse,
2464
+ parser_exports,
2465
+ init_parser,
2466
+ validateSemantic,
2467
+ validateSemanticFull,
2468
+ applyEvolve,
2469
+ runENY,
2470
+ transpileToJS,
2471
+ transpileToDTS,
2472
+ transpileToWAT,
2473
+ hasWASMTarget,
2474
+ transpile_exports,
2475
+ init_transpile
2476
+ };