forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_hex.nula
More file actions
727 lines (699 loc) · 34.6 KB
/
Copy pathcompile_hex.nula
File metadata and controls
727 lines (699 loc) · 34.6 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
// Work around a host-compiler quirk where bitwise `&` is miscompiled in
// some high-register-pressure contexts. Extract the low bits arithmetically.
fn low17(x: Int) -> Int {
let q = x / 0x20000;
x - q * 0x20000
}
fn low16(x: Int) -> Int {
let q = x / 0x10000;
x - q * 0x10000
}
fn low8(x: Int) -> Int {
let q = x / 0x100;
x - q * 0x100
}
fn low6(x: Int) -> Int {
let q = x / 0x40;
x - q * 0x40
}
fn bit17(x: Int) -> Int {
let t = x / 0x20000;
t - (t / 2) * 2
}
// bootstrap/compile_hex.nula — Compile Nulang Core expressions to hex bytecode.
// Supports: arithmetic, comparisons, booleans, if/then/else, let bindings,
// closures fn(x) => body, function application f(arg).
// Environment is stored as a string of 4-byte records (code points 0-63, 6 bits each).
// Each binding encodes 24 bits: hash (16 bits) + reg (8 bits).
fn is_digit(c: Int) -> Bool { c >= 48 and c <= 57 }
fn is_alpha(c: Int) -> Bool { (c >= 65 and c <= 90) or (c >= 97 and c <= 122) }
fn is_alphanum(c: Int) -> Bool { is_alpha(c) or is_digit(c) or c == 95 }
fn is_space(c: Int) -> Bool { c == 32 or c == 10 or c == 13 or c == 9 }
fn skip_ws(src: String, pos: Int, len: Int) -> Int {
if pos >= len then pos
else if is_space(perform String.charAt(src, pos)) then skip_ws(src, pos + 1, len)
else pos
}
fn read_int(src: String, pos: Int, len: Int, acc: Int) -> Int {
if pos >= len then (acc << 16) + pos
else {
let c = perform String.charAt(src, pos);
if is_digit(c) then read_int(src, pos + 1, len, acc * 10 + (c - 48))
else (acc << 16) + pos
}
}
fn read_ident(src: String, pos: Int, len: Int, h: Int) -> Int {
if pos >= len then ((low16(h)) << 16) + pos
else {
let c = perform String.charAt(src, pos);
if is_alphanum(c) then {
let nh = h * 5 + c;
read_ident(src, pos + 1, len, nh)
} else ((low16(h)) << 16) + pos
}
}
fn read_string(src: String, pos: Int, len: Int, acc: String) -> String {
if pos >= len then acc
else {
let c = perform String.charAt(src, pos);
if c == 34 then acc
else read_string(src, pos + 1, len, acc + perform String.from_char(c))
}
}
fn read_string_end(src: String, pos: Int, len: Int) -> Int {
if pos >= len then pos
else {
let c = perform String.charAt(src, pos);
if c == 34 then pos + 1
else read_string_end(src, pos + 1, len)
}
}
// Environment stored as a string of 4-byte records (code points 0-63, 6 bits each).
// Each binding encodes 24 bits: hash (16 bits) + reg (8 bits).
fn env_push(env: String, h: Int, reg: Int) -> String {
let v = ((low16(h)) << 8) + (low8(reg));
let c0 = (low6((v >> 18))) + 1;
let c1 = (low6((v >> 12))) + 1;
let c2 = (low6((v >> 6))) + 1;
let c3 = (low6(v)) + 1;
env +
perform String.from_char(c0) +
perform String.from_char(c1) +
perform String.from_char(c2) +
perform String.from_char(c3)
}
fn env_lookup(env: String, elen: Int, h: Int) -> Int {
if elen <= 0 then 0
else {
let i = (elen - 1) * 4;
let b0 = perform String.charAt(env, i) - 1;
let b1 = perform String.charAt(env, i + 1) - 1;
let b2 = perform String.charAt(env, i + 2) - 1;
let b3 = perform String.charAt(env, i + 3) - 1;
let v = (b0 << 18) + (b1 << 12) + (b2 << 6) + b3;
let hh = low16((v >> 8));
let reg = low8(v);
if hh == (low16(h)) then reg
else env_lookup(env, elen - 1, h)
}
}
// Count capture-worthy entries in the env (registers 100-127 or 11-19).
fn count_captures(env: String, elen: Int) -> Int {
if elen <= 0 then 0
else {
let i = (elen - 1) * 4;
let b0 = perform String.charAt(env, i) - 1;
let b1 = perform String.charAt(env, i + 1) - 1;
let b2 = perform String.charAt(env, i + 2) - 1;
let b3 = perform String.charAt(env, i + 3) - 1;
let v = (b0 << 18) + (b1 << 12) + (b2 << 6) + b3;
let reg = low8(v);
let is_cap = reg >= 100 and reg < 128;
let rest = count_captures(env, elen - 1);
if is_cap then 1 + rest else rest
}
}
// Rebuild env by appending remapped entries for captured variables.
// Processes entries oldest→newest (idx 0→elen-1). For each captured
// entry, appends an env_push mapping the same hash to r(11+slot).
// Returns the augmented env; the capture count is in emit order.
fn remap_captures(env: String, elen: Int, idx: Int, slot: Int) -> String {
if idx >= elen then env
else {
let i = idx * 4;
let b0 = perform String.charAt(env, i) - 1;
let b1 = perform String.charAt(env, i + 1) - 1;
let b2 = perform String.charAt(env, i + 2) - 1;
let b3 = perform String.charAt(env, i + 3) - 1;
let v = (b0 << 18) + (b1 << 12) + (b2 << 6) + b3;
let hh = low16((v >> 8));
let reg = low8(v);
let is_cap = reg >= 100 and reg < 128;
if is_cap then {
let tail = remap_captures(env, elen, idx + 1, slot + 1);
env_push(tail, hh, 11 + slot)
} else {
remap_captures(env, elen, idx + 1, slot)
}
}
}
fn prec_of(c: Int) -> Int {
if c == 43 then 2 else if c == 45 then 2
else if c == 42 then 3 else if c == 47 then 3
else if c == 38 then 2
else 0
}
fn parse_cmp(src: String, p: Int, len: Int) -> Int {
if p >= len then 0 else {
let c = perform String.charAt(src, p);
if c == 61 and p + 1 < len then {
let c2 = perform String.charAt(src, p + 1);
if c2 == 61 then (1 << 16) + (0x40 << 8) + 2 else 0
} else if c == 33 and p + 1 < len then {
let c2 = perform String.charAt(src, p + 1);
if c2 == 61 then (1 << 16) + (0xFF << 8) + 2 else 0
} else if c == 60 then {
if p + 1 < len then { let c2 = perform String.charAt(src, p + 1);
if c2 == 61 then (1 << 16) + (0x43 << 8) + 2 else (1 << 16) + (0x41 << 8) + 1
} else (1 << 16) + (0x41 << 8) + 1
} else if c == 62 then {
if p + 1 < len then { let c2 = perform String.charAt(src, p + 1);
if c2 == 61 then (1 << 16) + (0x44 << 8) + 2 else (1 << 16) + (0x42 << 8) + 1
} else (1 << 16) + (0x42 << 8) + 1
} else 0
}
}
fn parse_shift(src: String, p: Int, len: Int) -> Int {
if p + 1 < len then {
let c1 = perform String.charAt(src, p);
let c2 = perform String.charAt(src, p + 1);
if c1 == 60 and c2 == 60 then (3 << 16) + (0x2A << 8) + 2
else if c1 == 62 and c2 == 62 then (3 << 16) + (0x2B << 8) + 2
else 0
} else 0
}
// Convert nybble (0-15) to hex char
fn hex_digit(n: Int) -> String {
if n < 10 then perform Int.to_string(n)
else if n == 10 then "a" else if n == 11 then "b"
else if n == 12 then "c" else if n == 13 then "d"
else if n == 14 then "e" else "f"
}
fn remark(msg: String) {
perform IO.print("; " + msg)
}
fn emit_hex(w: Int) {
perform IO.print("" +
hex_digit((w >> 28) & 0xF) +
hex_digit((w >> 24) & 0xF) +
hex_digit((w >> 20) & 0xF) +
hex_digit((w >> 16) & 0xF) +
hex_digit((w >> 12) & 0xF) +
hex_digit((w >> 8) & 0xF) +
hex_digit((w >> 4) & 0xF) +
hex_digit(w & 0xF)
)
}
// Encode instruction: opcode<<24 | op1<<16 | op2<<8 | op3
fn instr(opcode: Int, op1: Int, op2: Int, op3: Int) -> Int {
(opcode << 24) + (op1 << 16) + (op2 << 8) + op3
}
// Recursively emit CapLoad (is_store=0) or CapStore (is_store!=0)
// for capture slots [slot..count-1].
fn emit_caps(closure_reg: Int, count: Int, slot: Int, is_store: Int) -> Int {
if slot >= count then 0
else {
let cap_reg = 11 + slot;
if is_store != 0 then {
emit_hex(instr(0x62, closure_reg, slot, cap_reg))
} else {
emit_hex(instr(0x61, slot, cap_reg, 0))
};
emit_caps(closure_reg, count, slot + 1, is_store)
}
}
// Scan env entries oldest→newest and emit Move+CapStore for each capture.
// Returns the next slot index.
fn emit_fnend_caps(env: String, elen: Int, idx: Int, slot: Int, closure_reg: Int) -> Int {
if idx >= elen then slot
else {
let i = idx * 4;
let b0 = perform String.charAt(env, i) - 1;
let b1 = perform String.charAt(env, i + 1) - 1;
let b2 = perform String.charAt(env, i + 2) - 1;
let b3 = perform String.charAt(env, i + 3) - 1;
let v = (b0 << 18) + (b1 << 12) + (b2 << 6) + b3;
let reg = low8(v);
let is_cap = reg >= 100 and reg < 128;
if is_cap then {
let cap_dst = 11 + slot;
emit_hex(instr(0x12, reg, cap_dst, 0));
emit_hex(instr(0x62, closure_reg, slot, cap_dst));
emit_fnend_caps(env, elen, idx + 1, slot + 1, closure_reg)
} else {
emit_fnend_caps(env, elen, idx + 1, slot, closure_reg)
}
}
}
// Returns (result_reg << 18) | pos. The bool bit is (1<<17).
fn comp(src: String, pos: Int, len: Int, left: Int, min_prec: Int, nr: Int, env: String, elen: Int) -> Int {
let no_left = 1 << 40;
let pair = if left == no_left then {
let p = skip_ws(src, pos, len);
if p >= len then (nr << 18) + p
else {
let c = perform String.charAt(src, p);
if is_digit(c) then {
let packed = read_int(src, p, len, 0);
let num = packed >> 16;
let q = low16(packed);
if num == 0 then emit_hex(instr(0x03, nr, 0, 0))
else if num == 1 then emit_hex(instr(0x04, nr, 0, 0))
else if num == 2 then emit_hex(instr(0x05, nr, 0, 0))
else if num == -1 then emit_hex(instr(0x06, nr, 0, 0))
else {
remark("const " + perform Int.to_string(num));
emit_hex(instr(0x07, 0, 0, nr))
};
(nr << 18) + q
} else if c == 40 then {
let inner = comp(src, p + 1, len, no_left, -1, nr, env, elen);
let rv = inner >> 18;
let rp = low17(inner);
let q2 = skip_ws(src, rp, len);
if q2 < len then {
let c2 = perform String.charAt(src, q2);
if c2 == 41 then (rv << 18) + (q2 + 1)
else (rv << 18) + q2
} else (rv << 18) + q2
} else if c == 34 then {
let s = read_string(src, p + 1, len, "");
let q = read_string_end(src, p + 1, len);
let quote = perform String.from_char(34);
remark("const " + quote + s + quote);
emit_hex(instr(0x07, 0, 0, nr));
(nr << 18) + q
} else if is_alpha(c) then {
let ident = read_ident(src, p, len, 0);
let h = ident >> 16;
let q = low16(ident);
if h == 3321 then {
// let
let q2 = skip_ws(src, q, len);
let var_id = read_ident(src, q2, len, 0);
let vh = var_id >> 16;
let q3 = low16(var_id);
let q4 = skip_ws(src, q3, len);
if q4 < len then {
let c3 = perform String.charAt(src, q4);
if c3 == 61 then {
// Pre-bind the variable in a high register so recursive function
// bodies can refer to themselves without the placeholder being
// clobbered by temporaries during body compilation.
let vh16 = low16(vh);
let save_reg = low8(200 + nr + elen);
let env_rec = env_push(env, vh16, save_reg);
let bound = comp(src, q4 + 1, len, no_left, -1, nr, env_rec, elen + 1);
let vr = bound >> 18;
let vp = low17(bound);
emit_hex(instr(0x12, vr, save_reg, 0));
let vp2 = skip_ws(src, vp, len);
if vp2 + 1 < len then {
let ci = perform String.charAt(src, vp2);
let cn = perform String.charAt(src, vp2 + 1);
if ci == 105 and cn == 110 then {
let nxt = nr + 1;
comp(src, vp2 + 2, len, no_left, -1, nxt, env_rec, elen + 1)
} else (save_reg << 18) + vp2
} else (save_reg << 18) + vp2
} else (nr << 18) + p
} else (nr << 18) + p
} else if h == 620 then {
// fn
let q2 = skip_ws(src, q, len);
if q2 < len then {
let c1 = perform String.charAt(src, q2);
if c1 == 40 then {
let q3 = skip_ws(src, q2 + 1, len);
let param_id = read_ident(src, q3, len, 0);
let ph = param_id >> 16;
let q4 = low16(param_id);
let q5_raw = skip_ws(src, q4, len);
let q5 = if q5_raw < len then {
let c_colon = perform String.charAt(src, q5_raw);
if c_colon == 58 then {
let after_colon = skip_ws(src, q5_raw + 1, len);
let type_id = read_ident(src, after_colon, len, 0);
low16(type_id)
} else { q5_raw }
} else { q5_raw };
if q5 < len then {
let c2 = perform String.charAt(src, q5);
if c2 == 41 then {
let q6 = skip_ws(src, q5 + 1, len);
if q6 + 1 < len then {
let c3 = perform String.charAt(src, q6);
let c4 = perform String.charAt(src, q6 + 1);
if c3 == 61 and c4 == 62 then {
remark("Jmp -> fn_end");
emit_hex(instr(0x50, 0, 0, 0));
remark("FN_START");
// Keep the parameter in a stable register well above temporaries.
// Arguments are passed in r10 by the caller; copy them to a
// save slot so r10 can be reused for subsequent calls without
// losing the parameter value.
let param_save = 100 + elen;
emit_hex(instr(0x12, 10, param_save, 0));
// Multi-capture: scan all env entries for captured
// variables (registers 100-127 from outer params, or
// 11-19 from previous captures). Each capture gets
// its own slot and register r(11+slot).
let capture_count = count_captures(env, elen);
let env_remapped = remap_captures(env, elen, 0, 0);
let env2 = if capture_count > 0 then {
env_push(env_remapped, low16(ph), param_save)
} else {
env_push(env, low16(ph), param_save)
};
let elen2 = elen + capture_count + 1;
let _ = emit_caps(0, capture_count, 0, 0);
// Forward the current parameter to the next
// capture register so nested closures can store
// it via CapStore without a separate Move.
emit_hex(instr(0x12, param_save, 11 + capture_count, 0));
let body = comp(src, q6 + 2, len, no_left, -1, 8, env2, elen2);
let body_reg = body >> 18;
let body_end = low17(body);
emit_hex(instr(0x12, body_reg, 0, 0));
emit_hex(instr(0x57, 0, 0, 0));
remark("fn_end:");
let closure_reg = nr;
emit_hex(instr(0x60, 0xFF, 0xFF, closure_reg));
let _ = emit_caps(closure_reg, capture_count, 0, 1);
(closure_reg << 18) + body_end
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else if h == 627 then {
// if
let cond = comp(src, q, len, no_left, -1, nr, env, elen);
let cr = cond >> 18;
let cp = low17(cond);
let cnext = nr + 1;
let cp2 = skip_ws(src, cp, len);
let then_id = read_ident(src, cp2, len, 0);
let th = then_id >> 16;
let tp = low16(then_id);
if th == 17715 then {
let lb = bit17(cond);
let result_reg = nr + 2;
remark("if");
if lb == 0 then {
remark("truthiness: check int 0");
emit_hex(instr(0x03, 7, 0, 0));
let tmp = cnext + 1;
emit_hex(instr(0x40, cr, 7, tmp));
emit_hex(instr(0x49, tmp, tmp, 0));
remark("JmpF -> else");
emit_hex(instr(0x52, tmp, 0, 0));
remark("truthiness: check bool false");
let bf = tmp + 1;
emit_hex(instr(0x04, bf, 0, 0));
let bfc = bf + 1;
emit_hex(instr(0x40, 7, bf, bfc));
let bfck = bfc + 1;
emit_hex(instr(0x40, cr, bfc, bfck));
emit_hex(instr(0x49, bfck, bfck, 0));
remark("JmpF -> else");
emit_hex(instr(0x52, bfck, 0, 0))
} else {
remark("JmpF -> else");
emit_hex(instr(0x52, cr, 0, 0))
};
remark("then:");
let then_body = comp(src, tp, len, no_left, -1, cnext, env, elen);
let tr = then_body >> 18;
let tbp = low17(then_body);
emit_hex(instr(0x12, tr, result_reg, 0));
remark("Jmp -> end");
emit_hex(instr(0x50, 0, 0, 0));
remark("else:");
let tbp2 = skip_ws(src, tbp, len);
let else_id = read_ident(src, tbp2, len, 0);
let eh = else_id >> 16;
let ep = low16(else_id);
if eh == 16001 then {
let else_body = comp(src, ep, len, no_left, -1, cnext, env, elen);
let er = else_body >> 18;
let ebp = low17(else_body);
emit_hex(instr(0x12, er, result_reg, 0));
remark("end:");
(result_reg << 18) + ebp
} else (nr << 18) + p
} else (nr << 18) + p
} else if h == 18036 then {
// true
emit_hex(instr(0x04, nr, 0, 0));
(nr << 18) + q
} else if h == 13715 then {
// false (low16 of hash 79251)
emit_hex(instr(0x03, nr, 0, 0));
(nr << 18) + q
} else if h == 3421 then {
// not
let inner = comp(src, q, len, no_left, -1, nr, env, elen);
let iv = inner >> 18;
let ip = low17(inner);
let dr = nr + 1;
emit_hex(instr(0x03, dr, 0, 0));
let dr2 = nr + 2;
emit_hex(instr(0x40, iv, dr, dr2));
(dr2 << 18) + (1 << 17) + ip
} else if h == 64461 then {
// nperform("effect.name" [, arg1 [, arg2 [, arg3]]])
let q2 = skip_ws(src, q, len);
if q2 < len then {
let c1 = perform String.charAt(src, q2);
if c1 == 40 then {
let q3 = skip_ws(src, q2 + 1, len);
if q3 < len then {
let c2 = perform String.charAt(src, q3);
if c2 == 34 then {
let eff_start = q3 + 1;
let q4 = read_string_end(src, q3 + 1, len);
let q5 = skip_ws(src, q4, len);
if q5 < len then {
let c3 = perform String.charAt(src, q5);
let dst = nr;
if c3 == 41 then {
// No arguments
let eff_name = read_string(src, eff_start, len, "");
let quote = perform String.from_char(34);
remark("const " + quote + eff_name + quote);
emit_hex(instr(0x90, 0, 0, dst));
(dst << 18) + skip_ws(src, q5 + 1, len)
} else if c3 == 44 then {
// One or more arguments
let no_left_np = 1 << 40;
let p0 = skip_ws(src, q5 + 1, len);
let inner0 = comp(src, p0, len, no_left_np, 0, nr + 1, env, elen);
let r0 = inner0 >> 18;
let e0 = low17(inner0);
emit_hex(instr(0x12, r0, 0, 0));
let ap0 = skip_ws(src, e0, len);
if ap0 < len then {
let cp0 = perform String.charAt(src, ap0);
if cp0 == 41 then {
// Exactly one argument
let eff_name = read_string(src, eff_start, len, "");
let quote = perform String.from_char(34);
remark("const " + quote + eff_name + quote);
emit_hex(instr(0x90, 0, 0, dst));
(dst << 18) + skip_ws(src, ap0 + 1, len)
} else if cp0 == 44 then {
// Two or more arguments
let p1 = skip_ws(src, ap0 + 1, len);
let inner1 = comp(src, p1, len, no_left_np, 0, nr + 11, env, elen);
let r1 = inner1 >> 18;
let e1 = low17(inner1);
emit_hex(instr(0x12, r1, 1, 0));
let ap1 = skip_ws(src, e1, len);
if ap1 < len then {
let cp1 = perform String.charAt(src, ap1);
if cp1 == 41 then {
// Exactly two arguments
let eff_name = read_string(src, eff_start, len, "");
let quote = perform String.from_char(34);
remark("const " + quote + eff_name + quote);
emit_hex(instr(0x90, 0, 0, dst));
(dst << 18) + skip_ws(src, ap1 + 1, len)
} else if cp1 == 44 then {
// Three arguments
let p2 = skip_ws(src, ap1 + 1, len);
let inner2 = comp(src, p2, len, no_left_np, 0, nr + 21, env, elen);
let r2 = inner2 >> 18;
let e2 = low17(inner2);
emit_hex(instr(0x12, r2, 2, 0));
let ap2 = skip_ws(src, e2, len);
if ap2 < len then {
let cp2 = perform String.charAt(src, ap2);
if cp2 == 41 then {
let eff_name = read_string(src, eff_start, len, "");
let quote = perform String.from_char(34);
remark("const " + quote + eff_name + quote);
emit_hex(instr(0x90, 0, 0, dst));
(dst << 18) + skip_ws(src, ap2 + 1, len)
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else (nr << 18) + p
} else {
let reg = env_lookup(env, elen, low16(h));
if reg == 0 then (nr << 18) + p
else {
emit_hex(instr(0x12, reg, nr, 0));
(nr << 18) + q
}
}
} else { (nr << 18) + p }
}
} else {
let is_bool_bit = 1 << 39;
let lb = if left >= is_bool_bit then 1 else 0;
let rl = if left >= is_bool_bit then left - is_bool_bit else left;
(rl << 18) + (lb << 17) + pos
};
let lr = pair >> 18;
let lp = low17(pair);
let p = skip_ws(src, lp, len);
if p >= len then pair
else {
let c = perform String.charAt(src, p);
if c == 40 then {
let save_reg = low8(150 + nr + elen);
emit_hex(instr(0x12, lr, save_reg, 0));
let arg = comp(src, p + 1, len, no_left, -1, nr + 1, env, elen);
let arg_end = low17(arg);
let ap = skip_ws(src, arg_end, len);
if ap < len then {
let c2 = perform String.charAt(src, ap);
if c2 == 41 then {
// Zero-arg call: comp consumed nothing (arg_end still
// at the starting position, which was the close-paren).
let has_arg = arg_end != p + 1;
if has_arg then {
let arg_reg = arg >> 18;
emit_hex(instr(0x12, arg_reg, 10, 0))
} else { };
let dst = nr;
let argc = if has_arg then 1 else 0;
emit_hex(instr(0x64, save_reg, argc, dst));
comp(src, ap + 1, len, dst, min_prec, dst + 1, env, elen)
} else pair
} else pair
} else if c == 61 or c == 33 or c == 60 or c == 62 then {
let shift = parse_shift(src, p, len);
if shift != 0 then {
let sprec = shift >> 16;
let sop = low8((shift >> 8));
let slen = low8(shift);
if sprec < min_prec then pair else {
let rhs = comp(src, p + slen, len, no_left, sprec, nr + 1, env, elen);
let dr = nr + 2;
let rr = rhs >> 18;
let rp = low17(rhs);
emit_hex(instr(sop, lr, rr, dr));
comp(src, rp, len, dr + (1 << 39), min_prec, dr + 1, env, elen)
}
} else {
let cmp = parse_cmp(src, p, len);
if cmp == 0 then pair else {
let cprec = cmp >> 16;
let cop = low8((cmp >> 8));
let clen = low8(cmp);
if cprec < min_prec then pair else {
let rhs = comp(src, p + clen, len, no_left, cprec, nr + 1, env, elen);
let dr = nr + 2;
let rr = rhs >> 18;
let rp = low17(rhs);
if cop == 0xFF then {
emit_hex(instr(0x40, lr, rr, dr));
emit_hex(instr(0x49, dr, dr, 0))
} else {
emit_hex(instr(cop, lr, rr, dr))
};
comp(src, rp, len, dr + (1 << 39), min_prec, dr + 1, env, elen)
}
}
}
} else if is_alpha(c) then {
let ident = read_ident(src, p, len, 0);
let kw = ident >> 16;
let q = low16(ident);
if kw == 3075 then {
if 0 < min_prec then pair else {
let lb = bit17(pair);
remark("and");
if lb == 0 then {
emit_hex(instr(0x03, 7, 0, 0));
let tmp = nr + 1;
emit_hex(instr(0x40, lr, 7, tmp));
emit_hex(instr(0x49, tmp, tmp, 0));
remark("JmpF -> and_end");
emit_hex(instr(0x52, tmp, 0, 0))
} else {
remark("JmpF -> and_end");
emit_hex(instr(0x52, lr, 0, 0))
};
let rhs = comp(src, q, len, no_left, 0, nr + 1, env, elen);
let rr = rhs >> 18;
let rp = low17(rhs);
remark("and_end:");
comp(src, rp, len, rr, min_prec, rr + 1, env, elen)
}
} else if kw == 669 then {
if 0 < min_prec then pair else {
let lb = bit17(pair);
remark("or");
if lb == 0 then {
emit_hex(instr(0x03, 7, 0, 0));
let tmp = nr + 1;
emit_hex(instr(0x40, lr, 7, tmp));
emit_hex(instr(0x49, tmp, tmp, 0));
remark("JmpF -> or_right");
emit_hex(instr(0x52, tmp, 0, 0))
} else {
remark("JmpF -> or_right");
emit_hex(instr(0x52, lr, 0, 0))
};
let dr = nr + 2;
emit_hex(instr(0x04, dr, 0, 0));
remark("Jmp -> or_end");
emit_hex(instr(0x50, 0, 0, 0));
remark("or_right:");
let rhs = comp(src, q, len, no_left, 0, dr + 1, env, elen);
let rr = rhs >> 18;
let rp = low17(rhs);
remark("or_end:");
comp(src, rp, len, rr, min_prec, rr + 1, env, elen)
}
} else pair
} else {
let prec = prec_of(c);
if prec == 0 or prec < min_prec then pair else {
let opcode = if c == 43 then 0x20 else if c == 45 then 0x21
else if c == 42 then 0x22 else 0x23;
let rhs = comp(src, p + 1, len, no_left, prec, nr + 1, env, elen);
let rr = rhs >> 18;
let rp = low17(rhs);
let dr = nr + 2;
emit_hex(instr(opcode, lr, rr, dr));
comp(src, rp, len, dr, min_prec, dr + 1, env, elen)
}
}
}
}
fn compile(src: String) {
let len = perform String.length(src);
perform IO.print("; " + src);
let result = comp(src, 0, len, 1 << 40, -1, 8, "", 0);
let reg = result >> 18;
emit_hex(instr(0x12, reg, 0, 0));
emit_hex(instr(0x01, 0, 0, 0));
perform IO.print("; result in r" + perform Int.to_string(result >> 18))
}
fn main() {
let input = perform IO.read();
if input == "" then compile("1 < 2 and 2 < 3") else compile(input)
}