retex-engine 1.3.0-alpha.2

The TeX typesetting engine for retex
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
//! The table of equivalents (TeX's `eqtb`) and its storage.
//!
//! TeX keeps every quantity that obeys grouping — meanings of control
//! sequences, registers, parameters, character codes, fonts — in one big
//! array. Each entry carries the group *level* at which it was last defined.
//! retex keeps the quantities in typed vectors instead. [`Loc`] addresses any
//! one of them, so that the save stack ([`crate::save`]) can restore values
//! generically.

use std::collections::HashMap;
use std::rc::Rc;

use crate::cmd::Cmd;
use crate::glue::Glue;
use crate::hash::CsId;
use crate::nodes::NodeRef;
use crate::primitives_gen as pg;
use crate::token::{Macro, TokenList, empty_list};

/// The level of the outermost group (TeX's `level_one`).
pub const LEVEL_ONE: u16 = 1;
/// The level of undefined entries (TeX's `level_zero`).
pub const LEVEL_ZERO: u16 = 0;

/// Number of registers of each kind (LuaTeX: 65536).
pub const NUM_REGISTERS: usize = 65536;
/// Number of math families (LuaTeX: 256).
pub const NUM_FAMILIES: usize = 256;
/// The value of an unset attribute.
pub const UNUSED_ATTRIBUTE: i32 = -0x7FFF_FFFF;

/// The meaning of a control sequence: command code, modifier and, for
/// macros, the definition.
#[derive(Clone, Debug)]
pub struct Meaning {
    /// The command code (`eq_type`).
    pub cmd: Cmd,
    /// The modifier (`equiv` for non-macro commands).
    pub chr: u32,
    /// The definition, for `call` commands.
    pub mac: Option<Rc<Macro>>,
}

impl Meaning {
    /// The meaning of an undefined control sequence.
    pub const UNDEFINED: Meaning = Meaning {
        cmd: Cmd::UndefinedCs,
        chr: 0,
        mac: None,
    };

    /// A primitive or other non-macro meaning.
    pub const fn prim(cmd: Cmd, chr: u32) -> Meaning {
        Meaning {
            cmd,
            chr,
            mac: None,
        }
    }

    /// Identity of meanings as TeX's `eq_define` tests it (same command
    /// and same equivalent; macros compare by reference).
    pub fn same_as(&self, other: &Meaning) -> bool {
        self.cmd == other.cmd
            && self.chr == other.chr
            && match (&self.mac, &other.mac) {
                (None, None) => true,
                (Some(a), Some(b)) => Rc::ptr_eq(a, b),
                _ => false,
            }
    }
}

/// A value together with the level at which it was defined.
#[derive(Clone, Debug)]
pub struct Slot<T> {
    /// The value.
    pub val: T,
    /// The group level of its definition.
    pub level: u16,
}

impl<T> Slot<T> {
    fn new(val: T) -> Self {
        Slot {
            val,
            level: LEVEL_ONE,
        }
    }
}

/// Kinds of per-character code tables, other than category codes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CodeKind {
    /// `\lccode`.
    Lc,
    /// `\uccode`.
    Uc,
    /// `\sfcode`.
    Sf,
    /// `\mathcode` / `\Umathcode` (packed, see [`crate::codes`]).
    Math,
    /// `\delcode` / `\Udelcode` (packed, see [`crate::codes`]).
    Del,
}

