yomi-core 0.1.0

Core of the yomi scripting language: parser, type checker, VM and resumable saves
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
//! A compiled program (bytecode).

use std::sync::Arc;

use crate::pool::{Idx, Pool};
use crate::value::{Ty, TypeDecl, Value};

/// The reference to the program that a `Vm` holds. Either a `Program` borrowed from the host, or an extended program
/// built (and owned) by the REPL (section 7.8 of `spec/host/07-repl.md`).
#[derive(Debug, Clone)]
pub(crate) enum ProgRef<'a> {
    /// Borrows the `Program` given by the host.
    Borrowed(&'a Program),
    /// An extended program built by the REPL (shared between copies).
    Shared(Arc<Program>),
}

impl std::ops::Deref for ProgRef<'_> {
    type Target = Program;

    fn deref(&self) -> &Program {
        match self {
            ProgRef::Borrowed(p) => p,
            ProgRef::Shared(p) => p,
        }
    }
}

/// A register number (an index into the frame's array, where temporaries follow the local variables).
pub(crate) type Reg = u16;

/// A register-based instruction. Values are passed through registers (`Reg`). Variable-length data is held as indices
/// into the tables of [`RegCode`]. The place for return values and resume values is the `dst` of the instruction that
/// is returned to (`Call`, `CallFn`, `Stop`, `ChoiceStop`).
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum RegOp {
    Int {
        dst: Reg,
        n: i64,
    },
    Const {
        dst: Reg,
        k: Idx<Value>,
    },
    /// Copies to a temporary (the size is not counted).
    Move {
        dst: Reg,
        src: Reg,
    },
    /// Copies into a local variable (the size is counted).
    Set {
        dst: Reg,
        src: Reg,
    },
    /// Moves the value of a temporary into a local variable (the size is counted; the temporary becomes `()`).
    SetMove {
        dst: Reg,
        src: Reg,
    },
    LoadGlobal {
        dst: Reg,
        g: u32,
    },
    StoreGlobal {
        g: u32,
        src: Reg,
    },
    LoadHost {
        dst: Reg,
        h: u32,
    },
    StoreHost {
        h: u32,
        src: Reg,
    },
    /// Reads the value reached from a variable through `struct` fields (without copying the whole value of the variable).
    LoadField {
        dst: Reg,
        place: PlaceRef,
        path: Idx<Box<[u32]>>,
    },
    GetField {
        dst: Reg,
        src: Reg,
        field: u32,
    },
    IdField {
        dst: Reg,
        src: Reg,
        name: bool,
    },
    AddI {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    SubI {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    MulI {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    DivI {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    RemI {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    AddIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    SubIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    MulIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    DivIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    RemIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    LtI {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    LeI {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    LtIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    LeIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    GtIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    GeIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    EqIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    NeIK {
        dst: Reg,
        a: Reg,
        k: i64,
    },
    /// `float` arithmetic (no `%`).
    ArithF {
        op: ArithOp,
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    /// `float` ordering comparison (`a < b`, `a <= b`; `>` and `>=` swap the operands).
    LtF {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    LeF {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    /// Equality of values (any type).
    Eq {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    Ne {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    /// Negation (`int` and `float`).
    Neg {
        dst: Reg,
        a: Reg,
    },
    Not {
        dst: Reg,
        a: Reg,
    },
    ToInt {
        dst: Reg,
        a: Reg,
    },
    ToFloat {
        dst: Reg,
        a: Reg,
    },
    ToStr {
        dst: Reg,
        a: Reg,
    },
    /// Concatenates the strings in the `n` registers starting at `start`.
    Concat {
        dst: Reg,
        start: Reg,
        n: u16,
    },
    Range {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    Random {
        dst: Reg,
        a: Reg,
        b: Reg,
    },
    RandomFloat {
        dst: Reg,
    },
    Jump {
        to: u32,
    },
    JumpIfFalse {
        cond: Reg,
        to: u32,
    },
    JumpIfTrue {
        cond: Reg,
        to: u32,
    },
    /// Integer comparison and conditional jump (jumps to `to` if `a < b` is false). The form used for the conditions of `if`
    /// and `while` that does not put the comparison result in a temporary.
    JumpUnlessLtI {
        a: Reg,
        b: Reg,
        to: u32,
    },
    JumpUnlessLeI {
        a: Reg,
        b: Reg,
        to: u32,
    },
    JumpUnlessLtIK {
        a: Reg,
        k: i64,
        to: u32,
    },
    JumpUnlessLeIK {
        a: Reg,
        k: i64,
        to: u32,
    },
    JumpUnlessGtIK {
        a: Reg,
        k: i64,
        to: u32,
    },
    JumpUnlessGeIK {
        a: Reg,
        k: i64,
        to: u32,
    },
    JumpUnlessEqIK {
        a: Reg,
        k: i64,
        to: u32,
    },
    JumpUnlessNeIK {
        a: Reg,
        k: i64,
        to: u32,
    },
    MakeList {
        dst: Reg,
        start: Reg,
        n: u16,
    },
    MakeSome {
        dst: Reg,
        a: Reg,
    },
    MakeStruct {
        dst: Reg,
        start: Reg,
        ty: u32,
        order: Idx<Vec<u32>>,
    },
    MakeEnum {
        dst: Reg,
        start: Reg,
        n: u16,
        ty: u32,
        variant: u32,
    },
    /// Makes a `map` from the `2 * n` registers starting at `start` (key then value).
    MakeMap {
        dst: Reg,
        start: Reg,
        n: u16,
    },
    Index {
        dst: Reg,
        base: Reg,
        index: Reg,
    },
    MapGet {
        dst: Reg,
        map: Reg,
        key: Reg,
    },
    MapHas {
        dst: Reg,
        map: Reg,
        key: Reg,
    },
    MapKeys {
        dst: Reg,
        map: Reg,
    },
    MapValues {
        dst: Reg,
        map: Reg,
    },
    Len {
        dst: Reg,
        a: Reg,
    },
    IsEmpty {
        dst: Reg,
        a: Reg,
    },
    ListContains {
        dst: Reg,
        list: Reg,
        item: Reg,
    },
    /// A stable ascending copy of `list`, ordered by `keys` (the same register as `list` for a plain sort).
    ListSortBy {
        dst: Reg,
        list: Reg,
        keys: Reg,
    },
    ListReverse {
        dst: Reg,
        list: Reg,
    },
    /// The total of `list`. `float` is whether the elements are `float` (the total of an empty list is `0.0` or `0`).
    ListSum {
        dst: Reg,
        list: Reg,
        float: bool,
    },
    /// The first element of `list` with the least (or greatest, if `max`) key in `keys`, as an `Option`.
    ListExtreme {
        dst: Reg,
        list: Reg,
        keys: Reg,
        max: bool,
    },
    /// Moves to `next` if `src` is not equal to the constant.
    MatchLit {
        src: Reg,
        k: Idx<Value>,
        next: u32,
    },
    MatchSome {
        src: Reg,
        next: u32,
    },
    MatchNone {
        src: Reg,
        next: u32,
    },
    MatchVariant {
        src: Reg,
        name: Idx<String>,
        next: u32,
    },
    /// Discards a temporary (discards the subject of a `match` on entering an arm).
    Clear {
        r: Reg,
    },
    /// The `i`-th content of `src` (a `Some` or an `enum`).
    Payload {
        dst: Reg,
        src: Reg,
        i: u32,
    },
    /// The next element of a `for` (`list`, `idx` and `var` are local variables). Moves to `end` when finished.
    ForNext {
        list: Reg,
        idx: Reg,
        var: Reg,
        end: u32,
    },
    /// Same as `ForNext`, and also puts the index in `key` (`for i, x in xs`).
    ForNextAt {
        list: Reg,
        idx: Reg,
        var: Reg,
        key: Reg,
        end: u32,
    },
    /// Compacts the holes left by removals in the copy of the `map` that a `for` iterates.
    MapCompact {
        map: Reg,
    },
    /// Puts the pair at position `idx` of the `map` into `key` and `var`, and advances `idx`. Moves to `end` if there is none.
    ForMapNext {
        map: Reg,
        idx: Reg,
        key: Reg,
        var: Reg,
        end: u32,
    },
    /// `for i in range(a, b)`: if `idx` is less than `limit`, puts it in `var` and advances; otherwise moves to `end`.
    ForRange {
        idx: Reg,
        limit: Reg,
        var: Reg,
        end: u32,
    },
    /// Updates a place in place. Starting at `start`, the indices and keys (in the order of the path's `Index` and `Key`)
    /// come first, followed by the update values.
    Mutate {
        place: PlaceRef,
        plan: Idx<(Vec<PathStep>, Action)>,
        start: Reg,
    },
    ArgsBegin,
    TextBegin,
    /// Records the string in `src` as the value of a `${expr}` of the current body.
    Capture {
        src: Reg,
    },
    /// An effect command. If it has a return value, puts it in `dst`.
    CallEffect {
        dst: Reg,
        start: Reg,
        n: u16,
        native: u32,
    },
    CallQuery {
        dst: Reg,
        start: Reg,
        n: u16,
        native: u32,
    },
    Seen {
        dst: Reg,
        a: Reg,
    },
    Visited {
        dst: Reg,
        a: Reg,
    },
    OnceAvailable {
        dst: Reg,
        key: Idx<String>,
    },
    MarkSeen {
        key: Idx<String>,
    },
    MarkOnce {
        key: Idx<String>,
    },
    ChoiceBegin,
    ChoiceItem {
        text: Reg,
        id: Reg,
        orig: u32,
    },
    JumpLabel {
        label: u32,
    },
    /// Calls a label. The `n` registers from `start` are the arguments. The return value goes into `dst`.
    Call {
        dst: Reg,
        start: Reg,
        n: u16,
        label: u32,
        site: u32,
    },
    CallFn {
        dst: Reg,
        start: Reg,
        n: u16,
        f: u32,
    },
    /// Calls a function value (`callee`). The `n` registers from `start` are the arguments. The return value goes into `dst`.
    CallValue {
        dst: Reg,
        callee: Reg,
        start: Reg,
        n: u16,
    },
    /// Makes a function value. The `n` registers from `start` are the captured values.
    MakeFn {
        dst: Reg,
        start: Reg,
        n: u16,
        f: u32,
    },
    ReturnFn {
        src: Option<Reg>,
    },
    Return {
        src: Option<Reg>,
    },
    /// Stops at a stop command. The `n` registers from `start` are the arguments. The resume value goes into `dst` (only for
    /// stop points that receive it).
    Stop {
        dst: Reg,
        start: Reg,
        n: u16,
        cmd: u32,
        stop: u32,
    },
    /// Stops at a choice. On resume, the number of the chosen option (in the order written) goes into `dst`.
    ChoiceStop {
        dst: Reg,
        cmd: u32,
        stop: u32,
    },
    /// Moves to the position numbered by `src`.
    JumpTable {
        src: Reg,
        table: Idx<Vec<u32>>,
    },
    /// The end of REPL input. For expression input, keeps its value (`src`).
    ReplDone {
        src: Option<Reg>,
    },
    End,
}

/// The register-based code of one label or `fn`.
#[derive(Debug, Clone, PartialEq, Default)]
pub(crate) struct RegCode {
    pub ops: Vec<RegOp>,
    /// The number of registers (local variables and temporaries).
    pub regs: u32,
    /// The number of local variables (registers after these are temporaries).
    pub vars: u32,
    pub consts: Pool<Value>,
    pub paths: Pool<Box<[u32]>>,
    pub orders: Pool<Vec<u32>>,
    pub names: Pool<String>,
    pub plans: Pool<(Vec<PathStep>, Action)>,
    pub tables: Pool<Vec<u32>>,
}

/// The variable of the place to update.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PlaceRef {
    Local(u32),
    Global(u32),
    Host(u32),
}

/// One step of the path to a place.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PathStep {
    /// The `i`-th field of a `struct`.
    Field(u32),
    /// An index into a `list` (the value is taken from the stack).
    Index,
    /// A key of a `map` (the value is taken from the stack). A `Set` at the last step appends the key if it is missing.
    Key,
}

/// The kind of update to a place.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Action {
    /// Assignment.
    Set,
    /// `place op= value` (`op` is `ArithOp::Add` and so on).
    Arith(ArithOp),
    /// Appends to the end of a `list`.
    Push,
    /// Inserts at an index of a `list` (`0 <= i <= len`). The operands are the value (top) and the index (below it).
    Insert,
    /// Removes the element at an index of a `list` (for a `map`, removes the pair with the key; does nothing if missing).
    Remove,
    /// Empties a `list` or `map`.
    Clear,
    /// Shuffles the elements of a `list` with the core's random generator (Fisher–Yates).
    Shuffle,
}

/// The operation of a compound assignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ArithOp {
    Add,
    Sub,
    Mul,
    Div,
    Rem,
}

/// The kind of stop point: a sugar (plain line, quote line, choice) or a command written with `@`. Text tables
/// (`spec/host/05-texts.md`) replace only the bodies of sugar.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StopKind {
    /// A stop command called with `@` (its arguments are not replaced).
    #[default]
    Command,
    /// A plain line (argument 0 is the body).
    Plain,
    /// A quote line (argument 1 is the body).
    Quote,
    /// A choice (argument 0 is the `list` of option texts, argument 1 the `list` of stable IDs).
    Choice,
}

impl StopKind {
    /// The stable English name.
    pub fn code(self) -> &'static str {
        match self {
            StopKind::Command => "command",
            StopKind::Plain => "plain",
            StopKind::Quote => "quote",
            StopKind::Choice => "choice",
        }
    }
}

/// A part of the original text of a body (`${expr}` is only a number; the expression itself is not kept).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TextPart {
    /// Characters (after unescaping).
    Lit(String),
    /// One `${expr}`.
    Interp,
}

/// Information about a stop point.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct StopInfo {
    /// The kind of sugar (`@` commands are `Command`).
    pub kind: StopKind,
    /// The original text of the body of a plain or quote line (empty otherwise). For a command with `text_arg`, the original
    /// text of that argument.
    pub text: Vec<TextPart>,
    /// A stop point of a command whose arguments can be translated by a text table (when an argument that is a `text_param`
    /// in the registry is written as a string literal). The argument number, and its number in the record of `${expr}` values
    /// (`StopRecord::values`).
    pub text_arg: Option<TextArg>,
    /// The start of the instructions that compute the arguments.
    pub args_pc: u32,
    /// The position of the stopping instruction.
    pub pc: u32,
    /// The stable ID.
    pub id: Option<String>,
    /// The content hash (command name + original text of the arguments).
    pub hash: u64,
    /// Line.
    pub line: u32,
    /// For a choice stop point, information about its options.
    pub choice: Option<ChoiceInfo>,
    /// Whether the stop point receives the return value in a variable (`let name = @command(...)`, `name = @command(...)`).
    /// If true, the resume value (`resume`) is pushed onto the stack, and the instruction after the stop stores it in the
    /// variable.
    pub binds: bool,
}

/// An argument of a command that can be translated by a text table (section 5.2a of `spec/host/05-texts.md`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct TextArg {
    /// The argument number.
    pub arg: u32,
    /// The number in the record of `${expr}` values (only string literal arguments are counted).
    pub values: u32,
}

/// One identity declaration (the display name can be translated by a text table; section 5.2a of
/// `spec/host/05-texts.md`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct IdentityInfo {
    /// The declaring module (an index into `Program::modules`).
    pub module: u32,
    /// The declared name (`案内人`).
    pub name: String,
    /// The display name (original text).
    pub display: String,
    /// The display name as parts of the original text of a body (has no `${expr}`).
    pub parts: Vec<TextPart>,
}

/// Information about the options of a choice stop point (in the order written).
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ChoiceInfo {
    pub options: Vec<ChoiceOptInfo>,
}

/// Information about one option.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ChoiceOptInfo {
    /// The stable ID.
    pub id: Option<String>,
    /// The original text of the option.
    pub text: Vec<TextPart>,
    /// The line of the option.
    pub line: u32,
}

/// A local variable.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct LocalInfo {
    pub name: String,
    pub ty: Ty,
}

/// Information about where a `call` was placed (used on load to find the position of the caller).
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CallSite {
    /// The position of the `Call` instruction. Execution returns to the one after it.
    pub pc: u32,
    /// The content hash (`call` + how the called label is written + original text of the arguments).
    pub hash: u64,
    /// The called label (a sequential number across the whole program).
    pub target: u32,
    /// Line.
    pub line: u32,
}

/// The code of a label.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct LabelCode {
    /// The module it belongs to (an index into `Program::modules`).
    pub module: u32,
    pub name: String,
    pub reg: Box<RegCode>,
    /// The line number of each instruction.
    pub lines: Vec<u32>,
    /// Local variables. The first `params` are the parameters.
    pub locals: Vec<LocalInfo>,
    /// The number of parameters.
    pub params: u32,
    /// The return type.
    pub ret: Option<Ty>,
    pub stops: Vec<StopInfo>,
    pub calls: Vec<CallSite>,
}

