grift_parser 1.4.0

Lisp parser for the Grift Scheme language
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
use grift_parser::*;

// ========================================================================
// Reserved Slots Tests
// ========================================================================

#[test]
fn test_reserved_slots_are_singletons() {
    let lisp: Lisp<100> = Lisp::new();
    
    // Multiple calls to nil() should return the same index
    let nil1 = lisp.nil().unwrap();
    let nil2 = lisp.nil().unwrap();
    let nil3 = lisp.nil().unwrap();
    assert_eq!(nil1, nil2);
    assert_eq!(nil2, nil3);
    
    // Multiple calls to true_val() should return the same index
    let true1 = lisp.true_val().unwrap();
    let true2 = lisp.true_val().unwrap();
    let true3 = lisp.true_val().unwrap();
    assert_eq!(true1, true2);
    assert_eq!(true2, true3);
    
    // Multiple calls to false_val() should return the same index
    let false1 = lisp.false_val().unwrap();
    let false2 = lisp.false_val().unwrap();
    let false3 = lisp.false_val().unwrap();
    assert_eq!(false1, false2);
    assert_eq!(false2, false3);
    
    // Each singleton should be different
    assert_ne!(nil1, true1);
    assert_ne!(nil1, false1);
    assert_ne!(true1, false1);
}

#[test]
fn test_reserved_slots_have_correct_values() {
    let lisp: Lisp<100> = Lisp::new();
    
    // Verify the values in reserved slots
    assert_eq!(lisp.get(lisp.nil().unwrap()).unwrap(), Value::Nil);
    assert_eq!(lisp.get(lisp.true_val().unwrap()).unwrap(), Value::True);
    assert_eq!(lisp.get(lisp.false_val().unwrap()).unwrap(), Value::False);
}

#[test]
fn test_reserved_slots_occupy_first_slots() {
    let lisp: Lisp<100> = Lisp::new();
    
    // Nil, Void, True, and False are pre-allocated in the first slots
    assert_eq!(lisp.nil().unwrap().raw(), 0);
    assert_eq!(lisp.void_val().unwrap().raw(), 1);
    assert_eq!(lisp.true_val().unwrap().raw(), 2);
    assert_eq!(lisp.false_val().unwrap().raw(), 3);
    
    // Verify they contain the expected values
    assert_eq!(lisp.get(lisp.nil().unwrap()).unwrap(), Value::Nil);
    assert_eq!(lisp.get(lisp.void_val().unwrap()).unwrap(), Value::Void);
    assert_eq!(lisp.get(lisp.true_val().unwrap()).unwrap(), Value::True);
    assert_eq!(lisp.get(lisp.false_val().unwrap()).unwrap(), Value::False);
}

#[test]
fn test_reserved_slots_not_reallocated() {
    let lisp: Lisp<100> = Lisp::new();
    
    // After creating the Lisp context, 6 slots should be used
    // (nil, void, true, false, intern_table_cons, string_intern_table_cons)
    assert_eq!(lisp.arena().len(), 6);
    
    // Calling nil/void_val/true_val/false_val should NOT increase allocation count
    // (they return pre-allocated slots)
    let _ = lisp.nil();
    let _ = lisp.void_val();
    let _ = lisp.true_val();
    let _ = lisp.false_val();
    assert_eq!(lisp.arena().len(), 6);
    
    // Calling many times should not increase count
    for _ in 0..100 {
        let _ = lisp.nil();
        let _ = lisp.void_val();
        let _ = lisp.true_val();
        let _ = lisp.false_val();
    }
    assert_eq!(lisp.arena().len(), 6);
}

#[test]
fn test_boolean_uses_reserved_slots() {
    let lisp: Lisp<100> = Lisp::new();
    
    // boolean() should use the reserved slots
    let b_true = lisp.boolean(true).unwrap();
    let b_false = lisp.boolean(false).unwrap();
    
    assert_eq!(b_true, lisp.true_val().unwrap());
    assert_eq!(b_false, lisp.false_val().unwrap());
}

#[test]
fn test_reserved_slots_survive_gc() {
    let lisp: Lisp<100> = Lisp::new();
    
    let nil = lisp.nil().unwrap();
    let void_val = lisp.void_val().unwrap();
    let true_val = lisp.true_val().unwrap();
    let false_val = lisp.false_val().unwrap();
    
    // Allocate some garbage
    let _ = lisp.number(1);
    let _ = lisp.number(2);
    let _ = lisp.number(3);
    
    // Run GC with empty roots - reserved slots should NOT be collected
    // because they're implicitly roots
    let stats = lisp.gc(&[nil, void_val, true_val, false_val]);
    
    // The numbers should be collected
    assert_eq!(stats.collected, 3);
    
    // Reserved slots should still be valid
    assert_eq!(lisp.get(nil).unwrap(), Value::Nil);
    assert_eq!(lisp.get(void_val).unwrap(), Value::Void);
    assert_eq!(lisp.get(true_val).unwrap(), Value::True);
    assert_eq!(lisp.get(false_val).unwrap(), Value::False);
}

#[test]
fn test_regular_allocation_starts_after_reserved_slots() {
    let lisp: Lisp<100> = Lisp::new();
    
    // First regular allocation should be at slot 6 (after reserved 0-5)
    // Slots: 0=nil, 1=void, 2=true, 3=false, 4=intern_table_cons, 5=string_intern_table_cons
    let num = lisp.number(42).unwrap();
    assert_eq!(num.raw(), 6);
    
    // Next allocations continue from there
    let num2 = lisp.number(43).unwrap();
    assert_eq!(num2.raw(), 7);
}

// ========================================================================
// Parser Tests
// ========================================================================

#[test]
fn test_parse_number() {
    let lisp: Lisp<100> = Lisp::new();
    
    let idx = parse(&lisp, "42").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Number(42));
    
    let idx = parse(&lisp, "-123").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Number(-123));
}