/// A location in the table of equivalents.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Loc {
    /// The meaning of a control sequence.
    Cs(CsId),
    /// An integer parameter (`assign_int` index).
    IntPar(u32),
    /// A `\count` register.
    Count(u32),
    /// An `\attribute` register.
    Attr(u32),
    /// A dimension parameter (`assign_dimen` index).
    DimenPar(u32),
    /// A `\dimen` register.
    Dimen(u32),
    /// A glue parameter (`assign_glue` index) or, offset by
    /// [`MU_GLUE_OFFSET`], a muglue parameter.
    GluePar(u32),
    /// A `\skip` register.
    Skip(u32),
    /// A `\muskip` register.
    MuSkip(u32),
    /// A token-list parameter (`assign_toks` index).
    ToksPar(u32),
    /// A `\toks` register.
    Toks(u32),
    /// A `\box` register.
    Box(u32),
    /// The current font.
    CurFont,
    /// A math font: `size * NUM_FAMILIES + family` (size 0 text, 1 script,
    /// 2 scriptscript).
    MathFont(u32),
    /// `\parshape`.
    ParShape,
    /// An e-TeX penalty array (`set_etex_shape` index).
    EtexShape(u32),
    /// A category code in catcode table `table`.
    Cat {
        /// The catcode table number.
        table: u32,
        /// The character.
        ch: u32,
    },
    /// Another character code.
    Code(CodeKind, u32),
    /// A math parameter (`\Umath…`): `param * 8 + style`.
    MathParam(u32),
    /// `\localleftbox` (0) or `\localrightbox` (1).
    LocalBox(u32),
    /// A direction parameter (`assign_dir` index: `\pagedir` …
    /// `\linedir`).
    DirPar(u32),
    /// LuaTeX's group-local counters `\nolocalwhatsits` (0) and
    /// `\nolocaldirs` (1), seen only in tracing (see `dirs`).
    LocalCount(u32),
}

/// Number of integer parameters, including LuaTeX's internal backend
/// integers (`\pdfvariable compresslevel` is index 137 … 164).
pub const INT_PARS: usize = 165;
/// Number of dimension parameters, including backend dimensions (28–33).
pub const DIMEN_PARS: usize = 34;
/// Number of token-list parameters, including backend token lists (11–17).
pub const TOKS_PARS: usize = 18;

/// Offset added to `assign_mu_glue` indices so that glue and muglue
/// parameters share one vector.
pub const MU_GLUE_OFFSET: u32 = pg::assign_glue::COUNT;

/// A saved value, as stored on the save stack.
#[derive(Clone, Debug)]
pub enum Val {
    /// A meaning.
    Meaning(Meaning),
    /// An integer, dimension or packed code.
    Int(i64),
    /// Glue.
    Glue(Glue),
    /// A token list.
    Toks(TokenList),
    /// A box (node list) or null.
    Node(NodeRef),
    /// A shape array (`\parshape` or penalties), or none.
    Shape(Option<Rc<[i32]>>),
}

/// A table of per-character codes with a default function.
///
/// Storage is a two-level page table: 256-character pages, allocated on
/// first assignment and filled with the defaults. Lookups are two array
/// indexings, with no hashing, which matters because the scanner consults
/// these tables for every character.
#[derive(Clone, Debug)]
pub struct CodeTable {
    /// Pages are shared between tables (catcode tables are mostly copies
    /// of one another) and copied on first write.
    pages: Vec<Option<Rc<Page>>>,
    default: fn(u32) -> i64,
}

/// Characters per page.
const PAGE: usize = 256;
/// One page of a code table.
pub type Page = [Slot<i64>; PAGE];

impl CodeTable {
    /// A table where every character has value `default(c)`.
    pub fn new(default: fn(u32) -> i64) -> Self {
        let mut t = CodeTable {
            pages: Vec::new(),
            default,
        };
        // The ASCII/Latin-1 page is always present.
        t.page_mut(0);
        t
    }

    /// The pages allocated, by number (see [`crate::fingerprint`]).
    pub fn pages(&self) -> &[Option<Rc<Page>>] {
        &self.pages
    }

    fn page_mut(&mut self, c: u32) -> &mut Page {
        let k = c as usize / PAGE;
        if self.pages.len() <= k {
            self.pages.resize_with(k + 1, || None);
        }
        let default = self.default;
        let page = self.pages[k].get_or_insert_with(|| {
            let base = (k * PAGE) as u32;
            Rc::new(std::array::from_fn(|i| Slot::new(default(base + i as u32))))
        });
        Rc::make_mut(page)
    }