impl LabelCode {
    /// Whether the label can only be called with `call` (it has parameters or a return type).
    pub fn callable(&self) -> bool {
        self.params > 0 || self.ret.is_some()
    }
}

/// The code of a `fn`. Unlike a label, it has neither stop points nor `call` positions (it does not go into saves).
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct FnCode {
    /// The module it belongs to (an index into `Program::modules`).
    pub module: u32,
    pub name: String,
    pub reg: Box<RegCode>,
    pub lines: Vec<u32>,
    /// Local variables. The first `params` are the parameters, followed by `captures` captured values.
    pub locals: Vec<LocalInfo>,
    /// The number of parameters.
    pub params: u32,
    /// The number of captured values (only for lambdas; copied from the function value).
    pub captures: u32,
    /// The return type.
    pub ret: Option<Ty>,
}

/// A module variable.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct GlobalInfo {
    /// The module it belongs to (an index into `Program::modules`).
    pub module: u32,
    /// Whether it is a `persistent var` (false means `save var`). It goes into persistent data, not into saves.
    pub persistent: bool,
    pub name: String,
    pub ty: Ty,
    pub init: Value,
}

/// The compiled `migrate` blocks and the frozen schemas they use (`spec/language/07-migrate.md`).
#[derive(Debug, Clone, PartialEq, Default)]
pub(crate) struct MigrateCode {
    /// In version order.
    pub blocks: Vec<MigrateBlock>,
    /// The frozen schemas, by version.
    pub schemas: std::collections::BTreeMap<u32, FrozenSchema>,
}