#[test]
fn test_parse_symbol() {
    let lisp: Lisp<100> = Lisp::new();
    
    let idx = parse(&lisp, "hello").unwrap();
    assert!(lisp.symbol_matches(idx, "hello").unwrap());
}

#[test]
fn test_parse_empty_list() {
    let lisp: Lisp<100> = Lisp::new();
    
    // () is the empty list
    let idx = parse(&lisp, "()").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Nil);
    
    // 'nil' is now just a regular symbol in Scheme, not the empty list
    let idx = parse(&lisp, "nil").unwrap();
    assert!(lisp.get(idx).unwrap().is_symbol());
}

#[test]
fn test_parse_booleans() {
    let lisp: Lisp<100> = Lisp::new();
    
    let idx = parse(&lisp, "#t").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::True);
    
    let idx = parse(&lisp, "#f").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::False);
    
    let idx = parse(&lisp, "#T").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::True);
    
    let idx = parse(&lisp, "#F").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::False);
}

#[test]
fn test_parse_list() {
    let lisp: Lisp<100> = Lisp::new();
    
    let idx = parse(&lisp, "(1 2 3)").unwrap();
    
    // Check it's a cons
    let val = lisp.get(idx).unwrap();
    assert!(val.is_cons());
    
    // Check first element
    let car = lisp.car(idx).unwrap();
    assert_eq!(lisp.get(car).unwrap(), Value::Number(1));
}

#[test]
fn test_parse_nested() {
    let lisp: Lisp<100> = Lisp::new();
    
    let idx = parse(&lisp, "(+ 1 (- 3 2))").unwrap();
    assert!(lisp.get(idx).unwrap().is_cons());
}

#[test]
fn test_parse_quote() {
    let lisp: Lisp<100> = Lisp::new();
    
    let idx = parse(&lisp, "'x").unwrap();
    
    // Should be (quote x)
    let car = lisp.car(idx).unwrap();
    assert!(lisp.symbol_matches(car, "quote").unwrap());
}

#[test]
fn test_symbol_equality() {
    let lisp: Lisp<100> = Lisp::new();
    
    let a = lisp.symbol("hello").unwrap();
    let b = lisp.symbol("hello").unwrap();
    let c = lisp.symbol("world").unwrap();
    
    assert!(lisp.symbol_eq(a, b).unwrap());
    assert!(!lisp.symbol_eq(a, c).unwrap());
}

// NOTE: test_set_car_cdr removed - this is a PURE Lisp!

#[test]
fn test_gc() {
    let lisp: Lisp<100> = Lisp::new();
    
    let root = parse(&lisp, "(1 2 3)").unwrap();
    
    // Allocate garbage
    for i in 0..20 {
        lisp.number(i * 1000).unwrap();
    }
    
    let stats = lisp.gc(&[root]);
    assert!(stats.collected > 0);
}

// ========================================================================
// Contiguous String Tests
// ========================================================================

#[test]
fn test_string_basic() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello = lisp.string("hello").unwrap();
    
    assert_eq!(lisp.string_len(hello).unwrap(), 5);
    assert_eq!(lisp.string_char_at(hello, 0).unwrap(), 'h');
    assert_eq!(lisp.string_char_at(hello, 1).unwrap(), 'e');
    assert_eq!(lisp.string_char_at(hello, 2).unwrap(), 'l');
    assert_eq!(lisp.string_char_at(hello, 3).unwrap(), 'l');
    assert_eq!(lisp.string_char_at(hello, 4).unwrap(), 'o');
}

#[test]
fn test_string_empty() {
    let lisp: Lisp<100> = Lisp::new();
    
    let empty = lisp.string("").unwrap();
    
    assert_eq!(lisp.string_len(empty).unwrap(), 0);
    // Accessing index 0 on empty string should fail
    assert!(lisp.string_char_at(empty, 0).is_err());
}

#[test]
fn test_string_single_char() {
    let lisp: Lisp<100> = Lisp::new();
    
    let single = lisp.string("x").unwrap();
    
    assert_eq!(lisp.string_len(single).unwrap(), 1);
    assert_eq!(lisp.string_char_at(single, 0).unwrap(), 'x');
    assert!(lisp.string_char_at(single, 1).is_err());
}

#[test]
fn test_string_unicode() {
    let lisp: Lisp<100> = Lisp::new();
    
    // Unicode string: "héllo" (with accent)
    let unicode = lisp.string("héllo").unwrap();
    
    assert_eq!(lisp.string_len(unicode).unwrap(), 5);
    assert_eq!(lisp.string_char_at(unicode, 0).unwrap(), 'h');
    assert_eq!(lisp.string_char_at(unicode, 1).unwrap(), 'é');
    assert_eq!(lisp.string_char_at(unicode, 2).unwrap(), 'l');
}

#[test]
fn test_string_matches() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello = lisp.string("hello").unwrap();
    
    assert!(lisp.string_matches(hello, "hello").unwrap());
    assert!(!lisp.string_matches(hello, "Hello").unwrap());
    assert!(!lisp.string_matches(hello, "hello!").unwrap());
    assert!(!lisp.string_matches(hello, "hell").unwrap());
    assert!(!lisp.string_matches(hello, "").unwrap());
}

#[test]
fn test_string_eq_contiguous() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello1 = lisp.string("hello").unwrap();
    let hello2 = lisp.string("hello").unwrap();
    let world = lisp.string("world").unwrap();
    
    // Same content
    assert!(lisp.string_eq_contiguous(hello1, hello2).unwrap());
    
    // Same index
    assert!(lisp.string_eq_contiguous(hello1, hello1).unwrap());
    
    // Different content
    assert!(!lisp.string_eq_contiguous(hello1, world).unwrap());
}

#[test]
fn test_string_to_bytes() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello = lisp.string("hello").unwrap();
    let mut buf = [0u8; 10];
    
    let len = lisp.string_to_bytes(hello, &mut buf).unwrap();
    
    assert_eq!(len, 5);
    assert_eq!(&buf[..5], b"hello");
}