    #[inline]
    fn slot(&self, c: u32) -> Option<&Slot<i64>> {
        match self.pages.get(c as usize / PAGE) {
            Some(Some(p)) => Some(&p[c as usize % PAGE]),
            _ => None,
        }
    }

    /// The value for `c`.
    #[inline]
    pub fn get(&self, c: u32) -> i64 {
        self.slot(c).map_or_else(|| (self.default)(c), |s| s.val)
    }

    /// The level of the definition for `c`.
    pub fn level(&self, c: u32) -> u16 {
        self.slot(c).map_or(LEVEL_ONE, |s| s.level)
    }

    /// Assigns `val` to `c` at level `cur` when that needs no save-stack
    /// entry (the value is unchanged, or was defined at level `cur`, or
    /// `cur` is level one). Returns `false`, changing nothing, otherwise.
    #[inline]
    pub fn define_in_place(&mut self, c: u32, val: i64, cur: u16) -> bool {
        let (old, level) = match self.slot(c) {
            Some(s) => (s.val, s.level),
            None => ((self.default)(c), LEVEL_ONE),
        };
        if old == val {
            return true;
        }
        if level != cur && cur > LEVEL_ONE {
            return false;
        }
        let s = &mut self.page_mut(c)[c as usize % PAGE];
        s.val = val;
        s.level = cur;
        true
    }

    /// `\lccode c = c` (or the like) for every `c < upto`, at level `cur`
    /// (above level one, in a group where no character has been assigned
    /// yet): the bulk form of that many local assignments. Returns the
    /// table as it was, from which the save-stack entry restores exactly the
    /// characters whose value changed.
    pub fn define_identity(&mut self, upto: u32, cur: u16) -> CodeTable {
        let old = self.clone();
        for c in 0..upto {
            if self.get(c) != i64::from(c) {
                self.page_mut(c)[c as usize % PAGE] = Slot {
                    val: i64::from(c),
                    level: cur,
                };
            }
        }
        old
    }

    /// Undoes [`define_identity`](Self::define_identity) at the end of
    /// the group, as the individual save-stack entries would: every
    /// character it changed gets its old value and level back, unless it
    /// has been assigned globally since.
    pub fn restore_identity(&mut self, old: &CodeTable, upto: u32) {
        for c in 0..upto {
            let val = old.get(c);
            if val != i64::from(c) && self.level(c) != LEVEL_ONE {
                self.set(c, val, old.level(c));
            }
        }
    }

    /// Sets value and level for `c`.
    pub fn set(&mut self, c: u32, val: i64, level: u16) {
        self.page_mut(c)[c as usize % PAGE] = Slot { val, level };
    }

    /// The default function of this table.
    pub fn default_fn(&self) -> fn(u32) -> i64 {
        self.default
    }

    /// The values of every allocated page that differs from the defaults,
    /// as (page number, values), for format dumping.
    pub fn dump_pages(&self) -> Vec<(usize, [i64; PAGE])> {
        let mut out = Vec::new();
        for (k, p) in self.pages.iter().enumerate() {
            let Some(p) = p else { continue };
            let base = (k * PAGE) as u32;
            let vals: [i64; PAGE] = std::array::from_fn(|i| p[i].val);
            if vals
                .iter()
                .enumerate()
                .any(|(i, v)| *v != (self.default)(base + i as u32))
            {
                out.push((k, vals));
            }
        }
        out
    }

    /// Installs a page (format loading); the page may be shared.
    pub fn load_page(&mut self, k: usize, page: Rc<Page>) {
        if self.pages.len() <= k {
            self.pages.resize_with(k + 1, || None);
        }
        self.pages[k] = Some(page);
    }

