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
use core::{fmt::Write, marker::PhantomData, mem::size_of, ptr::NonNull};

use crate::{
    dictionary::{BuiltinEntry, DictLocation, DictionaryEntry, EntryHeader, EntryKind},
    fastr::comptime_fastr,
    vm::TmpFaStr,
    word::Word,
    Error, Forth, Lookup, Mode, ReplaceErr,
};

#[cfg(feature = "floats")]
pub mod floats;

// NOTE: This macro exists because we can't have const constructors that include
// "mut" items, which unfortunately covers things like `fn(&mut T)`. Use a macro
// until this is resolved.
#[macro_export]
macro_rules! builtin {
    ($name:literal, $func:expr) => {
        BuiltinEntry {
            hdr: EntryHeader {
                name: comptime_fastr($name),
                kind: EntryKind::StaticBuiltin,
                len: 0,
                _pd: core::marker::PhantomData,
            },
            func: $func,
        }
    };
}

/// Constructs an [`AsyncBuiltinEntry`](crate::dictionary::AsyncBuiltinEntry)
/// for an asynchronous builtin word.
///
/// See the [documentation for `AsyncForth`](crate::AsyncForth) for details on
/// using asynchronous builtin words.
#[macro_export]
macro_rules! async_builtin {
    ($name:literal) => {
        $crate::dictionary::AsyncBuiltinEntry {
            hdr: $crate::dictionary::EntryHeader {
                name: $crate::fastr::comptime_fastr($name),
                kind: $crate::dictionary::EntryKind::AsyncBuiltin,
                len: 0,
                _pd: core::marker::PhantomData,
            },
        }
    };
}

#[macro_export]
macro_rules! builtin_if_feature {
    ($feature:literal, $name:literal, $func:expr) => {
        #[cfg(feature = $feature)]
        builtin!($name, $func)
    };
}

// let literal_dict = self.find_word("(literal)").ok_or(Error::WordNotInDict)?;