#[test]
fn test_string_to_bytes_truncated() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello = lisp.string("hello").unwrap();
    let mut buf = [0u8; 3]; // Too small
    
    let len = lisp.string_to_bytes(hello, &mut buf).unwrap();
    
    assert_eq!(len, 3);
    assert_eq!(&buf[..3], b"hel");
}

#[test]
fn test_string_to_bytes_skips_non_ascii() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // "hé" has 2 chars: 'h' (ASCII) and 'é' (non-ASCII)
    let mixed = lisp.string("héllo").unwrap();
    let mut buf = [0u8; 10];
    
    let len = lisp.string_to_bytes(mixed, &mut buf).unwrap();
    
    // Only ASCII chars are copied, 'é' is skipped
    assert_eq!(len, 4); // h, l, l, o
    assert_eq!(&buf[..4], b"hllo");
}

#[test]
fn test_string_free() {
    let lisp: Lisp<100> = Lisp::new();
    
    let initial_allocated = lisp.stats().allocated;
    
    let hello = lisp.string("hello").unwrap();
    let after_alloc = lisp.stats().allocated;
    
    // With inline length: 5 chars + 1 String value = 6 slots (no length header)
    assert_eq!(after_alloc - initial_allocated, 6);
    
    lisp.string_free(hello).unwrap();
    let after_free = lisp.stats().allocated;
    
    // Should be back to initial
    assert_eq!(after_free, initial_allocated);
}

#[test]
fn test_string_multiple() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let s1 = lisp.string("foo").unwrap();
    let s2 = lisp.string("bar").unwrap();
    let s3 = lisp.string("baz").unwrap();
    
    // All strings should be independent
    assert!(lisp.string_matches(s1, "foo").unwrap());
    assert!(lisp.string_matches(s2, "bar").unwrap());
    assert!(lisp.string_matches(s3, "baz").unwrap());
    
    // Free one, others should still work
    lisp.string_free(s2).unwrap();
    
    assert!(lisp.string_matches(s1, "foo").unwrap());
    assert!(lisp.string_matches(s3, "baz").unwrap());
}

#[test]
fn test_string_memory_layout() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello = lisp.string("hello").unwrap();
    
    // String value should be Value::String { len, data }
    // With inline length and layout: data[0..] = Char values (no length header)
    match lisp.get(hello).unwrap() {
        Value::String { len, data } => {
            // Check inline length
            assert_eq!(len, 5);
            
            // Data slots should contain Char values starting at data (no header)
            let idx0 = lisp.arena().index_at_offset(data, 0).unwrap();
            let idx1 = lisp.arena().index_at_offset(data, 1).unwrap();
            
            assert_eq!(lisp.get(idx0).unwrap(), Value::Char('h'));
            assert_eq!(lisp.get(idx1).unwrap(), Value::Char('e'));
        }
        _ => panic!("Expected Value::String"),
    }
}

#[test]
fn test_string_char_out_of_bounds() {
    let lisp: Lisp<100> = Lisp::new();
    
    let hello = lisp.string("hi").unwrap();
    
    assert!(lisp.string_char_at(hello, 0).is_ok());
    assert!(lisp.string_char_at(hello, 1).is_ok());
    assert!(lisp.string_char_at(hello, 2).is_err());
    assert!(lisp.string_char_at(hello, 100).is_err());
}

// ========================================================================
// Mutation Tests (set_car, set_cdr)
// ========================================================================

#[test]
fn test_set_car() {
    let lisp: Lisp<100> = Lisp::new();
    
    let a = lisp.number(1).unwrap();
    let b = lisp.number(2).unwrap();
    let c = lisp.number(3).unwrap();
    
    let pair = lisp.cons(a, b).unwrap();
    
    // Initially car is a (1)
    assert_eq!(lisp.car(pair).unwrap(), a);
    
    // Mutate car to c (3)
    lisp.set_car(pair, c).unwrap();
    
    // Now car should be c
    assert_eq!(lisp.car(pair).unwrap(), c);
    
    // cdr should be unchanged
    assert_eq!(lisp.cdr(pair).unwrap(), b);
}

#[test]
fn test_set_cdr() {
    let lisp: Lisp<100> = Lisp::new();
    
    let a = lisp.number(1).unwrap();
    let b = lisp.number(2).unwrap();
    let c = lisp.number(3).unwrap();
    
    let pair = lisp.cons(a, b).unwrap();
    
    // Initially cdr is b (2)
    assert_eq!(lisp.cdr(pair).unwrap(), b);
    
    // Mutate cdr to c (3)
    lisp.set_cdr(pair, c).unwrap();
    
    // Now cdr should be c
    assert_eq!(lisp.cdr(pair).unwrap(), c);
    
    // car should be unchanged
    assert_eq!(lisp.car(pair).unwrap(), a);
}

#[test]
fn test_set_car_on_non_pair_fails() {
    let lisp: Lisp<100> = Lisp::new();
    
    let num = lisp.number(42).unwrap();
    let new_val = lisp.number(99).unwrap();
    
    assert!(lisp.set_car(num, new_val).is_err());
}

#[test]
fn test_set_cdr_on_non_pair_fails() {
    let lisp: Lisp<100> = Lisp::new();
    
    let num = lisp.number(42).unwrap();
    let new_val = lisp.number(99).unwrap();
    
    assert!(lisp.set_cdr(num, new_val).is_err());
}

// ========================================================================
// Symbol Interning Tests
// ========================================================================

#[test]
fn test_symbol_interning_same_name_returns_same_index() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let sym1 = lisp.symbol("foo").unwrap();
    let sym2 = lisp.symbol("foo").unwrap();
    let sym3 = lisp.symbol("foo").unwrap();
    
    // Same symbol name should return the same index
    assert_eq!(sym1, sym2);
    assert_eq!(sym2, sym3);
}

#[test]
fn test_symbol_interning_different_names_return_different_indices() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let foo = lisp.symbol("foo").unwrap();
    let bar = lisp.symbol("bar").unwrap();
    let baz = lisp.symbol("baz").unwrap();
    
    // Different symbol names should return different indices
    assert_ne!(foo, bar);
    assert_ne!(bar, baz);
    assert_ne!(foo, baz);
}