    /// Iterates, in code-point order, over the characters of all allocated
    /// pages: the first 256 characters and every page containing an
    /// assignment.
    pub fn explicit(&self) -> impl Iterator<Item = (u32, i64, u16)> + '_ {
        self.pages.iter().enumerate().flat_map(|(k, p)| {
            p.iter().flat_map(move |p| {
                p.iter()
                    .enumerate()
                    .map(move |(i, s)| ((k * PAGE + i) as u32, s.val, s.level))
            })
        })
    }
}

/// Default category codes of IniTeX: escape for `\`, letters for ASCII
/// letters, `%` comment, end of line for ^^M, space, ignored ^^@, invalid
/// ^^?, everything else other. LuaTeX also ignores U+FEFF, the byte order
/// mark some editors put at the start of UTF-8 files.
pub fn initex_catcode(c: u32) -> i64 {
    match c {
        0x5C => 0,                       // escape
        0x25 => 14,                      // comment
        0x41..=0x5A | 0x61..=0x7A => 11, // letter
        0x0D => 5,                       // car_ret
        0x20 => 10,                      // spacer
        0x00 | 0xFEFF => 9,              // ignore
        0x7F => 15,                      // invalid
        _ => 12,                         // other
    }
}

/// The full table of equivalents.
#[derive(Clone, Debug)]
pub struct Eqtb {
    /// Meanings of control sequences, indexed by [`CsId`].
    pub cs: Vec<Slot<Meaning>>,
    /// Integer parameters.
    pub int_par: Vec<Slot<i32>>,
    /// `\count` registers.
    pub count: Vec<Slot<i32>>,
    /// `\attribute` registers.
    pub attr: Vec<Slot<i32>>,
    /// Dimension parameters.
    pub dimen_par: Vec<Slot<i32>>,
    /// `\dimen` registers.
    pub dimen: Vec<Slot<i32>>,
    /// Glue and muglue parameters.
    pub glue_par: Vec<Slot<Glue>>,
    /// `\skip` registers.
    pub skip: Vec<Slot<Glue>>,
    /// `\muskip` registers.
    pub muskip: Vec<Slot<Glue>>,
    /// Token-list parameters.
    pub toks_par: Vec<Slot<TokenList>>,
    /// `\toks` registers.
    pub toks: Vec<Slot<TokenList>>,
    /// `\box` registers.
    pub boxes: Vec<Slot<NodeRef>>,
    /// The current font.
    pub cur_font: Slot<u32>,
    /// Math fonts, `size * NUM_FAMILIES + family`.
    pub math_font: Vec<Slot<u32>>,
    /// `\parshape`: pairs (indent, length) flattened.
    pub par_shape: Slot<Option<Rc<[i32]>>>,
    /// e-TeX penalty arrays (`\interlinepenalties`, …).
    pub etex_shape: Vec<Slot<Option<Rc<[i32]>>>>,
    /// Catcode tables, indexed by table number.
    pub catcode_tables: HashMap<u32, CodeTable, crate::hash::FastHash>,
    /// `\lccode`.
    pub lc: CodeTable,
    /// `\uccode`.
    pub uc: CodeTable,
    /// `\sfcode`.
    pub sf: CodeTable,
    /// `\mathcode`.
    pub math: CodeTable,
    /// `\delcode`.
    pub del: CodeTable,
    /// `\Umath…` parameters: `param * 8 + style`.
    pub math_param: HashMap<u32, Slot<i32>>,
    /// `\localleftbox`, `\localrightbox`.
    pub local_box: [Slot<NodeRef>; 2],
    /// Direction parameters (`\pagedir` …, as numbers 0–3).
    pub dir_par: [Slot<i32>; pg::assign_dir::COUNT as usize],
    /// `\nolocalwhatsits`, `\nolocaldirs`.
    pub local_count: [Slot<i32>; 2],
    /// Incremented whenever an attribute register changes.
    pub attr_generation: u64,
    /// Whether any attribute register was ever set.
    pub attrs_in_use: bool,
}