/// One `migrate N { … }` block.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct MigrateBlock {
    pub version: u32,
    pub ops: Vec<MigrateOp>,
}

/// One statement of a `migrate` block. Variable names are `module.variable`.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum MigrateOp {
    Rename {
        old: String,
        new: String,
        persistent: bool,
    },
    Convert {
        old: String,
        new: String,
        persistent: bool,
        /// The types as written, without version prefixes.
        old_ty: String,
        new_ty: String,
        /// The hidden label that evaluates the expression (parameter 0 is the old value; the result is left on the stack).
        label: usize,
        /// Whether the new side is a frozen schema (otherwise the current scripts).
        new_frozen: bool,
    },
    Drop {
        old: String,
        persistent: bool,
    },
}

/// A frozen schema as the program keeps it: the shapes of the declarations (as in `TypeDecl::signature`), for expanding the
/// types of variables, and the names of the types that have a versioned copy (`v2::game.Item`) for running `convert`.
#[derive(Debug, Clone, PartialEq, Default)]
pub(crate) struct FrozenSchema {
    pub decls: Vec<(String, String)>,
    pub versioned: Vec<String>,
}

/// A compiled program (one or more modules).
#[derive(Debug, Clone, PartialEq)]
pub struct Program {
    /// The names of the modules (in the order given).
    pub(crate) modules: Vec<String>,
    /// The variables of all modules (in module order, then declaration order).
    pub(crate) globals: Vec<GlobalInfo>,
    /// The labels of all modules (in module order, then declaration order).
    pub(crate) labels: Vec<LabelCode>,
    /// The `fn`s of all modules (in module order, then declaration order).
    pub(crate) fns: Vec<FnCode>,
    /// The names of the stop commands used.
    pub(crate) commands: Vec<String>,
    /// The names of the effect and query commands used.
    pub(crate) natives: Vec<String>,
    /// The names of the host-declared variables used.
    pub(crate) host_vars: Vec<String>,
    /// The `struct`s and `enum`s declared by users (in module order, then declaration order).
    pub(crate) types: Vec<TypeDecl>,
    /// The lines that can have stable IDs (in script order; used by `assign-ids`).
    pub(crate) id_sites: Vec<IdSite>,
    /// Identity declarations (in module order, then declaration order).
    pub(crate) identities: Vec<IdentityInfo>,
    /// The compiled sources (in the same order, from the start of `modules`). Kept so that the REPL can build an extended
    /// program with added declarations. In the REPL's extended program, the `<repl>` module is not included (shorter than
    /// `modules`).
    pub(crate) sources: Vec<String>,
    pub(crate) schema_version: u32,
    pub(crate) min_save_version: u32,
    pub(crate) migrate: MigrateCode,
}