#[test]
fn test_symbol_interning_from_bytes() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let sym1 = lisp.symbol("test").unwrap();
    let sym2 = lisp.symbol_from_bytes(b"test").unwrap();
    
    // Same content should return the same symbol
    assert_eq!(sym1, sym2);
}

#[test]
fn test_symbol_interning_preserves_content() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let sym = lisp.symbol("hello").unwrap();
    
    // The symbol should still match its name
    assert!(lisp.symbol_matches(sym, "hello").unwrap());
    assert!(!lisp.symbol_matches(sym, "world").unwrap());
}

#[test]
fn test_intern_table_is_gc_root() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // Create some interned symbols
    let sym1 = lisp.symbol("a").unwrap();
    let sym2 = lisp.symbol("b").unwrap();
    let sym3 = lisp.symbol("c").unwrap();
    
    // Create some garbage
    for i in 0..50 {
        let _ = lisp.number(i);
    }
    
    // Run GC with no explicit roots
    let empty_roots: &[ArenaIndex] = &[];
    lisp.gc(empty_roots);
    
    // Interned symbols should still be accessible
    assert!(lisp.get(sym1).is_ok());
    assert!(lisp.get(sym2).is_ok());
    assert!(lisp.get(sym3).is_ok());
    
    // And should still match their names
    assert!(lisp.symbol_matches(sym1, "a").unwrap());
    assert!(lisp.symbol_matches(sym2, "b").unwrap());
    assert!(lisp.symbol_matches(sym3, "c").unwrap());
}

// ========================================================================
// Symbol Helper Method Tests
// ========================================================================

#[test]
fn test_symbol_len() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let empty = lisp.symbol("").unwrap();
    let short = lisp.symbol("hi").unwrap();
    let longer = lisp.symbol("hello world").unwrap();
    
    assert_eq!(lisp.symbol_len(empty).unwrap(), 0);
    assert_eq!(lisp.symbol_len(short).unwrap(), 2);
    assert_eq!(lisp.symbol_len(longer).unwrap(), 11);
}

#[test]
fn test_symbol_char_at() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello = lisp.symbol("hello").unwrap();
    
    assert_eq!(lisp.symbol_char_at(hello, 0).unwrap(), Some('h'));
    assert_eq!(lisp.symbol_char_at(hello, 1).unwrap(), Some('e'));
    assert_eq!(lisp.symbol_char_at(hello, 2).unwrap(), Some('l'));
    assert_eq!(lisp.symbol_char_at(hello, 3).unwrap(), Some('l'));
    assert_eq!(lisp.symbol_char_at(hello, 4).unwrap(), Some('o'));
    assert_eq!(lisp.symbol_char_at(hello, 5).unwrap(), None);
    assert_eq!(lisp.symbol_char_at(hello, 100).unwrap(), None);
}

#[test]
fn test_symbol_to_bytes() {
    let lisp: Lisp<1000> = Lisp::new();
    
    let hello = lisp.symbol("hello").unwrap();
    
    let mut buf = [0u8; 32];
    let len = lisp.symbol_to_bytes(hello, &mut buf).unwrap();
    
    assert_eq!(len, 5);
    assert_eq!(&buf[..len], b"hello");
}

#[test]
fn test_contiguous_symbol_uses_less_memory() {
    let lisp: Lisp<20000> = Lisp::new();
    
    // Get initial allocation count (includes reserved slots)
    let initial = lisp.arena().len();
    
    // Create a symbol with contiguous strings
    // "factorial" (9 chars) = 1 (length) + 9 (chars) + 1 (Symbol) = 11 slots
    // Plus 2 slots for the intern table entry
    let _sym = lisp.symbol("factorial").unwrap();
    
    let after_symbol = lisp.arena().len();
    let slots_used = after_symbol - initial;
    
    // Old format would use: 9 Char + 9 Cons + 1 Symbol = 19 slots
    // New format uses: 1 Number + 9 Char + 1 Symbol + 2 Cons (intern table) = 13 slots
    // So we expect significantly fewer slots
    assert!(slots_used < 19, "Expected fewer than 19 slots, got {}", slots_used);
}

// ========================================================================
// Array Tests
// ========================================================================

#[test]
fn test_array_basic() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    // Create an array of 5 elements initialized to nil
    let arr = lisp.make_array(5, nil).unwrap();
    
    // Check length
    assert_eq!(lisp.array_len(arr).unwrap(), 5);
    
    // Check all elements are nil
    for i in 0..5 {
        let elem = lisp.array_get(arr, i).unwrap();
        assert_eq!(lisp.get(elem).unwrap(), Value::Nil);
    }
}

#[test]
fn test_array_empty() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    // Create an empty array
    let arr = lisp.make_array(0, nil).unwrap();
    
    // Check length is 0
    assert_eq!(lisp.array_len(arr).unwrap(), 0);
    
    // Check it's actually an array
    assert!(lisp.get(arr).unwrap().is_array());
}

#[test]
fn test_array_get_set() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    // Create array
    let arr = lisp.make_array(3, nil).unwrap();
    
    // Set values
    let val1 = lisp.number(42).unwrap();
    let val2 = lisp.number(100).unwrap();
    lisp.array_set(arr, 0, val1).unwrap();
    lisp.array_set(arr, 2, val2).unwrap();
    
    // Get values back
    let elem0 = lisp.array_get(arr, 0).unwrap();
    let elem1 = lisp.array_get(arr, 1).unwrap();
    let elem2 = lisp.array_get(arr, 2).unwrap();
    
    assert_eq!(lisp.get(elem0).unwrap(), Value::Number(42));
    assert_eq!(lisp.get(elem1).unwrap(), Value::Nil);  // Unchanged
    assert_eq!(lisp.get(elem2).unwrap(), Value::Number(100));
}