impl Eqtb {
    /// The initial table of IniTeX: everything undefined or zero, with
    /// TeX's few non-zero defaults.
    pub fn initex() -> Self {
        use pg::assign_int as ip;
        let regs_i = || vec![Slot::new(0); NUM_REGISTERS];
        let mut int_par = vec![Slot::new(0i32); INT_PARS];
        int_par[ip::TOLERANCE as usize].val = 10000;
        int_par[ip::MAG as usize].val = 1000;
        int_par[ip::MAXDEADCYCLES as usize].val = 25;
        int_par[ip::ESCAPECHAR as usize].val = '\\' as i32;
        int_par[crate::backend::MAJOR_VERSION as usize].val = 1;
        int_par[ip::ENDLINECHAR as usize].val = 13;
        let mut catcode_tables: HashMap<u32, CodeTable, crate::hash::FastHash> = HashMap::default();
        catcode_tables.insert(0, CodeTable::new(initex_catcode));
        Eqtb {
            cs: Vec::new(),
            int_par,
            count: regs_i(),
            attr: vec![Slot::new(UNUSED_ATTRIBUTE); NUM_REGISTERS],
            dimen_par: vec![Slot::new(0); DIMEN_PARS],
            dimen: regs_i(),
            glue_par: vec![
                Slot::new(Glue::ZERO);
                (pg::assign_glue::COUNT + pg::assign_mu_glue::COUNT) as usize
            ],
            skip: vec![Slot::new(Glue::ZERO); NUM_REGISTERS],
            muskip: vec![Slot::new(Glue::ZERO); NUM_REGISTERS],
            toks_par: vec![Slot::new(empty_list()); TOKS_PARS],
            toks: vec![Slot::new(empty_list()); NUM_REGISTERS],
            boxes: vec![Slot::new(NodeRef::NULL); NUM_REGISTERS],
            cur_font: Slot::new(0),
            math_font: vec![Slot::new(0); 3 * NUM_FAMILIES],
            par_shape: Slot::new(None),
            etex_shape: vec![Slot::new(None); pg::set_etex_shape::COUNT as usize],
            catcode_tables,
            lc: CodeTable::new(crate::codes::initex_lccode),
            uc: CodeTable::new(crate::codes::initex_uccode),
            sf: CodeTable::new(crate::codes::initex_sfcode),
            math: CodeTable::new(crate::codes::initex_mathcode),
            del: CodeTable::new(crate::codes::initex_delcode),
            math_param: HashMap::new(),
            local_box: [Slot::new(NodeRef::NULL), Slot::new(NodeRef::NULL)],
            dir_par: std::array::from_fn(|_| Slot::new(0)),
            local_count: [Slot::new(0), Slot::new(0)],
            attr_generation: 0,
            attrs_in_use: false,
        }
    }

    /// Makes sure `cs` has an entry.
    #[inline]
    pub fn ensure_cs(&mut self, cs: CsId) {
        let n = cs.0 as usize + 1;
        if self.cs.len() < n {
            self.cs.resize(
                n,
                Slot {
                    val: Meaning::UNDEFINED,
                    level: LEVEL_ZERO,
                },
            );
        }
    }

    /// The meaning of a control sequence.
    #[inline]
    pub fn meaning(&self, cs: CsId) -> &Meaning {
        match self.cs.get(cs.0 as usize) {
            Some(slot) => &slot.val,
            None => &Meaning::UNDEFINED,
        }
    }

    /// The integer parameter with index `i`.
    #[inline]
    pub fn int(&self, i: u32) -> i32 {
        self.int_par[i as usize].val
    }

    /// The dimension parameter with index `i`.
    #[inline]
    pub fn dimen(&self, i: u32) -> i32 {
        self.dimen_par[i as usize].val
    }

    /// The glue parameter with index `i`.
    #[inline]
    pub fn glue(&self, i: u32) -> Glue {
        self.glue_par[i as usize].val
    }

    /// The token-list parameter with index `i`.
    #[inline]
    pub fn toks_param(&self, i: u32) -> &TokenList {
        &self.toks_par[i as usize].val
    }