/// One line that can have a stable ID (a plain line, quote line, option text or stop command line).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct IdSite {
    /// The module it belongs to (an index into `Program::modules`).
    pub module: u32,
    /// The name of the label it belongs to.
    pub label: String,
    /// The kind (option texts are `Choice`).
    pub kind: StopKind,
    /// The stable ID written.
    pub id: Option<String>,
    /// When there is no ID, the position to append `#id` (line and column; columns are 1-based in characters).
    pub line: u32,
    pub col: u32,
}

impl Program {
    /// The schema version of the scripts (the largest `N` of the `migrate N` blocks; 1 when there are none).
    pub fn schema_version(&self) -> u32 {
        self.schema_version
    }

    /// The oldest save schema version that can still be loaded (`min_save_version`; 1 when not written).
    pub fn min_save_version(&self) -> u32 {
        self.min_save_version
    }

    /// Declarations of `struct`s and `enum`s declared by users (in module order, then declaration order).
    pub fn types(&self) -> &[TypeDecl] {
        &self.types
    }

    /// Keys and original text parts of the bodies to be replaced. In script order: display names of identities
    /// (`identity:module.name`, in declaration order), followed by, in the order of modules, labels, stop points and options
    /// as written, plain lines, quote lines and option texts with stable IDs, and stop command lines whose translatable
    /// arguments (such as input prompts) are written as string literals.
    pub(crate) fn text_entries(&self) -> Vec<(String, &[TextPart])> {
        let mut out = Vec::new();
        for d in &self.identities {
            out.push((
                identity_key(&self.modules[d.module as usize], &d.name),
                d.parts.as_slice(),
            ));
        }
        for l in &self.labels {
            let path = format!("{}.{}", self.modules[l.module as usize], l.name);
            for st in &l.stops {
                if let Some(choice) = &st.choice {
                    for o in &choice.options {
                        if let Some(id) = &o.id {
                            out.push((format!("{path}#{id}"), o.text.as_slice()));
                        }
                    }
                } else if (st.kind != StopKind::Command || st.text_arg.is_some())
                    && let Some(id) = &st.id
                {
                    out.push((format!("{path}#{id}"), st.text.as_slice()));
                }
            }
        }
        out
    }

