endbasic-core 0.13.0

The EndBASIC programming language - core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
// EndBASIC
// Copyright 2026 Julio Merino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Virtual machine for EndBASIC execution.

#[cfg(test)]
use crate::CallError;
use crate::UpcallError;
use crate::bytecode::{ExitCode, Register};
use crate::callable::{Callable, Scope};
use crate::compiler::SymbolKey;
use crate::image::{GlobalVarInfo, Image};
use crate::mem::{ConstantDatum, DatumPtr, Heap, HeapDatum};
use crate::num::U24;
use crate::reader::LineCol;
use std::collections::HashMap;
use std::rc::Rc;

mod context;
use context::{Context, ErrorHandler, InternalStopReason};

/// Default maximum number of call stack frames.
const DEFAULT_MAX_CALL_STACK: usize = 4096;

/// Limits for VM execution resources.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Limits {
    /// Maximum number of frames the call stack can contain.
    pub max_call_stack: usize,

    /// Maximum number of entries the heap can contain.
    pub max_heap_entries: U24,
}

impl Default for Limits {
    fn default() -> Self {
        Self { max_call_stack: DEFAULT_MAX_CALL_STACK, max_heap_entries: U24::MAX }
    }
}

/// Error returned when a global variable access encounters a type or shape mismatch.
///
/// This is distinct from a missing variable, which is represented by `None` in the
/// return value of `get_global` and `get_global_array`.
#[derive(Debug, thiserror::Error)]
pub enum GetGlobalError {
    /// The variable exists but is an array; use `get_global_array` instead.
    #[error("'{0}' is an array variable; use get_global_array to access it")]
    IsArray(String),

    /// The variable exists but is a scalar; use `get_global` instead.
    #[error("'{0}' is a scalar variable; use get_global to access it")]
    IsScalar(String),

    /// The array subscripts are out of bounds or invalid.
    #[error("{0}")]
    SubscriptOutOfBounds(String),
}

/// Result type for global variable access operations.
pub type GetGlobalResult<T> = Result<T, GetGlobalError>;

/// Opaque handle to invoke a pending upcall.
pub struct UpcallHandler<'a> {
    vm: &'a mut Vm,
    image: &'a Image,
}

impl<'a> UpcallHandler<'a> {
    /// Invokes the pending upcall.
    pub async fn invoke(self) -> Result<(), UpcallError> {
        let vm = self.vm;
        let image = self.image;
        let (index, first_reg, upcall_pc) = vm
            .pending_upcall
            .take()
            .expect("This is only reachable when the VM has a pending upcall");
        let (upcall, scope) = vm.prepare_upcall(image, index, first_reg, upcall_pc);
        let result = upcall.async_exec(scope).await;
        match vm.handle_upcall_result(image, upcall_pc, result) {
            Ok(()) => Ok(()),
            Err(e) => {
                vm.park_at_eof(image);
                Err(e)
            }
        }
    }
}

/// Representation of termination states from program execution.
pub enum StopReason<'a> {
    /// Execution terminated due to an `END` instruction.
    End(ExitCode),

    /// Execution terminated due to natural fallthrough.
    Eof,

    /// Execution stopped due to an instruction-level exception.
    Exception(LineCol, String),

    /// Execution stopped due to an asynchronous upcall that requires service from the caller.
    UpcallAsync(UpcallHandler<'a>),

    /// Execution stopped to yield control back to the caller.
    Yield,
}

/// Virtual machine for EndBASIC program execution.
pub struct Vm {
    /// Mapping of all available upcall names to their handlers.
    upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>>,

    /// Upcall names already resolved into `upcalls`.
    upcall_names: Vec<SymbolKey>,

    /// Upcalls used by the current image in index order.
    upcalls: Vec<Rc<dyn Callable>>,

    /// Heap memory for dynamic allocations.
    heap: Heap,

    /// Processor context for execution.
    context: Context,

    /// Last error seen by the VM, if any.
    last_error: Option<(LineCol, String)>,

    /// Details about the pending upcall that has to be handled by the caller.
    ///
    /// The tuple contains the upcall index, the first argument register, and the PC of the
    /// upcall instruction (for arg position lookup in `DebugInfo`).
    pending_upcall: Option<(u16, Register, usize)>,
}

