aver-lang 0.11.0

VM and transpiler for Aver, a statically-typed language designed for AI-assisted development
Documentation
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
#[allow(unused_imports)]
use crate::aver_generated::domain::ast::*;
#[allow(unused_imports)]
use crate::*;

/// Tag simple single-expression functions so eval can skip the stmt walker.
#[inline(always)]
pub fn annotateFastFns(
    mut fns: aver_rt::AverList<FnDef>,
    mut fnMap: aver_rt::AverMap<AverStr, i64>,
    mut acc: aver_rt::AverList<FnDef>,
) -> aver_rt::AverList<FnDef> {
    loop {
        crate::cancel_checkpoint();
        return aver_list_match!(fns, [] => acc.reverse(), [f, rest] => { {
            let __tmp1 = fnMap.clone();
            let __tmp2 = aver_rt::AverList::prepend(annotateFastFn(&f, &fnMap), &acc);
            fns = rest;
            fnMap = __tmp1;
            acc = __tmp2;
            continue;
        } });
    }
}

/// Attach a narrow fast-path tag to a function definition.
pub fn annotateFastFn(fd: &FnDef, fnMap: &aver_rt::AverMap<AverStr, i64>) -> FnDef {
    crate::cancel_checkpoint();
    let selfId = fnMap.get(&fd.name).cloned().unwrap_or((-1i64));
    FnDef {
        name: fd.name.clone(),
        params: fd.params.clone(),
        body: fd.body.clone(),
        slotCount: fd.slotCount,
        slotMap: fd.slotMap.clone(),
        fastPath: classifyFastPath(&fd.body),
        tailLoop: classifyTailLoop(selfId, fd.body.clone()),
    }
}

/// Precompute whether the final expression position can self-tail-call directly.
#[inline(always)]
pub fn classifyTailLoop(mut selfId: i64, mut body: aver_rt::AverList<Stmt>) -> bool {
    loop {
        crate::cancel_checkpoint();
        return aver_list_match!(body, [] => false, [stmt, rest] => { { let __list_subject = rest.clone(); if __list_subject.is_empty() { stmtNeedsTailLoop(selfId, &stmt) } else { {
            body = rest;
            continue;
        } } } });
    }
}

/// Only the final expression statement can trigger the tail-loop slot evaluator.
pub fn stmtNeedsTailLoop(selfId: i64, stmt: &Stmt) -> bool {
    crate::cancel_checkpoint();
    match stmt.clone() {
        Stmt::StmtExpr(expr) => exprNeedsTailLoop(selfId, expr),
        _ => false,
    }
}

/// Recognize direct self-calls in tail position, including bool branches and matches.
pub fn exprNeedsTailLoop(mut selfId: i64, mut expr: Expr) -> bool {
    loop {
        crate::cancel_checkpoint();
        return match expr {
            Expr::ExprCallDirect(fnId, _) => (fnId == selfId),
            Expr::ExprBoolBranch(_, thenExpr, elseExpr) => {
                let thenExpr = (*thenExpr).clone();
                let elseExpr = (*elseExpr).clone();
                if exprNeedsTailLoop(selfId, thenExpr) {
                    true
                } else {
                    {
                        expr = elseExpr;
                        continue;
                    }
                }
            }
            Expr::ExprMatch(_, arms) => armsNeedTailLoop(selfId, arms),
            _ => false,
        };
    }
}

/// Return true when any match arm ends in a direct self-tail-call.
#[inline(always)]
pub fn armsNeedTailLoop(mut selfId: i64, mut arms: aver_rt::AverList<MatchArm>) -> bool {
    loop {
        crate::cancel_checkpoint();
        return aver_list_match!(arms, [] => false, [arm, rest] => { if exprNeedsTailLoop(selfId, arm.body.clone()) { true } else { {
            arms = rest;
            continue;
        } } });
    }
}

