ferth 0.2.0

A safe, native-sized Forth. no_std compatible.
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
use crate::data::{Data, Mem};
use crate::double::Double;
use crate::error::{Ior, KernelError};
use crate::header::{Flags, Header};
use crate::host::{Io, MaybeClock};
use crate::log::debug;
use crate::state::{Booted, Booting};
use crate::vm::{Op, Vm};
use crate::{BL, Error, FALSE, Result, SIZE, TRUE};

use super::builtins;
use super::env;
use super::layout;
use super::{Builtin, Kernel, MAX_BUILTINS, MIN_DATA_SPACE};
use crate::packed::PackedInstr;

use env::Environment;
use layout::Layout;

pub use env::Config;

const KERNEL: &[u8] = include_bytes!("../kernel.fth");

#[derive(Clone, Copy)]
enum Token {
    Lit(usize),
    Name(&'static [u8]),
    If,
    Else,
    Then,
    Begin,
    While,
    Repeat,
    Inline(&'static [Token]),
    // Fetch a variable value at runtime.
    Variable(usize),
}

impl<M: Mem, H: Io> Kernel<M, H, Booting> {
    pub fn new(mem: M, host: H, config: Config) -> Result<Self> {
        let env = Environment {
            config,
            ..Default::default()
        };
        let data = Data::new(mem);
        if data.size() < MIN_DATA_SPACE {
            return Err(KernelError::DataSpaceTooSmall(MIN_DATA_SPACE).into());
        }
        let vm = Vm::new(
            data.size(),
            env.config.stack_cells,
            env.config.return_stack_cells,
        )?;
        let layout_base = Vm::DATA_BASE;
        Ok(Self {
            vm,
            data,
            host,
            builtins: [None; MAX_BUILTINS],
            builtins_len: 0,
            layout_base,
            env,
            state: Booting {},
        })
    }

    pub fn boot(mut self) -> Result<Kernel<M, H, Booted>>
    where
        H: MaybeClock,
    {
        self.reserve_variables()?;
        debug!("KERNEL", "Reserved variables");
        self.compile_opcodes()?;
        debug!("KERNEL", "Compiled opcodes");
        self.register_builtins()?;
        debug!("KERNEL", "Registered builtins");
        self.compile_environment()?;
        debug!("KERNEL", "Compiled environment");
        self.define_variables()?;
        debug!("KERNEL", "Defined variables");
        self.compile_wrappers()?;
        debug!("KERNEL", "Compiled wrappers");
        self.compile_interpreter()?;
        debug!("KERNEL", "Compiled compiler");
        self.load_kernel()?;
        debug!("KERNEL", "Loaded kernel");
        let mut xt = |name: &'static str| -> Result<usize> {
            self.dict()
                .find(name.as_bytes())?
                .map(|(xt, _)| xt)
                .ok_or(KernelError::MissingEntryPoint(name).into())
        };
        let state = Booted {
            xt_catch: xt("catch")?,
            xt_interpret: xt("(interpret)")?,
            xt_throw: xt("throw")?,
        };
        Ok(Kernel {
            vm: self.vm,
            data: self.data,
            host: self.host,
            builtins: self.builtins,
            builtins_len: self.builtins_len,
            layout_base: self.layout_base,
            env: self.env,
            state,
        })
    }

    /// Reserve cells for system variables.
    fn reserve_variables(&mut self) -> Result<()> {
        let variables: &[(usize, usize)] = &[
            (Layout::HERE, self.layout_base + Layout::DATA),
            (Layout::LATEST, 0),
            (Layout::SOURCE_ADDR, self.layout_base + Layout::INPUT),
            (Layout::SOURCE_LEN, 0),
            (Layout::SOURCE_ID, 0),
            (Layout::TO_IN, 0),
            (Layout::BASE, 10),
            (Layout::STATE, 0),
            (Layout::SP0, self.vm.sp0()),
            (Layout::RP0, self.vm.rp0() - SIZE),
            (Layout::DIAGNOSTIC_ADDR, 0),
            (Layout::DIAGNOSTIC_LEN, 0),
        ];
        for (offset, value) in variables {
            self.data.write_cell(self.layout_base + offset, *value)?;
        }
        Ok(())
    }

    /// Compile opcode primitives.
    ///
    /// The inner interpreter implements these words directly. They comprise the most fundamental
    /// set of execution, stack, memory, and arithmetic operations.
    fn compile_opcodes(&mut self) -> Result<()> {
        let opcodes: &[(&[u8], Op)] = &[
            (b"!", Op::Store),
            (b"(docreate)", Op::DoCreate),
            (b"(exit)", Op::Exit),
            (b"(jmp)", Op::Jmp),
            (b"(jmpz)", Op::JmpZ),
            (b"(lit)", Op::Lit),
            (b"(nand)", Op::Nand),
            (b"(do)", Op::Do),
            (b"(?do)", Op::QDo),
            (b"(+loop)", Op::PlusLoop),
            (b"unloop", Op::Unloop),
            (b"i", Op::I),
            (b"j", Op::J),
            (b"(s\")", Op::Str),
            (b"um*", Op::UmMul),
            (b"+", Op::Add),
            (b"0<", Op::LtZ),
            (b"0=", Op::EqZ),
            (b">r", Op::ToR),
            (b"@", Op::Fetch),
            (b"c!", Op::CStore),
            (b"c@", Op::CFetch),
            (b"(sp@)", Op::SpFetch),
            (b"(sp!)", Op::SpStore),
            (b"(rp@)", Op::RpFetch),
            (b"(rp!)", Op::RpStore),
            (b"drop", Op::Drop),
            (b"dup", Op::Dup),
            (b"r>", Op::RFrom),
            (b"swap", Op::Swap),
            (b"lshift", Op::LShift),
            (b"rshift", Op::RShift),
            (b"um/mod", Op::UmDivMod),
            (b"execute", Op::Execute),
            (b"(call)", Op::Call),
            (b"(yield)", Op::Yield),
            (b"(parse)", Op::Parse),
            (br#"(parse\")"#, Op::ParseEscaped),
            (b"(number)", Op::Number),
            (b"(>number)", Op::ToNumber),
            (b"(compile,)", Op::CompileComma),
            (b"(decode)", Op::Decode),
        ];
        let compile_only: &[&[u8]] = &[
            b"(lit)",
            b"(exit)",
            b"(jmp)",
            b"(jmpz)",
            b"(call)",
            b"(docreate)",
            b"(s\")",
            b"(yield)",
            b"(do)",
            b"(?do)",
            b"(+loop)",
            b"i",
            b"j",
            b"unloop",
        ];
        for (name, op) in opcodes {
            let mut flags = Flags::PRIMITIVE;
            if compile_only.contains(name) {
                flags = flags | Flags::COMPILE_ONLY;
            }
            let xt = self.define(name, flags)?;
            let instr = PackedInstr::new(*op, xt, 0)?;
            self.data.write_cell(xt, instr.into())?;
        }
        Ok(())
    }

    /// Compile builtin primitives.
    ///
    /// These words require host facilities such as I/O or a system clock.
    fn register_builtins(&mut self) -> Result<()>
    where
        H: MaybeClock,
    {
        let builtins: &[(&[u8], Builtin<M, H>)] = &[
            (b"emit", builtins::emit),
            (b"(find)", builtins::find),
            (b"key", builtins::key),
            (b"refill", builtins::refill),
            (b"(header)", builtins::header),
        ];
        for (name, f) in builtins {
            self.register_builtin(name, *f)?;
        }
        #[cfg(feature = "std")]
        {
            self.register_builtin(b"ms", builtins::ms)?;
            self.register_builtin(b"(utime)", builtins::utime)?;
        }
        #[cfg(feature = "time")]
        {
            self.register_builtin(b"time&date", builtins::time_and_date)?;
        }
        Ok(())
    }

    /// Compile interpreter words such as `:`, `;`, and `(interpret)`.
    #[rustfmt::skip]
    fn compile_interpreter(&mut self) -> Result<()> {
        macro_rules! compile {
            ($s:expr, $flags:expr, [$($body:expr),* $(,)?]) => {
                self.compile($s, $flags.into(), &[$($body),*])?;
            };
        }

        use Token::{Lit as L, Name as N, *};

        // This sequence hand-compiles the words `:`, `;`, `literal`, `(interpret)`, and their
        // direct dependencies. This code *is* Forth, just not written as text.
        //
        // Along the way, we need to define some helper words such as `and` and `over`. Kernel code
        // is trusted input, and the bootstrap words do not need to handle every case. Some of
        // these definitions are deliberately incomplete. For example, the definition of `<` is not
        // overflow-safe.
        //
        // To avoid polluting the wordlist with incomplete definitions, the kernel *inlines* as
        // many definitions as it can. We will redefine them correctly in Forth.
        //
        // `N(name)` compiles a call to a previously defined word. Any reference to an XT that
        // should be a data value at runtime (`['] word`) must be a literal (`L(xt)`).
        //
        // At the end of the boot, a check verifies that every word with the `BOOTSTRAP` flag has
        // been redefined in Forth.
        const HERE: Token = Variable(Layout::HERE);
        const LATEST: Token = Variable(Layout::LATEST);

        // cells
        compile!(b"cells", 0, [L(SIZE), N(b"um*"), N(b"drop")]);

        // : +! ( u addr -- ) dup >r @ + r> ! ;
        compile!(b"+!", 0, [N(b"dup"), N(b">r"), N(b"@"), N(b"+"), N(b"r>"), N(b"!")]);

        // : allot ( n -- ) (here) +! ;
        let here_addr = self.dict().addr(Layout::HERE);
        compile!(b"allot", 0, [L(here_addr), N(b"+!")]);

        // : , ( x -- ) here ! 1 cells allot ;
        //
        // `here` is always cell-aligned at this stage so it is safe call `,` without `align`.
        compile!(
            b",",
            0,
            [HERE, N(b"!"), L(1), N(b"cells"), N(b"allot")]
        );

        // : literal ( x -- ) ['] (lit) , , ; immediate
        //
        // The first lit is the (lit) opcode. The second lit is the XT of (lit).
        // At runtime this executes as `(lit) xt`, compiling the XT of (lit) and then the
        // original top of stack.
        compile!(
            b"literal",
            Flags::IMMEDIATE | Flags::COMPILE_ONLY,
            [L(Op::Lit as usize), N(b","), N(b",")]
        );

        // : ['] bl parse (find) drop literal ; immediate
        //
        // This definition does not check the flag value because bootstrap input is trusted.
        // `literal` is immediate, so in Forth, it would require `postpone literal` to compile into
        // this definition. Here we can compile it directly.
        compile!(
            b"[']",
            Flags::IMMEDIATE | Flags::BOOTSTRAP | Flags::COMPILE_ONLY,
            [L(BL), N(b"parse"), N(b"(find)"), N(b"drop"), N(b"literal")]
        );

        // : :
        //   bl parse (header)
        //   -1 state !
        // ;
        //
        // Parse a word and create a definition for it. This simple definition does not set the
        // hidden flag. It also does not skip leading whitespace or process whitespace characters
        // other than SPACE (BL). The Forth kernel replaces it.
        compile!(
            b":",
            Flags::BOOTSTRAP,
            [
                L(BL), N(b"parse"), N(b"(header)"),
                L(TRUE), N(b"state"), N(b"!"),
            ]
        );

        // : ;
        //   ['] (exit) compile,
        //   \ Calculate and set bodylen.
        //   (latest) @                 ( xt )
        //   dup 3 cells -              ( xt bodylen-addr )
        //   swap                       ( bodylen-addr xt )
        //   here swap -                ( bodylen-addr bodylen )
        //   swap !                     ( )
        //   \ Unset hidden flag.
        //   (latest) @                 ( latest )
        //   (flags-addr) dup c@        ( flags-addr flags )
        //   (hidden-flag) invert and   ( flags-addr flags' )
        //   swap c!
        //   0 state !
        // ; immediate
        //
        // Compile a literal to compile `exit`, store the bodylen, unset the hidden flag, then exit
        // compilation mode.
        //
        // Unlike `:`, this definition must be functionally complete. It must unset the hidden flag
        // and set bodylen. We cannot define a simpler version of `;` here and redefine a more
        // complete version in in Forth because this `;` would terminate that definition.
        //
        // However, it possible to extend this definition later. The Forth kernel extends it to
        // support tail-call optimization.
        const INVERT: Token = Inline(&[N(b"dup"), N(b"(nand)")]);
        const MINUS: Token = Inline(&[INVERT, L(1), N(b"+"), N(b"+")]);
        const AND: Token = Inline(&[N(b"(nand)"), N(b"dup"), N(b"(nand)")]);
        compile!(
            b";",
            Flags::IMMEDIATE | Flags::COMPILE_ONLY,
            [
                L(Op::Exit as usize), N(b","),
                // Calculate and set bodylen.
                LATEST,
                N(b"dup"), L(3 * SIZE), MINUS,
                N(b"swap"),
                HERE, N(b"swap"), MINUS,
                N(b"swap"), N(b"!"),
                // Unset hidden flag.
                LATEST,
                L(2 * SIZE), MINUS, L(1), N(b"+"), N(b"dup"), N(b"c@"),
                L(Flags::HIDDEN.into()), INVERT, AND,
                N(b"swap"), N(b"c!"),
                L(FALSE), N(b"state"), N(b"!"),
            ]
        );

        // (interpret)
        //
        // This one is thorny. First, the Forth:
        //
        //     : (interpret)
        //       begin
        //         >in @ (source-len) @ <       ( flag )
        //       while
        //         bl parse                     ( c-addr u )
        //         dup if                       ( c-addr u )
        //           2dup (find)                ( c-addr u 0 | c-addr u xt flag )
        //           ?dup if                    ( c-addr u xt flag )
        //             2swap 2drop              ( xt flag )
        //             0< (state) @ and if      ( xt )
        //               compile,               ( )
        //             else
        //               execute                ( )
        //             then
        //           else                       ( c-addr u )
        //             (number?) if             ( n )
        //               (state) @ if           ( n )
        //                 postpone literal     ( )
        //               then
        //             else                     ( c-addr u )
        //               \ TODO
        //               2drop                  ( )
        //               0 1 0 um/mod
        //             then
        //           then
        //         else                         ( c-addr u )
        //           2drop                      ( )
        //         then
        //       repeat
        //     ;
        //
        // The control flow words execute immediately and compile jumps to the current value of
        // `here`. We need to compile these jumps manually by setting labels and patching the jumps
        // in a second pass (see `compile`).
        //
        // As above, note that `postpone literal` has the effect of compiling a call to `literal`
        // into the current definition. We can compile `literal` directly here.

        const OVER: Token = Inline(&[N(b">r"), N(b"dup"), N(b"r>"), N(b"swap")]);
        const TWO_DUP: Token = Inline(&[OVER, OVER]);
        const TWO_DROP: Token = Inline(&[N(b"drop"), N(b"drop")]);
        const ROT: Token = Inline(&[N(b">r"), N(b"swap"), N(b"r>"), N(b"swap")]);
        const TWO_SWAP: Token = Inline(&[ROT, N(b">r"), ROT, N(b"r>")]);
        const Q_DUP: Token = Inline(&[N(b"dup"), If, N(b"dup"), Then]);
        const LT: Token = Inline(&[N(b"dup"), N(b"(nand)"), L(1), N(b"+"), N(b"+"), N(b"0<")]);
        compile!(
            b"(interpret)",
            Flags::BOOTSTRAP,
            [
                Begin,
                    Variable(Layout::TO_IN), Variable(Layout::SOURCE_LEN), LT,
                While,
                    L(BL), N(b"parse"),
                    N(b"dup"), If,
                        TWO_DUP, N(b"(find)"),
                        Q_DUP, If,
                            TWO_SWAP, TWO_DROP,
                            N(b"0<"), Variable(Layout::STATE), AND, If,
                                N(b"compile,"),
                            Else,
                                N(b"execute"),
                            Then,
                        Else,
                            N(b"(number?)"), If,
                                Variable(Layout::STATE), If,
                                    // NOTE: `postpone literal` compiles to `literal`.
                                    N(b"literal"),
                                Then,
                            Else,
                                TWO_DROP,
                                L(0), L(1), L(0), N(b"um/mod"),
                            Then,
                        Then,
                    Else,
                        TWO_DROP,
                    Then,
                Repeat,
            ]
        );
        Ok(())
    }

    fn load_kernel(&mut self) -> Result<()> {
        for line in KERNEL.split(|&b| b == b'\n') {
            if !line.is_empty() {
                self.dict().set_source(line)?;
                let xt = self
                    .dict()
                    .find(b"(interpret)")?
                    .map(|(xt, _)| xt)
                    .ok_or(KernelError::MissingEntryPoint("(interpret)"))?;
                self.execute(xt)?;
            }
        }
        Ok(())
    }

    fn compile_environment(&mut self) -> Result<()> {
        let flag = |b: bool| -> usize { if b { TRUE } else { FALSE } };
        self.compile(
            b"(/counted-string)",
            Flags::EMPTY,
            &[Token::Lit(self.env.counted_string)],
        )?;
        self.compile(
            b"(/hold)",
            Flags::EMPTY,
            &[Token::Lit(self.env.config.hold)],
        )?;
        self.compile(b"(/pad)", Flags::EMPTY, &[Token::Lit(self.env.config.pad)])?;
        self.compile(
            b"(address-unit-bits)",
            Flags::EMPTY,
            &[Token::Lit(self.env.address_unit_bits)],
        )?;
        self.compile(
            b"(floored)",
            Flags::EMPTY,
            &[Token::Lit(flag(self.env.floored))],
        )?;
        self.compile(
            b"(max-char)",
            Flags::EMPTY,
            &[Token::Lit(self.env.max_char)],
        )?;
        let (lo, hi): (usize, usize) = Double(self.env.max_d.0 as _).into();
        self.compile(b"(max-d)", Flags::EMPTY, &[Token::Lit(lo), Token::Lit(hi)])?;
        self.compile(
            b"(max-n)",
            Flags::EMPTY,
            &[Token::Lit(self.env.max_n as usize)],
        )?;
        self.compile(b"(max-u)", Flags::EMPTY, &[Token::Lit(self.env.max_u)])?;
        let (lo, hi): (usize, usize) = self.env.max_ud.into();
        self.compile(b"(max-ud)", Flags::EMPTY, &[Token::Lit(lo), Token::Lit(hi)])?;
        self.compile(
            b"(return-stack-cells)",
            Flags::EMPTY,
            &[Token::Lit(self.env.config.return_stack_cells)],
        )?;
        self.compile(
            b"(stack-cells)",
            Flags::EMPTY,
            &[Token::Lit(self.env.config.stack_cells)],
        )?;
        Ok(())
    }

    /// Define variables for the system variable addresses.
    fn define_variables(&mut self) -> Result<()> {
        let variables: &[(&[u8], usize)] = &[
            (b"(here)", Layout::HERE),
            (b"(latest)", Layout::LATEST),
            (b"(source-addr)", Layout::SOURCE_ADDR),
            (b"(source-len)", Layout::SOURCE_LEN),
            (b"(source-id)", Layout::SOURCE_ID),
            (b">in", Layout::TO_IN),
            (b"base", Layout::BASE),
            (b"state", Layout::STATE),
            (b"(sp0)", Layout::SP0),
            (b"(rp0)", Layout::RP0),
            (b"(diagnostic-addr)", Layout::DIAGNOSTIC_ADDR),
            (b"(diagnostic-len)", Layout::DIAGNOSTIC_LEN),
        ];
        for (name, offset) in variables {
            self.compile(name, Flags::EMPTY, &[Token::Lit(self.layout_base + offset)])?;
        }
        Ok(())
    }

    fn compile_wrappers(&mut self) -> Result<()> {
        use Token::{Lit as L, Name as N};
        self.compile(
            b"(buf-base)",
            Flags::EMPTY,
            &[L(self.layout_base + Layout::TRANSIENT)],
        )?;
        self.compile(b"(buf-size)", Flags::EMPTY, &[L(layout::INPUT_BUFFER_SIZE)])?;
        self.compile(
            b"parse",
            Flags::EMPTY,
            &[
                N(b"(source-addr)"),
                N(b"@"),
                N(b"(source-len)"),
                N(b"@"),
                N(b">in"),
                N(b"@"),
                N(b"(parse)"),
                N(b">in"),
                N(b"!"),
            ],
        )?;
        self.compile(
            b"compile,",
            Flags::EMPTY,
            &[
                N(b"(here)"),
                N(b"@"),
                N(b"(compile,)"),
                N(b"(here)"),
                N(b"!"),
            ],
        )?;
        self.compile(
            b">number",
            Flags::EMPTY,
            &[N(b"base"), N(b"@"), N(b"(>number)")],
        )?;
        self.compile(
            b"(number?)",
            Flags::EMPTY,
            &[N(b"base"), N(b"@"), N(b"(number)")],
        )?;
        Ok(())
    }

    fn compile(&mut self, name: &[u8], flags: Flags, body: &[Token]) -> Result<usize> {
        let xt = self.define(name, flags)?;
        // `define` advances `here`. We want to compile the body directly.
        self.dict().set_here(xt)?;
        self.compile_tokens(body)?;
        self.dict().comma(Op::Exit as usize)?;
        let here = self.dict().here()?;
        self.data
            .write_cell(Header::new(xt).bodylen_addr(), here - xt)?;
        Ok(xt)
    }

    fn compile_tokens(&mut self, tokens: &[Token]) -> Result<()> {
        let mut labels = [0usize; 16];
        let mut depth = 0;
        for &token in tokens {
            match token {
                Token::Lit(x) => {
                    self.dict().comma(Op::Lit as usize)?;
                    self.dict().comma(x)?;
                }
                Token::Name(name) => {
                    let xt = self
                        .dict()
                        .find(name)?
                        .map(|(xt, _)| xt)
                        .ok_or(Error::Throw(Ior::UNDEFINED_WORD))?;
                    self.push(xt)?;
                    self.compile_comma()?;
                }
                Token::If | Token::While => {
                    let xt = self.dict().find(b"(jmpz)")?.unwrap().0; // TODO: unwrap
                    self.push(xt)?;
                    self.compile_comma()?;
                    let hole = self.dict().here()?;
                    self.dict().comma(0)?;
                    labels[depth] = hole;
                    depth += 1;
                }
                Token::Else => {
                    depth -= 1;
                    let orig = labels[depth];
                    let xt = self.dict().find(b"(jmp)")?.unwrap().0; // TODO: unwrap
                    self.push(xt)?;
                    self.compile_comma()?;
                    let hole = self.dict().here()?;
                    self.dict().comma(0)?;
                    let here = self.dict().here()?;
                    self.data.write_cell(orig, here)?;
                    labels[depth] = hole;
                    depth += 1;
                }
                Token::Then => {
                    depth -= 1;
                    let hole = labels[depth];
                    let here = self.dict().here()?;
                    self.data.write_cell(hole, here)?;
                }
                Token::Begin => {
                    let here = self.dict().here()?;
                    labels[depth] = here;
                    depth += 1;
                }
                Token::Repeat => {
                    depth -= 1;
                    let hole = labels[depth];
                    depth -= 1;
                    let dest = labels[depth];
                    let xt = self.dict().find(b"(jmp)")?.unwrap().0; // TODO: unwrap
                    self.push(xt)?;
                    self.compile_comma()?;
                    self.dict().comma(dest)?;
                    let here = self.dict().here()?;
                    self.data.write_cell(hole, here)?;
                }
                Token::Inline(t) => self.compile_tokens(t)?,
                Token::Variable(offset) => {
                    let addr = self.dict().addr(offset);
                    self.compile_tokens(&[Token::Lit(addr), Token::Name(b"@")])?;
                }
            }
        }
        Ok(())
    }

    fn compile_comma(&mut self) -> Result<()> {
        let xt = self
            .dict()
            .find(b"(compile,)")?
            .map(|(xt, _)| xt)
            .ok_or(Error::Throw(Ior::UNDEFINED_WORD))?;
        let here = self.dict().here()?;
        self.push(here)?;
        self.execute(xt)?;
        let here = self.pop()?;
        self.dict().set_here(here)
    }

    fn register_builtin(&mut self, name: &[u8], f: Builtin<M, H>) -> Result<()> {
        let idx = self.builtins_len;
        if idx >= MAX_BUILTINS {
            return Err(KernelError::BuiltinTableFull.into());
        }
        self.builtins[idx] = Some(f);
        self.builtins_len += 1;
        let code_addr = self.define(name, Flags::PRIMITIVE)?;
        let instr = PackedInstr::new(Op::Yield, code_addr, idx)?;
        Ok(self.data.write_cell(code_addr, instr.into())?)
    }

    fn define(&mut self, name: &[u8], flags: Flags) -> Result<usize> {
        let code_addr = self.dict().create(name, flags.into())?;
        self.dict().set_here(code_addr + SIZE)?;
        self.dict().set_latest(code_addr)?;
        Ok(code_addr)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::header::{Flags, Header, Info};
    use crate::host::NullHost;

    #[test]
    fn tag_boot_words_with_kind() {
        let mut k = Kernel::new([0u8; 65536], NullHost, Config::default())
            .unwrap()
            .boot()
            .unwrap();
        let mut flags = |name: &[u8]| {
            let (xt, _) = k.dict().find(name).unwrap().unwrap();
            let header = Header::new(xt);
            let info: Info = k.data.read_cell(header.info_addr()).unwrap().into();
            info.flags()
        };
        assert!(flags(b"dup").contains(Flags::PRIMITIVE));
        assert!(flags(b"(find)").contains(Flags::PRIMITIVE));
        assert!(!flags(b"cells").contains(Flags::PRIMITIVE));
    }
}