#[test]
fn test_array_out_of_bounds() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let arr = lisp.make_array(3, nil).unwrap();
    
    // Valid index
    assert!(lisp.array_get(arr, 2).is_ok());
    
    // Out of bounds
    assert!(lisp.array_get(arr, 3).is_err());
    assert!(lisp.array_get(arr, 100).is_err());
    
    // Out of bounds set
    assert!(lisp.array_set(arr, 3, nil).is_err());
}

#[test]
fn test_array_with_different_value_types() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let arr = lisp.make_array(4, nil).unwrap();
    
    // Set different value types
    let num = lisp.number(123).unwrap();
    let b = lisp.true_val().unwrap();
    let sym = lisp.symbol("foo").unwrap();
    let pair = lisp.cons(lisp.number(1).unwrap(), lisp.number(2).unwrap()).unwrap();
    
    lisp.array_set(arr, 0, num).unwrap();
    lisp.array_set(arr, 1, b).unwrap();
    lisp.array_set(arr, 2, sym).unwrap();
    lisp.array_set(arr, 3, pair).unwrap();
    
    // Verify
    let elem0 = lisp.array_get(arr, 0).unwrap();
    let elem1 = lisp.array_get(arr, 1).unwrap();
    let elem2 = lisp.array_get(arr, 2).unwrap();
    let elem3 = lisp.array_get(arr, 3).unwrap();
    
    assert!(lisp.get(elem0).unwrap().is_number());
    assert!(lisp.get(elem1).unwrap().is_true());
    assert!(lisp.get(elem2).unwrap().is_symbol());
    assert!(lisp.get(elem3).unwrap().is_cons());
}

#[test]
fn test_array_is_array_predicate() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let arr = lisp.make_array(3, nil).unwrap();
    let num = lisp.number(42).unwrap();
    let pair = lisp.cons(nil, nil).unwrap();
    
    assert!(lisp.get(arr).unwrap().is_array());
    assert!(!lisp.get(num).unwrap().is_array());
    assert!(!lisp.get(pair).unwrap().is_array());
    assert!(!lisp.get(nil).unwrap().is_array());
}

#[test]
fn test_array_len_on_non_array() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    assert!(lisp.array_len(nil).is_err());
}

#[test]
fn test_array_free() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let initial = lisp.arena().len();
    
    let arr = lisp.make_array(5, nil).unwrap();
    let after_alloc = lisp.arena().len();
    
    // With inline length: 5 data slots + 1 Array value = 6 slots (no length header)
    assert_eq!(after_alloc - initial, 6);
    
    lisp.array_free(arr).unwrap();
    let after_free = lisp.arena().len();
    
    // All slots should be freed
    assert_eq!(after_free, initial);
}

#[test]
fn test_array_memory_layout() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let initial = lisp.arena().len();
    
    // Create array of 10 elements
    let _arr = lisp.make_array(10, nil).unwrap();
    
    // With inline length: 10 data slots + 1 Array value = 11 slots (no length header)
    let after = lisp.arena().len();
    assert_eq!(after - initial, 11);
}

#[test]
fn test_array_type_name() {
    let arr = Value::Array { len: 0, data: ArenaIndex::NIL };
    assert_eq!(arr.type_name(), "array");
}

// ========================================================================
// String/Array Unification Tests
// ========================================================================

#[test]
fn test_string_type_name() {
    let s = Value::String { len: 0, data: ArenaIndex::NIL };
    assert_eq!(s.type_name(), "string");
}

#[test]
fn test_string_is_string_predicate() {
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let s = lisp.string("hello").unwrap();
    let arr = lisp.make_array(3, nil).unwrap();
    let num = lisp.number(42).unwrap();
    
    assert!(lisp.get(s).unwrap().is_string());
    assert!(!lisp.get(arr).unwrap().is_string());
    assert!(!lisp.get(num).unwrap().is_string());
    assert!(!lisp.get(nil).unwrap().is_string());
}

#[test]
fn test_string_and_array_consistent_layout() {
    // This test verifies that strings and arrays have consistent memory layouts:
    // Both use Value::Type { len, data } with inline length and data pointing directly to elements
    let lisp: Lisp<1000> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let initial = lisp.arena().len();
    
    // Create a string with 5 characters
    let s = lisp.string("hello").unwrap();
    let after_string = lisp.arena().len();
    
    // With inline length: 5 chars + 1 String value = 6 slots (no length header)
    assert_eq!(after_string - initial, 6);
    
    // Create an array with 5 elements
    let arr = lisp.make_array(5, nil).unwrap();
    let after_array = lisp.arena().len();
    
    // With inline length: 5 elements + 1 Array value = 6 slots (no length header)
    assert_eq!(after_array - after_string, 6);
    
    // Verify consistent structure
    match lisp.get(s).unwrap() {
        Value::String { .. } => assert_eq!(lisp.string_len(s).unwrap(), 5),
        _ => panic!("Expected String"),
    }
    
    match lisp.get(arr).unwrap() {
        Value::Array { .. } => assert_eq!(lisp.array_len(arr).unwrap(), 5),
        _ => panic!("Expected Array"),
    }
}

#[test]
fn test_string_gc_trace() {
    // Verify that GC properly traces strings (they should survive collection)
    let lisp: Lisp<200> = Lisp::new();
    
    let s = lisp.string("hello world").unwrap();
    
    // Create some garbage
    for i in 0..50 {
        lisp.number(i * 1000).unwrap();
    }
    
    // Run GC with the string as a root
    let stats = lisp.gc(&[s]);
    assert!(stats.collected > 0);
    
    // String should still be valid
    assert!(lisp.get(s).unwrap().is_string());
    assert_eq!(lisp.string_len(s).unwrap(), 11);
    assert!(lisp.string_matches(s, "hello world").unwrap());
}

// ========================================================================
// Syntax Object Tests (Phase 2 of EXTENDING_SCHEME_MACROS.md)
// ========================================================================