    /// Finds the stop point of a command with translatable arguments from one history entry (`module.label` of the label and
    /// the stable ID of the stop point).
    pub(crate) fn command_text_arg(&self, label: &str, id: &str) -> Option<TextArg> {
        let (module, name) = label.split_once('.')?;
        let l = self
            .labels
            .iter()
            .find(|l| l.name == name && self.modules[l.module as usize] == module)?;
        l.stops
            .iter()
            .find(|st| st.kind == StopKind::Command && st.id.as_deref() == Some(id))
            .and_then(|st| st.text_arg)
    }

    /// The text table key for the `Identity` of a quote line (the declared name and the original display name). `module` is
    /// the name of the module containing the quote line. When multiple modules have declarations with the same name and
    /// display name, the one in `module` comes first.
    pub(crate) fn identity_key_of(&self, module: &str, id: &str, display: &str) -> Option<String> {
        let mut found = self
            .identities
            .iter()
            .filter(|d| d.name == id && d.display == display)
            .map(|d| self.modules[d.module as usize].as_str());
        let first = found.next()?;
        let m = if first == module || found.any(|m| m == module) {
            module
        } else {
            first
        };
        Some(identity_key(m, id))
    }

    /// The name of the first module (for a program with one module, that module).
    pub fn module(&self) -> &str {
        self.modules.first().map_or("", String::as_str)
    }