    /// The current catcode table number.
    #[inline]
    pub fn cur_cat_table(&self) -> u32 {
        self.int(pg::assign_int::CATCODETABLE) as u32
    }

    /// The category code of `c` in the current catcode table.
    #[inline]
    pub fn catcode(&self, c: u32) -> u8 {
        self.catcode_in(self.cur_cat_table(), c)
    }

    /// The category code of `c` in table `table`. Missing tables read as
    /// IniTeX defaults.
    pub fn catcode_in(&self, table: u32, c: u32) -> u8 {
        match self.catcode_tables.get(&table) {
            Some(t) => t.get(c) as u8,
            None => initex_catcode(c) as u8,
        }
    }

    /// Reads the value at `loc`.
    pub fn get(&self, loc: Loc) -> Val {
        match loc {
            Loc::Cs(cs) => Val::Meaning(self.meaning(cs).clone()),
            Loc::IntPar(i) => Val::Int(self.int_par[i as usize].val.into()),
            Loc::Count(i) => Val::Int(self.count[i as usize].val.into()),
            Loc::Attr(i) => Val::Int(self.attr[i as usize].val.into()),
            Loc::DimenPar(i) => Val::Int(self.dimen_par[i as usize].val.into()),
            Loc::Dimen(i) => Val::Int(self.dimen[i as usize].val.into()),
            Loc::GluePar(i) => Val::Glue(self.glue_par[i as usize].val),
            Loc::Skip(i) => Val::Glue(self.skip[i as usize].val),
            Loc::MuSkip(i) => Val::Glue(self.muskip[i as usize].val),
            Loc::ToksPar(i) => Val::Toks(self.toks_par[i as usize].val.clone()),
            Loc::Toks(i) => Val::Toks(self.toks[i as usize].val.clone()),
            Loc::Box(i) => Val::Node(self.boxes[i as usize].val),
            Loc::CurFont => Val::Int(self.cur_font.val.into()),
            Loc::MathFont(i) => Val::Int(self.math_font[i as usize].val.into()),
            Loc::ParShape => Val::Shape(self.par_shape.val.clone()),
            Loc::EtexShape(i) => Val::Shape(self.etex_shape[i as usize].val.clone()),
            Loc::Cat { table, ch } => Val::Int(self.catcode_in(table, ch).into()),
            Loc::Code(kind, ch) => Val::Int(self.code_table(kind).get(ch)),
            Loc::MathParam(i) => Val::Int(
                self.math_param
                    .get(&i)
                    .map_or(crate::codes::UNDEFINED_MATH_PARAM.into(), |s| s.val.into()),
            ),
            Loc::LocalBox(i) => Val::Node(self.local_box[i as usize].val),
            Loc::DirPar(i) => Val::Int(self.dir_par[i as usize].val.into()),
            Loc::LocalCount(i) => Val::Int(self.local_count[i as usize].val.into()),
        }
    }

    /// Whether `loc` holds `val` already (TeX's test for "reassigning"),
    /// without cloning the current value.
    pub fn holds(&self, loc: Loc, val: &Val) -> bool {
        match (loc, val) {
            (Loc::Cs(cs), Val::Meaning(m)) => self.meaning(cs).same_as(m),
            (Loc::IntPar(i), Val::Int(v)) => i64::from(self.int_par[i as usize].val) == *v,
            (Loc::Count(i), Val::Int(v)) => i64::from(self.count[i as usize].val) == *v,
            (Loc::Dimen(i), Val::Int(v)) => i64::from(self.dimen[i as usize].val) == *v,
            (Loc::DimenPar(i), Val::Int(v)) => i64::from(self.dimen_par[i as usize].val) == *v,
            (Loc::Code(kind, ch), Val::Int(v)) => self.code_table(kind).get(ch) == *v,
            (Loc::ToksPar(i), Val::Toks(t)) => Rc::ptr_eq(&self.toks_par[i as usize].val, t),
            (Loc::Toks(i), Val::Toks(t)) => Rc::ptr_eq(&self.toks[i as usize].val, t),
            _ => same_val(&self.get(loc), val),
        }
    }