#[test]
fn test_syntax_object_creation() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // Create a syntax object wrapping a symbol
    let x = lisp.symbol("x").unwrap();
    let nil = lisp.nil().unwrap();
    let stx = lisp.syntax(x, nil, nil).unwrap();
    
    // Verify it's a syntax object
    assert!(lisp.get(stx).unwrap().is_syntax());
    assert_eq!(lisp.get(stx).unwrap().type_name(), "syntax");
}

#[test]
fn test_syntax_object_parts() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // Create a syntax object with marks and substitutions
    let expr = lisp.symbol("test").unwrap();
    let mark1 = lisp.symbol("m1").unwrap();
    let marks = lisp.cons(mark1, lisp.nil().unwrap()).unwrap();
    let subst = lisp.nil().unwrap();
    
    let stx = lisp.syntax(expr, marks, subst).unwrap();
    
    // Extract parts and verify
    let (extracted_expr, extracted_marks, extracted_subst) = lisp.syntax_parts(stx).unwrap();
    
    assert_eq!(extracted_expr, expr);
    assert_eq!(extracted_marks, marks);
    assert_eq!(extracted_subst, subst);
}

#[test]
fn test_syntax_to_datum() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // Create a syntax object
    let expr = lisp.number(42).unwrap();
    let nil = lisp.nil().unwrap();
    let stx = lisp.syntax(expr, nil, nil).unwrap();
    
    // Unwrap to get the datum
    let datum = lisp.syntax_to_datum(stx).unwrap();
    assert_eq!(datum, expr);
    assert_eq!(lisp.get(datum).unwrap().as_number(), Some(42));
}

#[test]
fn test_syntax_to_datum_passthrough() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // Non-syntax values should pass through unchanged
    let num = lisp.number(100).unwrap();
    let result = lisp.syntax_to_datum(num).unwrap();
    assert_eq!(result, num);
    
    let sym = lisp.symbol("hello").unwrap();
    let result = lisp.syntax_to_datum(sym).unwrap();
    assert_eq!(result, sym);
}

#[test]
fn test_syntax_object_with_list() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // Create a syntax object wrapping a list (+ 1 2)
    let plus = lisp.symbol("+").unwrap();
    let one = lisp.number(1).unwrap();
    let two = lisp.number(2).unwrap();
    let list = lisp.list([plus, one, two]).unwrap();
    
    let nil = lisp.nil().unwrap();
    let stx = lisp.syntax(list, nil, nil).unwrap();
    
    // Verify extraction
    let datum = lisp.syntax_to_datum(stx).unwrap();
    assert!(lisp.get(datum).unwrap().is_cons());
    
    let first = lisp.car(datum).unwrap();
    assert!(lisp.symbol_matches(first, "+").unwrap());
}

#[test]
fn test_syntax_object_gc() {
    let lisp: Lisp<1000> = Lisp::new();
    
    // Create a syntax object with some structure
    let expr = lisp.symbol("test-gc").unwrap();
    let mark = lisp.symbol("mark1").unwrap();
    let marks = lisp.cons(mark, lisp.nil().unwrap()).unwrap();
    let subst = lisp.nil().unwrap();
    
    let stx = lisp.syntax(expr, marks, subst).unwrap();
    
    // Create garbage
    for i in 0..50 {
        lisp.number(i * 100).unwrap();
    }
    
    // Run GC with syntax object as root
    let stats = lisp.gc(&[stx]);
    assert!(stats.collected > 0);
    
    // Syntax object and its parts should survive
    assert!(lisp.get(stx).unwrap().is_syntax());
    let (extracted_expr, _, _) = lisp.syntax_parts(stx).unwrap();
    assert!(lisp.symbol_matches(extracted_expr, "test-gc").unwrap());
}

// ========================================================================
// ContFrame Tests (call/cc infrastructure)
// ========================================================================

#[test]
fn test_cont_frame_creation() {
    let lisp: Lisp<100> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let env = lisp.nil().unwrap();
    
    // Create a Done continuation (type 0, no data, no parent)
    let cont = lisp.cont_frame(0, nil, nil, env).unwrap();
    
    // Verify it's a ContFrame
    assert!(lisp.get(cont).unwrap().is_cont_frame());
}

#[test]
fn test_cont_frame_parts_extraction() {
    let lisp: Lisp<100> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let env = lisp.symbol("test-env").unwrap();
    
    // Create a Done continuation (type 0)
    let cont = lisp.cont_frame(0, nil, nil, env).unwrap();
    
    // Extract parts
    let (cont_type, data, parent, extracted_env) = lisp.cont_frame_parts(cont).unwrap();
    assert_eq!(cont_type, 0);
    assert_eq!(data, nil);
    assert_eq!(parent, nil);
    assert_eq!(extracted_env, env);
}

#[test]
fn test_cont_frame_with_data() {
    let lisp: Lisp<100> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    // Create some data for an IfBranch continuation (type 2)
    let then_expr = lisp.symbol("then").unwrap();
    let else_expr = lisp.symbol("else").unwrap();
    let data = lisp.cons(then_expr, else_expr).unwrap();
    let env = lisp.nil().unwrap();
    
    let cont = lisp.cont_frame(2, data, nil, env).unwrap();
    
    // Extract and verify
    let (cont_type, extracted_data, parent, _) = lisp.cont_frame_parts(cont).unwrap();
    assert_eq!(cont_type, 2);
    assert_eq!(extracted_data, data);
    assert_eq!(parent, nil);
    
    // Verify we can access the data contents
    let extracted_then = lisp.car(extracted_data).unwrap();
    let extracted_else = lisp.cdr(extracted_data).unwrap();
    assert!(lisp.symbol_matches(extracted_then, "then").unwrap());
    assert!(lisp.symbol_matches(extracted_else, "else").unwrap());
}