    /// The list of module names (in the order passed to compilation).
    pub fn module_names(&self) -> impl Iterator<Item = &str> {
        self.modules.iter().map(String::as_str)
    }

    /// The list of label names of the first module (in declaration order).
    pub fn label_names(&self) -> impl Iterator<Item = &str> {
        self.labels
            .iter()
            .filter(|l| l.module == 0)
            .map(|l| l.name.as_str())
    }

    /// The list of label names of the given module (in declaration order). Empty if the module does not exist.
    pub fn labels_of(&self, module: &str) -> Vec<&str> {
        let Some(idx) = self.modules.iter().position(|m| m == module) else {
            return Vec::new();
        };
        self.labels
            .iter()
            .filter(|l| l.module as usize == idx)
            .map(|l| l.name.as_str())
            .collect()
    }
}

/// The text table key for the display name of an identity (`identity:module.name`; line keys contain `#`, so they do not
/// collide).
pub(crate) fn identity_key(module: &str, name: &str) -> String {
    format!("identity:{module}.{name}")
}

/// FNV-1a (64 bits). Gives the same value on every platform.
pub(crate) fn fnv1a(parts: &[&[u8]]) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    for p in parts {
        for b in *p {
            h ^= u64::from(*b);
            h = h.wrapping_mul(0x0000_0100_0000_01b3);
        }
        h ^= 0xff;
        h = h.wrapping_mul(0x0000_0100_0000_01b3);
    }
    h
}