impl Vm {
    /// Resolves upcall metadata and builds an execution scope for invocation.
    fn prepare_upcall<'a>(
        &'a mut self,
        image: &'a Image,
        index: u16,
        first_reg: Register,
        upcall_pc: usize,
    ) -> (Rc<dyn Callable>, Scope<'a>) {
        let upcall = self.upcalls[usize::from(index)].clone();
        let is_function = upcall.metadata().return_type().is_some();
        let scope = self.upcall_scope(image, first_reg, is_function, upcall_pc);
        (upcall, scope)
    }

    /// Handles the result of an upcall invocation.
    ///
    /// Returns `Ok(())` if invocation succeeded or if an exception handler consumed the
    /// failure.  Returns `Err` only if execution must stop with an uncaught exception.
    fn handle_upcall_result(
        &mut self,
        image: &Image,
        upcall_pc: usize,
        result: crate::callable::CallResult<()>,
    ) -> Result<(), UpcallError> {
        match result {
            Ok(()) => Ok(()),
            Err(e) => {
                let default_pos = image.debug_info.instrs[upcall_pc].linecol;
                let upcall_error = e.to_upcall_error(default_pos);
                let (pos, message) = upcall_error.parts();
                if self.handle_exception(image, upcall_pc, pos, message) {
                    Ok(())
                } else {
                    Err(upcall_error)
                }
            }
        }
    }

    /// Returns the scalar value named `key` from `vars`, decoding values from `read_raw`.
    fn get_scalar_var(
        &self,
        image: &Image,
        key: &SymbolKey,
        vars: &HashMap<SymbolKey, GlobalVarInfo>,
        read_raw: fn(&Context, u8) -> u64,
    ) -> GetGlobalResult<Option<ConstantDatum>> {
        let Some(info) = vars.get(key) else {
            return Ok(None);
        };
        if info.ndims != 0 {
            return Err(GetGlobalError::IsArray(key.to_string()));
        }
        let raw = read_raw(&self.context, info.reg);
        Ok(Some(ConstantDatum::from_raw(raw, info.subtype, &image.constants, &self.heap)))
    }

    /// Returns the array element named `key` from `vars`, decoding values from `read_raw`.
    fn get_array_var(
        &self,
        image: &Image,
        key: &SymbolKey,
        vars: &HashMap<SymbolKey, GlobalVarInfo>,
        subscripts: &[i32],
        read_raw: fn(&Context, u8) -> u64,
    ) -> GetGlobalResult<Option<ConstantDatum>> {
        let Some(info) = vars.get(key) else {
            return Ok(None);
        };
        if info.ndims == 0 {
            return Err(GetGlobalError::IsScalar(key.to_string()));
        }
        let raw = read_raw(&self.context, info.reg);
        let ptr = DatumPtr::from(raw);
        let heap_idx = ptr.heap_index();
        let HeapDatum::Array(a) = self.heap.get(heap_idx) else {
            panic!("Array variable does not point to an array on the heap");
        };
        let flat_idx = a.flat_index(subscripts).map_err(GetGlobalError::SubscriptOutOfBounds)?;
        let v = a.values[flat_idx];
        Ok(Some(ConstantDatum::from_raw(v, info.subtype, &image.constants, &self.heap)))
    }

    /// Creates a new VM with the given `upcalls_by_name` as the available built-in callables.
    pub fn new(upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>>) -> Self {
        Self::new_with_limits(upcalls_by_name, Limits::default())
    }

    /// Creates a new VM with the given `upcalls_by_name` and resource `limits`.
    pub fn new_with_limits(
        upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>>,
        limits: Limits,
    ) -> Self {
        Self {
            upcalls_by_name,
            upcall_names: vec![],
            upcalls: vec![],
            heap: Heap::new(limits.max_heap_entries),
            context: Context::new(limits.max_call_stack),
            last_error: None,
            pending_upcall: None,
        }
    }

    /// Resets any existing execution state, including upcall caches and the program counter.
    pub fn reset(&mut self) {
        self.upcall_names.clear();
        self.upcalls.clear();
        self.heap.clear();
        self.context.clear_runtime_state();
        self.last_error = None;
        self.pending_upcall = None;
    }

    /// Resets runtime state (registers, heap, last error, call stack, program counter) but preserves
    /// upcall caches so the same image can continue to be executed.
    ///
    /// Note: because the program counter is also reset, callers need to either re-set it to a
    /// specific location or replace the image with one that resumes from the start.
    pub fn clear(&mut self) {
        self.heap.clear();
        self.context.clear_runtime_state();
        self.last_error = None;
        self.pending_upcall = None;
    }

    /// Clears the current error handler without affecting execution state or captured errors.
    pub fn clear_error_handler(&mut self) {
        self.context.clear_error_handler();
    }

    /// Synchronizes cached upcall handlers with the externally-owned `image`.
    fn sync_upcalls(&mut self, image: &Image) {
        debug_assert!(
            image.upcalls.starts_with(self.upcall_names.as_slice()),
            "Vm::reset() is required before executing a different image",
        );

        for key in &image.upcalls[self.upcalls.len()..] {
            self.upcalls.push(
                self.upcalls_by_name
                    .get(key)
                    .expect("All upcalls exposed during compilation must be present at runtime")
                    .clone(),
            );
            self.upcall_names.push(key.clone());
        }
    }

    /// Parks execution at the current EOF instruction so later appended code can resume.
    fn park_at_eof(&mut self, image: &Image) {
        debug_assert!(!image.code.is_empty());
        self.context.set_pc(image.code.len() - 1);
    }

    /// Constructs a `Scope` for an upcall with arguments starting at `reg`.
    ///
    /// `upcall_pc` is the address of the UPCALL instruction in the image, used to look up
    /// per-argument source locations from `DebugInfo`.  `is_function` indicates whether the
    /// upcall is a function (with a return value slot) so that `Scope::arg_offset` can be set
    /// appropriately.
    fn upcall_scope<'a>(
        &'a mut self,
        image: &'a Image,
        reg: Register,
        is_function: bool,
        upcall_pc: usize,
    ) -> Scope<'a> {
        let arg_linecols = image
            .debug_info
            .instrs
            .get(upcall_pc)
            .map(|m| m.arg_linecols.as_slice())
            .unwrap_or(&[]);
        self.context.upcall_scope(
            reg,
            is_function,
            image.constants.as_slice(),
            &mut self.heap,
            arg_linecols,
            &self.last_error,
            image.data.as_slice(),
        )
    }

    /// Handles an exception raised at `pc`, corresponding to `pos`, with `message`.  Returns true if the error was handled.
    fn handle_exception(
        &mut self,
        image: &Image,
        pc: usize,
        pos: LineCol,
        message: String,
    ) -> bool {
        self.last_error = Some((pos, message));

        match self.context.error_handler() {
            ErrorHandler::None => false,
            ErrorHandler::Jump { active: true, .. } => false,
            ErrorHandler::Jump { active: false, addr } => {
                self.context.set_error_handler_active();
                self.context.set_pc(addr);
                true
            }
            ErrorHandler::ResumeNext => {
                let mut next_pc = image.code.len();
                for (idx, meta) in image.debug_info.instrs.iter().enumerate().skip(pc + 1) {
                    if meta.is_stmt_start {
                        next_pc = idx;
                        break;
                    }
                }
                self.context.set_pc(next_pc);
                true
            }
        }
    }

    /// Returns the value of the global scalar variable `key` as a `ConstantDatum`.
    ///
    /// Returns `Ok(None)` if the variable is not defined (no image is loaded or the
    /// variable was not declared).  Returns `Err` if the variable exists but is an
    /// array; in that case, use `get_global_array` instead.
    pub fn get_global(
        &self,
        image: &Image,
        key: &SymbolKey,
    ) -> GetGlobalResult<Option<ConstantDatum>> {
        self.get_scalar_var(image, key, &image.debug_info.global_vars, Context::get_global_reg_raw)
    }

    /// Returns the value of an element in the global array variable `key` at the given
    /// `subscripts` as a `ConstantDatum`.
    ///
    /// Returns `Ok(None)` if the variable is not defined (no image is loaded or the
    /// variable was not declared).  Returns `Err` if the variable exists but is a scalar
    /// (use `get_global` instead), or if the subscripts are out of bounds.
    pub fn get_global_array(
        &self,
        image: &Image,
        key: &SymbolKey,
        subscripts: &[i32],
    ) -> GetGlobalResult<Option<ConstantDatum>> {
        self.get_array_var(
            image,
            key,
            &image.debug_info.global_vars,
            subscripts,
            Context::get_global_reg_raw,
        )
    }

    /// Returns the value of the program-scope scalar variable `key` as a `ConstantDatum`.
    ///
    /// Returns `Ok(None)` if the variable is not defined (no image is loaded or the
    /// variable was not declared).  Returns `Err` if the variable exists but is an
    /// array; in that case, use `get_program_array` instead.
    pub fn get_program(
        &self,
        image: &Image,
        key: &SymbolKey,
    ) -> GetGlobalResult<Option<ConstantDatum>> {
        self.get_scalar_var(
            image,
            key,
            &image.debug_info.program_vars,
            Context::get_program_reg_raw,
        )
    }

    /// Returns the value of an element in the program-scope array variable `key` at the
    /// given `subscripts` as a `ConstantDatum`.
    ///
    /// Returns `Ok(None)` if the variable is not defined (no image is loaded or the
    /// variable was not declared).  Returns `Err` if the variable exists but is a scalar
    /// (use `get_program` instead), or if the subscripts are out of bounds.
    pub fn get_program_array(
        &self,
        image: &Image,
        key: &SymbolKey,
        subscripts: &[i32],
    ) -> GetGlobalResult<Option<ConstantDatum>> {
        self.get_array_var(
            image,
            key,
            &image.debug_info.program_vars,
            subscripts,
            Context::get_program_reg_raw,
        )
    }

    /// Starts or resumes execution of `image`.
    ///
    /// Returns a `StopReason` indicating why execution stopped, which may be due to program
    /// termination, an exception, or a pending upcall that requires caller handling.
    pub fn exec<'a>(&'a mut self, image: &'a Image) -> StopReason<'a> {
        self.sync_upcalls(image);

        loop {
            if self.pending_upcall.is_some() {
                return StopReason::UpcallAsync(UpcallHandler { vm: self, image });
            }

            match self.context.exec(image, &mut self.heap) {
                InternalStopReason::End(code) => {
                    self.park_at_eof(image);
                    return StopReason::End(code);
                }
                InternalStopReason::Eof => return StopReason::Eof,
                InternalStopReason::Exception(pc, e) => {
                    let pos = image.debug_info.instrs[pc].linecol;
                    if !self.handle_exception(image, pc, pos, e.clone()) {
                        self.park_at_eof(image);
                        return StopReason::Exception(pos, e);
                    }
                }
                InternalStopReason::Upcall(index, first_reg, upcall_pc) => {
                    let (upcall, scope) = self.prepare_upcall(image, index, first_reg, upcall_pc);
                    let result = upcall.exec(scope);
                    if let Err(upcall_error) = self.handle_upcall_result(image, upcall_pc, result) {
                        let (pos, message) = upcall_error.parts();
                        self.park_at_eof(image);
                        return StopReason::Exception(pos, message);
                    }
                }

                InternalStopReason::UpcallAsync(index, first_reg, upcall_pc) => {
                    self.pending_upcall = Some((index, first_reg, upcall_pc));
                    return StopReason::UpcallAsync(UpcallHandler { vm: self, image });
                }

                InternalStopReason::Yield => return StopReason::Yield,
            }
        }
    }

    /// Stops execution of `image` so that the next call to `exec` starts at EOF.
    ///
    /// This is useful when external events interrupt execution and the caller wants
    /// to avoid resuming a partially-run image by mistake.
    pub fn interrupt(&mut self, image: &Image) {
        self.pending_upcall = None;
        self.park_at_eof(image);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Compiler;
    use crate::ast::{ArgSep, ExprType};
    use crate::callable::{
        ArgSepSyntax, CallResult, CallableMetadata, CallableMetadataBuilder, RequiredValueSyntax,
        SingularArgSyntax,
    };
    use crate::compiler::SymbolKey;
    use crate::image::Image;
    use crate::reader::LineCol;
    use crate::testutils::OutCommand;
    use async_trait::async_trait;
    use futures_lite::future::yield_now;
    use std::borrow::Cow;
    use std::cell::RefCell;
    use std::collections::HashMap;
    use std::io;
    use std::rc::Rc;

    /// A test callable that captures the source positions of argument register slots.
    ///
    /// On each invocation, records the result of `scope.get_pos(n)` for `0..nargs` into
    /// `positions`.
    struct PosCapture {
        metadata: Rc<CallableMetadata>,
        nargs: u8,
        positions: Rc<RefCell<Vec<LineCol>>>,
    }

    impl PosCapture {
        /// Creates a new `PosCapture` callable named `POS_CAPTURE` that expects
        /// `nargs` required integer arguments separated by commas.
        fn new(nargs: u8, positions: Rc<RefCell<Vec<LineCol>>>) -> Rc<Self> {
            let singular: Vec<SingularArgSyntax> = (0..nargs)
                .map(|i| {
                    let sep = if i == nargs - 1 {
                        ArgSepSyntax::End
                    } else {
                        ArgSepSyntax::Exactly(ArgSep::Long)
                    };
                    SingularArgSyntax::RequiredValue(
                        RequiredValueSyntax {
                            name: Cow::Borrowed("arg"),
                            vtype: ExprType::Integer,
                        },
                        sep,
                    )
                })
                .collect();
            let md = CallableMetadataBuilder::new("POS_CAPTURE")
                .with_dynamic_syntax(vec![(singular, None)])
                .test_build();
            Rc::from(Self { metadata: md, nargs, positions })
        }
    }

    impl Callable for PosCapture {
        fn metadata(&self) -> Rc<CallableMetadata> {
            self.metadata.clone()
        }

        fn exec(&self, scope: Scope<'_>) -> CallResult<()> {
            let mut positions = self.positions.borrow_mut();
            for i in 0..self.nargs {
                positions.push(scope.get_pos(i));
            }
            Ok(())
        }
    }

    struct ReturnFortyTwoFunction {
        metadata: Rc<CallableMetadata>,
    }

    impl ReturnFortyTwoFunction {
        fn new() -> Rc<Self> {
            let md = CallableMetadataBuilder::new("RET42")
                .with_return_type(ExprType::Integer)
                .with_syntax(&[(&[], None)])
                .test_build();
            Rc::from(Self { metadata: md })
        }
    }

    impl Callable for ReturnFortyTwoFunction {
        fn metadata(&self) -> Rc<CallableMetadata> {
            self.metadata.clone()
        }

        fn exec(&self, scope: Scope<'_>) -> CallResult<()> {
            scope.return_integer(42)
        }
    }

    struct IoErrorCommand {
        metadata: Rc<CallableMetadata>,
    }

    impl IoErrorCommand {
        fn new() -> Rc<Self> {
            let md = CallableMetadataBuilder::new("IOFAIL")
                .with_dynamic_syntax(vec![(vec![], None)])
                .test_build();
            Rc::from(Self { metadata: md })
        }
    }

    struct AsyncIncrementFunction {
        metadata: Rc<CallableMetadata>,
    }

    impl AsyncIncrementFunction {
        fn new() -> Rc<Self> {
            let md = CallableMetadataBuilder::new("ASYNC_INCREMENT")
                .with_return_type(ExprType::Integer)
                .with_async(true)
                .with_syntax(&[(
                    &[SingularArgSyntax::RequiredValue(
                        RequiredValueSyntax {
                            name: Cow::Borrowed("value"),
                            vtype: ExprType::Integer,
                        },
                        ArgSepSyntax::End,
                    )],
                    None,
                )])
                .test_build();
            Rc::from(Self { metadata: md })
        }
    }

    #[async_trait(?Send)]
    impl Callable for AsyncIncrementFunction {
        fn metadata(&self) -> Rc<CallableMetadata> {
            self.metadata.clone()
        }

        async fn async_exec(&self, scope: Scope<'_>) -> CallResult<()> {
            let value = scope.get_integer(0) + 1;
            yield_now().await;
            scope.return_integer(value)
        }
    }

    struct AsyncIoErrorCommand {
        metadata: Rc<CallableMetadata>,
    }

    impl AsyncIoErrorCommand {
        fn new() -> Rc<Self> {
            let md = CallableMetadataBuilder::new("ASYNC_IOFAIL")
                .with_async(true)
                .with_dynamic_syntax(vec![(vec![], None)])
                .test_build();
            Rc::from(Self { metadata: md })
        }
    }

    #[async_trait(?Send)]
    impl Callable for AsyncIoErrorCommand {
        fn metadata(&self) -> Rc<CallableMetadata> {
            self.metadata.clone()
        }

        async fn async_exec(&self, _scope: Scope<'_>) -> CallResult<()> {
            yield_now().await;
            Err(CallError::from(io::Error::other("mock async I/O error")))
        }
    }

    #[async_trait(?Send)]
    impl Callable for IoErrorCommand {
        fn metadata(&self) -> Rc<CallableMetadata> {
            self.metadata.clone()
        }

        fn exec(&self, _scope: Scope<'_>) -> CallResult<()> {
            Err(CallError::from(io::Error::other("mock I/O error")))
        }
    }

    /// Runs the VM to completion, invoking every upcall as it is encountered.
    async fn run_to_end(vm: &mut Vm, image: &Image) {
        loop {
            match vm.exec(image) {
                StopReason::End(_) => break,
                StopReason::Eof => break,
                StopReason::Exception(_, msg) => panic!("Unexpected exception: {}", msg),
                StopReason::UpcallAsync(handler) => handler.invoke().await.unwrap(),
                StopReason::Yield => (),
            }
        }
    }

    #[test]
    fn test_exec_without_load_is_eof() {
        let mut vm = Vm::new(HashMap::default());
        let image = Image::default();
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Unexpected stop reason"),
        }
    }

    #[test]
    fn test_exec_empty_image_is_eof() {
        let mut vm = Vm::new(HashMap::default());
        let image = Image::default();
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Unexpected stop reason"),
        }
    }

    #[test]
    fn test_exec_empty_compilation_is_eof() {
        let mut vm = Vm::new(HashMap::default());
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image = compiler.compile(&mut b"".as_slice()).unwrap();
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Unexpected stop reason"),
        }
    }

    #[tokio::test]
    async fn test_exec_upcall_flow() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"OUT 30: OUT 20".as_slice()).unwrap();

        let mut vm = Vm::new(upcalls_by_name);

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF"),
        }
        assert_eq!(["30", "20"], *data.borrow().as_slice());
    }

    #[tokio::test]
    async fn test_exec_async_upcall_flow() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("ASYNC_INCREMENT"), AsyncIncrementFunction::new());
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"OUT ASYNC_INCREMENT(123): OUT 5".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);

        match vm.exec(&image) {
            StopReason::UpcallAsync(handler) => handler.invoke().await.unwrap(),
            _ => panic!("Execution should stop at ASYNC_INCREMENT upcall"),
        }

        assert!(data.borrow().is_empty());

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF"),
        }

        assert_eq!(["124", "5"], *data.borrow().as_slice());
    }

    #[tokio::test]
    async fn test_exec_async_upcall_error_can_resume_after_append() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("ASYNC_IOFAIL"), AsyncIoErrorCommand::new());
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let mut compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let mut image = Image::default();
        compiler.compile_more(&mut image, &mut b"ASYNC_IOFAIL".as_slice()).unwrap();

        let mut vm = Vm::new(upcalls_by_name);
        match vm.exec(&image) {
            StopReason::UpcallAsync(handler) => {
                let error = handler.invoke().await.unwrap_err();
                let (pos, message) = error.parts();
                assert_eq!(LineCol { line: 1, col: 1 }, pos);
                assert_eq!("mock async I/O error", message);
            }
            _ => panic!("Execution should stop at ASYNC_IOFAIL upcall"),
        }

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should park at EOF after an ASYNC_IOFAIL exception"),
        }

        compiler.compile_more(&mut image, &mut b"OUT 2".as_slice()).unwrap();
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should resume at newly appended code"),
        }
        assert_eq!(["2"], *data.borrow().as_slice());

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after appended code"),
        }
    }

    #[tokio::test]
    async fn test_interrupt_cancels_pending_async_upcall() {
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("ASYNC_INCREMENT"), AsyncIncrementFunction::new());

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"x = ASYNC_INCREMENT(123)".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);

        match vm.exec(&image) {
            StopReason::UpcallAsync(_) => (),
            _ => panic!("Execution should stop at ASYNC_INCREMENT upcall"),
        }

        vm.interrupt(&image);
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after interrupting a pending upcall"),
        }
    }

    #[tokio::test]
    async fn test_interrupt_after_pending_async_upcall_can_resume_after_append() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("ASYNC_INCREMENT"), AsyncIncrementFunction::new());
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let mut compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let mut image = Image::default();
        compiler.compile_more(&mut image, &mut b"x = ASYNC_INCREMENT(123)".as_slice()).unwrap();

        let mut vm = Vm::new(upcalls_by_name);
        match vm.exec(&image) {
            StopReason::UpcallAsync(_) => (),
            _ => panic!("Execution should stop at ASYNC_INCREMENT upcall"),
        }

        vm.interrupt(&image);
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after interrupting a pending upcall"),
        }

        compiler.compile_more(&mut image, &mut b"OUT 2".as_slice()).unwrap();
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should resume at newly appended code"),
        }
        assert_eq!(["2"], *data.borrow().as_slice());

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after appended code"),
        }
    }

    #[tokio::test]
    async fn test_exec_end_code_default() {
        let mut vm = Vm::new(HashMap::default());
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image = compiler.compile(&mut b"END".as_slice()).unwrap();
        match vm.exec(&image) {
            StopReason::End(code) if code.is_success() => (),
            _ => panic!("Unexpected stop reason"),
        }
    }

    #[tokio::test]
    async fn test_exec_end_code_explicit() {
        let mut vm = Vm::new(HashMap::default());
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image = compiler.compile(&mut b"END 3".as_slice()).unwrap();
        match vm.exec(&image) {
            StopReason::End(code) if code.to_i32() == 3 => (),
            _ => panic!("Unexpected stop reason"),
        }
    }

    #[tokio::test]
    async fn test_exec_end_can_resume_after_append() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let mut compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let mut image = Image::default();
        compiler.compile_more(&mut image, &mut b"END 3".as_slice()).unwrap();

        let mut vm = Vm::new(upcalls_by_name);
        match vm.exec(&image) {
            StopReason::End(code) if code.to_i32() == 3 => (),
            _ => panic!("Unexpected stop reason"),
        }
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should park at EOF after END"),
        }

        compiler.compile_more(&mut image, &mut b"OUT 2".as_slice()).unwrap();
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should resume at newly appended code"),
        }
        assert_eq!(["2"], *data.borrow().as_slice());

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after appended code"),
        }
    }

    #[tokio::test]
    async fn test_exec_exception_can_resume_after_append() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let mut compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let mut image = Image::default();
        compiler.compile_more(&mut image, &mut b"a = 1 / 0".as_slice()).unwrap();

        let mut vm = Vm::new(upcalls_by_name);
        match vm.exec(&image) {
            StopReason::Exception(_, msg) if msg == "Division by zero" => (),
            _ => panic!("Unexpected stop reason"),
        }
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should park at EOF after an exception"),
        }

        compiler.compile_more(&mut image, &mut b"OUT 2".as_slice()).unwrap();
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should resume at newly appended code"),
        }
        assert_eq!(["2"], *data.borrow().as_slice());

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after appended code"),
        }
    }

    #[tokio::test]
    async fn test_exec_upcall_can_return_with_scope_helper() {
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("RET42"), ReturnFortyTwoFunction::new());

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"x = RET42".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);
        run_to_end(&mut vm, &image).await;

        assert_eq!(
            Some(ConstantDatum::Integer(42)),
            vm.get_program(&image, &SymbolKey::from("x")).unwrap()
        );
    }

    #[tokio::test]
    async fn test_exec_upcall_io_error_is_reported() {
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("IOFAIL"), IoErrorCommand::new());

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"IOFAIL".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);

        match vm.exec(&image) {
            StopReason::Exception(_, msg) if msg == "mock I/O error" => (),
            _ => panic!("Execution should stop at an IOFAIL exception"),
        };

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after serving error"),
        }
    }

    #[tokio::test]
    async fn test_exec_upcall_io_error_can_be_caught() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("IOFAIL"), IoErrorCommand::new());
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler
            .compile(
                &mut br#"
                    ON ERROR GOTO @recover
                    IOFAIL
                    END 5
                    @recover
                    OUT "ok"
                "#
                .as_slice(),
            )
            .unwrap();
        let mut vm = Vm::new(upcalls_by_name);

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should complete after handling IOFAIL"),
        };

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should have reached EOF after OUT"),
        }

        assert_eq!(["ok"], *data.borrow().as_slice());
        assert_eq!(
            Some((LineCol { line: 3, col: 21 }, "mock I/O error".to_owned())),
            vm.last_error
        );
    }

    #[tokio::test]
    async fn test_exec_yields_on_backward_jump() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image = compiler.compile(&mut b"x = 0: DO: x = x + 1: LOOP".as_slice()).unwrap();
        let mut vm = Vm::new(HashMap::default());

        match vm.exec(&image) {
            StopReason::Yield => (),
            _ => panic!("Execution should yield in a loop"),
        }
        assert_eq!(
            Some(ConstantDatum::Integer(1)),
            vm.get_program(&image, &SymbolKey::from("x")).unwrap()
        );

        match vm.exec(&image) {
            StopReason::Yield => (),
            _ => panic!("Execution should continue yielding in a loop"),
        }
        assert_eq!(
            Some(ConstantDatum::Integer(2)),
            vm.get_program(&image, &SymbolKey::from("x")).unwrap()
        );

        vm.interrupt(&image);
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF after interrupt"),
        }
    }

    #[tokio::test]
    async fn test_exec_yields_after_gosub_return() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image =
            compiler.compile(&mut b"GOSUB @foo: END\n@foo: x = x + 1: RETURN".as_slice()).unwrap();
        let mut vm = Vm::new(HashMap::default());

        match vm.exec(&image) {
            StopReason::Yield => (),
            _ => panic!("Execution should yield after returning from GOSUB"),
        }
        assert_eq!(
            Some(ConstantDatum::Integer(1)),
            vm.get_program(&image, &SymbolKey::from("x")).unwrap()
        );

        match vm.exec(&image) {
            StopReason::End(code) if code.is_success() => (),
            _ => panic!("Execution should continue after yield"),
        }
    }

    #[tokio::test]
    async fn test_interrupt_parks_execution_at_eof() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"OUT 1: OUT 2".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF"),
        }
        assert_eq!(["1", "2"], *data.borrow().as_slice());

        vm.interrupt(&image);
        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should be parked at EOF after interruption"),
        }
        assert_eq!(["1", "2"], *data.borrow().as_slice());
    }

    #[tokio::test]
    async fn test_clear_resets_runtime_state() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image = compiler.compile(&mut b"x = 7".as_slice()).unwrap();
        let mut vm = Vm::new(HashMap::default());
        run_to_end(&mut vm, &image).await;

        assert_eq!(
            Some(ConstantDatum::Integer(7)),
            vm.get_program(&image, &SymbolKey::from("x")).unwrap()
        );

        vm.clear();

        assert_eq!(
            Some(ConstantDatum::Integer(0)),
            vm.get_program(&image, &SymbolKey::from("x")).unwrap()
        );
    }

    #[tokio::test]
    async fn test_clear_preserves_upcall_caches() {
        let data = Rc::from(RefCell::from(vec![]));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("OUT"), OutCommand::new(data.clone()));

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"OUT 3".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should stop at EOF"),
        }
        assert_eq!(["3"], *data.borrow().as_slice());

        vm.clear();

        match vm.exec(&image) {
            StopReason::Eof => (),
            _ => panic!("Execution should still stop at EOF after clear"),
        }
        assert_eq!(["3", "3"], *data.borrow().as_slice());
    }

    #[tokio::test]
    async fn test_reset_preserves_call_stack_limit() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image = compiler
            .compile(
                &mut br#"
                    SUB recurse(n%)
                        IF n < 20 THEN
                            recurse n + 1
                        END IF
                    END SUB

                    recurse 0
                "#
                .as_slice(),
            )
            .unwrap();
        let mut vm = Vm::new_with_limits(
            HashMap::default(),
            Limits { max_call_stack: 8, max_heap_entries: U24::MAX },
        );

        match vm.exec(&image) {
            StopReason::Exception(_, msg) if msg == "Out of call stack space" => (),
            _ => panic!("Execution should stop when the call stack limit is reached"),
        }

        vm.reset();

        match vm.exec(&image) {
            StopReason::Exception(_, msg) if msg == "Out of call stack space" => (),
            _ => panic!("Execution should preserve the configured call stack limit after reset"),
        }
    }

    #[tokio::test]
    async fn test_scope_get_pos_no_args() {
        let positions: Rc<RefCell<Vec<LineCol>>> = Rc::default();
        let cmd = PosCapture::new(0, positions.clone());
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("POS_CAPTURE"), cmd);

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"POS_CAPTURE".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);
        run_to_end(&mut vm, &image).await;

        let pos = positions.borrow();
        assert_eq!(&[] as &[LineCol], pos.as_slice());
    }

    #[tokio::test]
    async fn test_scope_get_pos_single_arg() {
        let positions: Rc<RefCell<Vec<LineCol>>> = Rc::default();
        let cmd = PosCapture::new(1, positions.clone());
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("POS_CAPTURE"), cmd);

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"POS_CAPTURE 42".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);
        run_to_end(&mut vm, &image).await;

        let pos = positions.borrow();
        assert_eq!(&[LineCol { line: 1, col: 13 }], pos.as_slice());
    }

    #[tokio::test]
    async fn test_scope_get_pos_multiple_args() {
        let positions: Rc<RefCell<Vec<LineCol>>> = Rc::default();
        let cmd = PosCapture::new(3, positions.clone());
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("POS_CAPTURE"), cmd);

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"POS_CAPTURE 1, 2, 3".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);
        run_to_end(&mut vm, &image).await;

        let pos = positions.borrow();
        assert_eq!(
            &[
                LineCol { line: 1, col: 13 },
                LineCol { line: 1, col: 16 },
                LineCol { line: 1, col: 19 }
            ],
            pos.as_slice()
        );
    }

    #[tokio::test]
    async fn test_scope_get_pos_expression_arg() {
        let positions: Rc<RefCell<Vec<LineCol>>> = Rc::default();
        let cmd = PosCapture::new(1, positions.clone());
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::new();
        upcalls_by_name.insert(SymbolKey::from("POS_CAPTURE"), cmd);

        let compiler = Compiler::new(&upcalls_by_name, &[]).unwrap();
        let image = compiler.compile(&mut b"POS_CAPTURE 1 + 2".as_slice()).unwrap();
        let mut vm = Vm::new(upcalls_by_name);
        run_to_end(&mut vm, &image).await;

        let pos = positions.borrow();
        assert_eq!(&[LineCol { line: 1, col: 13 }], pos.as_slice());
    }

    #[tokio::test]
    async fn test_get_program_scalar() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image = compiler.compile(&mut b"x = 123".as_slice()).unwrap();
        let mut vm = Vm::new(HashMap::default());
        run_to_end(&mut vm, &image).await;

        assert_eq!(
            Some(ConstantDatum::Integer(123)),
            vm.get_program(&image, &SymbolKey::from("x")).unwrap()
        );
        assert_eq!(None, vm.get_program(&image, &SymbolKey::from("missing")).unwrap());
    }

    #[tokio::test]
    async fn test_get_program_array() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image =
            compiler.compile(&mut b"DIM arr(2) AS INTEGER: arr(1) = 45".as_slice()).unwrap();
        let mut vm = Vm::new(HashMap::default());
        run_to_end(&mut vm, &image).await;

        assert_eq!(
            Some(ConstantDatum::Integer(45)),
            vm.get_program_array(&image, &SymbolKey::from("arr"), &[1]).unwrap()
        );
    }

    #[tokio::test]
    async fn test_get_program_type_mismatch_errors() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image =
            compiler.compile(&mut b"x = 1: DIM arr(2) AS INTEGER: arr(1) = 45".as_slice()).unwrap();
        let mut vm = Vm::new(HashMap::default());
        run_to_end(&mut vm, &image).await;

        match vm.get_program(&image, &SymbolKey::from("arr")) {
            Err(GetGlobalError::IsArray(name)) => assert_eq!("ARR", name),
            other => panic!("Unexpected result: {:?}", other),
        }

        match vm.get_program_array(&image, &SymbolKey::from("x"), &[0]) {
            Err(GetGlobalError::IsScalar(name)) => assert_eq!("X", name),
            other => panic!("Unexpected result: {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_get_program_array_out_of_bounds() {
        let compiler = Compiler::new(&HashMap::default(), &[]).unwrap();
        let image =
            compiler.compile(&mut b"DIM arr(2) AS INTEGER: arr(1) = 45".as_slice()).unwrap();
        let mut vm = Vm::new(HashMap::default());
        run_to_end(&mut vm, &image).await;

        match vm.get_program_array(&image, &SymbolKey::from("arr"), &[3]) {
            Err(GetGlobalError::SubscriptOutOfBounds(_)) => (),
            other => panic!("Unexpected result: {:?}", other),
        }
    }
}