#[test]
fn test_cont_frame_linked_list() {
    let lisp: Lisp<100> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let env = lisp.nil().unwrap();
    
    // Create a chain of continuation frames
    // Done -> ApplyForced -> IfBranch
    let done_cont = lisp.cont_frame(0, nil, nil, env).unwrap();
    let apply_cont = lisp.cont_frame(1, nil, done_cont, env).unwrap();
    let if_cont = lisp.cont_frame(2, nil, apply_cont, env).unwrap();
    
    // Verify the chain
    let (if_type, _, if_parent, _) = lisp.cont_frame_parts(if_cont).unwrap();
    assert_eq!(if_type, 2);
    assert_eq!(if_parent, apply_cont);
    
    let (apply_type, _, apply_parent, _) = lisp.cont_frame_parts(if_parent).unwrap();
    assert_eq!(apply_type, 1);
    assert_eq!(apply_parent, done_cont);
    
    let (done_type, _, done_parent, _) = lisp.cont_frame_parts(apply_parent).unwrap();
    assert_eq!(done_type, 0);
    assert_eq!(done_parent, nil);  // Done has no parent
}

#[test]
fn test_cont_frame_parent_helper() {
    let lisp: Lisp<100> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let env = lisp.nil().unwrap();
    
    // Create a chain
    let done_cont = lisp.cont_frame(0, nil, nil, env).unwrap();
    let child_cont = lisp.cont_frame(1, nil, done_cont, env).unwrap();
    
    // Use the parent helper
    let parent = lisp.cont_frame_parent(child_cont).unwrap();
    assert_eq!(parent, done_cont);
    
    // Done's parent should be nil
    let done_parent = lisp.cont_frame_parent(done_cont).unwrap();
    assert_eq!(done_parent, nil);
}

#[test]
fn test_cont_frame_gc_survival() {
    let lisp: Lisp<500> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let env = lisp.nil().unwrap();
    
    // Create a chain of continuation frames
    let done_cont = lisp.cont_frame(0, nil, nil, env).unwrap();
    let child_cont = lisp.cont_frame(1, nil, done_cont, env).unwrap();
    
    // Create garbage (intentionally discarded for GC testing)
    for i in 0..50 {
        let _ = lisp.number(i * 100).unwrap();
    }
    
    // Run GC with child_cont as root
    let stats = lisp.gc(&[child_cont]);
    assert!(stats.collected > 0);
    
    // Both continuations should survive (child references parent)
    assert!(lisp.get(child_cont).unwrap().is_cont_frame());
    assert!(lisp.get(done_cont).unwrap().is_cont_frame());
    
    // Verify chain is still intact
    let parent = lisp.cont_frame_parent(child_cont).unwrap();
    assert_eq!(parent, done_cont);
}

#[test]
fn test_cont_frame_type_name() {
    let lisp: Lisp<100> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    let cont = lisp.cont_frame(0, nil, nil, nil).unwrap();
    
    assert_eq!(lisp.get(cont).unwrap().type_name(), "cont-frame");
}

// Continuation Value Tests (first-class call/cc continuations)
// ============================================================================

#[test]
fn test_continuation_creation() {
    let lisp: Lisp<200> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let env = nil;
    
    // Create a simple continuation with empty cont_chain
    let cont = lisp.continuation(nil, env, nil).unwrap();
    
    // Verify it's a Continuation
    assert!(lisp.get(cont).unwrap().is_continuation());
    assert_eq!(lisp.get(cont).unwrap().type_name(), "continuation");
}

#[test]
fn test_continuation_parts_extraction() {
    let lisp: Lisp<200> = Lisp::new();
    let nil = lisp.nil().unwrap();
    
    // Create a more complex continuation
    let cont_chain = nil; // Empty chain for this test
    let sym_a = lisp.symbol("a").unwrap();
    let num_42 = lisp.number(42).unwrap();
    let capture_env = lisp.cons(sym_a, num_42).unwrap();
    let dw_chain = nil; // No dynamic-wind for this test
    
    let cont = lisp.continuation(cont_chain, capture_env, dw_chain).unwrap();
    
    // Extract and verify parts
    let (extracted_chain, extracted_env, extracted_dw) = lisp.continuation_parts(cont).unwrap();
    
    assert_eq!(extracted_chain, cont_chain);
    assert_eq!(extracted_env, capture_env);
    assert_eq!(extracted_dw, dw_chain);
}

#[test]
fn test_continuation_with_cont_frame_chain() {
    let lisp: Lisp<500> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let env = nil;
    
    // Create a chain of ContFrames
    let frame1 = lisp.cont_frame(0, nil, nil, env).unwrap(); // Done
    let frame2 = lisp.cont_frame(1, nil, frame1, env).unwrap(); // ApplyForced
    let frame3 = lisp.cont_frame(2, nil, frame2, env).unwrap(); // IfBranch
    
    // Create a continuation with this chain
    let cont = lisp.continuation(frame3, env, nil).unwrap();
    
    // Extract and verify the chain
    let (extracted_chain, _, _) = lisp.continuation_parts(cont).unwrap();
    assert_eq!(extracted_chain, frame3);
    
    // Walk the chain
    let (type3, _, parent3, _) = lisp.cont_frame_parts(extracted_chain).unwrap();
    assert_eq!(type3, 2);
    
    let (type2, _, parent2, _) = lisp.cont_frame_parts(parent3).unwrap();
    assert_eq!(type2, 1);
    
    let (type1, _, parent1, _) = lisp.cont_frame_parts(parent2).unwrap();
    assert_eq!(type1, 0);
    assert!(parent1.is_nil()); // Done has no parent
}

#[test]
fn test_continuation_gc_survival() {
    let lisp: Lisp<500> = Lisp::new();
    let nil = lisp.nil().unwrap();
    let sym_x = lisp.symbol("x").unwrap();
    let num_1 = lisp.number(1).unwrap();
    let env = lisp.cons(sym_x, num_1).unwrap();
    
    // Create a continuation with some data
    let frame = lisp.cont_frame(0, nil, nil, env).unwrap();
    let cont = lisp.continuation(frame, env, nil).unwrap();
    
    // Allocate some garbage
    for _ in 0..50 {
        lisp.cons(nil, nil).unwrap();
    }
    
    // Run GC with continuation as root
    let stats = lisp.gc(&[cont]);
    assert!(stats.collected > 0);
    
    // Verify continuation survived and is still valid
    assert!(lisp.get(cont).unwrap().is_continuation());
    let (chain, _, _) = lisp.continuation_parts(cont).unwrap();
    assert!(lisp.get(chain).unwrap().is_cont_frame());
}