    /// The level at which the value at `loc` was defined.
    pub fn level(&self, loc: Loc) -> u16 {
        match loc {
            Loc::Cs(cs) => self.cs.get(cs.0 as usize).map_or(LEVEL_ZERO, |s| s.level),
            Loc::IntPar(i) => self.int_par[i as usize].level,
            Loc::Count(i) => self.count[i as usize].level,
            Loc::Attr(i) => self.attr[i as usize].level,
            Loc::DimenPar(i) => self.dimen_par[i as usize].level,
            Loc::Dimen(i) => self.dimen[i as usize].level,
            Loc::GluePar(i) => self.glue_par[i as usize].level,
            Loc::Skip(i) => self.skip[i as usize].level,
            Loc::MuSkip(i) => self.muskip[i as usize].level,
            Loc::ToksPar(i) => self.toks_par[i as usize].level,
            Loc::Toks(i) => self.toks[i as usize].level,
            Loc::Box(i) => self.boxes[i as usize].level,
            Loc::CurFont => self.cur_font.level,
            Loc::MathFont(i) => self.math_font[i as usize].level,
            Loc::ParShape => self.par_shape.level,
            Loc::EtexShape(i) => self.etex_shape[i as usize].level,
            Loc::Cat { table, ch } => self
                .catcode_tables
                .get(&table)
                .map_or(LEVEL_ONE, |t| t.level(ch)),
            Loc::Code(kind, ch) => self.code_table(kind).level(ch),
            Loc::MathParam(i) => self.math_param.get(&i).map_or(LEVEL_ONE, |s| s.level),
            Loc::LocalBox(i) => self.local_box[i as usize].level,
            Loc::DirPar(i) => self.dir_par[i as usize].level,
            Loc::LocalCount(i) => self.local_count[i as usize].level,
        }
    }

