doge-compiler 0.3.1

Compiler for the Doge programming language — lexer, parser, semantic checks, and Rust codegen.
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
use super::*;
use crate::parser::parse;

fn gen(source: &str) -> Result<String, Diagnostic> {
    let script = parse("examples/hello.doge", source).expect("parse should succeed");
    let program = crate::modules::single_file_program("examples/hello.doge", source, script)?;
    generate_program(&program)
}

#[test]
fn golden_hello_output() {
    let out = gen("such age = 7\nbark \"age is \" + str(age)\nwow\n").unwrap();
    let expected = "\
#![allow(warnings)]
use doge_runtime::*;

static LINES: &[&str] = &[
    \"such age = 7\",
    \"bark \\\"age is \\\" + str(age)\",
    \"wow\",
    \"\",
];

struct Env {
    cur_line: u32,
    depth: usize,
    v_age: Value,
}

fn main() -> std::process::ExitCode {
    set_script_args(std::env::args().skip(1).collect());
    let mut env = Env {
        cur_line: 0,
        depth: 0,
        v_age: Value::None,
    };
    match run(&mut env) {
        Ok(()) => std::process::ExitCode::SUCCESS,
        Err(e) => {
            let line = LINES.get((env.cur_line as usize).saturating_sub(1)).copied().unwrap_or(\"\");
            eprintln!(\"very error. much broken.\\n\\n  examples/hello.doge:{}\\n    {line}\\n  {e}\", env.cur_line);
            std::process::ExitCode::FAILURE
        }
    }
}

fn run(env: &mut Env) -> DogeResult<()> {
    env.cur_line = 1;
    env.v_age = Value::int(7i64);
    env.cur_line = 2;
    let _ = bark(&add(Value::str(\"age is \"), to_str(&env.v_age.clone()))?);
    Ok(())
}
";
    assert_eq!(out, expected);
}

#[test]
fn golden_function_shape() {
    let out =
        gen("such greet much name:\n    return name\nwow\nbark greet(\"kabosu\")\nwow\n").unwrap();
    let expected = "\
#![allow(warnings)]
use doge_runtime::*;

static LINES: &[&str] = &[
    \"such greet much name:\",
    \"    return name\",
    \"wow\",
    \"bark greet(\\\"kabosu\\\")\",
    \"wow\",
    \"\",
];

struct Env {
    cur_line: u32,
    depth: usize,
}

fn main() -> std::process::ExitCode {
    set_script_args(std::env::args().skip(1).collect());
    let mut env = Env {
        cur_line: 0,
        depth: 0,
    };
    match run(&mut env) {
        Ok(()) => std::process::ExitCode::SUCCESS,
        Err(e) => {
            let line = LINES.get((env.cur_line as usize).saturating_sub(1)).copied().unwrap_or(\"\");
            eprintln!(\"very error. much broken.\\n\\n  examples/hello.doge:{}\\n    {line}\\n  {e}\", env.cur_line);
            std::process::ExitCode::FAILURE
        }
    }
}

fn run(env: &mut Env) -> DogeResult<()> {
    env.cur_line = 4;
    let _ = bark(&f_greet(Value::str(\"kabosu\"), &mut *env)?);
    Ok(())
}

fn f_greet(v_name: Value, env: &mut Env) -> DogeResult<Value> {
    enter_call(&mut env.depth)?;
    let result = b_greet(v_name, env);
    exit_call(&mut env.depth);
    result
}

fn b_greet(mut v_name: Value, env: &mut Env) -> DogeResult<Value> {
    env.cur_line = 2;
    return Ok(v_name.clone());
    Ok(Value::None)
}
";
    assert_eq!(out, expected);
}

#[test]
fn decl_inside_if_is_hoisted() {
    let out = gen("such c = 1\nif c:\n    such y = 2\nbark y\nwow\n").unwrap();
    assert!(out.contains("    v_y: Value,\n"));
    assert!(out.contains("env.v_y = Value::int(2i64);"));
    assert!(out.contains("let _ = bark(&env.v_y.clone());"));
}

#[test]
fn for_variable_is_hoisted() {
    let out = gen("such xs = [1, 2]\nfor x in xs:\n    bark x\nwow\n").unwrap();
    assert!(out.contains("    v_x: Value,\n"));
    assert!(out.contains("'l0: for item in iter_value(&env.v_xs.clone())? {"));
    assert!(out.contains("env.v_x = item;"));
}

#[test]
fn destructuring_declaration_unpacks_into_each_binding() {
    let out = gen("such a, b = [1, 2]\nbark a\nwow\n").unwrap();
    assert!(out.contains("let vals0 = unpack_value(&src0, 2, false)?;"));
    assert!(out.contains("env.v_a = vals0[0].clone();"));
    assert!(out.contains("env.v_b = vals0[1].clone();"));
}

#[test]
fn destructuring_with_a_collector_passes_the_rest_flag() {
    let out = gen("such first, many rest = [1, 2, 3]\nbark first\nwow\n").unwrap();
    assert!(out.contains("let vals0 = unpack_value(&src0, 1, true)?;"));
    assert!(out.contains("env.v_first = vals0[0].clone();"));
    assert!(out.contains("env.v_rest = vals0[1].clone();"));
}

#[test]
fn for_loop_destructuring_unpacks_each_item() {
    let out = gen("such d = {\"a\": 1}\nfor k, v in d:\n    bark k\nwow\n").unwrap();
    assert!(out.contains("'l0: for item in iter_value(&env.v_d.clone())? {"));
    assert!(out.contains("let vals1 = unpack_value(&src1, 2, false)?;"));
}

#[test]
fn and_or_short_circuit_shape() {
    let and = gen("such a = true\nsuch b = false\nbark a and b\nwow\n").unwrap();
    assert!(and.contains(
            "{ let l = env.v_a.clone(); if !l.truthy() { Value::Bool(false) } else { Value::Bool((env.v_b.clone()).truthy()) } }"
        ));
    let or = gen("such a = true\nsuch b = false\nbark a or b\nwow\n").unwrap();
    assert!(or.contains(
            "{ let l = env.v_a.clone(); if l.truthy() { Value::Bool(true) } else { Value::Bool((env.v_b.clone()).truthy()) } }"
        ));
}

#[test]
fn rust_keyword_idents_are_mangled() {
    // `match` is a Rust keyword; the `v_` prefix keeps the generated code legal.
    let out = gen("such match = 1\nbark match\nwow\n").unwrap();
    assert!(out.contains("    v_match: Value,\n"));
    assert!(out.contains("env.v_match = Value::int(1i64);"));
}

#[test]
fn string_escapes_survive() {
    let out = gen("such s = \"a\\\"b\\nc\"\nwow\n").unwrap();
    // The Doge string a"b<newline>c becomes an escaped Rust string literal.
    assert!(out.contains("Value::str(\"a\\\"b\\nc\")"));
}

#[test]
fn const_compiles_like_decl() {
    let out = gen("so PI = 3\nbark PI\nwow\n").unwrap();
    assert!(out.contains("    v_PI: Value,\n"));
    assert!(out.contains("env.v_PI = Value::int(3i64);"));
    assert!(out.contains("let _ = bark(&env.v_PI.clone());"));
}

#[test]
fn try_block_shape() {
    let out = gen("such x = 0\npls\n    very x = 1 // 0\noh no err!\n    bark err\nwow\n").unwrap();
    assert!(out.contains("let attempt0: DogeResult<()> = 'p0: {"));
    assert!(out.contains("Err(e) => break 'p0 Err(e)"));
    assert!(out.contains("if let Err(e) = attempt0 {"));
    assert!(out.contains("env.v_err = error_value(&e, \"examples/hello.doge\", env.cur_line);"));
}

#[test]
fn bonk_returns_err() {
    let out = gen("bonk \"nope\"\nwow\n").unwrap();
    assert!(out.contains("return Err(bonk_error(&Value::str(\"nope\")));"));
}

#[test]
fn bonk_in_try_breaks_to_label() {
    let out = gen("pls\n    bonk \"nope\"\noh no err!\n    bark err\nwow\n").unwrap();
    assert!(out.contains("break 'p0 Err(bonk_error(&Value::str(\"nope\")));"));
}

#[test]
fn loops_are_labeled_and_bork_uses_labels() {
    // A bork inside a pls inside a for must break the labeled loop, crossing
    // the labeled try block.
    let out =
            gen("such xs = [1]\nfor x in xs:\n    pls\n        bork\n    oh no err!\n        bark err\nwow\n")
                .unwrap();
    assert!(out.contains("'l0: for item in"));
    assert!(out.contains("'p1: {"));
    assert!(out.contains("break 'l0;"));
}

#[test]
fn interpolation_emits_an_interp_call() {
    let out = gen("such name = \"kabosu\"\nbark \"hi {name}, {1 + 1}\"\nwow\n").unwrap();
    assert!(out.contains("interp(&["));
    // The literal segments survive escaping and the holes compile as exprs.
    assert!(out.contains("Value::str(\"hi \")"));
    assert!(out.contains("add("));
}

#[test]
fn interpolation_literal_escapes_survive() {
    // A literal `"` inside the interpolated text must stay escaped in the
    // generated Rust string literal.
    let out = gen("bark \"a \\\" {1}\"\nwow\n").unwrap();
    assert!(out.contains("Value::str(\"a \\\" \")"));
}

#[test]
fn builtin_arity_error_is_precise() {
    let err = gen("bark len(1, 2, 3)\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very args. much wrong.");
    assert_eq!(err.message, "len takes 1 argument, got 3");
    assert_eq!(err.hint.as_deref(), Some("len(thing)"));

    let range_err = gen("bark range(1, 2, 3)\nwow\n").unwrap_err();
    assert_eq!(range_err.message, "range takes 1 or 2 arguments, got 3");
}

#[test]
fn function_arity_error_is_precise() {
    let err = gen("such add2 much a, b:\n    return a + b\nwow\nbark add2(1)\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very args. much wrong.");
    assert_eq!(err.message, "add2 takes 2 arguments, got 1");
    assert_eq!(err.hint.as_deref(), Some("add2(a, b)"));
}

#[test]
fn default_is_filled_at_a_direct_call() {
    let out = gen("such greet much name, mood = \"happy\":\n    return name\nwow\nbark greet(\"kabosu\")\nwow\n")
        .unwrap();
    // The omitted `mood` argument is supplied from its literal default.
    assert!(out.contains("f_greet(Value::str(\"kabosu\"), Value::str(\"happy\"), &mut *env)"));
}

#[test]
fn variadic_packs_the_surplus_into_a_list() {
    let out = gen(
        "such party much host, many rest:\n    return host\nwow\nparty(\"a\", \"b\", \"c\")\nwow\n",
    )
    .unwrap();
    assert!(out.contains(
        "f_party(Value::str(\"a\"), Value::list(vec![Value::str(\"b\"), Value::str(\"c\")]), &mut *env)"
    ));
}

#[test]
fn keyword_args_bind_by_name_with_ordered_temporaries() {
    let out = gen("such f much a, b, c:\n    return a\nwow\nf(1, c = 3, b = 2)\nwow\n").unwrap();
    // Provided arguments evaluate left-to-right into temporaries, then fill their
    // slots in binding order (a, b, c) → temp for `c` lands in the third slot.
    assert!(out.contains("let __a0 = Value::int(1i64); "));
    assert!(out.contains("let __a1 = Value::int(3i64); "));
    assert!(out.contains("let __a2 = Value::int(2i64); "));
    assert!(out.contains("f_f(__a0, __a2, __a1, &mut *env)"));
}

#[test]
fn range_arity_error_reports_the_accepted_span() {
    let err =
        gen("such greet much name, mood = \"happy\":\n    return name\nwow\nbark greet()\nwow\n")
            .unwrap_err();
    assert_eq!(err.headline, "very args. much wrong.");
    assert_eq!(err.message, "greet takes 1 to 2 arguments, got 0");
}

#[test]
fn variadic_arity_error_says_at_least() {
    let err =
        gen("such party much host, many rest:\n    return host\nwow\nparty()\nwow\n").unwrap_err();
    assert_eq!(err.message, "party takes at least 1 argument, got 0");
}

#[test]
fn unknown_keyword_argument_is_an_error() {
    let err =
        gen("such greet much name:\n    return name\nwow\ngreet(mood = 1)\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very keyword. much unknown.");
}

#[test]
fn keyword_argument_on_a_method_is_rejected() {
    let src = "many Shibe:\n    such speak much mood:\n        return mood\n    wow\nwow\nsuch k = Shibe()\nk.speak(mood = 1)\nwow\n";
    let err = gen(src).unwrap_err();
    assert_eq!(err.headline, "very keyword. much dynamic.");
}

#[test]
fn dispatcher_arm_fills_defaults_and_packs_variadic() {
    // Called through a value, so the arm — not the call site — fills the default.
    let out = gen("such greet much name, mood = \"happy\":\n    return name\nwow\nsuch g = greet\nbark g(\"kabosu\")\nwow\n")
        .unwrap();
    assert!(out.contains("if args.len() < 1 { return Err(function_arity_error(\"greet\", 1usize, Some(2usize), args.len())); }"));
    assert!(out.contains("if args.len() < 2 { args.push(Value::str(\"happy\")); }"));
}

#[test]
fn range_one_and_two_args() {
    let one = gen("for i in range(3):\n    bark i\nwow\n").unwrap();
    assert!(one.contains("range(&Value::int(0i64), &Value::int(3i64))?"));
    let two = gen("for i in range(2, 5):\n    bark i\nwow\n").unwrap();
    assert!(two.contains("range(&Value::int(2i64), &Value::int(5i64))?"));
}

#[test]
fn function_as_value_constructs_a_function_value() {
    let out = gen("such greet:\n    bark 1\nwow\nsuch g = greet\nwow\n").unwrap();
    // A top-level function name used as a value builds a `Value::function`.
    assert!(out.contains("env.v_g = Value::function(0u32, \"greet\", vec![]);"));
}

#[test]
fn builtin_as_value_constructs_a_function_value() {
    // `bark len` — a bare builtin name used as a value.
    let out = gen("bark len\nwow\n").unwrap();
    assert!(out.contains("Value::function(0u32, \"len\", vec![])"));
}

#[test]
fn indirect_call_goes_through_the_dispatcher() {
    let out = gen("such x = 1\nx()\nwow\n").unwrap();
    // Every indirect call goes through the `call_value` shim, which routes a
    // bound method to `call_method` and everything else to `call_function`.
    assert!(out.contains("call_value(env.v_x.clone(), vec![], &mut *env)?"));
    assert!(out.contains("fn call_value(callee: Value"));
    assert!(out.contains("fn call_function(f: &FunctionData"));
}

#[test]
fn method_read_as_a_value_binds_and_emits_the_gate() {
    // `such f = a.speak` reads a method as a value: `attr_get_or_bind` binds it,
    // and `class_has_method` is the object-receiver gate, listing every method.
    let out = gen(
        "many Shibe:\n    such speak:\n        return 1\n    wow\nwow\nsuch a = Shibe()\nsuch f = a.speak\nwow\n",
    )
    .unwrap();
    assert!(out.contains("attr_get_or_bind(&env.v_a.clone(), \"speak\", &class_has_method)"));
    assert!(out.contains("fn class_has_method(class_id: u32, name: &str) -> bool"));
    assert!(out.contains("(0u32, \"speak\")"));
}

#[test]
fn calling_a_bound_method_routes_through_call_method() {
    // Reading then calling a method value forces the method dispatcher to exist
    // even though no method is called directly by name.
    let out = gen(
        "many Shibe:\n    such speak:\n        return 1\n    wow\nwow\nsuch a = Shibe()\nsuch f = a.speak\nf()\nwow\n",
    )
    .unwrap();
    assert!(out.contains("if let Value::BoundMethod(m) = &callee"));
    assert!(out.contains("call_method(m.receiver.clone(), &m.method, args, env)"));
    assert!(out.contains("fn call_method(recv: Value"));
}

#[test]
fn nested_funcdef_becomes_a_closure() {
    let out = gen("such outer:\n    such inner:\n        bark 1\n    wow\nwow\nwow\n").unwrap();
    // The nested name is a hoisted cell, set to a closure value; the closure
    // body is emitted as a `c_`/`cb_` pair.
    assert!(out.contains("let v_inner: Cell = Rc::new(RefCell::new(Value::None));"));
    assert!(out.contains("cell_set(&v_inner, Value::function(1u32, \"inner\", vec![]));"));
    assert!(out.contains("fn c_1(env: &mut Env)"));
    assert!(out.contains("fn cb_1(env: &mut Env)"));
}

#[test]
fn closure_captures_an_enclosing_variable() {
    // `bump` reads and writes `count`, which becomes a shared cell in `outer`.
    let out = gen(
            "such outer:\n    such count = 0\n    such bump:\n        very count = count + 1\n        return count\n    wow\n    return bump()\nwow\nwow\n",
        )
        .unwrap();
    assert!(out.contains("let v_count: Cell = Rc::new(RefCell::new(Value::None));"));
    // The closure receives `count` as a leading cell parameter.
    assert!(out.contains("fn cb_1(v_count: Cell, env: &mut Env)"));
    assert!(out.contains("cell_set(&v_count, add(cell_get(&v_count), Value::int(1i64))?);"));
    // Construction threads the shared cell into the function value.
    assert!(
        out.contains("cell_set(&v_bump, Value::function(1u32, \"bump\", vec![v_count.clone()]));")
    );
}

#[test]
fn direct_nested_call_keeps_compile_time_arity() {
    let err = gen(
            "such outer:\n    such add2 much a, b:\n        return a + b\n    wow\n    return add2(1)\nwow\nwow\n",
        )
        .unwrap_err();
    assert_eq!(err.headline, "very args. much wrong.");
    assert_eq!(err.message, "add2 takes 2 arguments, got 1");
}

#[test]
fn reassigning_a_nested_function_is_an_error() {
    let err = gen(
        "such outer:\n    such inner:\n        bark 1\n    wow\n    very inner = 5\nwow\nwow\n",
    )
    .unwrap_err();
    assert_eq!(err.headline, "very function. much fixed.");
}

#[test]
fn module_func_as_value_constructs_a_function_value() {
    let out = gen("so nerd\nsuch s = nerd.sqrt\nwow\n").unwrap();
    assert!(out.contains("Value::function("));
    assert!(out.contains("\"nerd.sqrt\", vec![]"));
}

#[test]
fn so_math_hints_at_nerd() {
    let err = gen("so math\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very import. much unknown.");
    assert!(err.hint.as_deref().unwrap_or_default().contains("so nerd"));
}

#[test]
fn unknown_module_is_an_error() {
    let err = gen("so bogus\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very import. much unknown.");
    assert_eq!(err.message, "doge has no module named bogus");
    assert!(err
        .hint
        .as_deref()
        .unwrap_or_default()
        .contains("nerd, strings"));
}

#[test]
fn module_call_emits_runtime_fn() {
    let out = gen("so nerd\nbark nerd.sqrt(16)\nwow\n").unwrap();
    assert!(out.contains("nerd_sqrt(&Value::int(16i64))?"));
}

#[test]
fn module_const_emits_value() {
    let out = gen("so nerd\nbark nerd.pi\nwow\n").unwrap();
    assert!(out.contains("Value::Float(std::f64::consts::PI)"));
}

#[test]
fn unknown_member_is_an_error() {
    let err = gen("so nerd\nbark nerd.bogus(1)\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very module. much unknown.");
    assert_eq!(err.message, "nerd has no member bogus");
}

#[test]
fn module_member_arity_error_is_precise() {
    let err = gen("so nerd\nbark nerd.sqrt(1, 2)\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very args. much wrong.");
    assert_eq!(err.message, "nerd.sqrt takes 1 argument, got 2");
    assert_eq!(err.hint.as_deref(), Some("nerd.sqrt(x)"));
}

#[test]
fn module_const_called_is_an_error() {
    let err = gen("so nerd\nbark nerd.pi(1)\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very module. much confuse.");
    assert_eq!(err.message, "nerd.pi is a constant, not a function");
}

#[test]
fn module_as_value_is_an_error() {
    let err = gen("so nerd\nbark nerd\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very module. much confuse.");
    assert_eq!(err.message, "nerd is a module, not a value");
}

#[test]
fn bare_module_func_is_a_value() {
    // `bark nerd.sqrt` prints the function value rather than erroring.
    let out = gen("so nerd\nbark nerd.sqrt\nwow\n").unwrap();
    assert!(out.contains("Value::function("));
    assert!(out.contains("\"nerd.sqrt\", vec![]"));
}

#[test]
fn calling_a_module_is_an_error() {
    let err = gen("so nerd\nbark nerd(1)\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very module. much confuse.");
    assert_eq!(err.message, "nerd is a module, not a function");
}

#[test]
fn assign_to_module_name_is_an_error() {
    let err = gen("so nerd\nnerd = 5\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very module. much fixed.");
}

#[test]
fn assign_into_module_is_an_error() {
    let err = gen("so nerd\nnerd.x = 5\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very module. much fixed.");
    assert_eq!(err.message, "cannot assign into a module");
}

#[test]
fn nested_import_is_an_error() {
    let err = gen("such f:\n    so nerd\nwow\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very nested. much import.");
}

#[test]
fn assign_to_function_name_is_an_error() {
    let err = gen("such greet:\n    bark 1\nwow\nvery greet = 5\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very function. much fixed.");
}

#[test]
fn fn_local_vs_global_resolution() {
    // The function reassigns a top-level name (env field) and declares its
    // own local (a plain `v_`).
    let out = gen(
            "such total = 0\nsuch tally much n:\n    such step = n\n    very total = total + step\n    return total\nwow\nbark tally(2)\nwow\n",
        )
        .unwrap();
    assert!(out.contains("let mut v_step: Value = Value::None;"));
    assert!(out.contains("env.v_total = add(env.v_total.clone(), v_step.clone())?;"));
}

#[test]
fn bare_return_and_missing_return_yield_none() {
    let out = gen("such f:\n    return\nwow\nf()\nwow\n").unwrap();
    assert!(out.contains("return Ok(Value::None);"));
    // The body still ends with the fall-off-end none.
    assert!(out.contains("    Ok(Value::None)\n}\n"));
}

#[test]
fn object_golden_shape() {
    let src = "many Shibe:\n    such init much name, age:\n        self.name = name\n        self.age = age\n    wow\n\n    such speak:\n        bark self.name + \" says bork\"\n    wow\nwow\n\nsuch kabosu = Shibe(\"kabosu\", 18)\nkabosu.speak()\nwow\n";
    let out = gen(src).unwrap();
    let expected = r#"#![allow(warnings)]
use doge_runtime::*;

static LINES: &[&str] = &[
    "many Shibe:",
    "    such init much name, age:",
    "        self.name = name",
    "        self.age = age",
    "    wow",
    "",
    "    such speak:",
    "        bark self.name + \" says bork\"",
    "    wow",
    "wow",
    "",
    "such kabosu = Shibe(\"kabosu\", 18)",
    "kabosu.speak()",
    "wow",
    "",
];

struct Env {
    cur_line: u32,
    depth: usize,
    v_kabosu: Value,
}

fn main() -> std::process::ExitCode {
    set_script_args(std::env::args().skip(1).collect());
    let mut env = Env {
        cur_line: 0,
        depth: 0,
        v_kabosu: Value::None,
    };
    match run(&mut env) {
        Ok(()) => std::process::ExitCode::SUCCESS,
        Err(e) => {
            let line = LINES.get((env.cur_line as usize).saturating_sub(1)).copied().unwrap_or("");
            eprintln!("very error. much broken.\n\n  examples/hello.doge:{}\n    {line}\n  {e}", env.cur_line);
            std::process::ExitCode::FAILURE
        }
    }
}

fn run(env: &mut Env) -> DogeResult<()> {
    env.cur_line = 12;
    env.v_kabosu = n_0(Value::str("kabosu"), Value::int(18i64), &mut *env)?;
    env.cur_line = 13;
    let _ = call_method(env.v_kabosu.clone(), "speak", vec![], &mut *env)?;
    Ok(())
}

fn n_0(v_name: Value, v_age: Value, env: &mut Env) -> DogeResult<Value> {
    let obj = Value::object(0u32, "Shibe");
    mf_0_init(obj.clone(), v_name, v_age, env)?;
    Ok(obj)
}

fn mf_0_init(v_self: Value, v_name: Value, v_age: Value, env: &mut Env) -> DogeResult<Value> {
    enter_call(&mut env.depth)?;
    let result = mb_0_init(v_self, v_name, v_age, env);
    exit_call(&mut env.depth);
    result
}

fn mb_0_init(mut v_self: Value, mut v_name: Value, mut v_age: Value, env: &mut Env) -> DogeResult<Value> {
    env.cur_line = 3;
    attr_set(&v_self.clone(), "name", v_name.clone())?;
    env.cur_line = 4;
    attr_set(&v_self.clone(), "age", v_age.clone())?;
    Ok(Value::None)
}

fn mf_0_speak(v_self: Value, env: &mut Env) -> DogeResult<Value> {
    enter_call(&mut env.depth)?;
    let result = mb_0_speak(v_self, env);
    exit_call(&mut env.depth);
    result
}

fn mb_0_speak(mut v_self: Value, env: &mut Env) -> DogeResult<Value> {
    env.cur_line = 8;
    let _ = bark(&add(attr_get_or_bind(&v_self.clone(), "name", &class_has_method)?, Value::str(" says bork"))?);
    Ok(Value::None)
}

fn call_method(recv: Value, name: &str, mut args: Vec<Value>, env: &mut Env) -> DogeResult<Value> {
    if !matches!(recv, Value::Object(_)) { return builtin_method(&recv, name, args); }
    match (object_class_id(&recv)?, name) {
        (0u32, "init") => {
            if args.len() != 2 { return Err(method_arity_error("Shibe", "init", 2usize, Some(2usize), args.len())); }
            mf_0_init(recv, args.remove(0), args.remove(0), env)
        }
        (0u32, "speak") => {
            if args.len() != 0 { return Err(method_arity_error("Shibe", "speak", 0usize, Some(0usize), args.len())); }
            mf_0_speak(recv, env)
        }
        _ => Err(no_such_method(&recv, name)),
    }
}

fn class_has_method(class_id: u32, name: &str) -> bool { matches!((class_id, name), (0u32, "init") | (0u32, "speak")) }
"#;
    assert_eq!(out, expected);
}

#[test]
fn attr_get_and_set_emission() {
    // A field read as a value binds a method when there is no field, so it emits
    // `attr_get_or_bind`; a field write is still a plain `attr_set`.
    let out = gen("such x = 1\nx.name = 2\nbark x.name\nwow\n").unwrap();
    assert!(out.contains("attr_set(&env.v_x.clone(), \"name\", Value::int(2i64))?;"));
    assert!(out.contains("attr_get_or_bind(&env.v_x.clone(), \"name\", &class_has_method)?"));
}

#[test]
fn attr_in_try_breaks_to_label() {
    let out = gen("such x = 1\npls\n    bark x.name\noh no err!\n    bark err\nwow\n").unwrap();
    assert!(out.contains(
        "match attr_get_or_bind(&env.v_x.clone(), \"name\", &class_has_method) { Ok(v) => v, Err(e) => break 'p0 Err(e) }"
    ));
}

#[test]
fn method_call_is_dynamic() {
    let out =
        gen("many S:\n    such go:\n        bark 1\n    wow\nwow\nsuch a = S()\na.go()\nwow\n")
            .unwrap();
    assert!(out.contains("call_method(env.v_a.clone(), \"go\", vec![], &mut *env)?"));
    assert!(out.contains("object_class_id(&recv)?"));
    assert!(out.contains(
        "if !matches!(recv, Value::Object(_)) { return builtin_method(&recv, name, args); }"
    ));
}

#[test]
fn self_resolves_to_param() {
    let out = gen("many Shibe:\n    such speak:\n        bark self\n    wow\nwow\nsuch k = Shibe()\nk.speak()\nwow\n").unwrap();
    assert!(out.contains("fn mb_0_speak(mut v_self: Value, env: &mut Env)"));
    assert!(out.contains("bark(&v_self.clone())"));
}

#[test]
fn constructor_arity_error_is_precise() {
    let err = gen("many Shibe:\n    such init much name, age:\n        self.name = name\n    wow\nwow\nsuch k = Shibe(\"x\")\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very args. much wrong.");
    assert_eq!(err.message, "Shibe takes 2 arguments, got 1");
    assert_eq!(err.hint.as_deref(), Some("Shibe(name, age)"));
}

#[test]
fn no_init_class_takes_no_args() {
    let out =
        gen("many Thing:\n    such go:\n        bark 1\n    wow\nwow\nsuch t = Thing()\nwow\n")
            .unwrap();
    assert!(out.contains("fn n_0(env: &mut Env) -> DogeResult<Value> {"));
    assert!(out.contains("let obj = Value::object(0u32, \"Thing\");"));
    assert!(!out.contains("mf_0_init"));
    let err =
        gen("many Thing:\n    such go:\n        bark 1\n    wow\nwow\nsuch t = Thing(1)\nwow\n")
            .unwrap_err();
    assert_eq!(err.message, "Thing takes 0 arguments, got 1");
    assert_eq!(err.hint.as_deref(), Some("Thing()"));
}

#[test]
fn class_as_value_constructs_a_class_value() {
    // A bare class name used as a value builds a `Value::class` over its
    // constructor arm — its id follows the builtins (there are eight).
    let out = gen("many Shibe:\n    such go:\n        bark 1\n    wow\nwow\nsuch g = Shibe\nwow\n")
        .unwrap();
    assert!(out.contains("env.v_g = Value::class(8u32, \"Shibe\");"));
}

#[test]
fn calling_a_class_value_dispatches_to_the_constructor() {
    // Calling the stored class value routes through `call_function`, whose ctor
    // arm checks arity against `init` and calls the class's `n_<id>` constructor.
    let out =
        gen("many Shibe:\n    such init much n:\n        self.n = n\n    wow\nwow\nsuch g = Shibe\nsuch s = g(1)\nwow\n")
            .unwrap();
    assert!(out.contains("Value::class(8u32, \"Shibe\")"));
    assert!(out.contains("8u32 => {"));
    assert!(out.contains("function_arity_error(\"Shibe\", 1usize, Some(1usize), args.len())"));
    assert!(out.contains("n_0(args.remove(0), &mut *env)"));
}

#[test]
fn assign_to_class_name_is_an_error() {
    let err = gen("many Shibe:\n    such go:\n        bark 1\n    wow\nwow\nvery Shibe = 5\nwow\n")
        .unwrap_err();
    assert_eq!(err.headline, "very object. much fixed.");
}

#[test]
fn nested_objdef_is_an_error() {
    let err = gen("such f:\n    many Inner:\n        such g:\n            bark 1\n        wow\n    wow\nwow\nwow\n").unwrap_err();
    assert_eq!(err.headline, "very nested. much object.");
}

/// A child dispatches its inherited methods to the ancestor that defines them,
/// while its own methods override. `B much A` inherits `foo` (defined by class 0)
/// and overrides `bar`.
#[test]
fn inherited_methods_dispatch_to_the_defining_class() {
    let src = "many A:\n    such foo:\n        return 1\n    wow\n    such bar:\n        return 2\n    wow\nwow\nmany B much A:\n    such bar:\n        return 3\n    wow\nwow\nsuch b = B()\nbark b.foo()\nbark b.bar()\nwow\n";
    let out = gen(src).unwrap();
    // B (class 1) inherits foo from A (class 0), overrides bar with its own mf_1_bar.
    assert!(
        out.contains("(1u32, \"foo\") => {"),
        "B dispatches foo:\n{out}"
    );
    assert!(
        out.contains("mf_0_foo(recv"),
        "B's foo calls A's wrapper mf_0_foo:\n{out}"
    );
    assert!(
        out.contains("(1u32, \"bar\") => {"),
        "B dispatches bar:\n{out}"
    );
    assert!(
        out.contains("mf_1_bar(recv"),
        "B's bar overrides with mf_1_bar:\n{out}"
    );
}

/// A child with no `init` of its own constructs through its parent's `init`.
#[test]
fn child_inherits_the_parent_init() {
    let src = "many A:\n    such init much name:\n        self.name = name\n    wow\nwow\nmany B much A:\n    such greet:\n        return self.name\n    wow\nwow\nsuch b = B(\"kabosu\")\nwow\n";
    let out = gen(src).unwrap();
    // n_1 (B's constructor) runs A's init wrapper, mf_0_init, on the new object.
    assert!(
        out.contains("fn n_1(v_name: Value, env: &mut Env)"),
        "B's constructor takes A's init parameters:\n{out}"
    );
    assert!(
        out.contains("mf_0_init(obj.clone()"),
        "B's constructor runs the inherited init:\n{out}"
    );
    // A wrong argument count is checked against the inherited init.
    let err = gen("many A:\n    such init much name:\n        self.name = name\n    wow\nwow\nmany B much A:\n    such greet:\n        return self.name\n    wow\nwow\nsuch b = B()\nwow\n").unwrap_err();
    assert_eq!(err.message, "B takes 1 argument, got 0");
}

/// `super.method(args)` resolves statically to the nearest ancestor that defines
/// the method, called with the current `self` as the receiver.
#[test]
fn super_call_targets_the_parent_wrapper() {
    let src = "many A:\n    such speak:\n        return 1\n    wow\nwow\nmany B much A:\n    such speak:\n        return super.speak()\n    wow\nwow\nsuch b = B()\nbark b.speak()\nwow\n";
    let out = gen(src).unwrap();
    assert!(
        out.contains("mf_0_speak(v_self.clone(), &mut *env)"),
        "super.speak() in B calls A's wrapper with self:\n{out}"
    );
}

/// `super` outside a method (or in a class without a parent) is caught by the
/// checker; codegen never reaches it in a checked program. Reaching codegen
/// directly still errors rather than panicking.
#[test]
fn super_without_a_parent_class_is_a_codegen_error() {
    let err =
        gen("many A:\n    such go:\n        return super.foo()\n    wow\nwow\nsuch a = A()\nwow\n")
            .unwrap_err();
    assert_eq!(err.headline, "very super. much orphan.");
}

#[test]
fn lines_static_escapes_quotes() {
    let out = gen("bark \"hi\"\nwow\n").unwrap();
    assert!(out.contains("static LINES: &[&str] = &["));
    assert!(out.contains(r#"    "bark \"hi\"","#));
}

#[test]
fn no_dispatcher_without_objects() {
    let out = gen("bark 1\nwow\n").unwrap();
    assert!(!out.contains("fn call_method"));
}

/// Build a two-file program by hand (no filesystem) to lock the module
/// name-mangling: a module's function is `f1_…`, its constant is a `g1_…`
/// `Env` field, and a multi-file program carries a `FILES` table.
#[test]
fn module_names_are_mangled_by_file_id() {
    use crate::modules::{Program, ProgramFile};
    let m_src = "so K = 7\nsuch sq much x:\n    return x * x\nwow\nwow\n";
    let e_src = "so m\nbark m.sq(3)\nbark m.K\nwow\n";
    let m = parse("m.doge", m_src).unwrap();
    let e = parse("app.doge", e_src).unwrap();
    let program = Program {
        files: vec![
            ProgramFile {
                file_id: 0,
                is_entry: true,
                name: "app".into(),
                path: "app.doge".into(),
                source: e_src.into(),
                script: e,
                stdlib_imports: vec![],
                user_imports: vec![("m".into(), 1)],
            },
            ProgramFile {
                file_id: 1,
                is_entry: false,
                name: "m".into(),
                path: "m.doge".into(),
                source: m_src.into(),
                script: m,
                stdlib_imports: vec![],
                user_imports: vec![],
            },
        ],
        init_order: vec![1],
    };

    let out = generate_program(&program).unwrap();
    assert!(
        out.contains("f_1_sq("),
        "module call is file-id mangled:\n{out}"
    );
    assert!(
        out.contains("env.g1_K"),
        "module const is a g1_ field:\n{out}"
    );
    assert!(
        out.contains("static FILES"),
        "multi-file uses a FILES table"
    );
    assert!(out.contains("env.cur_file ="), "multi-file tracks cur_file");
}

/// A caught error binds a structured value whose location is the raise site. A
/// single-file program embeds its one path; a multi-file program reads the path
/// from the `FILES` table by the runtime `cur_file`.
#[test]
fn try_in_multifile_reads_the_file_from_the_files_table() {
    use crate::modules::{Program, ProgramFile};
    let e_src = "so m\npls\n    such x = 1 // 0\noh no err!\n    bark err\nwow\n";
    let m_src = "so K = 7\nwow\n";
    let e = parse("app.doge", e_src).unwrap();
    let m = parse("m.doge", m_src).unwrap();
    let program = Program {
        files: vec![
            ProgramFile {
                file_id: 0,
                is_entry: true,
                name: "app".into(),
                path: "app.doge".into(),
                source: e_src.into(),
                script: e,
                stdlib_imports: vec![],
                user_imports: vec![("m".into(), 1)],
            },
            ProgramFile {
                file_id: 1,
                is_entry: false,
                name: "m".into(),
                path: "m.doge".into(),
                source: m_src.into(),
                script: m,
                stdlib_imports: vec![],
                user_imports: vec![],
            },
        ],
        init_order: vec![1],
    };

    let out = generate_program(&program).unwrap();
    assert!(
        out.contains("error_value(&e, FILES[env.cur_file as usize].0, env.cur_line)"),
        "multi-file catch reads the file from the FILES table:\n{out}"
    );
}

/// Objects from every file share one program-wide class-id space, and a module's
/// class is constructed by member. Two files may each define a `Shibe`: the
/// entry's is class 0, the module's is class 1, and `m.Shibe()` builds the latter.
#[test]
fn module_objects_get_global_class_ids() {
    use crate::modules::{Program, ProgramFile};
    let e_src =
        "so m\nmany Shibe:\n    such woof:\n        return 1\n    wow\nwow\nsuch a = Shibe()\nsuch b = m.Shibe()\nbark a.woof()\nbark b.bork_it()\nwow\n";
    let m_src = "many Shibe:\n    such bork_it:\n        return 2\n    wow\nwow\nwow\n";
    let e = parse("app.doge", e_src).unwrap();
    let m = parse("m.doge", m_src).unwrap();
    let program = Program {
        files: vec![
            ProgramFile {
                file_id: 0,
                is_entry: true,
                name: "app".into(),
                path: "app.doge".into(),
                source: e_src.into(),
                script: e,
                stdlib_imports: vec![],
                user_imports: vec![("m".into(), 1)],
            },
            ProgramFile {
                file_id: 1,
                is_entry: false,
                name: "m".into(),
                path: "m.doge".into(),
                source: m_src.into(),
                script: m,
                stdlib_imports: vec![],
                user_imports: vec![],
            },
        ],
        init_order: vec![1],
    };

    let out = generate_program(&program).unwrap();
    // The entry's Shibe is class 0, the module's is class 1 — distinct ids.
    assert!(
        out.contains("fn n_0("),
        "entry Shibe constructs via n_0:\n{out}"
    );
    assert!(
        out.contains("fn n_1("),
        "module Shibe constructs via n_1:\n{out}"
    );
    assert!(
        out.contains("fn mf_1_bork_it("),
        "module method is mf_1_:\n{out}"
    );
    // Name resolution is per-file: `Shibe()` in the entry stays n_0, and the
    // module-qualified `m.Shibe()` builds n_1.
    assert!(
        out.contains("Value::object(1u32, \"Shibe\")"),
        "module Shibe tags instances with class id 1:\n{out}"
    );
    // The dispatcher carries an arm for each class's own method.
    assert!(
        out.contains("(0u32, \"woof\")"),
        "class 0 dispatches woof:\n{out}"
    );
    assert!(
        out.contains("(1u32, \"bork_it\")"),
        "class 1 dispatches bork_it:\n{out}"
    );
}

#[test]
fn pack_zoom_emits_the_pup_trampoline() {
    let out = gen(
        "so pack\nsuch w much n:\n    return n\nwow\nsuch p = pack.zoom(w, [1])\nbark pack.fetch(p)\nwow\n",
    )
    .unwrap();
    // zoom is special-cased: it passes the generated trampoline and a globals
    // snapshot, and the trampoline pair is emitted.
    assert!(
        out.contains("pack_zoom(pup_entry, snapshot_env(&*env)"),
        "zoom call passes the trampoline and globals snapshot:\n{out}"
    );
    assert!(
        out.contains("fn snapshot_env(env: &Env) -> Packed {"),
        "snapshot_env is emitted:\n{out}"
    );
    assert!(
        out.contains(
            "fn pup_entry(globals: Packed, f: Packed, args: Vec<Packed>) -> Result<Packed, PackedError> {"
        ),
        "pup_entry trampoline is emitted:\n{out}"
    );
    // The snapshot packs the top-level binding `p`; the trampoline restores it.
    assert!(
        out.contains("pack_snapshot(&env.v_p)"),
        "snapshot_env packs each global:\n{out}"
    );
    // The pup runs the call through the same call_value shim any indirect call uses.
    assert!(
        out.contains("finish_pup(call_value("),
        "the pup runs its call through call_value:\n{out}"
    );
}

#[test]
fn a_program_without_pack_has_no_trampoline() {
    let out = gen("such age = 7\nbark age\nwow\n").unwrap();
    assert!(
        !out.contains("pup_entry") && !out.contains("snapshot_env"),
        "no pup trampoline when pack is unused:\n{out}"
    );
}