-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorb_ide.jsx
More file actions
1236 lines (1163 loc) · 61.7 KB
/
Copy pathorb_ide.jsx
File metadata and controls
1236 lines (1163 loc) · 61.7 KB
1
2
3
4
5
6
7
8
9
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
52
53
54
55
56
57
58
59
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
264
265
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
343
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { useState, useRef, useCallback, useEffect, useMemo, useReducer } from "react";
// ============================================================
// EMBEDDED ORB INTERPRETER (JavaScript port)
// ============================================================
class OrbVec {
constructor(data = [], width = 0) {
this.data = data;
this.width = width || data.length;
}
copy() { return new OrbVec([...this.data], this.width); }
toString() { return `<${this.data.map(v => Number.isInteger(v) ? v : v.toFixed(2)).join(", ")}>`; }
}
function makeDefault(typeName, vecWidth) {
if (typeName === "INT") return 0;
if (typeName === "FLOAT") return 0.0;
if (typeName === "STRING") return "";
if (typeName === "VEC") return new OrbVec(new Array(vecWidth || 4).fill(0.0), vecWidth || 4);
return 0;
}
class OrbEnv {
constructor(parent = null, name = "<global>") {
this.parent = parent;
this.name = name;
this.vars = {};
this.consts = {};
this.labels = {};
}
get(name) {
if (name in this.vars) return this.vars[name];
if (name in this.consts) return this.consts[name];
if (this.parent) return this.parent.get(name);
throw new Error(`Undefined: ${name}`);
}
set(name, val) {
let env = this;
while (env) {
if (name in env.vars) { env.vars[name] = val; return; }
env = env.parent;
}
this.vars[name] = val;
}
define(name, val) { this.vars[name] = val; }
defineConst(name, val) { this.consts[name] = val; }
dump() {
const r = this.parent ? this.parent.dump() : {};
Object.assign(r, this.vars);
return r;
}
}
class OrbRegisters {
constructor() {
this.regs = new Array(32).fill(0);
this.flags = { Z: false, N: false, C: false };
this.stack = [];
}
get(name) { return this.regs[parseInt(name.slice(1))]; }
set(name, val) { this.regs[parseInt(name.slice(1))] = val; }
updateFlags(r) {
if (typeof r === "number") { this.flags.Z = r === 0; this.flags.N = r < 0; }
}
dump() {
const r = {};
this.regs.forEach((v, i) => { if (v !== 0) r[`R${i}`] = v; });
r.FLAGS = { ...this.flags };
return r;
}
}
// Minimal recursive descent parser for Orb (JS port)
function orbParse(src) {
// Simplified tokenizer
const tokens = [];
let i = 0, line = 1, col = 1;
const kws = new Set([
"MODULE","END","PORTS","IN","OUT","INOUT","DIM","CONST","AS","INT","FLOAT","STRING","VEC",
"IF","THEN","ELIF","ELSE","FOR","TO","STEP","NEXT","WHILE","WEND","GOTO","GOSUB","RETURN",
"PRINT","INPUT","HALT","INSPECT","VADD","VSUB","VMUL","VDIV","VMADD","VLOAD","VSTORE",
"VSUM","VDOT","VMIN","VMAX","INTO","FROM","ASM","SEND","RECV","READPORT","LOCK","UNLOCK",
"TIMEOUT","BOARD","PLACE","ROUTE","SHARE","BETWEEN","SET","PROBE","EXPORT","IMPORT",
"WIRE","MAIN","AT","DATAFLOW","MESSAGE","SHARED","AND","OR","NOT","OPT","EQU",
"MOV","ADD","SUB","MUL","DIV","XOR","SHL","SHR","CMP","JMP","JEQ","JNE","JGT","JLT",
"CALL","RET","PUSH","POP","NOP","HLT","LDA","STA","LDX","STX","JSR","RTS",
"BEQ","BNE","BCS","BCC","BPL","BMI","SEI","CLI","PHR","PLR"
]);
const asmOps = new Set([
"MOV","ADD","SUB","MUL","DIV","AND","OR","XOR","NOT","SHL","SHR",
"VADD","VSUB","VMUL","VDIV","VMADD","VLOAD","VSTORE",
"CMP","JMP","JEQ","JNE","JGT","JLT","CALL","RET","PUSH","POP","NOP","HLT",
"LDA","STA","LDX","STX","JSR","RTS","BEQ","BNE","BCS","BCC","BPL","BMI",
"SEI","CLI","PHR","PLR"
]);
let inAsm = false;
while (i < src.length) {
const ch = src[i];
if (ch === " " || ch === "\t" || ch === "\r") { i++; col++; continue; }
if (ch === "\n") { tokens.push({ t: "NL", v: "\\n", l: line, c: col }); i++; line++; col = 1; continue; }
if (ch === "/" && src[i + 1] === "/") {
while (i < src.length && src[i] !== "\n") i++;
continue;
}
if (ch === "R" && src[i+1] === "E" && src[i+2] === "M" && (i+3>=src.length || !/\w/.test(src[i+3]))) {
while (i < src.length && src[i] !== "\n") i++;
continue;
}
if (ch === '"') {
let s = ""; i++; col++;
while (i < src.length && src[i] !== '"') { s += src[i]; i++; col++; }
i++; col++;
tokens.push({ t: "STR", v: s, l: line, c: col });
continue;
}
if (/\d/.test(ch)) {
let n = "";
if (ch === "0" && (src[i+1] === "x" || src[i+1] === "X")) {
n = "0x"; i += 2; col += 2;
while (i < src.length && /[0-9a-fA-F]/.test(src[i])) { n += src[i]; i++; col++; }
tokens.push({ t: "HEX", v: n, l: line, c: col }); continue;
}
while (i < src.length && /\d/.test(src[i])) { n += src[i]; i++; col++; }
if (i < src.length && src[i] === "." && /\d/.test(src[i+1])) {
n += "."; i++; col++;
while (i < src.length && /\d/.test(src[i])) { n += src[i]; i++; col++; }
tokens.push({ t: "FLT", v: n, l: line, c: col });
} else {
tokens.push({ t: "NUM", v: n, l: line, c: col });
}
continue;
}
if (/[a-zA-Z_]/.test(ch)) {
let w = "";
while (i < src.length && /[\w]/.test(src[i])) { w += src[i]; i++; col++; }
const up = w.toUpperCase();
// Compound: END MODULE, END IF, etc.
if (up === "END") {
let j = i; while (j < src.length && (src[j] === " " || src[j] === "\t")) j++;
let w2 = "";
while (j < src.length && /[\w]/.test(src[j])) { w2 += src[j]; j++; }
const compound = `END ${w2.toUpperCase()}`;
if (["END MODULE","END PORTS","END SHARED","END IF","END ASM","END BOARD"].includes(compound)) {
tokens.push({ t: "KW", v: compound, l: line, c: col });
i = j; col += (j - i);
if (compound === "END ASM") inAsm = false;
continue;
}
}
if (up === "ASM") inAsm = true;
// Register check
if (/^R\d+$/.test(up) && parseInt(up.slice(1)) <= 31) {
tokens.push({ t: "REG", v: up, l: line, c: col }); continue;
}
if (kws.has(up)) {
tokens.push({ t: "KW", v: up, l: line, c: col });
} else {
tokens.push({ t: "ID", v: w, l: line, c: col });
}
continue;
}
if (ch === "@") {
i++; col++; let w = "";
while (i < src.length && /[\w]/.test(src[i])) { w += src[i]; i++; col++; }
tokens.push({ t: "LABEL", v: "@" + w, l: line, c: col }); continue;
}
if (ch === "#") {
if (inAsm || (i + 1 < src.length && (src[i+1] === "(" || /[\d\-]/.test(src[i+1])))) {
if (src[i+1] === "(") {
tokens.push({ t: "OP", v: "#", l: line, c: col }); i++; col++; continue;
}
let imm = "#"; i++; col++;
if (src[i] === "-") { imm += "-"; i++; col++; }
if (src[i] === "0" && (src[i+1] === "x" || src[i+1] === "X")) {
imm += "0x"; i += 2; col += 2;
while (i < src.length && /[0-9a-fA-F]/.test(src[i])) { imm += src[i]; i++; col++; }
} else {
while (i < src.length && /[\d.]/.test(src[i])) { imm += src[i]; i++; col++; }
}
tokens.push({ t: "IMM", v: imm, l: line, c: col }); continue;
}
}
if (ch === "." && i + 1 < src.length && /[a-zA-Z]/.test(src[i+1])) {
if (inAsm) {
let lb = "."; i++; col++;
while (i < src.length && /[\w]/.test(src[i])) { lb += src[i]; i++; col++; }
if (i < src.length && src[i] === ":") { lb += ":"; i++; col++; }
tokens.push({ t: "ASMLBL", v: lb, l: line, c: col }); continue;
}
}
// Two-char ops
if (ch === "=" && src[i+1] === "=") { tokens.push({t:"OP",v:"==",l:line,c:col}); i+=2; col+=2; continue; }
if (ch === "!" && src[i+1] === "=") { tokens.push({t:"OP",v:"!=",l:line,c:col}); i+=2; col+=2; continue; }
if (ch === "<" && src[i+1] === "=") { tokens.push({t:"OP",v:"<=",l:line,c:col}); i+=2; col+=2; continue; }
if (ch === ">" && src[i+1] === "=") { tokens.push({t:"OP",v:">=",l:line,c:col}); i+=2; col+=2; continue; }
tokens.push({ t: "OP", v: ch, l: line, c: col }); i++; col++;
}
tokens.push({ t: "EOF", v: "", l: line, c: col });
// --- Simplified AST builder ---
let pos = 0;
const cur = () => tokens[pos] || { t: "EOF", v: "" };
const adv = () => tokens[pos++] || { t: "EOF", v: "" };
const at = (...vs) => vs.some(v => cur().v === v || cur().t === v);
const expect = (v) => { if (cur().v !== v && cur().t !== v) throw new Error(`Expected ${v}, got ${cur().v} at L${cur().l}`); return adv(); };
const skipNL = () => { while (cur().t === "NL") adv(); };
const readId = () => { const t = adv(); return t.v; };
function parseExpr() { return parseOr(); }
function parseOr() { let l = parseAnd(); while (cur().v === "OR") { adv(); l = { t: "BinOp", op: "OR", l, r: parseAnd() }; } return l; }
function parseAnd() { let l = parseNot(); while (cur().v === "AND") { adv(); l = { t: "BinOp", op: "AND", l, r: parseNot() }; } return l; }
function parseNot() { if (cur().v === "NOT") { adv(); return { t: "UnOp", op: "NOT", a: parseCmp() }; } return parseCmp(); }
function parseCmp() {
let l = parseAdd();
while (["==","!=","<",">","<=",">="].includes(cur().v)) { const op = adv().v; l = { t: "BinOp", op, l, r: parseAdd() }; }
return l;
}
function parseAdd() {
let l = parseMul();
while (cur().v === "+" || cur().v === "-") { const op = adv().v; l = { t: "BinOp", op, l, r: parseMul() }; }
return l;
}
function parseMul() {
let l = parseUnary();
while (cur().v === "*" || cur().v === "/" || cur().v === "%") { const op = adv().v; l = { t: "BinOp", op, l, r: parseUnary() }; }
return l;
}
function parseUnary() {
if (cur().v === "-") { adv(); return { t: "UnOp", op: "-", a: parsePostfix() }; }
if (cur().v === "~") { adv(); return { t: "UnOp", op: "~", a: parsePostfix() }; }
return parsePostfix();
}
function parsePostfix() {
let e = parsePrimary();
while (true) {
if (cur().v === "[" && e.t === "Id") {
adv(); const idx = parseExpr(); expect("]"); e = { t: "Index", target: e, index: idx };
} else if (cur().v === ".") {
adv(); const f = readId(); e = { t: "Dot", target: e, field: f };
} else if (cur().v === "(" && e.t === "Id") {
adv(); const args = [];
if (cur().v !== ")") { args.push(parseExpr()); while (cur().v === ",") { adv(); args.push(parseExpr()); } }
expect(")"); e = { t: "Call", name: e.name, args };
} else break;
}
return e;
}
function parsePrimary() {
const tk = cur();
if (tk.t === "NUM") { adv(); return { t: "Num", v: parseInt(tk.v) }; }
if (tk.t === "FLT") { adv(); return { t: "Num", v: parseFloat(tk.v) }; }
if (tk.t === "HEX") { adv(); return { t: "Num", v: parseInt(tk.v, 16) }; }
if (tk.t === "STR") { adv(); return { t: "Str", v: tk.v }; }
if (tk.t === "ID") { adv(); return { t: "Id", name: tk.v }; }
if (tk.t === "REG") { adv(); return { t: "Id", name: tk.v }; }
if (tk.t === "ASMLBL") { adv(); return { t: "Id", name: tk.v.replace(/:$/, "") }; }
if (tk.t === "IMM") { adv(); const raw = tk.v.slice(1); return { t: "Num", v: raw.startsWith("0x") ? parseInt(raw,16) : parseFloat(raw) }; }
if (tk.v === "(") { adv(); const e = parseExpr(); expect(")"); return e; }
if (tk.v === "<") {
adv(); const els = [parseAdd()]; while (cur().v === ",") { adv(); els.push(parseAdd()); } expect(">");
return { t: "Vec", elements: els };
}
if (tk.v === "#") { adv(); expect("("); const e = parseExpr(); expect(")"); return e; }
throw new Error(`Unexpected: ${tk.v} (${tk.t}) at L${tk.l}`);
}
function parseType() {
const tn = adv().v;
let w = null;
if (cur().v === "[") { adv(); w = parseInt(adv().v); expect("]"); }
return { typeName: tn, vecWidth: w };
}
function parseStmt() {
skipNL();
const tk = cur();
if (tk.t === "EOF") return null;
if (tk.v === "DIM") {
adv(); const name = readId(); expect("AS"); const { typeName, vecWidth } = parseType();
let init = null; if (cur().v === "=") { adv(); init = parseExpr(); }
return { t: "VarDecl", name, typeName, vecWidth, init };
}
if (tk.v === "CONST") {
adv(); const name = readId(); expect("AS"); const { typeName } = parseType();
expect("="); const val = parseExpr();
return { t: "ConstDecl", name, typeName, value: val };
}
if (tk.v === "IF") {
adv(); const cond = parseExpr(); expect("THEN"); skipNL();
const thenB = []; while (!at("ELIF","ELSE","END IF")) { thenB.push(parseStmt()); skipNL(); }
const elifs = [];
while (cur().v === "ELIF") { adv(); const ec = parseExpr(); expect("THEN"); skipNL(); const eb = []; while (!at("ELIF","ELSE","END IF")) { eb.push(parseStmt()); skipNL(); } elifs.push({ cond: ec, body: eb }); }
let elseB = [];
if (cur().v === "ELSE") { adv(); skipNL(); while (cur().v !== "END IF") { elseB.push(parseStmt()); skipNL(); } }
expect("END IF");
return { t: "IfStmt", cond, thenBody: thenB, elifs, elseBody: elseB };
}
if (tk.v === "FOR") {
adv(); const vn = readId(); expect("="); const start = parseExpr(); expect("TO"); const end = parseExpr();
let step = null; if (cur().v === "STEP") { adv(); step = parseExpr(); }
skipNL(); const body = []; while (cur().v !== "NEXT") { body.push(parseStmt()); skipNL(); }
adv(); if (cur().t === "ID") adv();
return { t: "ForStmt", varName: vn, start, end, step, body };
}
if (tk.v === "WHILE") {
adv(); const cond = parseExpr(); skipNL();
const body = []; while (cur().v !== "WEND") { body.push(parseStmt()); skipNL(); }
adv();
return { t: "WhileStmt", cond, body };
}
if (tk.v === "PRINT") {
adv(); const vals = [parseExpr()]; while (cur().v === ",") { adv(); vals.push(parseExpr()); }
return { t: "PrintStmt", values: vals };
}
if (tk.v === "HALT") {
adv(); let msg = ""; if (cur().t === "STR") msg = adv().v;
return { t: "HaltStmt", message: msg };
}
if (tk.v === "INSPECT") { adv(); const tgt = readId(); return { t: "InspectStmt", target: tgt }; }
if (tk.v === "VLOAD") { adv(); const tgt = readId(); expect("FROM"); const src = parseExpr(); return { t: "VecLoad", target: tgt, source: src }; }
if (tk.v === "VSTORE") { adv(); const src = readId(); expect("TO"); const tgt = parseExpr(); return { t: "VecStore", source: src, target: tgt }; }
if (["VADD","VSUB","VMUL","VDIV","VMADD"].includes(tk.v)) {
const op = adv().v; const a = parseExpr(); expect(","); const b = parseExpr(); expect("INTO"); const tgt = readId();
return { t: "VecArith", op, a, b, target: tgt };
}
if (["VSUM","VDOT","VMIN","VMAX"].includes(tk.v)) {
const op = adv().v; const src = parseExpr(); expect("INTO"); const tgt = readId();
return { t: "VecReduce", op, source: src, target: tgt };
}
if (tk.v === "SEND") { adv(); const val = parseExpr(); expect("TO"); const tgt = readId(); let ch=null; if(cur().v==="."){adv();ch=readId();} return { t:"SendStmt", value:val, targetMod:tgt, targetCh:ch }; }
if (tk.v === "RECV") { adv(); const v=readId(); expect("FROM"); const sm=readId(); let sc=null; if(cur().v==="."){adv();sc=readId();} let to=null; if(cur().v==="TIMEOUT"){adv();to=parseExpr();} return { t:"RecvStmt", variable:v, srcMod:sm, srcCh:sc, timeout:to }; }
if (tk.v === "LOCK") { adv(); const tgt=readId(); skipNL(); const body=[]; while(cur().v!=="UNLOCK"){body.push(parseStmt());skipNL();} adv(); readId(); return { t:"LockStmt", target:tgt, body }; }
if (tk.v === "RETURN") { adv(); let val=null; if(cur().t!=="NL"&&cur().t!=="EOF") val=parseExpr(); return { t:"ReturnStmt", value:val }; }
if (tk.v === "GOTO") { adv(); return { t: "GotoStmt", target: readId() }; }
if (tk.v === "GOSUB") { adv(); return { t: "GosubStmt", target: readId() }; }
if (tk.t === "LABEL") { const name = adv().v.slice(1); if (cur().v === ":") adv(); return { t: "LabelStmt", name }; }
// ASM block
if (tk.v === "ASM") {
adv(); let name = null; if (cur().t === "ID") name = readId();
skipNL(); const body = [];
while (cur().v !== "END ASM") { body.push(parseAsmLine()); skipNL(); }
adv();
return { t: "AsmBlock", name, body };
}
// Inline asm [...]
if (tk.v === "[") {
adv(); skipNL(); const body = [];
while (cur().v !== "]") { body.push(parseAsmLine()); skipNL(); }
adv();
return { t: "AsmInline", body };
}
// Assignment or expression
if (tk.t === "ID") {
const name = tk.v;
if (tokens[pos + 1]?.v === "=" && tokens[pos + 1]?.v !== "==") {
adv(); adv(); const val = parseExpr();
return { t: "Assignment", target: name, value: val };
}
if (tokens[pos + 1]?.v === "[") {
adv(); adv(); const idx = parseExpr(); expect("]"); expect("=");
const val = parseExpr();
return { t: "Assignment", target: name, index: idx, value: val };
}
const expr = parseExpr();
return { t: "ExprStmt", expr };
}
adv(); return null;
}
function parseAsmLine() {
skipNL();
const tk = cur();
if (tk.t === "ASMLBL" && tk.v.endsWith(":")) {
adv(); return { t: "AsmLabelDef", name: tk.v.slice(1, -1) };
}
if (tk.t === "ASMLBL" && !tk.v.endsWith(":") && tokens[pos+1]?.v === "EQU") {
const name = adv().v.slice(1); adv(); const val = parseExpr();
return { t: "AsmEquate", name, value: val };
}
if (tk.v === "OPT") { adv(); const val = parseExpr(); return { t: "AsmOpt", value: val }; }
if (asmOps.has(tk.v?.toUpperCase())) {
const op = adv().v; const operands = [];
if (cur().t !== "NL" && cur().t !== "EOF" && cur().v !== "]" && cur().v !== "END ASM") {
operands.push(parseAsmOperand());
while (cur().v === ",") { adv(); operands.push(parseAsmOperand()); }
}
return { t: "AsmInstr", op, operands };
}
// Orb statement inside asm
return parseStmt();
}
function parseAsmOperand() {
const tk = cur();
if (tk.t === "REG") { adv(); return { kind: "reg", v: tk.v }; }
if (tk.t === "IMM") { adv(); const raw=tk.v.slice(1); return { kind: "imm", v: raw.startsWith("0x")?parseInt(raw,16):parseFloat(raw) }; }
if (tk.v === "#") { adv(); expect("("); const e = parseExpr(); expect(")"); return { kind: "exprimm", expr: e }; }
if (tk.v === "[") {
adv(); const reg = adv().v; let off = null;
if (cur().v === ",") { adv(); off = parseExpr(); }
expect("]");
return off ? { kind: "indexed", v: reg, offset: off } : { kind: "indirect", v: reg };
}
if (tk.t === "ASMLBL") { adv(); return { kind: "lblref", v: tk.v.replace(/:$/, "") }; }
if (tk.t === "ID") { adv(); return { kind: "var", v: tk.v }; }
const val = parseExpr();
return { kind: "expr", expr: val };
}
function parsePortDecl() {
const dir = adv().v; const name = readId(); expect("AS"); const { typeName, vecWidth } = parseType();
return { t: "PortDecl", direction: dir, name, typeName, vecWidth };
}
function parseModule() {
expect("MODULE"); const name = readId();
let commMode = "DATAFLOW";
if (at("DATAFLOW","MESSAGE","SHARED")) commMode = adv().v;
skipNL();
let ports = [];
if (cur().v === "PORTS") { adv(); skipNL(); while (cur().v !== "END PORTS") { ports.push(parsePortDecl()); skipNL(); } adv(); skipNL(); }
let sharedVars = [];
if (cur().v === "SHARED") { adv(); skipNL(); while (cur().v !== "END SHARED") { sharedVars.push(parseStmt()); skipNL(); } adv(); skipNL(); }
let body = [];
if (cur().v === "{") { adv(); skipNL(); while (cur().v !== "}") { const s = parseStmt(); if (s) body.push(s); skipNL(); } adv(); skipNL(); }
expect("END MODULE");
return { t: "ModuleDecl", name, commMode, ports, sharedVars, body };
}
function parseBoard() {
expect("BOARD"); const name = readId(); skipNL();
const body = [];
while (cur().v !== "END BOARD") {
skipNL();
const tk = cur();
if (tk.v === "PLACE") { adv(); const mt=readId(); expect("AS"); const iname=readId(); let px=null,py=null; if(cur().v==="AT"){adv();px=parseExpr();expect(",");py=parseExpr();} body.push({t:"PlaceStmt",moduleType:mt,instanceName:iname,px,py}); }
else if (tk.v === "WIRE") { adv(); const sm=readId(); expect("."); const sp=readId(); expect("TO"); const dm=readId(); expect("."); const dp=readId(); body.push({t:"WireStmt",srcMod:sm,srcPort:sp,dstMod:dm,dstPort:dp}); }
else if (tk.v === "ROUTE") { adv(); const sm=readId(); expect("."); const sp=readId(); expect("TO"); const dm=readId(); expect("."); const dp=readId(); body.push({t:"RouteStmt",srcMod:sm,srcPort:sp,dstMod:dm,dstPort:dp}); }
else if (tk.v === "SHARE") { adv(); const sn=readId(); expect("BETWEEN"); const mods=[readId()]; while(cur().v===","){adv();mods.push(readId());} body.push({t:"ShareStmt",stateName:sn,modules:mods}); }
else if (tk.v === "SET") { adv(); const m=readId(); expect("."); const p=readId(); expect("="); const val=parseExpr(); body.push({t:"SetStmt",mod:m,port:p,value:val}); }
else if (tk.v === "PROBE") { adv(); const m=readId(); expect("."); const p=readId(); let lb=null; if(cur().v==="AS"){adv();lb=adv().v;} body.push({t:"ProbeStmt",mod:m,port:p,label:lb}); }
else if (tk.v === "EXPORT") { adv(); const m=readId(); expect("."); const p=readId(); expect("AS"); const en=readId(); body.push({t:"ExportStmt",mod:m,port:p,extName:en}); }
else { adv(); }
skipNL();
}
expect("END BOARD");
return { t: "BoardDecl", name, body };
}
// Parse program
const program = { t: "Program", body: [] };
skipNL();
while (cur().t !== "EOF") {
skipNL();
if (cur().t === "EOF") break;
if (cur().v === "MODULE") program.body.push(parseModule());
else if (cur().v === "BOARD") program.body.push(parseBoard());
else if (cur().v === "IMPORT") { adv(); const p = adv().v; program.body.push({ t: "ImportStmt", path: p }); }
else adv();
skipNL();
}
return program;
}
// Interpreter
class OrbInterpreter {
constructor(outputFn) {
this.moduleTypes = {};
this.instances = {};
this.wires = [];
this.routes = [];
this.sharedStates = {};
this.globalEnv = new OrbEnv(null, "<global>");
this.regs = new OrbRegisters();
this.asmLabels = {};
this.outputFn = outputFn || ((...a) => console.log(...a));
this.output = [];
this.trace = [];
this.inspectLog = [];
this.halted = false;
this.haltMsg = "";
this.error = null;
}
run(src) {
try {
const ast = orbParse(src);
// Phase 1: register types
for (const n of ast.body) {
if (n.t === "ModuleDecl") this.moduleTypes[n.name] = n;
}
// Phase 2-4: execute boards
for (const n of ast.body) {
if (n.t === "BoardDecl") this.execBoard(n);
}
} catch (e) {
this.error = e.message;
this.outputFn(`ERROR: ${e.message}`);
}
}
execBoard(board) {
for (const n of board.body) {
if (n.t === "PlaceStmt") this.placeInstance(n);
}
for (const n of board.body) {
if (n.t === "WireStmt") this.wires.push([n.srcMod, n.srcPort, n.dstMod, n.dstPort]);
else if (n.t === "RouteStmt") this.routes.push([n.srcMod, n.srcPort, n.dstMod, n.dstPort]);
else if (n.t === "ShareStmt") this.sharedStates[n.stateName] = { name: n.stateName, value: null, lockedBy: null, modules: n.modules };
else if (n.t === "SetStmt") this.applySet(n);
}
this.executeInstances();
}
placeInstance(node) {
const def = this.moduleTypes[node.moduleType];
if (!def) throw new Error(`Unknown module: ${node.moduleType}`);
const env = new OrbEnv(this.globalEnv, node.instanceName);
const inst = {
name: node.instanceName, moduleType: node.moduleType,
commMode: def.commMode, def, env,
inPorts: {}, outPorts: {}, msgChannels: {},
executed: false, halted: false, haltMsg: ""
};
for (const p of def.ports) {
const port = { name: p.name, dir: p.direction, typeName: p.typeName, vecWidth: p.vecWidth, value: makeDefault(p.typeName, p.vecWidth), connected: false };
if (p.direction === "IN" || p.direction === "INOUT") { inst.inPorts[p.name] = port; env.define(p.name, port.value); }
if (p.direction === "OUT" || p.direction === "INOUT") { inst.outPorts[p.name] = port; if (p.direction === "OUT") env.define(p.name, port.value); }
}
this.instances[node.instanceName] = inst;
}
applySet(node) {
const inst = this.instances[node.mod];
if (!inst) return;
const val = this.evalExpr(node.value, this.globalEnv);
if (node.port in inst.inPorts) { inst.inPorts[node.port].value = val; inst.env.set(node.port, val); }
else inst.env.define(node.port, val);
}
executeInstances() {
const deps = {}; for (const n in this.instances) deps[n] = new Set();
for (const [sm,,dm] of this.wires) { if (deps[dm]) deps[dm].add(sm); }
const visited = new Set(), order = [];
const visit = (n) => { if (visited.has(n)) return; visited.add(n); (deps[n]||new Set()).forEach(d => visit(d)); order.push(n); };
for (const n in this.instances) visit(n);
for (const name of order) {
const inst = this.instances[name];
// Sync input ports
for (const [sm, sp, dm, dp] of this.wires) {
if (dm === name) {
const si = this.instances[sm];
if (si && sp in si.outPorts) {
const val = si.outPorts[sp].value;
if (dp in inst.inPorts) { inst.inPorts[dp].value = val; inst.env.set(dp, val instanceof OrbVec ? val.copy() : val); }
}
}
}
try {
this.execBody(inst.def.body, inst.env);
inst.executed = true;
for (const pn in inst.outPorts) { try { inst.outPorts[pn].value = inst.env.get(pn); } catch(e) {} }
} catch (e) {
if (e.message?.startsWith("HALT:")) { inst.halted = true; inst.haltMsg = e.message.slice(5); this.halted = true; this.haltMsg = `[${name}] ${inst.haltMsg}`; }
else { this.error = `[${name}] ${e.message}`; this.outputFn(`ERROR in ${name}: ${e.message}`); }
}
}
}
execBody(body, env) {
if (!body) return;
for (let i = 0; i < body.length; i++) {
if (body[i]?.t === "LabelStmt") env.labels[body[i].name] = i;
}
let i = 0;
while (i < body.length) {
const node = body[i];
if (!node) { i++; continue; }
this.trace.push({ type: node.t, line: node.l });
const result = this.execStmt(node, env);
if (typeof result === "string" && result.startsWith("__goto:")) {
const target = result.slice(7);
if (target in env.labels) { i = env.labels[target]; continue; }
}
i++;
}
}
execStmt(node, env) {
if (!node) return;
switch (node.t) {
case "VarDecl": {
let val = makeDefault(node.typeName, node.vecWidth);
if (node.init) val = this.evalExpr(node.init, env);
env.define(node.name, val); break;
}
case "ConstDecl": env.defineConst(node.name, this.evalExpr(node.value, env)); break;
case "Assignment": {
const val = this.evalExpr(node.value, env);
if (node.index) {
const idx = this.evalExpr(node.index, env);
const tgt = env.get(node.target);
if (tgt instanceof OrbVec) tgt.data[Math.floor(idx)] = Number(val);
} else env.set(node.target, val);
break;
}
case "IfStmt": {
if (this.truthy(this.evalExpr(node.cond, env))) { this.execBody(node.thenBody, env); }
else {
let matched = false;
for (const el of (node.elifs||[])) { if (this.truthy(this.evalExpr(el.cond, env))) { this.execBody(el.body, env); matched = true; break; } }
if (!matched && node.elseBody?.length) this.execBody(node.elseBody, env);
}
break;
}
case "ForStmt": {
let c = this.evalExpr(node.start, env);
const end = this.evalExpr(node.end, env);
const step = node.step ? this.evalExpr(node.step, env) : 1;
env.define(node.varName, c);
while ((step > 0 && c <= end) || (step < 0 && c >= end)) {
env.set(node.varName, c); this.execBody(node.body, env); c += step;
}
break;
}
case "WhileStmt": { while (this.truthy(this.evalExpr(node.cond, env))) this.execBody(node.body, env); break; }
case "PrintStmt": {
const parts = node.values.map(v => { const r = this.evalExpr(v, env); return r instanceof OrbVec ? r.toString() : String(r); });
const line = parts.join("");
this.output.push(line); this.outputFn(line); break;
}
case "HaltStmt": throw new Error(`HALT:${node.message}`);
case "InspectStmt": {
const val = env.get(node.target);
this.inspectLog.push({ target: node.target, value: val instanceof OrbVec ? val.toString() : String(val) });
this.outputFn(`[INSPECT ${node.target}] = ${val instanceof OrbVec ? val.toString() : val}`);
break;
}
case "LabelStmt": break;
case "GotoStmt": return `__goto:${node.target}`;
case "VecLoad": { const src = this.evalExpr(node.source, env); env.set(node.target, src instanceof OrbVec ? src.copy() : src); break; }
case "VecStore": { const src = env.get(node.source); if (node.target?.t === "Id") env.set(node.target.name, src instanceof OrbVec ? src.copy() : src); break; }
case "VecArith": {
const a = this.evalExpr(node.a, env), b = this.evalExpr(node.b, env);
if (a instanceof OrbVec && b instanceof OrbVec) {
const w = Math.min(a.width, b.width), r = new OrbVec(new Array(w).fill(0), w);
const ops = { VADD:(x,y)=>x+y, VSUB:(x,y)=>x-y, VMUL:(x,y)=>x*y, VDIV:(x,y)=>y?x/y:0, VMADD:(x,y)=>x*y };
for (let i=0;i<w;i++) r.data[i] = (ops[node.op]||ops.VADD)(a.data[i], b.data[i]);
env.set(node.target, r);
}
break;
}
case "SendStmt": break; // Message passing conceptual in single-threaded
case "RecvStmt": env.set(node.variable, ""); break;
case "LockStmt": this.execBody(node.body, env); break;
case "AsmInline": case "AsmBlock": this.execAsmBody(node.body, env); break;
case "AsmInstr": this.execAsmInstr(node, env); break;
case "AsmLabelDef": break;
case "AsmEquate": { const v = this.evalExpr(node.value, env); env.defineConst(`.${node.name}`, v); this.asmLabels[`.${node.name}`] = v; break; }
case "AsmOpt": break;
}
return null;
}
execAsmBody(body, env) {
if (!body) return;
for (let i = 0; i < body.length; i++) {
const n = body[i]; if (!n) continue;
if (n.t === "AsmLabelDef") this.asmLabels[`.${n.name}`] = i;
if (n.t === "AsmEquate") { const v = this.evalExpr(n.value, env); env.defineConst(`.${n.name}`, v); this.asmLabels[`.${n.name}`] = v; }
}
let i = 0;
while (i < body.length) {
const n = body[i]; if (!n) { i++; continue; }
if (n.t === "AsmInstr") {
const r = this.execAsmInstr(n, env);
if (typeof r === "string" && r.startsWith("__jmp:")) {
const tgt = r.slice(6);
if (tgt in this.asmLabels && typeof this.asmLabels[tgt] === "number") { i = this.asmLabels[tgt]; continue; }
}
} else if (n.t !== "AsmLabelDef" && n.t !== "AsmEquate" && n.t !== "AsmOpt") {
this.execStmt(n, env);
}
i++;
}
}
execAsmInstr(node, env) {
const op = node.op.toUpperCase(), ops = node.operands || [];
const resolve = (o) => {
if (!o) return 0;
if (o.kind === "reg") return this.regs.get(o.v);
if (o.kind === "imm") return o.v;
if (o.kind === "exprimm") return this.evalExpr(o.expr, env);
if (o.kind === "var") return env.get(o.v);
if (o.kind === "indirect") return this.regs.get(o.v);
if (o.kind === "indexed") return this.regs.get(o.v) + this.evalExpr(o.offset, env);
if (o.kind === "lblref") return o.v;
if (o.kind === "expr") return this.evalExpr(o.expr, env);
return 0;
};
const store = (o, v) => {
if (o.kind === "reg") this.regs.set(o.v, v);
else if (o.kind === "var") env.set(o.v, v);
else if (o.kind === "indirect" || o.kind === "indexed") this.regs.set(o.v, v);
};
const n = ops.length;
if (["MOV","LDA","LDX"].includes(op) && n >= 2) { store(ops[0], resolve(ops[1])); }
else if (["STA","STX"].includes(op) && n >= 2) { store(ops[1], resolve(ops[0])); }
else if (op==="ADD") { if(n===3){const r=resolve(ops[1])+resolve(ops[2]);store(ops[0],r);this.regs.updateFlags(r);}else if(n===2){const r=resolve(ops[0])+resolve(ops[1]);store(ops[0],r);this.regs.updateFlags(r);} }
else if (op==="SUB") { if(n===3){const r=resolve(ops[1])-resolve(ops[2]);store(ops[0],r);this.regs.updateFlags(r);}else if(n===2){const r=resolve(ops[0])-resolve(ops[1]);store(ops[0],r);this.regs.updateFlags(r);} }
else if (op==="MUL") { if(n===3){const r=resolve(ops[1])*resolve(ops[2]);store(ops[0],r);this.regs.updateFlags(r);}else if(n===2){const r=resolve(ops[0])*resolve(ops[1]);store(ops[0],r);this.regs.updateFlags(r);} }
else if (op==="CMP" && n>=2) { this.regs.updateFlags(resolve(ops[0])-resolve(ops[1])); }
else if (op==="JMP"&&n>=1) return `__jmp:${resolve(ops[0])}`;
else if ((op==="JEQ"||op==="BEQ")&&n>=1&&this.regs.flags.Z) return `__jmp:${resolve(ops[0])}`;
else if ((op==="JNE"||op==="BNE")&&n>=1&&!this.regs.flags.Z) return `__jmp:${resolve(ops[0])}`;
else if (op==="PUSH"||op==="PHR") { if(n>=1) this.regs.stack.push(resolve(ops[0])); }
else if (op==="POP"||op==="PLR") { if(n>=1&&this.regs.stack.length) store(ops[0], this.regs.stack.pop()); }
else if (["AND","OR","XOR","NOT","SHL","SHR","SEI","CLI","NOP","HLT","JSR","RTS","RET"].includes(op)) { /* minimal */ }
return null;
}
evalExpr(node, env) {
if (!node) return 0;
if (typeof node === "number") return node;
if (typeof node === "string") return node;
switch (node.t) {
case "Num": return node.v;
case "Str": return node.v;
case "Id":
if (node.name in this.asmLabels) return this.asmLabels[node.name];
return env.get(node.name);
case "Vec": return new OrbVec(node.elements.map(e => Number(this.evalExpr(e, env))), node.elements.length);
case "BinOp": {
const l = this.evalExpr(node.l, env), r = this.evalExpr(node.r, env);
if (typeof l==="string"||typeof r==="string") { if(node.op==="+") return String(l)+String(r); return ({"==":l==r,"!=":l!=r})[node.op]?1:0; }
const ops={"+":()=>l+r,"-":()=>l-r,"*":()=>l*r,"/":()=>r?l/r:0,"%":()=>r?l%r:0,"==":()=>l===r?1:0,"!=":()=>l!==r?1:0,"<":()=>l<r?1:0,">":()=>l>r?1:0,"<=":()=>l<=r?1:0,">=":()=>l>=r?1:0,"AND":()=>l&&r?1:0,"OR":()=>l||r?1:0};
return (ops[node.op]||(() => 0))();
}
case "UnOp":
if (node.op==="-") return -this.evalExpr(node.a, env);
if (node.op==="~") return ~Math.floor(this.evalExpr(node.a, env));
if (node.op==="NOT") return this.truthy(this.evalExpr(node.a, env))?0:1;
return 0;
case "Index": {
const tgt = this.evalExpr(node.target, env), idx = this.evalExpr(node.index, env);
if (tgt instanceof OrbVec) return tgt.data[Math.floor(idx)];
return 0;
}
case "Dot": return 0;
case "Call": {
const args = node.args.map(a => this.evalExpr(a, env));
const builtins = { abs:a=>Math.abs(a[0]), sqrt:a=>Math.sqrt(a[0]), sin:a=>Math.sin(a[0]), cos:a=>Math.cos(a[0]), floor:a=>Math.floor(a[0]), ceil:a=>Math.ceil(a[0]), min:a=>Math.min(...a), max:a=>Math.max(...a), int:a=>Math.floor(a[0]), float:a=>Number(a[0]), str:a=>String(a[0]) };
const fn = builtins[node.name?.toLowerCase()];
return fn ? fn(args) : 0;
}
}
return 0;
}
truthy(v) { if (typeof v==="number") return v!==0; if (typeof v==="string") return v.length>0; if (v instanceof OrbVec) return v.data.some(x=>x!==0); return Boolean(v); }
getState() {
const insts = {};
for (const [name, inst] of Object.entries(this.instances)) {
insts[name] = {
moduleType: inst.moduleType, commMode: inst.commMode,
executed: inst.executed, halted: inst.halted,
inPorts: Object.fromEntries(Object.entries(inst.inPorts).map(([k,v])=>[k,{value: v.value instanceof OrbVec ? v.value.toString() : v.value, type: v.typeName}])),
outPorts: Object.fromEntries(Object.entries(inst.outPorts).map(([k,v])=>[k,{value: v.value instanceof OrbVec ? v.value.toString() : v.value, type: v.typeName}])),
vars: Object.fromEntries(Object.entries(inst.env.dump()).map(([k,v])=>[k, v instanceof OrbVec ? v.toString() : v])),
};
}
return { instances: insts, registers: this.regs.dump(), shared: this.sharedStates, halted: this.halted, error: this.error, output: [...this.output], inspectLog: [...this.inspectLog] };
}
}
// ============================================================
// COLOUR PALETTE
// ============================================================
const C = {
bg:"#080c14", panel:"#0d1117", panelBorder:"#1b2433", headerBg:"#0f1520",
modFill:"#111827", modStroke:"#2563eb", modHeader:"#152040", modHeaderText:"#7db4f5",
modText:"#c8d6e5",
portIn:"#22d3ee", portOut:"#f59e0b",
wireData:"#3b82f6", wireMsg:"#f97316",
flowBox:"#1a2332", flowStroke:"#2d3f56", flowText:"#94a3b8",
flowDiamond:"#1a2332", flowDiamondStroke:"#eab308", flowDiamondText:"#fde68a",
flowLoop:"#0c1e30", flowLoopStroke:"#06b6d4", flowLoopText:"#67e8f9",
flowAsm:"#1a1540", flowAsmStroke:"#6366f1", flowAsmText:"#a5b4fc",
flowHalt:"#ef4444", flowInspect:"#8b5cf6", flowLabel:"#10b981",
setValue:"#34d399", probe:"#f472b6",
sharedFill:"#180a30", sharedStroke:"#a855f7",
accent:"#60a5fa", dim:"#374151", success:"#22c55e", danger:"#ef4444",
typeVec:"#3b82f6", typeInt:"#22c55e", typeFloat:"#f59e0b", typeStr:"#e2e8f0",
};
const TC = { VEC:C.typeVec, INT:C.typeInt, FLOAT:C.typeFloat, STRING:C.typeStr };
const COMM = { DATAFLOW:{color:C.wireData,dash:"",label:"DATAFLOW"}, MESSAGE:{color:C.wireMsg,dash:"6,4",label:"MESSAGE"}, SHARED:{color:C.sharedStroke,dash:"3,3",label:"SHARED"} };
const FONT = "'JetBrains Mono','Fira Code','Cascadia Code',monospace";
// ============================================================
// LAYOUT
// ============================================================
function layoutBoard(board, modTypes, state) {
const places = board.body.filter(n=>n.t==="PlaceStmt");
const wires = board.body.filter(n=>n.t==="WireStmt");
const routes = board.body.filter(n=>n.t==="RouteStmt");
const sets = board.body.filter(n=>n.t==="SetStmt");
const probes = board.body.filter(n=>n.t==="ProbeStmt");
const shares = board.body.filter(n=>n.t==="ShareStmt");
const exports = board.body.filter(n=>n.t==="ExportStmt");
const MW=210, PH=28, PAD=70, CW=MW+180;
// Dependency sort
const deps={}; places.forEach(p=>{deps[p.instanceName]=new Set();});
wires.forEach(w=>{if(deps[w.dstMod])deps[w.dstMod].add(w.srcMod);});
const vis=new Set(),order=[];
const visit=n=>{if(vis.has(n))return;vis.add(n);(deps[n]||new Set()).forEach(d=>visit(d));order.push(n);};
places.forEach(p=>visit(p.instanceName));
const colA={}; order.forEach(n=>{let c=0;(deps[n]||new Set()).forEach(d=>{if(colA[d]!==undefined)c=Math.max(c,colA[d]+1);});colA[n]=c;});
const colR={}, insts={};
places.forEach(p => {
const col=colA[p.instanceName]||0;
if(!colR[col])colR[col]=0;
const row=colR[col]++;
const mod=modTypes[p.moduleType];
const inP=(mod?.ports||[]).filter(pt=>pt.direction==="IN"||pt.direction==="INOUT");
const outP=(mod?.ports||[]).filter(pt=>pt.direction==="OUT"||pt.direction==="INOUT");
const maxP=Math.max(inP.length,outP.length,1);
const h=60+maxP*PH;
const x=PAD+col*CW, y=PAD+row*(h+PAD);
// Get live port values from state
const instState = state?.instances?.[p.instanceName];
insts[p.instanceName] = {
...p, x,y,w:MW,h, mod, commMode:mod?.commMode||"DATAFLOW",
inPorts: inP.map((pt,i)=>({...pt,cx:x,cy:y+42+i*PH+PH/2,liveValue:instState?.inPorts?.[pt.name]?.value})),
outPorts: outP.map((pt,i)=>({...pt,cx:x+MW,cy:y+42+i*PH+PH/2,liveValue:instState?.outPorts?.[pt.name]?.value})),
executed: instState?.executed, instHalted: instState?.halted,
};
});
const rWires = wires.map(w=>{
const si=insts[w.srcMod],di=insts[w.dstMod];
const sp=si?.outPorts?.find(p=>p.name===w.srcPort); const dp=di?.inPorts?.find(p=>p.name===w.dstPort);
return {...w,sx:sp?.cx||0,sy:sp?.cy||0,dx:dp?.cx||0,dy:dp?.cy||0,style:"data"};
});
const rRoutes = routes.map(r=>{
const si=insts[r.srcMod],di=insts[r.dstMod];
return {...r,sx:si?(si.x+si.w):0,sy:si?(si.y+si.h/2):0,dx:di?di.x:0,dy:di?(di.y+di.h/2):0,style:"message"};
});
let mx=0,my=0; Object.values(insts).forEach(i=>{mx=Math.max(mx,i.x+i.w+PAD);my=Math.max(my,i.y+i.h+PAD);});
return {insts,wires:rWires,routes:rRoutes,sets,probes,shares,exports,bounds:{w:mx+80,h:my+80}};
}
function layoutFlow(body) {
const W=230,H=38,G=16,SX=50,AH=20; let y=44;
const nodes=[];
function add(n, x=SX) {
let shape="box",label="",fill=C.flowBox,stroke=C.flowStroke,tc=C.flowText,h=H,ch=null;
switch(n.t) {
case "VarDecl": label=`DIM ${n.name} AS ${n.typeName}${n.init?" = ...":""}`;break;
case "Assignment": label=`${n.target}${n.index?" [...]":""} = ...`;break;
case "VecLoad": label=`VLOAD ${n.target}`;fill="#0c2d48";stroke=C.typeVec;break;
case "VecStore": label=`VSTORE ${n.source}`;fill="#0c2d48";stroke=C.typeVec;break;
case "VecArith": label=`${n.op} → ${n.target}`;fill="#0c2d48";stroke=C.typeVec;break;
case "PrintStmt": label="PRINT ...";fill="#161f2d";stroke="#4b5563";break;
case "SendStmt": label=`SEND → ${n.targetMod}`;fill="#2a1a0a";stroke=C.wireMsg;tc="#fdba74";break;
case "RecvStmt": label=`RECV ← ${n.srcMod}`;fill="#2a1a0a";stroke=C.wireMsg;tc="#fdba74";break;
case "HaltStmt": shape="octagon";label=`HALT${n.message?": "+n.message:""}`;fill="#2d0a0a";stroke=C.flowHalt;tc="#fca5a5";break;
case "InspectStmt": shape="hexagon";label=`INSPECT ${n.target}`;fill="#1a0d2e";stroke=C.flowInspect;tc="#c4b5fd";break;
case "LabelStmt": shape="label";label=`@${n.name}`;fill="transparent";stroke=C.flowLabel;tc=C.flowLabel;h=26;break;
case "ForStmt": shape="loop";label=`FOR ${n.varName}`;fill=C.flowLoop;stroke=C.flowLoopStroke;tc=C.flowLoopText;ch=n.body;break;
case "WhileStmt": shape="loop";label="WHILE ...";fill=C.flowLoop;stroke=C.flowLoopStroke;tc=C.flowLoopText;ch=n.body;break;
case "IfStmt": shape="diamond";label="IF ...";fill=C.flowDiamond;stroke=C.flowDiamondStroke;tc=C.flowDiamondText;ch=n.thenBody;break;
case "LockStmt": shape="box";label=`LOCK ${n.target}`;fill=C.sharedFill;stroke=C.sharedStroke;tc="#c4b5fd";ch=n.body;break;
case "AsmInline": shape="asm";label="[ inline asm ]";fill=C.flowAsm;stroke=C.flowAsmStroke;tc=C.flowAsmText;h=H+(n.body?.length||0)*AH+6;break;
case "AsmBlock": shape="asm";label=`ASM ${n.name||""}`;fill=C.flowAsm;stroke=C.flowAsmStroke;tc=C.flowAsmText;h=H+Math.min(n.body?.length||0,12)*AH+6;break;
default: label=n.t||"?";
}
nodes.push({id:nodes.length,n,x,y,w:W,h,shape,label,fill,stroke,tc});
y+=h+G;
if(ch&&shape!=="asm"){ch.forEach(c=>add(c,x+28));}
if(n.t==="IfStmt") {
(n.elifs||[]).forEach(el=>{nodes.push({id:nodes.length,n:el,x:x+28,y,w:W,h:H,shape:"diamond",label:`ELIF`,fill:C.flowDiamond,stroke:"#a16207",tc:C.flowDiamondText});y+=H+G;(el.body||[]).forEach(c=>add(c,x+56));});
if(n.elseBody?.length){nodes.push({id:nodes.length,n:{t:"Else"},x:x+28,y,w:W,h:26,shape:"box",label:"ELSE",fill:C.flowDiamond,stroke:"#a16207",tc:C.flowDiamondText});y+=26+G;n.elseBody.forEach(c=>add(c,x+56));}
}
}
(body||[]).forEach(n=>add(n));
return {nodes,bounds:{w:420,h:y+30}};
}
// ============================================================
// SAMPLE SOURCE
// ============================================================
const SAMPLE = `// Orb Language — Live Demo
MODULE amplifier DATAFLOW
PORTS
IN signal AS VEC[4]
IN gain AS FLOAT
OUT output AS VEC[4]
END PORTS
{
DIM temp AS VEC[4]
DIM i AS INT = 0
VLOAD temp FROM signal
FOR i = 0 TO 3
temp[i] = temp[i] * gain
NEXT i
VSTORE temp TO output
IF gain > 1.0 THEN
PRINT "Amplifying by ", gain
ELIF gain == 1.0 THEN
PRINT "Unity gain"
ELSE
PRINT "Attenuating"
END IF
@done:
INSPECT temp
// BBC BASIC inline asm
[
MOV R0, gain
MOV R1, #10
ADD R0, R0, R1
MOV gain, R0
]
PRINT "Gain after asm: ", gain
}
END MODULE
MODULE mixer DATAFLOW
PORTS
IN input_a AS VEC[4]
IN input_b AS VEC[4]
OUT output AS VEC[4]
END PORTS
{
DIM result AS VEC[4]
DIM i AS INT = 0
VLOAD result FROM input_a
FOR i = 0 TO 3
result[i] = result[i] + input_b[i]
NEXT i
VSTORE result TO output
PRINT "Mixed: ", output
}
END MODULE
MODULE logger MESSAGE
{
DIM msg AS STRING = "waiting"
PRINT "Logger: ", msg
}
END MODULE
BOARD main_board
PLACE amplifier AS amp1
PLACE amplifier AS amp2
PLACE mixer AS mix
PLACE logger AS log
WIRE amp1.output TO mix.input_a
WIRE amp2.output TO mix.input_b
SET amp1.gain = 2.0
SET amp1.signal = <1.0, 2.0, 3.0, 4.0>
SET amp2.gain = 0.5
SET amp2.signal = <10.0, 20.0, 30.0, 40.0>
PROBE amp1.output AS "Amp1 Out"
PROBE mix.output AS "Final Mix"
SHARE gain_state BETWEEN amp1, amp2, mix
EXPORT mix.output AS master_out
END BOARD`;
// ============================================================
// REACT COMPONENTS
// ============================================================
function ModBlock({inst,sets,onDrill,state}) {
const {x,y,w,h,instanceName:iname,moduleType:mtype,commMode}=inst;
const cm=COMM[commMode]||COMM.DATAFLOW;
const sv={};(sets||[]).forEach(s=>{if(s.mod===iname)sv[s.port]=s.value;});
const executed=inst.executed;
return (
<g style={{cursor:"pointer"}} onDoubleClick={()=>onDrill(inst)}>
<rect x={x+3} y={y+3} width={w} height={h} rx={5} fill="rgba(0,0,0,0.35)"/>
<rect x={x} y={y} width={w} height={h} rx={5} fill={C.modFill} stroke={executed?C.success:cm.color} strokeWidth={executed?2:1.4} strokeDasharray={cm.dash}/>
<rect x={x} y={y} width={w} height={34} rx={5} fill={C.modHeader}/><rect x={x} y={y+30} width={w} height={4} fill={C.modHeader}/>