impl<T: 'static> Forth<T> {
    pub const FULL_BUILTINS: &'static [BuiltinEntry<T>] = &[
        //
        // Math operations
        //
        builtin!("+", Self::add),
        builtin!("-", Self::minus),
        builtin!("/", Self::div),
        builtin!("mod", Self::modu),
        builtin!("/mod", Self::div_mod),
        builtin!("*", Self::mul),
        builtin!("abs", Self::abs),
        builtin!("negate", Self::negate),
        builtin!("min", Self::min),
        builtin!("max", Self::max),
        //
        // Floating Math operations
        //
        builtin_if_feature!("floats", "f+", Self::float_add),
        builtin_if_feature!("floats", "f-", Self::float_minus),
        builtin_if_feature!("floats", "f/", Self::float_div),
        builtin_if_feature!("floats", "fmod", Self::float_modu),
        builtin_if_feature!("floats", "f/mod", Self::float_div_mod),
        builtin_if_feature!("floats", "f*", Self::float_mul),
        builtin_if_feature!("floats", "fabs", Self::float_abs),
        builtin_if_feature!("floats", "fnegate", Self::float_negate),
        builtin_if_feature!("floats", "fmin", Self::float_min),
        builtin_if_feature!("floats", "fmax", Self::float_max),
        //
        // Double intermediate math operations
        //
        builtin!("*/", Self::star_slash),
        builtin!("*/mod", Self::star_slash_mod),
        //
        // Logic operations
        //
        builtin!("not", Self::invert),
        // NOTE! This is `bitand`, not logical `and`! e.g. `&` not `&&`.
        builtin!("and", Self::and),
        builtin!("=", Self::equal),
        builtin!(">", Self::greater),
        builtin!("<", Self::less),
        builtin!("0=", Self::zero_equal),
        builtin!("0>", Self::zero_greater),
        builtin!("0<", Self::zero_less),
        //
        // Stack operations
        //
        builtin!("swap", Self::swap),
        builtin!("dup", Self::dup),
        builtin!("over", Self::over),
        builtin!("rot", Self::rot),
        builtin!("drop", Self::ds_drop),
        //
        // Double operations
        //
        builtin!("2swap", Self::swap_2),
        builtin!("2dup", Self::dup_2),
        builtin!("2over", Self::over_2),
        builtin!("2drop", Self::ds_drop_2),
        //
        // String/Output operations
        //
        builtin!("emit", Self::emit),
        builtin!("cr", Self::cr),
        builtin!("space", Self::space),
        builtin!("spaces", Self::spaces),
        builtin!(".", Self::pop_print),
        builtin!("u.", Self::unsigned_pop_print),
        builtin_if_feature!("floats", "f.", Self::float_pop_print),
        //
        // Define/forget
        //
        builtin!(":", Self::colon),
        builtin!("forget", Self::forget),
        //
        // Stack/Retstack operations
        //
        builtin!("d>r", Self::data_to_return_stack),
        // NOTE: REQUIRED for `do/loop`
        builtin!("2d>2r", Self::data2_to_return2_stack),
        builtin!("r>d", Self::return_to_data_stack),
        //
        // Loop operations
        //
        builtin!("i", Self::loop_i),
        builtin!("i'", Self::loop_itick),
        builtin!("j", Self::loop_j),
        builtin!("leave", Self::loop_leave),
        //
        // Memory operations
        //
        builtin!("@", Self::var_load),
        builtin!("!", Self::var_store),
        builtin!("b@", Self::byte_var_load),
        builtin!("b!", Self::byte_var_store),
        builtin!("w+", Self::word_add),
        builtin!("'", Self::addr_of),
        builtin!("execute", Self::execute),
        //
        // Constants
        //
        builtin!("0", Self::zero_const),
        builtin!("1", Self::one_const),
        //
        // Introspection
        //
        builtin!("builtins", Self::list_builtins),
        builtin!("dict", Self::list_dict),
        builtin!(".s", Self::list_stack),
        builtin!("free", Self::dict_free),
        //
        // Other
        //
        // NOTE: REQUIRED for `."`
        builtin!("(write-str)", Self::write_str_lit),
        // NOTE: REQUIRED for `do/loop`
        builtin!("(jmp-doloop)", Self::jump_doloop),
        // NOTE: REQUIRED for `if/then` and `if/else/then`
        builtin!("(jump-zero)", Self::jump_if_zero),
        // NOTE: REQUIRED for `if/else/then`
        builtin!("(jmp)", Self::jump),
        // NOTE: REQUIRED for `:` (if you want literals)
        builtin!("(literal)", Self::literal),
        // NOTE: REQUIRED for `:` (if you want literals)
        builtin!("(rliteral)", Self::rliteral),
        // NOTE: REQUIRED for `constant`
        builtin!("(constant)", Self::constant),
        // NOTE: REQUIRED for `variable` or `array`
        builtin!("(variable)", Self::variable),
        builtin!("panic", Self::panic),
    ];

    /// Dumps all stacks and ends the currently executing program
    pub fn panic(&mut self) -> Result<(), Error> {
        writeln!(&mut self.output, "cstack",)?;

        while let Some(c) = self.call_stack.pop() {
            writeln!(
                &mut self.output,
                "{} ({}/{})",
                unsafe { (*c.eh.as_ptr()).name.as_str() },
                c.idx,
                c.len,
            )?;
        }

        writeln!(&mut self.output, "\ndstack",)?;

        while self.pop_print().is_ok() {}

        writeln!(&mut self.output, "\nrstack",)?;

        while self.return_to_data_stack().is_ok() && self.pop_print().is_ok() {}

        Ok(())
    }

    pub fn dict_free(&mut self) -> Result<(), Error> {
        let capa = self.dict.alloc.capacity();
        let used = self.dict.alloc.used();
        let free = capa - used;
        writeln!(
            &mut self.output,
            "{}/{} bytes free ({} used)",
            free, capa, used
        )?;
        Ok(())
    }

    pub fn list_stack(&mut self) -> Result<(), Error> {
        let depth = self.data_stack.depth();
        write!(&mut self.output, "<{}> ", depth)?;
        for d in (0..depth).rev() {
            let val = self.data_stack.try_peek_back_n(d)?;
            write!(&mut self.output, "{} ", val.into_data())?;
        }
        self.output.push_str("\n")?;
        Ok(())
    }

    pub fn list_builtins(&mut self) -> Result<(), Error> {
        let Self {
            builtins, output, ..
        } = self;
        output.write_str("builtins: ")?;
        for bi in builtins.iter() {
            output.write_str(bi.hdr.name.as_str())?;
            output.write_str(", ")?;
        }
        output.write_str("\n")?;
        Ok(())
    }

    pub fn list_dict(&mut self) -> Result<(), Error> {
        let Self { output, dict, .. } = self;
        output.write_str("dictionary: ")?;
        for item in dict.entries() {
            output.write_str(unsafe { item.entry().as_ref() }.hdr.name.as_str())?;
            if let DictLocation::Parent(_) = item {
                // indicate that this binding is inherited from a parent.
                // XXX(eliza): i was initially gonna add "(inherited)" but that
                // makes some of the tests overflow their output buffer, lol.
                output.write_str("*")?;
            }
            output.write_str(", ")?;
        }
        output.write_str("\n")?;
        Ok(())
    }

    // addr offset w+
    pub fn word_add(&mut self) -> Result<(), Error> {
        let w_offset = self.data_stack.try_pop()?;
        let w_addr = self.data_stack.try_pop()?;
        let new_addr = unsafe {
            let offset = isize::try_from(w_offset.data).replace_err(Error::BadWordOffset)?;
            w_addr.ptr.cast::<Word>().offset(offset)
        };
        self.data_stack.push(Word::ptr(new_addr))?;
        Ok(())
    }

    pub fn byte_var_load(&mut self) -> Result<(), Error> {
        let w = self.data_stack.try_pop()?;
        let ptr = unsafe { w.ptr.cast::<u8>() };
        let val = unsafe { Word::data(i32::from(ptr.read())) };
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn byte_var_store(&mut self) -> Result<(), Error> {
        let w_addr = self.data_stack.try_pop()?;
        let w_val = self.data_stack.try_pop()?;
        unsafe {
            w_addr
                .ptr
                .cast::<u8>()
                .write((w_val.into_data() & 0xFF) as u8);
        }
        Ok(())
    }

    // TODO: Check alignment?
    pub fn var_load(&mut self) -> Result<(), Error> {
        let w = self.data_stack.try_pop()?;
        let ptr = unsafe { w.ptr.cast::<Word>() };
        let val = unsafe { ptr.read() };
        self.data_stack.push(val)?;
        Ok(())
    }

    // TODO: Check alignment?
    pub fn var_store(&mut self) -> Result<(), Error> {
        let w_addr = self.data_stack.try_pop()?;
        let w_val = self.data_stack.try_pop()?;
        unsafe {
            w_addr.ptr.cast::<Word>().write(w_val);
        }
        Ok(())
    }

    pub fn zero_const(&mut self) -> Result<(), Error> {
        self.data_stack.push(Word::data(0))?;
        Ok(())
    }

    pub fn one_const(&mut self) -> Result<(), Error> {
        self.data_stack.push(Word::data(1))?;
        Ok(())
    }

    pub fn constant(&mut self) -> Result<(), Error> {
        let me = self.call_stack.try_peek()?;
        let de = me.eh.cast::<DictionaryEntry<T>>();
        let cfa = unsafe { DictionaryEntry::<T>::pfa(de) };
        let val = unsafe { cfa.as_ptr().read() };
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn variable(&mut self) -> Result<(), Error> {
        let me = self.call_stack.try_peek()?;
        let de = me.eh.cast::<DictionaryEntry<T>>();
        let cfa = unsafe { DictionaryEntry::<T>::pfa(de) };
        let val = Word::ptr(cfa.as_ptr());
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn forget(&mut self) -> Result<(), Error> {
        // TODO: If anything we've defined in the dict has escaped into
        // the stack, variables, etc., we're definitely going to be in trouble.
        //
        // TODO: Check that we are in interpret and not compile mode?
        self.input.advance();
        let word = match self.input.cur_word() {
            None => return Err(Error::ForgetWithoutWordName),
            Some(s) => s,
        };
        let word_tmp = TmpFaStr::new_from(word);
        let defn = match self.find_in_dict(&word_tmp) {
            None => {
                if self.find_in_bis(&word_tmp).is_some() {
                    return Err(Error::CantForgetBuiltins);
                } else {
                    return Err(Error::ForgetNotInDict);
                }
            }
            Some(d) => d,
        };

        match defn {
            // The definition is in the current (mutable) dictionary. We can
            // forget it by zeroing out the entry in the current dictionary.
            DictLocation::Current(defn) => {
                // NOTE: We use the *name* pointer for rewinding, as we allocate the name before the item.
                let name_ptr = unsafe { defn.as_ref().hdr.name.as_ptr().cast_mut() };
                self.dict.tail = unsafe { defn.as_ref().link };
                let addr = defn.as_ptr();
                let name_contains = self.dict.alloc.contains(name_ptr.cast());
                let contains = self.dict.alloc.contains(addr.cast());
                let ordered = (addr as usize) <= (self.dict.alloc.cur as usize);

                if !(name_contains && contains && ordered) {
                    return Err(Error::InternalError);
                }

                let len = (self.dict.alloc.cur as usize) - (name_ptr as usize);
                unsafe {
                    name_ptr.write_bytes(0x00, len);
                }
                self.dict.alloc.cur = name_ptr;
            }
            // The definition is in a parent (frozen) dictionary. We can't
            // mutate that dictionary, so we must create a new entry in the
            // current dict saying that the definition is forgotten.
            // XXX(eliza): or this could be a runtime error? IDK...
            DictLocation::Parent(_de) => {
                todo!("eliza: forget parent definitions");
            }
        }

        Ok(())
    }

    pub fn over(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_peek_back_n(1)?;
        self.data_stack.push(a)?;
        Ok(())
    }

    pub fn over_2(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_peek_back_n(2)?;
        let b = self.data_stack.try_peek_back_n(3)?;
        self.data_stack.push(b)?;
        self.data_stack.push(a)?;
        Ok(())
    }

    pub fn rot(&mut self) -> Result<(), Error> {
        let n1 = self.data_stack.try_pop()?;
        let n2 = self.data_stack.try_pop()?;
        let n3 = self.data_stack.try_pop()?;
        self.data_stack.push(n2)?;
        self.data_stack.push(n1)?;
        self.data_stack.push(n3)?;
        Ok(())
    }

    pub fn ds_drop(&mut self) -> Result<(), Error> {
        let _a = self.data_stack.try_pop()?;
        Ok(())
    }

    pub fn ds_drop_2(&mut self) -> Result<(), Error> {
        let _a = self.data_stack.try_pop()?;
        let _b = self.data_stack.try_pop()?;
        Ok(())
    }

    pub fn swap(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        self.data_stack.push(a)?;
        self.data_stack.push(b)?;
        Ok(())
    }

    pub fn swap_2(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        let c = self.data_stack.try_pop()?;
        let d = self.data_stack.try_pop()?;
        self.data_stack.push(b)?;
        self.data_stack.push(a)?;
        self.data_stack.push(d)?;
        self.data_stack.push(c)?;
        Ok(())
    }

    pub fn space(&mut self) -> Result<(), Error> {
        self.output.push_bstr(b" ")?;
        Ok(())
    }

    pub fn spaces(&mut self) -> Result<(), Error> {
        let num = self.data_stack.try_pop()?;
        let num = num.into_data();

        if num.is_negative() {
            return Err(Error::LoopCountIsNegative);
        }
        for _ in 0..num {
            self.space()?;
        }
        Ok(())
    }

    pub fn cr(&mut self) -> Result<(), Error> {
        self.output.push_bstr(b"\n")?;
        Ok(())
    }

    fn skip_literal(&mut self) -> Result<(), Error> {
        let parent = self.call_stack.try_peek_back_n_mut(1)?;
        parent.offset(1)?;
        Ok(())
    }

    pub fn invert(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let val = if a == Word::data(0) {
            Word::data(-1)
        } else {
            Word::data(0)
        };
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn and(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        let val = Word::data(a.into_data() & b.into_data());
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn equal(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        let val = if a == b {
            Word::data(-1)
        } else {
            Word::data(0)
        };
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn greater(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        let val = if b.into_data() > a.into_data() { -1 } else { 0 };
        self.data_stack.push(Word::data(val))?;
        Ok(())
    }

    pub fn less(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        let val = if b.into_data() < a.into_data() { -1 } else { 0 };
        self.data_stack.push(Word::data(val))?;
        Ok(())
    }

    pub fn zero_equal(&mut self) -> Result<(), Error> {
        self.data_stack.push(Word::data(0))?;
        self.equal()
    }

    pub fn zero_greater(&mut self) -> Result<(), Error> {
        self.data_stack.push(Word::data(0))?;
        self.greater()
    }

    pub fn zero_less(&mut self) -> Result<(), Error> {
        self.data_stack.push(Word::data(0))?;
        self.less()
    }

    pub fn div_mod(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        if a.into_data() == 0 {
            return Err(Error::DivideByZero);
        }
        let rem = Word::data(b.into_data() % a.into_data());
        self.data_stack.push(rem)?;
        let val = Word::data(b.into_data() / a.into_data());
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn div(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        let val = {
            if a.into_data() == 0 {
                return Err(Error::DivideByZero);
            }
            Word::data(b.into_data() / a.into_data())
        };
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn modu(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        let val = {
            if a.into_data() == 0 {
                return Err(Error::DivideByZero);
            }
            Word::data(b.into_data() % a.into_data())
        };
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn loop_i(&mut self) -> Result<(), Error> {
        let a = self.return_stack.try_peek()?;
        self.data_stack.push(a)?;
        Ok(())
    }

    pub fn loop_itick(&mut self) -> Result<(), Error> {
        let a = self.return_stack.try_peek_back_n(1)?;
        self.data_stack.push(a)?;
        Ok(())
    }

    pub fn loop_j(&mut self) -> Result<(), Error> {
        let a = self.return_stack.try_peek_back_n(2)?;
        self.data_stack.push(a)?;
        Ok(())
    }

    pub fn loop_leave(&mut self) -> Result<(), Error> {
        // Pop the loop counter and limit
        let _ = self.return_stack.try_pop()?;
        let _ = self.return_stack.try_pop()?;
        // Pop the "end of loop" value
        let idx = self.return_stack.try_pop()?;
        let idx = idx.into_data();
        let idx = u16::try_from(idx).map_err(|_| Error::BadCfaOffset)?;

        // Move the parent's interpreter index forward to the end of loop index
        let parent = self.call_stack.try_peek_back_n_mut(1)?;
        parent.idx = idx;

        Ok(())
    }

    pub fn jump_doloop(&mut self) -> Result<(), Error> {
        let a = self.return_stack.try_pop()?;
        let b = self.return_stack.try_peek()?;
        let ctr = Word::data(a.into_data() + 1);
        let do_jmp = ctr != b;
        if do_jmp {
            self.return_stack.push(ctr)?;
            self.jump()
        } else {
            self.return_stack.try_pop()?;
            // also pop the loop len counter
            self.return_stack.try_pop()?;
            self.skip_literal()
        }
    }

    pub fn emit(&mut self) -> Result<(), Error> {
        let val = self.data_stack.try_pop()?;
        let val = val.into_data();
        self.output.push_bstr(&[val as u8])?;
        Ok(())
    }

    pub fn jump_if_zero(&mut self) -> Result<(), Error> {
        let do_jmp = {
            let val = self.data_stack.try_pop()?;
            val.into_data() == 0
        };
        if do_jmp {
            self.jump()
        } else {
            self.skip_literal()
        }
    }

    pub fn jump(&mut self) -> Result<(), Error> {
        let parent = self.call_stack.try_peek_back_n_mut(1)?;
        let offset = parent.get_current_val()?;
        parent.offset(offset)?;
        Ok(())
    }

    pub fn dup(&mut self) -> Result<(), Error> {
        let val = self.data_stack.try_peek()?;
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn dup_2(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        self.data_stack.push(b)?;
        self.data_stack.push(a)?;
        self.data_stack.push(b)?;
        self.data_stack.push(a)?;
        Ok(())
    }

    pub fn return_to_data_stack(&mut self) -> Result<(), Error> {
        let val = self.return_stack.try_pop()?;
        self.data_stack.push(val)?;
        Ok(())
    }

    pub fn data_to_return_stack(&mut self) -> Result<(), Error> {
        let val = self.data_stack.try_pop()?;
        self.return_stack.push(val)?;
        Ok(())
    }

    pub fn data2_to_return2_stack(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        self.return_stack.push(b)?;
        self.return_stack.push(a)?;
        Ok(())
    }

    pub fn pop_print(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        write!(&mut self.output, "{} ", a.into_data())?;
        Ok(())
    }

    pub fn unsigned_pop_print(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        write!(&mut self.output, "{} ", a.into_data() as u32)?;
        Ok(())
    }

    /// # Add (`+`)
    ///
    /// ```rust
    /// # use forth3::testutil::blocking_runtest;
    /// #
    /// # blocking_runtest(r#"
    /// > 1 2 +
    /// > .
    /// < 3 ok.
    /// # "#)
    pub fn add(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;

        // NOTE: CURSED BECAUSE OF POINTER MATH
        // context: https://cohost.org/jamesmunns/post/851945-oops-it-segfaults
        self.data_stack.push(Word::ptr_data(unsafe {
            let a = a.ptr as isize;
            let b = b.ptr as isize;
            a.wrapping_add(b)
        }))?;
        Ok(())
    }

    pub fn mul(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        self.data_stack
            .push(Word::data(a.into_data().wrapping_mul(b.into_data())))?;
        Ok(())
    }

    pub fn abs(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        self.data_stack
            .push(Word::data(a.into_data().wrapping_abs()))?;
        Ok(())
    }

    pub fn negate(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        self.data_stack
            .push(Word::data(a.into_data().wrapping_neg()))?;
        Ok(())
    }

    pub fn min(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        self.data_stack
            .push(Word::data(a.into_data().min(b.into_data())))?;
        Ok(())
    }

    pub fn max(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        self.data_stack
            .push(Word::data(a.into_data().max(b.into_data())))?;
        Ok(())
    }

    pub fn minus(&mut self) -> Result<(), Error> {
        let a = self.data_stack.try_pop()?;
        let b = self.data_stack.try_pop()?;
        // NOTE: CURSED BECAUSE OF POINTER MATH
        // context: https://cohost.org/jamesmunns/post/851945-oops-it-segfaults
        self.data_stack.push(Word::ptr_data(unsafe {
            let a = a.ptr as isize;
            let b = b.ptr as isize;
            b.wrapping_sub(a)
        }))?;
        Ok(())
    }

    pub fn star_slash(&mut self) -> Result<(), Error> {
        let n3 = self.data_stack.try_pop()?;
        let n2 = self.data_stack.try_pop()?;
        let n1 = self.data_stack.try_pop()?;
        self.data_stack.push(Word::data({
            (i64::from(n1.into_data()))
                .wrapping_mul(i64::from(n2.into_data()))
                .wrapping_div(i64::from(n3.into_data())) as i32
        }))?;
        Ok(())
    }

    pub fn star_slash_mod(&mut self) -> Result<(), Error> {
        let n3 = self.data_stack.try_pop()?;
        let n2 = self.data_stack.try_pop()?;
        let n1 = self.data_stack.try_pop()?;
        let top = i64::from(n1.into_data()).wrapping_mul(i64::from(n2.into_data()));
        let div = i64::from(n3.into_data());
        let quo = top / div;
        let rem = top % div;
        self.data_stack.push(Word::data(rem as i32))?;
        self.data_stack.push(Word::data(quo as i32))?;
        Ok(())
    }

    pub fn colon(&mut self) -> Result<(), Error> {
        let old_mode = core::mem::replace(&mut self.mode, Mode::Compile);
        let name = self.munch_name()?;

        // Allocate and initialize the dictionary entry
        //
        // TODO: Using `bump_write` here instead of just `bump` causes Miri to
        // get angry with a stacked borrows violation later when we attempt
        // to interpret a built word.
        // TODO(eliza): it's unfortunate we cannot easily use the "EntryBuilder"
        // type here, as it must mutably borrow the dictionary, and `munch_one`
        // must perform lookups...hmm...
        let dict_base = self.dict.alloc.bump::<DictionaryEntry<T>>()?;

        let mut len = 0u16;

        // Begin compiling until we hit the end of the line or a semicolon.
        loop {
            let munched = self.munch_one(&mut len)?;
            if munched == 0 {
                match self.input.cur_word() {
                    Some(";") => {
                        unsafe {
                            dict_base.as_ptr().write(DictionaryEntry {
                                hdr: EntryHeader {
                                    name,
                                    kind: EntryKind::Dictionary,
                                    len,
                                    _pd: PhantomData,
                                },
                                // TODO: Should we look up `(interpret)` for consistency?
                                // Use `find_word`?
                                func: Self::interpret,
                                // Don't link until we know we have a "good" entry!
                                link: self.dict.tail.take(),
                                parameter_field: [],
                            });
                        }
                        self.dict.tail = Some(dict_base);
                        self.mode = old_mode;
                        return Ok(());
                    }
                    Some(_) => {}
                    None => {
                        return Err(Error::ColonCompileMissingSemicolon);
                    }
                }
            }
        }
    }

    pub fn write_str_lit(&mut self) -> Result<(), Error> {
        let parent = self.call_stack.try_peek_back_n_mut(1)?;

        // The length in bytes is stored in the next word.
        let len = parent.get_current_val()?;
        let len_u16 = u16::try_from(len).replace_err(Error::LiteralStringTooLong)?;

        // Now we need to figure out how many words our inline string takes up
        let word_size = size_of::<Word>();
        let len_words = 1 + ((usize::from(len_u16) + (word_size - 1)) / word_size);
        let len_and_str = parent.get_next_n_words(len_words as u16)?;
        unsafe {
            // Skip the "len" word
            let start = len_and_str.as_ptr().add(1).cast::<u8>();
            // Then push the literal into the output buffer
            let u8_sli = core::slice::from_raw_parts(start, len_u16.into());
            self.output.push_bstr(u8_sli)?;
        }
        parent.offset(len_words as i32)?;
        Ok(())
    }

    /// `(rliteral)` is used mid-interpret to put the NEXT word of the parent's
    /// CFA into the *return* stack as a value
    pub fn rliteral(&mut self) -> Result<(), Error> {
        let parent = self.call_stack.try_peek_back_n_mut(1)?;
        let literal = parent.get_current_word()?;
        parent.offset(1)?;
        self.return_stack.push(literal)?;
        Ok(())
    }

    /// `(literal)` is used mid-interpret to put the NEXT word of the parent's
    /// CFA array into the stack as a value.
    pub fn literal(&mut self) -> Result<(), Error> {
        let parent = self.call_stack.try_peek_back_n_mut(1)?;
        let literal = parent.get_current_word()?;
        parent.offset(1)?;
        self.data_stack.push(literal)?;
        Ok(())
    }

    /// Looks up a name in the dictionary and places its address on the stack.
    pub fn addr_of(&mut self) -> Result<(), Error> {
        self.input.advance();
        let name = self.input.cur_word().ok_or(Error::AddrOfMissingName)?;
        match self.lookup(name)? {
            // The definition is in the current dictionary --- just push it.
            Lookup::Dict(DictLocation::Current(de)) => {
                self.data_stack.push(Word::ptr(de.as_ptr()))?
            }

            // The definition is in the parent (frozen) dictionary.
            // TODO(eliza): what should we do here?
            Lookup::Dict(DictLocation::Parent(de)) => {
                self.data_stack.push(Word::ptr(de.as_ptr()))?
            }

            Lookup::Builtin { bi } => self.data_stack.push(Word::ptr(bi.as_ptr()))?,

            #[cfg(feature = "async")]
            Lookup::Async { bi } => self.data_stack.push(Word::ptr(bi.as_ptr()))?,
            _ => return Err(Error::AddrOfNotAWord),
        }

        Ok(())
    }

    pub fn execute(&mut self) -> Result<(), Error> {
        let w = self.data_stack.try_pop()?;
        // pop the execute word off the stack
        self.call_stack.pop();
        unsafe {
            // Safety: YOLO :D
            let eh = w.ptr.cast::<EntryHeader<T>>();
            self.call_stack.push(crate::vm::CallContext {
                eh: NonNull::new_unchecked(eh),
                len: (*eh).len,
                idx: 0,
            })?;
        };

        Err(Error::PendingCallAgain)
    }
}