/// Single expression bodies can bypass evalStmts* and evaluate the expr directly.
pub fn classifyFastPath(body: &aver_rt::AverList<Stmt>) -> FnFastPath {
    crate::cancel_checkpoint();
    {
        let __list_subject = body.clone();
        if let Some((stmt, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            if (rest == aver_rt::AverList::empty()) {
                classifyFastStmt(&stmt)
            } else {
                FnFastPath::FastNone.clone()
            }
        } else {
            FnFastPath::FastNone.clone()
        }
    }
}

/// Only plain expression bodies get a fast expr tag.
pub fn classifyFastStmt(stmt: &Stmt) -> FnFastPath {
    crate::cancel_checkpoint();
    match stmt.clone() {
        Stmt::StmtExpr(expr) => classifyFastExpr(&expr),
        _ => FnFastPath::FastNone.clone(),
    }
}

/// Recognize a few leaf-like and branch-like expr shapes worth running without the full stmt walker.
#[inline(always)]
pub fn classifyFastExpr(expr: &Expr) -> FnFastPath {
    crate::cancel_checkpoint();
    match classifyFastLeafExpr(expr) {
        Some(leaf) => FnFastPath::FastLeaf(leaf),
        None => match expr.clone() {
            Expr::ExprCallDirect(fnId, args) => classifyFastForwardCall(fnId, &args),
            Expr::ExprMatch(scrutinee, arms) => {
                let scrutinee = (*scrutinee).clone();
                classifyFastMatch(&scrutinee, &arms)
            }
            _ => FnFastPath::FastSingleExpr.clone(),
        },
    }
}

/// Recognize a leaf-like expr that can be executed without descending the AST.
pub fn classifyFastLeafExpr(expr: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match expr.clone() {
        Expr::ExprInt(n) => Some(FastLeaf::LeafConstInt(n)),
        Expr::ExprFloat(f) => Some(FastLeaf::LeafConstFloat(f)),
        Expr::ExprStr(s) => Some(FastLeaf::LeafConstStr(s)),
        Expr::ExprBool(b) => Some(FastLeaf::LeafConstBool(b)),
        Expr::ExprSlot(slot) => Some(FastLeaf::LeafSlot(slot)),
        Expr::ExprFieldAccess(obj, field) => {
            let obj = (*obj).clone();
            classifyFastFieldAccess(&obj, field)
        }
        Expr::ExprCallBuiltin(name, args) => classifyFastBuiltinLeaf(name, &args),
        Expr::ExprAdd(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastBinopSlots(&BinOp::OpAdd, &a, &b)
        }
        Expr::ExprSub(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastBinopSlots(&BinOp::OpSub, &a, &b)
        }
        _ => classifyFastLeafExprTail(expr),
    }
}

/// Finish fast-leaf classification for arithmetic and comparison forms.
pub fn classifyFastLeafExprTail(expr: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match expr.clone() {
        Expr::ExprMul(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastBinopSlots(&BinOp::OpMul, &a, &b)
        }
        Expr::ExprDiv(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastBinopSlots(&BinOp::OpDiv, &a, &b)
        }
        Expr::ExprEq(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastCmpSlots(&CmpOp::CmpEq, &a, &b)
        }
        Expr::ExprNeq(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastCmpSlots(&CmpOp::CmpNeq, &a, &b)
        }
        Expr::ExprLt(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastCmpSlots(&CmpOp::CmpLt, &a, &b)
        }
        Expr::ExprGt(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastCmpSlots(&CmpOp::CmpGt, &a, &b)
        }
        Expr::ExprLte(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastCmpSlots(&CmpOp::CmpLte, &a, &b)
        }
        Expr::ExprGte(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastCmpSlots(&CmpOp::CmpGte, &a, &b)
        }
        _ => None,
    }
}

/// Recognize obj.field when obj is already a resolved slot.
pub fn classifyFastFieldAccess(obj: &Expr, field: AverStr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match obj.clone() {
        Expr::ExprSlot(slot) => Some(FastLeaf::LeafFieldAccess(slot, field)),
        _ => None,
    }
}

/// Recognize slot op slot for arithmetic.
pub fn classifyFastBinopSlots(op: &BinOp, a: &Expr, b: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match a.clone() {
        Expr::ExprSlot(sa) => match b.clone() {
            Expr::ExprSlot(sb) => Some(FastLeaf::LeafBinopSlots(op.clone(), sa, sb)),
            _ => None,
        },
        _ => None,
    }
}

/// Recognize slot cmp slot for comparisons.
pub fn classifyFastCmpSlots(op: &CmpOp, a: &Expr, b: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match a.clone() {
        Expr::ExprSlot(sa) => match b.clone() {
            Expr::ExprSlot(sb) => Some(FastLeaf::LeafCmpSlots(op.clone(), sa, sb)),
            _ => None,
        },
        _ => None,
    }
}

/// Recognize tiny builtin wrappers that only shuffle slots and integer constants.
#[inline(always)]
pub fn classifyFastBuiltinLeaf(name: AverStr, args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __dispatch_subject = name;
        if &*__dispatch_subject == "Vector.new" {
            classifyFastVectorNew(args)
        } else {
            if &*__dispatch_subject == "Vector.len" {
                classifyFastVectorLen(args)
            } else {
                if &*__dispatch_subject == "Option.withDefault" {
                    classifyFastOptionWithDefault(args)
                } else {
                    if &*__dispatch_subject == "Map.get" {
                        classifyFastMapGet(args)
                    } else {
                        if &*__dispatch_subject == "Map.set" {
                            classifyFastMapSet(args)
                        } else {
                            if &*__dispatch_subject == "Map.has" {
                                classifyFastMapHas(args)
                            } else {
                                if &*__dispatch_subject == "Map.remove" {
                                    classifyFastMapRemove(args)
                                } else {
                                    None
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

/// Recognize Map.get(slotMap, slotKey).
pub fn classifyFastMapGet(args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((mapExpr, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((keyExpr, ignored)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    classifyFastMapGetArgs(&mapExpr, &keyExpr)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Encode a direct map lookup when both operands are resolved slots.
pub fn classifyFastMapGetArgs(mapExpr: &Expr, keyExpr: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match mapExpr.clone() {
        Expr::ExprSlot(mapSlot) => match keyExpr.clone() {
            Expr::ExprSlot(keySlot) => Some(FastLeaf::LeafMapGet(mapSlot, keySlot)),
            _ => None,
        },
        _ => None,
    }
}

/// Recognize Map.set(slotMap, slotKey, slotValue).
pub fn classifyFastMapSet(args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((mapExpr, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((keyExpr, rest2)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    {
                        let __list_subject = rest2;
                        if let Some((valueExpr, ignored)) =
                            aver_rt::list_uncons_cloned(&__list_subject)
                        {
                            classifyFastMapSetArgs(&mapExpr, &keyExpr, &valueExpr)
                        } else {
                            None
                        }
                    }
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Encode a direct map update when all operands are resolved slots.
pub fn classifyFastMapSetArgs(
    mapExpr: &Expr,
    keyExpr: &Expr,
    valueExpr: &Expr,
) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match mapExpr.clone() {
        Expr::ExprSlot(mapSlot) => match keyExpr.clone() {
            Expr::ExprSlot(keySlot) => match valueExpr.clone() {
                Expr::ExprSlot(valueSlot) => {
                    Some(FastLeaf::LeafMapSet(mapSlot, keySlot, valueSlot))
                }
                _ => None,
            },
            _ => None,
        },
        _ => None,
    }
}

/// Recognize Map.has(slotMap, slotKey).
pub fn classifyFastMapHas(args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((mapExpr, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((keyExpr, ignored)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    classifyFastMapHasArgs(&mapExpr, &keyExpr)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Encode a direct map membership test when both operands are resolved slots.
pub fn classifyFastMapHasArgs(mapExpr: &Expr, keyExpr: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match mapExpr.clone() {
        Expr::ExprSlot(mapSlot) => match keyExpr.clone() {
            Expr::ExprSlot(keySlot) => Some(FastLeaf::LeafMapHas(mapSlot, keySlot)),
            _ => None,
        },
        _ => None,
    }
}

/// Recognize Map.remove(slotMap, slotKey).
pub fn classifyFastMapRemove(args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((mapExpr, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((keyExpr, ignored)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    classifyFastMapRemoveArgs(&mapExpr, &keyExpr)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Encode a direct map key removal when both operands are resolved slots.
pub fn classifyFastMapRemoveArgs(mapExpr: &Expr, keyExpr: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match mapExpr.clone() {
        Expr::ExprSlot(mapSlot) => match keyExpr.clone() {
            Expr::ExprSlot(keySlot) => Some(FastLeaf::LeafMapRemove(mapSlot, keySlot)),
            _ => None,
        },
        _ => None,
    }
}

/// Recognize Vector.len(slotVec).
pub fn classifyFastVectorLen(args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((vecExpr, ignored)) = aver_rt::list_uncons_cloned(&__list_subject) {
            match vecExpr {
                Expr::ExprSlot(slot) => Some(FastLeaf::LeafVectorLen(slot)),
                _ => None,
            }
        } else {
            None
        }
    }
}

/// Recognize Vector.new(slot, int) wrappers.
pub fn classifyFastVectorNew(args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((sizeExpr, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((fillExpr, ignored)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    classifyFastVectorNewArgs(&sizeExpr, &fillExpr)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Encode Vector.new(slot, int) without keeping the whole AST around.
pub fn classifyFastVectorNewArgs(sizeExpr: &Expr, fillExpr: &Expr) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match sizeExpr.clone() {
        Expr::ExprSlot(sizeSlot) => match fillExpr.clone() {
            Expr::ExprInt(fill) => Some(FastLeaf::LeafVectorNew(sizeSlot, fill)),
            _ => None,
        },
        _ => None,
    }
}

/// Recognize Option.withDefault(Vector.get(slot, slot), int).
pub fn classifyFastOptionWithDefault(args: &aver_rt::AverList<Expr>) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((optionExpr, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((defaultExpr, ignored)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    classifyFastOptionWithDefaultArgs(&optionExpr, &defaultExpr)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Encode the common Vector.get-with-default wrapper used in tiny helpers like cellAt.
pub fn classifyFastOptionWithDefaultArgs(
    optionExpr: &Expr,
    defaultExpr: &Expr,
) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match optionExpr.clone() {
        Expr::ExprCallBuiltin(name, innerArgs) => {
            if (name == AverStr::from("Vector.get")) {
                classifyFastVectorGet(&innerArgs, defaultExpr)
            } else {
                None
            }
        }
        _ => None,
    }
}

/// Recognize Vector.get(slotVec, slotIdx) with an integer default.
pub fn classifyFastVectorGet(
    args: &aver_rt::AverList<Expr>,
    defaultExpr: &Expr,
) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    {
        let __list_subject = args.clone();
        if let Some((vecExpr, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((idxExpr, ignored)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    classifyFastVectorGetArgs(&vecExpr, &idxExpr, defaultExpr)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Encode Vector.get(slotVec, slotIdx) with an integer fallback.
pub fn classifyFastVectorGetArgs(
    vecExpr: &Expr,
    idxExpr: &Expr,
    defaultExpr: &Expr,
) -> Option<FastLeaf> {
    crate::cancel_checkpoint();
    match vecExpr.clone() {
        Expr::ExprSlot(vecSlot) => match idxExpr.clone() {
            Expr::ExprSlot(idxSlot) => match defaultExpr.clone() {
                Expr::ExprInt(defaultValue) => {
                    Some(FastLeaf::LeafVectorGetOrInt(vecSlot, idxSlot, defaultValue))
                }
                _ => None,
            },
            _ => None,
        },
        _ => None,
    }
}

/// Recognize a small fixed-shape branch over bools or slot-based comparisons.
#[inline(always)]
pub fn classifyFastMatch(scrutinee: &Expr, arms: &aver_rt::AverList<MatchArm>) -> FnFastPath {
    crate::cancel_checkpoint();
    match classifyBoolArms(arms) {
        Some(pair) => {
            let (thenLeaf, elseLeaf) = pair;
            classifyFastMatchScrutinee(scrutinee, &thenLeaf, &elseLeaf)
        }
        None => classifyFastListMatch(scrutinee, arms),
    }
}

/// Recognize a two-arm list match with [] and [h, ..t] leaf bodies.
pub fn classifyFastListMatch(scrutinee: &Expr, arms: &aver_rt::AverList<MatchArm>) -> FnFastPath {
    crate::cancel_checkpoint();
    match scrutinee.clone() {
        Expr::ExprSlot(slot) => classifyFastListArms(slot, arms),
        _ => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Extract fixed empty/cons list arms regardless of order.
pub fn classifyFastListArms(slot: i64, arms: &aver_rt::AverList<MatchArm>) -> FnFastPath {
    crate::cancel_checkpoint();
    {
        let __list_subject = arms.clone();
        if let Some((arm1, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((arm2, tail)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    if (tail == aver_rt::AverList::empty()) {
                        classifyFastListArmPair(slot, &arm1, &arm2)
                    } else {
                        FnFastPath::FastSingleExpr.clone()
                    }
                } else {
                    FnFastPath::FastSingleExpr.clone()
                }
            }
        } else {
            FnFastPath::FastSingleExpr.clone()
        }
    }
}

/// Convert two list-pattern arms into a direct slot branch.
#[inline(always)]
pub fn classifyFastListArmPair(slot: i64, arm1: &MatchArm, arm2: &MatchArm) -> FnFastPath {
    crate::cancel_checkpoint();
    let leaf1 = classifyFastLeafExpr(&arm1.body);
    let leaf2 = classifyFastLeafExpr(&arm2.body);
    match leaf1 {
        Some(v1) => match leaf2 {
            Some(v2) => classifyFastListPatterns(
                slot,
                &arm1.pattern,
                &arm1.bindingSlots,
                &v1,
                &arm2.pattern,
                &arm2.bindingSlots,
                &v2,
            ),
            None => FnFastPath::FastSingleExpr.clone(),
        },
        None => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Order empty/cons arms into a direct list branch.
pub fn classifyFastListPatterns(
    slot: i64,
    p1: &Pattern,
    bindingSlots1: &aver_rt::AverMap<AverStr, i64>,
    leaf1: &FastLeaf,
    p2: &Pattern,
    bindingSlots2: &aver_rt::AverMap<AverStr, i64>,
    leaf2: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match p1.clone() {
        Pattern::PatEmpty => classifyFastListOther(slot, leaf1, p2, bindingSlots2, leaf2),
        Pattern::PatCons(head, tail) => {
            classifyFastListConsFirst(slot, head, tail, bindingSlots1, leaf1, p2, leaf2)
        }
        _ => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Finish list fast-path classification when the empty arm is known.
pub fn classifyFastListOther(
    slot: i64,
    emptyLeaf: &FastLeaf,
    other: &Pattern,
    bindingSlots: &aver_rt::AverMap<AverStr, i64>,
    otherLeaf: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match other.clone() {
        Pattern::PatCons(head, tail) => {
            classifyFastListCons(slot, emptyLeaf, head, tail, bindingSlots, otherLeaf)
        }
        _ => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Handle reversed list arms where the cons case appears first.
pub fn classifyFastListConsFirst(
    slot: i64,
    head: AverStr,
    tail: AverStr,
    bindingSlots: &aver_rt::AverMap<AverStr, i64>,
    consLeaf: &FastLeaf,
    other: &Pattern,
    otherLeaf: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match other {
        Pattern::PatEmpty => {
            classifyFastListCons(slot, otherLeaf, head, tail, bindingSlots, consLeaf)
        }
        _ => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Build the list-slot branch once head/tail binding slots are known.
#[inline(always)]
pub fn classifyFastListCons(
    slot: i64,
    emptyLeaf: &FastLeaf,
    head: AverStr,
    tail: AverStr,
    bindingSlots: &aver_rt::AverMap<AverStr, i64>,
    consLeaf: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match bindingSlots.get(&head).cloned() {
        Some(headSlot) => match bindingSlots.get(&tail).cloned() {
            Some(tailSlot) => FnFastPath::FastListSlotBranch(
                slot,
                emptyLeaf.clone(),
                headSlot,
                tailSlot,
                consLeaf.clone(),
            ),
            None => FnFastPath::FastSingleExpr.clone(),
        },
        None => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Recognize tiny direct-call wrappers that only forward slot arguments.
#[inline(always)]
pub fn classifyFastForwardCall(fnId: i64, args: &aver_rt::AverList<Expr>) -> FnFastPath {
    crate::cancel_checkpoint();
    match classifyFastForwardSlots(args.clone(), aver_rt::AverList::empty()) {
        Some(slotArgs) => FnFastPath::FastForwardCall(fnId, slotArgs),
        None => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Extract slot numbers from a direct call argument list.
#[inline(always)]
pub fn classifyFastForwardSlots(
    mut args: aver_rt::AverList<Expr>,
    mut acc: aver_rt::AverList<i64>,
) -> Option<aver_rt::AverList<i64>> {
    loop {
        crate::cancel_checkpoint();
        return aver_list_match!(args, [] => Some(acc.reverse()), [arg, rest] => { match arg {
            Expr::ExprSlot(slot) => {
            let __tmp1 = aver_rt::AverList::prepend(slot, &acc);
            args = rest;
            acc = __tmp1;
            continue;
        },
            _ => None
        } });
    }
}

/// Extract (then, else) leaves from a two-arm bool match, regardless of arm order.
pub fn classifyBoolArms(arms: &aver_rt::AverList<MatchArm>) -> Option<(FastLeaf, FastLeaf)> {
    crate::cancel_checkpoint();
    {
        let __list_subject = arms.clone();
        if let Some((arm1, rest)) = aver_rt::list_uncons_cloned(&__list_subject) {
            {
                let __list_subject = rest;
                if let Some((arm2, tail)) = aver_rt::list_uncons_cloned(&__list_subject) {
                    if (tail == aver_rt::AverList::empty()) {
                        classifyBoolArmPair(&arm1, &arm2)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
        } else {
            None
        }
    }
}

/// Convert two bool-pattern arms into ordered leaves.
#[inline(always)]
pub fn classifyBoolArmPair(arm1: &MatchArm, arm2: &MatchArm) -> Option<(FastLeaf, FastLeaf)> {
    crate::cancel_checkpoint();
    let leaf1 = classifyFastLeafExpr(&arm1.body);
    let leaf2 = classifyFastLeafExpr(&arm2.body);
    match leaf1 {
        Some(v1) => match leaf2 {
            Some(v2) => classifyBoolArmPatterns(&arm1.pattern, &v1, &arm2.pattern, &v2),
            None => None,
        },
        None => None,
    }
}

/// Order bool match arms into (trueLeaf, falseLeaf).
pub fn classifyBoolArmPatterns(
    p1: &Pattern,
    leaf1: &FastLeaf,
    p2: &Pattern,
    leaf2: &FastLeaf,
) -> Option<(FastLeaf, FastLeaf)> {
    crate::cancel_checkpoint();
    match p1.clone() {
        Pattern::PatBool(b1) => classifyBoolArmPatternsInner(b1, leaf1, p2, leaf2),
        _ => None,
    }
}

/// Finish ordering bool match arms once the first arm bool is extracted.
pub fn classifyBoolArmPatternsInner(
    b1: bool,
    leaf1: &FastLeaf,
    p2: &Pattern,
    leaf2: &FastLeaf,
) -> Option<(FastLeaf, FastLeaf)> {
    crate::cancel_checkpoint();
    match p2.clone() {
        Pattern::PatBool(b2) => classifyBoolArmPatternsPair(b1, leaf1, b2, leaf2),
        _ => None,
    }
}

/// Return (trueLeaf, falseLeaf) when the two bool arms are complementary.
pub fn classifyBoolArmPatternsPair(
    b1: bool,
    leaf1: &FastLeaf,
    b2: bool,
    leaf2: &FastLeaf,
) -> Option<(FastLeaf, FastLeaf)> {
    crate::cancel_checkpoint();
    match (b1, b2) {
        (true, false) => Some((leaf1.clone(), leaf2.clone())),
        (false, true) => Some((leaf2.clone(), leaf1.clone())),
        _ => None,
    }
}

/// Encode a recognized branch scrutinee with preclassified leaves.
pub fn classifyFastMatchScrutinee(
    scrutinee: &Expr,
    thenLeaf: &FastLeaf,
    elseLeaf: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match scrutinee.clone() {
        Expr::ExprSlot(slot) => {
            FnFastPath::FastBoolSlotBranch(slot, thenLeaf.clone(), elseLeaf.clone())
        }
        Expr::ExprEq(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastEqScrutinee(&a, &b, thenLeaf, elseLeaf)
        }
        Expr::ExprLt(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastLtScrutinee(&a, &b, thenLeaf, elseLeaf)
        }
        Expr::ExprGt(a, b) => {
            let a = (*a).clone();
            let b = (*b).clone();
            classifyFastLtScrutinee(&b, &a, thenLeaf, elseLeaf)
        }
        _ => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Recognize slot == int/string in either operand order.
pub fn classifyFastEqScrutinee(
    a: &Expr,
    b: &Expr,
    thenLeaf: &FastLeaf,
    elseLeaf: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match a.clone() {
        Expr::ExprSlot(slot) => classifyFastEqOther(slot, b, thenLeaf, elseLeaf),
        _ => match b.clone() {
            Expr::ExprSlot(slot) => classifyFastEqOther(slot, a, thenLeaf, elseLeaf),
            _ => FnFastPath::FastSingleExpr.clone(),
        },
    }
}

/// Encode equality against a constant once the slot side is known.
pub fn classifyFastEqOther(
    slot: i64,
    other: &Expr,
    thenLeaf: &FastLeaf,
    elseLeaf: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match other.clone() {
        Expr::ExprInt(n) => {
            FnFastPath::FastEqIntBranch(slot, n, thenLeaf.clone(), elseLeaf.clone())
        }
        Expr::ExprStr(s) => {
            FnFastPath::FastEqStringBranch(slot, s, thenLeaf.clone(), elseLeaf.clone())
        }
        _ => FnFastPath::FastSingleExpr.clone(),
    }
}

/// Recognize slot < slot branches like minInt.
pub fn classifyFastLtScrutinee(
    a: &Expr,
    b: &Expr,
    thenLeaf: &FastLeaf,
    elseLeaf: &FastLeaf,
) -> FnFastPath {
    crate::cancel_checkpoint();
    match a.clone() {
        Expr::ExprSlot(lhs) => match b.clone() {
            Expr::ExprSlot(rhs) => {
                FnFastPath::FastLtIntSlotsBranch(lhs, rhs, thenLeaf.clone(), elseLeaf.clone())
            }
            _ => FnFastPath::FastSingleExpr.clone(),
        },
        _ => FnFastPath::FastSingleExpr.clone(),
    }
}