// ========================================================================
// Block Comment Tests (#| ... |#)
// ========================================================================

#[test]
fn test_block_comment_basic() {
    let lisp: Lisp<100> = Lisp::new();
    // Block comment between tokens
    let idx = parse(&lisp, "#| comment |# 42").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Number(42));
}

#[test]
fn test_block_comment_nested() {
    let lisp: Lisp<100> = Lisp::new();
    // Nested block comments
    let idx = parse(&lisp, "#| outer #| inner |# still outer |# 7").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Number(7));
}

#[test]
fn test_block_comment_inline() {
    let lisp: Lisp<200> = Lisp::new();
    // Block comment inside a list
    let idx = parse(&lisp, "(+ 1 #| skip |# 2)").unwrap();
    // Should parse as (+ 1 2)
    let (car, cdr) = lisp.car_cdr(idx).unwrap();
    assert!(lisp.symbol_matches(car, "+").unwrap());
    let (one, rest) = lisp.car_cdr(cdr).unwrap();
    assert_eq!(lisp.get(one).unwrap(), Value::Number(1));
    let (two, nil) = lisp.car_cdr(rest).unwrap();
    assert_eq!(lisp.get(two).unwrap(), Value::Number(2));
    assert!(lisp.get(nil).unwrap().is_nil());
}

#[test]
fn test_block_comment_multiline() {
    let lisp: Lisp<100> = Lisp::new();
    let idx = parse(&lisp, "#|\nmultiline\ncomment\n|# 99").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Number(99));
}

// ========================================================================
// Datum Comment Tests (#;)
// ========================================================================

#[test]
fn test_datum_comment_number() {
    let lisp: Lisp<200> = Lisp::new();
    // #; skips the next datum (42), leaving 99
    let idx = parse(&lisp, "#; 42 99").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Number(99));
}

#[test]
fn test_datum_comment_list() {
    let lisp: Lisp<200> = Lisp::new();
    // #; skips the entire list (a b c)
    let idx = parse(&lisp, "#; (a b c) 55").unwrap();
    assert_eq!(lisp.get(idx).unwrap(), Value::Number(55));
}

#[test]
fn test_datum_comment_symbol() {
    let lisp: Lisp<200> = Lisp::new();
    let idx = parse(&lisp, "#; foo bar").unwrap();
    assert!(lisp.symbol_matches(idx, "bar").unwrap());
}

#[test]
fn test_datum_comment_in_list() {
    let lisp: Lisp<500> = Lisp::new();
    // (1 #; 2 3) should parse as (1 3)
    let idx = parse(&lisp, "(1 #; 2 3)").unwrap();
    let (car, cdr) = lisp.car_cdr(idx).unwrap();
    assert_eq!(lisp.get(car).unwrap(), Value::Number(1));
    let (car2, nil) = lisp.car_cdr(cdr).unwrap();
    assert_eq!(lisp.get(car2).unwrap(), Value::Number(3));
    assert!(lisp.get(nil).unwrap().is_nil());
}

// ========================================================================
// Bytevector Literal Tests (#u8(...))
// ========================================================================

#[test]
fn test_bytevector_empty() {
    let lisp: Lisp<200> = Lisp::new();
    let idx = parse(&lisp, "#u8()").unwrap();
    assert!(lisp.get(idx).unwrap().is_bytevector());
    assert_eq!(lisp.bytevector_len(idx).unwrap(), 0);
}

#[test]
fn test_bytevector_basic() {
    let lisp: Lisp<200> = Lisp::new();
    let idx = parse(&lisp, "#u8(0 10 5)").unwrap();
    assert!(lisp.get(idx).unwrap().is_bytevector());
    assert_eq!(lisp.bytevector_len(idx).unwrap(), 3);
    // Check individual bytes
    let b0 = lisp.bytevector_get(idx, 0).unwrap();
    assert_eq!(lisp.get(b0).unwrap(), Value::Number(0));
    let b1 = lisp.bytevector_get(idx, 1).unwrap();
    assert_eq!(lisp.get(b1).unwrap(), Value::Number(10));
    let b2 = lisp.bytevector_get(idx, 2).unwrap();
    assert_eq!(lisp.get(b2).unwrap(), Value::Number(5));
}

#[test]
fn test_bytevector_display() {
    let lisp: Lisp<200> = Lisp::new();
    let idx = parse(&lisp, "#u8(1 2 3)").unwrap();
    let display = format!("{}", lisp.display(idx));
    assert_eq!(display, "#u8(1 2 3)");
}

#[test]
fn test_bytevector_empty_display() {
    let lisp: Lisp<200> = Lisp::new();
    let idx = parse(&lisp, "#u8()").unwrap();
    let display = format!("{}", lisp.display(idx));
    assert_eq!(display, "#u8()");
}

// ========================================================================
// Fold-case Directive Tests
// ========================================================================

#[test]
fn test_fold_case_default() {
    let lisp: Lisp<100> = Lisp::new();
    // By default, symbols are case-folded (lowercased)
    let idx = parse(&lisp, "Hello").unwrap();
    assert!(lisp.symbol_matches(idx, "hello").unwrap());
}

#[test]
fn test_no_fold_case() {
    let lisp: Lisp<100> = Lisp::new();
    // #!no-fold-case preserves case
    let idx = parse(&lisp, "#!no-fold-case Hello").unwrap();
    assert!(lisp.symbol_matches(idx, "Hello").unwrap());
}

#[test]
fn test_fold_case_restore() {
    let lisp: Lisp<200> = Lisp::new();
    // Turn off, then back on
    let idx = parse(&lisp, "#!no-fold-case #!fold-case Hello").unwrap();
    assert!(lisp.symbol_matches(idx, "hello").unwrap());
}