    /// Stores `val` at `loc` with level `level`. Mismatched value kinds are
    /// ignored (they indicate a bug and are caught in debug builds).
    pub fn set(&mut self, loc: Loc, val: Val, level: u16) {
        fn int(v: &Val) -> i64 {
            match v {
                Val::Int(i) => *i,
                _ => {
                    debug_assert!(false, "expected an integer value");
                    0
                }
            }
        }
        match loc {
            Loc::Cs(cs) => {
                self.ensure_cs(cs);
                if let Val::Meaning(m) = val {
                    self.cs[cs.0 as usize] = Slot { val: m, level };
                }
            }
            Loc::IntPar(i) => {
                self.int_par[i as usize] = Slot {
                    val: int(&val) as i32,
                    level,
                }
            }
            Loc::Count(i) => {
                self.count[i as usize] = Slot {
                    val: int(&val) as i32,
                    level,
                }
            }
            Loc::Attr(i) => {
                self.attr[i as usize] = Slot {
                    val: int(&val) as i32,
                    level,
                };
                self.attr_generation += 1;
                self.attrs_in_use = true;
            }
            Loc::DimenPar(i) => {
                self.dimen_par[i as usize] = Slot {
                    val: int(&val) as i32,
                    level,
                }
            }
            Loc::Dimen(i) => {
                self.dimen[i as usize] = Slot {
                    val: int(&val) as i32,
                    level,
                }
            }
            Loc::GluePar(i) | Loc::Skip(i) | Loc::MuSkip(i) => {
                if let Val::Glue(g) = val {
                    let slot = Slot { val: g, level };
                    match loc {
                        Loc::GluePar(_) => self.glue_par[i as usize] = slot,
                        Loc::Skip(_) => self.skip[i as usize] = slot,
                        _ => self.muskip[i as usize] = slot,
                    }
                }
            }
            Loc::ToksPar(i) | Loc::Toks(i) => {
                if let Val::Toks(t) = val {
                    let slot = Slot { val: t, level };
                    if matches!(loc, Loc::ToksPar(_)) {
                        self.toks_par[i as usize] = slot;
                    } else {
                        self.toks[i as usize] = slot;
                    }
                }
            }
            Loc::Box(i) => {
                if let Val::Node(n) = val {
                    self.boxes[i as usize] = Slot { val: n, level };
                }
            }
            Loc::CurFont => {
                self.cur_font = Slot {
                    val: int(&val) as u32,
                    level,
                }
            }
            Loc::MathFont(i) => {
                self.math_font[i as usize] = Slot {
                    val: int(&val) as u32,
                    level,
                }
            }
            Loc::ParShape => {
                if let Val::Shape(s) = val {
                    self.par_shape = Slot { val: s, level };
                }
            }
            Loc::EtexShape(i) => {
                if let Val::Shape(s) = val {
                    self.etex_shape[i as usize] = Slot { val: s, level };
                }
            }
            Loc::Cat { table, ch } => {
                self.catcode_tables
                    .entry(table)
                    .or_insert_with(|| CodeTable::new(initex_catcode))
                    .set(ch, int(&val), level);
            }
            Loc::Code(kind, ch) => self.code_table_mut(kind).set(ch, int(&val), level),
            Loc::MathParam(i) => {
                self.math_param.insert(
                    i,
                    Slot {
                        val: int(&val) as i32,
                        level,
                    },
                );
            }
            Loc::LocalBox(i) => {
                if let Val::Node(n) = val {
                    self.local_box[i as usize] = Slot { val: n, level };
                }
            }
            Loc::DirPar(i) => {
                self.dir_par[i as usize] = Slot {
                    val: int(&val) as i32,
                    level,
                }
            }
            Loc::LocalCount(i) => {
                self.local_count[i as usize] = Slot {
                    val: int(&val) as i32,
                    level,
                }
            }
        }
    }

    /// The code table of the given kind.
    pub fn code_table(&self, kind: CodeKind) -> &CodeTable {
        match kind {
            CodeKind::Lc => &self.lc,
            CodeKind::Uc => &self.uc,
            CodeKind::Sf => &self.sf,
            CodeKind::Math => &self.math,
            CodeKind::Del => &self.del,
        }
    }

    pub(crate) fn code_table_mut(&mut self, kind: CodeKind) -> &mut CodeTable {
        match kind {
            CodeKind::Lc => &mut self.lc,
            CodeKind::Uc => &mut self.uc,
            CodeKind::Sf => &mut self.sf,
            CodeKind::Math => &mut self.math,
            CodeKind::Del => &mut self.del,
        }
    }

    /// Whether the location holds a "word" quantity (TeX regions 5–6:
    /// integers and dimensions, which use `xeq_level` semantics) as opposed
    /// to an "equivalent" (regions 1–4).
    pub fn is_word(loc: Loc) -> bool {
        matches!(
            loc,
            Loc::IntPar(_)
                | Loc::Count(_)
                | Loc::Attr(_)
                | Loc::DimenPar(_)
                | Loc::Dimen(_)
                | Loc::MathParam(_)
        )
    }
}

/// Compares two saved values for TeX's "reassigning" test.
pub fn same_val(a: &Val, b: &Val) -> bool {
    match (a, b) {
        (Val::Meaning(x), Val::Meaning(y)) => x.same_as(y),
        (Val::Int(x), Val::Int(y)) => x == y,
        (Val::Glue(x), Val::Glue(y)) => x == y,
        (Val::Toks(x), Val::Toks(y)) => Rc::ptr_eq(x, y),
        (Val::Node(x), Val::Node(y)) => x == y,
        (Val::Shape(x), Val::Shape(y)) => match (x, y) {
            (None, None) => true,
            (Some(p), Some(q)) => Rc::ptr_eq(p, q),
            _ => false,
        },
        _ => false,
    }
}