miruscriptx 1.6.0

MiruScriptX: a complete, general-purpose scripting language written from scratch in Rust.
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
//! MiruScriptX: a small, general-purpose scripting language written in Rust.
//!
//! This crate exposes the language as a library. The `miru` binary in
//! `src/main.rs` wraps it with a command line interface and a REPL.
//!
//! Source text is turned into tokens by the [`lexer`], parsed into an abstract
//! syntax tree by the [`parser`], compiled to bytecode by the [`compiler`], and
//! executed by the [`vm`].
//!
//! Learn the language in the `wiki/` folder and look things up in
//! `docs/language-reference.md`.

use std::cell::RefCell;
use std::fmt;
use std::io::Write;
use std::rc::Rc;

use crate::value::Input;

pub mod ast;
pub mod builtins;
pub mod chunk;
pub mod compiler;
pub mod formatter;
pub mod globals;
pub mod lexer;
pub mod ops;
pub mod parser;
pub mod random;
pub mod suggest;
pub mod token;
pub mod value;
pub mod vm;

/// The MiruScriptX version, taken from `Cargo.toml`.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Lex and parse a source string into a program (a list of statements).
pub fn parse_program(source: &str) -> Result<Vec<ast::Stmt>, MiruError> {
    let tokens = lexer::Lexer::tokenize(source)?;
    parser::Parser::parse(tokens)
}

/// Run a program and return the value of its final expression (what the REPL
/// echoes), discarding anything it printed.
pub fn eval_source(source: &str) -> Result<value::Value, MiruError> {
    let program = parse_program(source)?;
    vm::Vm::with_output(Box::new(std::io::sink())).run(&program)
}

/// Compile a source string and return its bytecode as readable assembly: the
/// top-level script followed by every function nested inside it. This is what
/// the `disasm` command prints, and it is the only way to see what the compiler
/// actually produced.
pub fn disassemble_source(source: &str) -> Result<String, MiruError> {
    let program = parse_program(source)?;
    // Compiling needs a global table to resolve names against. Nothing runs, so
    // a throwaway one seeded with the builtins gives the same slots a real run
    // would.
    let mut globals = globals::Globals::new();
    builtins::register(&mut globals);
    let script = compiler::Compiler::compile(&program, &mut globals)?;
    Ok(chunk::disassemble_program(&script))
}

/// Lex, parse, and reprint a source string in the canonical `miru fmt` style,
/// preserving comments and single blank lines. This is what the `fmt` command
/// runs on a file.
pub fn format_source(source: &str) -> Result<String, MiruError> {
    let (tokens, trivia) = lexer::Lexer::tokenize_with_trivia(source)?;
    let program = parser::Parser::parse(tokens)?;
    Ok(formatter::format_program(&program, &trivia))
}

/// Lex, parse, compile, and run a source string, sending `print` output to
/// `out` and reading `input()` from standard input.
///
/// Gives the code the program stopped with: `0` unless it called `exit`.
/// Diagnostics go to standard error unless the caller redirects them with
/// [`vm::Vm::set_error_output`].
pub fn run_source(source: &str, out: Box<dyn Write>, host: Host) -> Result<i32, MiruError> {
    run_source_from(source, None, out, host)
}

/// Everything a program is allowed to reach outside itself.
///
/// One struct rather than one parameter each, because there are three of them
/// now and a call site holding three positional boxes says nothing about which
/// is which. Each field is separate rather than one "allow everything" flag,
/// because a host really can have one and not the others: the browser
/// playground has a clock, no file system, and no keyboard.
pub struct Host {
    pub system: Box<dyn value::System>,
    pub clock: Box<dyn value::Clock>,
    pub keyboard: Box<dyn value::Keyboard>,
}

impl Host {
    /// A host that grants nothing. Every capability refuses.
    pub fn none() -> Host {
        Host {
            system: Box::new(value::NoSystem),
            clock: Box::new(value::NoClock),
            keyboard: Box::new(value::NoKeyboard),
        }
    }

    fn give_to(self, vm: &mut vm::Vm) {
        vm.set_system(self.system);
        vm.set_clock(self.clock);
        vm.set_keyboard(self.keyboard);
    }
}

impl Default for Host {
    fn default() -> Host {
        Host::none()
    }
}

/// Like [`run_source`], but for a program that came from `path`, which is what
/// an `import` inside it resolves against.
///
/// The path-free spelling above delegates here with `None`. Keeping one
/// implementation and a one-line wrapper, rather than two entry points, is why
/// the forty-odd existing callers did not have to change: a wrapper that is
/// literally `f(source, None, out)` cannot drift from what it wraps.
///
/// `host` is what the program is allowed to reach outside itself, and it is a
/// parameter rather than a default so that granting any of it is a decision
/// somebody wrote down. [`Host::none`] grants nothing.
pub fn run_source_from(
    source: &str,
    path: Option<&std::path::Path>,
    out: Box<dyn Write>,
    host: Host,
) -> Result<i32, MiruError> {
    let program = parse_program(source)?;
    let mut vm = vm::Vm::with_output(out);
    vm.set_input(Box::new(StdinInput));
    host.give_to(&mut vm);
    let result = vm.run_from(&program, path);
    // Flushed on both arms, not only on success. A program that failed still
    // printed whatever it printed before it failed, and this used to drop that
    // because the run was followed by `?`. An exit makes it matter more, since
    // an exit leaves the loop as an error and would otherwise lose the output
    // it was reporting.
    vm.flush();
    match (result, vm.exit_code()) {
        // A program that asked to stop has not failed, whatever error carried
        // it out of the dispatch loop. The code is read before the error, or a
        // plain `exit(0)` would be reported as a failure.
        (_, Some(code)) => Ok(code),
        (Ok(_), None) => Ok(0),
        (Err(error), None) => Err(error),
    }
}

/// An interactive session: a virtual machine whose globals persist from one
/// program to the next, so a variable or function defined by one input is still
/// there for the next. The REPL is built on this.
///
/// A failed input reports its error and leaves the session usable, so a typo or
/// a runtime error does not end the session or corrupt what came before.
pub struct Session {
    vm: vm::Vm,
}

impl Default for Session {
    fn default() -> Self {
        Session::new()
    }
}

impl Session {
    /// A session that prints to standard output and reads standard input.
    pub fn new() -> Session {
        let mut session = Session::with_output(Box::new(std::io::stdout()));
        session.vm.set_input(Box::new(StdinInput));
        session
    }

    /// A session that prints to a custom sink, as tests do.
    pub fn with_output(out: Box<dyn Write>) -> Session {
        Session {
            vm: vm::Vm::with_output(out),
        }
    }

    /// Replace the input source that `input()` reads from.
    pub fn set_input(&mut self, input: Box<dyn Input>) {
        self.vm.set_input(input);
    }

    /// Give the session a clock, which `now()` reads.
    ///
    /// A setter rather than an argument to [`Session::new`], because the REPL
    /// is the only caller that has a real one and every other caller wants the
    /// default, which is no clock at all.
    pub fn set_clock(&mut self, clock: Box<dyn value::Clock>) {
        self.vm.set_clock(clock);
    }

    /// Parse, compile, and run one input against the session's accumulated
    /// state, returning the value of its final expression (or `nil`).
    pub fn eval(&mut self, source: &str) -> Result<value::Value, MiruError> {
        let program = parse_program(source)?;
        let value = self.vm.run(&program)?;
        self.vm.flush();
        Ok(value)
    }

    /// Flush anything the session has buffered.
    pub fn flush(&mut self) {
        self.vm.flush();
    }
}

/// Everything a captured run produced.
///
/// Separate fields rather than one string, because the whole reason a program
/// has two streams is that they can be told apart. A capture that merged them
/// would make a test of `eprint` pass whether or not the builtin worked.
pub struct Capture {
    /// What `print` wrote.
    pub out: String,
    /// What `eprint` wrote.
    pub err: String,
    /// What the program stopped with: `0` unless it called `exit`.
    pub code: i32,
}

/// Run a source string and capture everything it printed. Handy for tests and
/// tooling that needs the output as a string rather than on a stream.
///
/// Gives standard output only. Use [`run_capture_all`] for the diagnostics and
/// the exit code as well. This spelling is kept because most callers want the
/// one string, and changing it would have churned every one of them for the
/// benefit of two.
pub fn run_capture(source: &str) -> Result<String, MiruError> {
    run_capture_with_input(source, &[])
}

/// Like [`run_capture`], but feeds the given lines to `input()` in order.
pub fn run_capture_with_input(source: &str, input: &[&str]) -> Result<String, MiruError> {
    run_capture_all_with_input(source, input).map(|captured| captured.out)
}

/// Run a source string and capture both streams and the exit code.
pub fn run_capture_all(source: &str) -> Result<Capture, MiruError> {
    run_capture_all_with_input(source, &[])
}

/// Like [`run_capture_all`], but feeds the given lines to `input()` in order.
pub fn run_capture_all_with_input(source: &str, input: &[&str]) -> Result<Capture, MiruError> {
    run_capture_all_with(source, input, Host::none())
}

/// Like [`run_capture_all_with_input`], and with a host behind it.
///
/// The spellings above keep granting nothing, which is what every golden case
/// wants: the ambient builtins are the ones whose results the source does not
/// determine, so a capture with a real clock or keyboard behind it could not
/// assert an answer. This entry point exists for the callers that do supply
/// one, which are the browser playground and the tests of the capabilities
/// themselves.
pub fn run_capture_all_with(
    source: &str,
    input: &[&str],
    host: Host,
) -> Result<Capture, MiruError> {
    let program = parse_program(source)?;
    let out = Rc::new(RefCell::new(Vec::<u8>::new()));
    let err = Rc::new(RefCell::new(Vec::<u8>::new()));
    let mut vm = vm::Vm::with_output(Box::new(SharedBuffer(Rc::clone(&out))));
    vm.set_error_output(Box::new(SharedBuffer(Rc::clone(&err))));
    vm.set_input(Box::new(ScriptedInput::new(input)));
    host.give_to(&mut vm);
    let result = vm.run(&program);
    // Flushed before either arm returns, and the code read before the error is
    // reported, for the same reasons as `run_source_from`.
    vm.flush();
    let captured = |code: i32| Capture {
        out: String::from_utf8_lossy(out.borrow().as_slice()).into_owned(),
        err: String::from_utf8_lossy(err.borrow().as_slice()).into_owned(),
        code,
    };
    match (result, vm.exit_code()) {
        (_, Some(code)) => Ok(captured(code)),
        (Ok(_), None) => Ok(captured(0)),
        (Err(error), None) => Err(error),
    }
}

/// A `Write` sink backed by a shared byte buffer, used by [`run_capture`].
struct SharedBuffer(Rc<RefCell<Vec<u8>>>);

impl Write for SharedBuffer {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.borrow_mut().extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// Reads lines from the process's standard input, one per `input()` call.
struct StdinInput;

impl Input for StdinInput {
    fn read_line(&mut self) -> Option<String> {
        let mut line = String::new();
        match std::io::stdin().read_line(&mut line) {
            Ok(0) => None,
            Ok(_) => Some(strip_trailing_newline(line)),
            Err(_) => None,
        }
    }
}

/// An [`Input`] backed by a fixed list of lines, used by the capture helpers.
struct ScriptedInput {
    lines: std::vec::IntoIter<String>,
}

impl ScriptedInput {
    fn new(lines: &[&str]) -> ScriptedInput {
        let lines: Vec<String> = lines.iter().map(|line| line.to_string()).collect();
        ScriptedInput {
            lines: lines.into_iter(),
        }
    }
}

impl Input for ScriptedInput {
    fn read_line(&mut self) -> Option<String> {
        self.lines.next()
    }
}

/// A [`value::Keyboard`] backed by a fixed list of key names.
///
/// The only way to test `read_key` without a terminal. Raw mode itself has no
/// automated test on any platform, so what this covers is everything above the
/// syscalls: that the builtin gives back what the host handed it, that it gives
/// `nil` when the keys run out, and that a program can loop on it.
pub struct ScriptedKeyboard {
    keys: std::vec::IntoIter<String>,
}

impl ScriptedKeyboard {
    pub fn new(keys: &[&str]) -> ScriptedKeyboard {
        let keys: Vec<String> = keys.iter().map(|key| key.to_string()).collect();
        ScriptedKeyboard {
            keys: keys.into_iter(),
        }
    }
}

impl value::Keyboard for ScriptedKeyboard {
    fn read_key(&mut self) -> Result<Option<String>, String> {
        Ok(self.keys.next())
    }
}

/// Remove a single trailing newline (and any preceding carriage return).
fn strip_trailing_newline(mut line: String) -> String {
    if line.ends_with('\n') {
        line.pop();
        if line.ends_with('\r') {
            line.pop();
        }
    }
    line
}

/// One function on the call path an error came through.
///
/// `line` is where the *call* was written, not where that function will resume,
/// so a trace reads as the sequence of call sites a reader can look up in their
/// own source.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraceEntry {
    /// The name of the function that was entered, or `None` for an anonymous
    /// one.
    pub function: Option<String>,
    pub line: usize,
}

/// How many innermost and outermost entries a rendered trace keeps when the
/// path is too long to print whole.
///
/// Runaway recursion reaches ten thousand frames, and printing every one buries
/// the error in its own trace. The two ends are the parts that carry
/// information: where it broke, and how the program got into the recursion.
const TRACE_HEAD: usize = 5;
const TRACE_TAIL: usize = 5;

/// Append one trace line.
fn write_trace_entry(out: &mut String, entry: &TraceEntry) {
    use std::fmt::Write;

    let name = entry.function.as_deref().unwrap_or("<anonymous>");
    let _ = write!(out, "\n  in {name}, called from line {}", entry.line);
}

/// An error produced anywhere in the pipeline (lexing, parsing, or running),
/// tagged with the 1-based source line and column where it occurred (0 when
/// unknown).
///
/// A runtime error raised inside a call also carries the path of calls it came
/// through, innermost first. Lexing and parsing errors leave `trace` empty:
/// there is no call stack yet when they happen.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MiruError {
    pub line: usize,
    pub column: usize,
    pub message: String,
    pub trace: Vec<TraceEntry>,
    /// Whether `try` is allowed to turn this into a value.
    ///
    /// Almost everything is catchable, because almost everything is a condition
    /// a program could reasonably handle. The call depth limit is not: it means
    /// recursion that does not terminate, which is a bug in the program rather
    /// than a situation to recover from, and letting a `try` swallow it would
    /// hide the one thing worth knowing.
    pub fatal: bool,
    /// The file this error is about, when it is not the one being rendered.
    ///
    /// An error raised inside an imported module has a line and column in *that*
    /// file, and whoever renders it holds the importing file's source. Naming
    /// the file is what stops a caret being drawn on an unrelated line.
    pub file: Option<String>,
}

impl MiruError {
    /// Create an error at a line, with an unknown column (`0`).
    pub fn new(line: usize, message: impl Into<String>) -> MiruError {
        MiruError::with_column(line, 0, message)
    }

    /// Create an error at a specific line and column. A `column` of `0` means
    /// the column is unknown.
    ///
    /// The trace starts empty. The VM fills it in as the error leaves the frame
    /// it was raised in, which is the only place the frames still exist.
    pub fn with_column(line: usize, column: usize, message: impl Into<String>) -> MiruError {
        MiruError {
            line,
            column,
            message: message.into(),
            trace: Vec::new(),
            fatal: false,
            file: None,
        }
    }

    /// Mark this error as one `try` may not catch. See [`MiruError::fatal`].
    pub fn as_fatal(mut self) -> MiruError {
        self.fatal = true;
        self
    }

    /// How wide to underline at this error's position: the length of the token
    /// that starts there, or 1 when there is no such token.
    ///
    /// The spans come from the lexer, which records them for the playground's
    /// syntax highlighting, and are index-parallel with the tokens it returns.
    /// Every token carries its own line and column, so the one at this error's
    /// position is found by matching on those rather than by converting the
    /// position into an offset and comparing that.
    ///
    /// Re-lexing here costs nothing worth counting. It happens once, on the
    /// error path, for a program that has already failed.
    ///
    /// Both fallbacks are real cases rather than defensive padding. A source
    /// that does not lex is reporting a lexer error, whose position points at a
    /// character rather than a token, so there is nothing to measure. And `Eof`
    /// covers no text at all, so every "found end of input" error would
    /// otherwise underline zero characters.
    fn underline_width(&self, source: &str) -> usize {
        let Ok((tokens, spans)) = crate::lexer::Lexer::tokenize_with_spans(source) else {
            return 1;
        };
        tokens
            .iter()
            .zip(&spans.tokens)
            .find(|(token, _)| token.line == self.line && token.column == self.column)
            .map_or(1, |(_, (_, len))| (*len).max(1))
    }

    /// Render the error with the offending source line, an underline under the
    /// token it blames, and the path of calls it came through.
    ///
    /// The source line and underline are omitted when the line or column is
    /// unknown or out of range, and the trace when there is none, so an error
    /// with neither renders as the one-line
    /// [`Display`](std::fmt::Display) form.
    pub fn render(&self, source: &str) -> String {
        use std::fmt::Write;

        let mut out = self.to_string();
        if let Some(text) = self
            .line
            .checked_sub(1)
            .filter(|_| self.column > 0)
            // An error from another file has a position in that file, and this
            // is not that file's text. Drawing a line from it would point at
            // something unrelated with total confidence.
            .filter(|_| self.file.is_none())
            .and_then(|index| source.lines().nth(index))
        {
            // Build the indent from the source itself so that tabs before the
            // column keep the underline aligned.
            let mut caret = String::new();
            for ch in text.chars().take(self.column - 1) {
                caret.push(if ch == '\t' { '\t' } else { ' ' });
            }
            // Underline the whole token rather than pointing at its first
            // character. Capped by what is left of the line, so a token that
            // somehow ran past the end could not carry the underline with it,
            // and never below one, since an error at end of input sits one
            // column past the last character and still has to mark something.
            let remaining = text.chars().count().saturating_sub(self.column - 1);
            for _ in 0..self.underline_width(source).min(remaining.max(1)) {
                caret.push('^');
            }
            let _ = write!(out, "\n    {text}\n    {caret}");
        }
        // Elide the middle of a long path rather than the data itself: `trace`
        // stays complete for anything that wants to inspect it, and only what
        // is printed is shortened. The threshold is one past what elision could
        // fit, so it never replaces a single frame with a line saying one frame
        // was replaced, which also keeps the count plural.
        if self.trace.len() > TRACE_HEAD + TRACE_TAIL + 1 {
            for entry in &self.trace[..TRACE_HEAD] {
                write_trace_entry(&mut out, entry);
            }
            let elided = self.trace.len() - TRACE_HEAD - TRACE_TAIL;
            let _ = write!(out, "\n  ... {elided} more frames");
            for entry in &self.trace[self.trace.len() - TRACE_TAIL..] {
                write_trace_entry(&mut out, entry);
            }
        } else {
            for entry in &self.trace {
                write_trace_entry(&mut out, entry);
            }
        }
        out
    }
}

impl fmt::Display for MiruError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let where_ = match (&self.file, self.line, self.column) {
            (None, 0, _) => String::new(),
            (None, line, 0) => format!(" (line {line})"),
            (None, line, column) => format!(" (line {line}, column {column})"),
            (Some(file), 0, _) => format!(" ({file})"),
            (Some(file), line, 0) => format!(" ({file}, line {line})"),
            (Some(file), line, column) => format!(" ({file}, line {line}, column {column})"),
        };
        write!(f, "error{where_}: {}", self.message)
    }
}

impl std::error::Error for MiruError {}

#[cfg(test)]
mod tests {
    use super::*;

    /// A clock stopped at one instant, which is what makes a time assertable.
    ///
    /// The seam exists so the library never reads the machine's clock. That it
    /// also makes `now` testable to the millisecond is the second thing it
    /// buys, and the reason the tests of the clock are here rather than in the
    /// integration tests, which can only check a bound.
    struct FixedClock(i64);

    impl value::Clock for FixedClock {
        fn now_millis(&mut self) -> Result<i64, String> {
            Ok(self.0)
        }
    }

    #[test]
    fn now_gives_back_exactly_what_the_host_clock_said() {
        let captured = run_capture_all_with(
            "print(now())",
            &[],
            Host {
                clock: Box::new(FixedClock(1_234_567)),
                ..Host::none()
            },
        )
        .expect("the program runs");
        assert_eq!(captured.out, "1234567\n");
    }

    /// A clock is read at each call rather than once per run, so a program that
    /// measures a duration sees two different times. Reading it once and
    /// caching would make every interval zero.
    #[test]
    fn each_call_reads_the_clock_again() {
        struct Ticking(i64);

        impl value::Clock for Ticking {
            fn now_millis(&mut self) -> Result<i64, String> {
                self.0 += 5;
                Ok(self.0)
            }
        }

        let captured = run_capture_all_with(
            "let a = now()\nlet b = now()\nprint(b - a)",
            &[],
            Host {
                clock: Box::new(Ticking(0)),
                ..Host::none()
            },
        )
        .expect("the program runs");
        assert_eq!(captured.out, "5\n");
    }

    #[test]
    fn read_key_gives_back_what_the_host_handed_it() {
        let captured = run_capture_all_with(
            "print(read_key())\nprint(read_key())\nprint(read_key())",
            &[],
            Host {
                keyboard: Box::new(ScriptedKeyboard::new(&["up", "a"])),
                ..Host::none()
            },
        )
        .expect("the program runs");
        // The third call is past the end, which is `nil` rather than a refusal:
        // the host had a keyboard and there is nothing more to read from it.
        assert_eq!(captured.out, "up\na\nnil\n");
    }

    /// A loop over `read_key` ends at the end of input rather than spinning,
    /// which is the shape every program using it will have.
    #[test]
    fn a_loop_over_read_key_ends() {
        let captured = run_capture_all_with(
            "let seen = 0\nwhile true {\n  let k = read_key()\n  if k == nil { break }\n  \
             if k == \"ctrl+c\" { break }\n  seen = seen + 1\n}\nprint(seen)",
            &[],
            Host {
                keyboard: Box::new(ScriptedKeyboard::new(&["a", "b", "ctrl+c", "d"])),
                ..Host::none()
            },
        )
        .expect("the program runs");
        // Two, not three: `ctrl+c` breaks before the counter moves, and the
        // `d` after it is never read.
        assert_eq!(captured.out, "2\n");
    }

    /// The captures that everything else uses grant no clock, which is what
    /// keeps the golden cases deterministic. Stated as a test because it is a
    /// property of the default rather than of any one case.
    #[test]
    fn the_ordinary_capture_grants_no_clock() {
        let Err(error) = run_capture_all("now()") else {
            panic!("the default capture granted a clock");
        };
        assert!(
            error.message.contains("there is no clock"),
            "the message was {:?}",
            error.message
        );
    }

    /// Build an error carrying `count` identical trace entries.
    fn traced(count: usize) -> MiruError {
        let mut error = MiruError::with_column(1, 1, "boom");
        error.trace = (0..count)
            .map(|_| TraceEntry {
                function: Some("r".to_string()),
                line: 1,
            })
            .collect();
        error
    }

    #[test]
    fn a_long_trace_is_elided_in_the_middle() {
        // The threshold is the last length elision cannot shorten. At it, every
        // frame prints; one past it, the middle collapses.
        let full = traced(TRACE_HEAD + TRACE_TAIL + 1).render("x");
        assert_eq!(full.matches("in r, called from line 1").count(), 11);
        assert!(!full.contains("more frames"), "{full}");

        let elided = traced(TRACE_HEAD + TRACE_TAIL + 2).render("x");
        assert_eq!(
            elided.matches("in r, called from line 1").count(),
            TRACE_HEAD + TRACE_TAIL
        );
        // Never "1 more frames": the threshold is set so at least two collapse.
        assert!(elided.contains("... 2 more frames"), "{elided}");

        let deep = traced(10_000).render("x");
        assert!(deep.contains("... 9990 more frames"), "{deep}");
        assert_eq!(deep.lines().count(), 1 + 2 + TRACE_HEAD + 1 + TRACE_TAIL);
    }

    fn out(source: &str) -> String {
        run_capture(source).expect("program should run")
    }

    #[test]
    fn print_writes_a_line() {
        assert_eq!(out("print(1 + 2)"), "3\n");
    }

    #[test]
    fn render_points_a_caret_at_the_column() {
        let err = MiruError::with_column(1, 5, "boom");
        assert_eq!(
            err.render("abcdefg"),
            "error (line 1, column 5): boom\n    abcdefg\n        ^"
        );
    }

    #[test]
    fn render_preserves_leading_tabs_in_the_caret() {
        let err = MiruError::with_column(1, 2, "boom");
        assert_eq!(
            err.render("\tx"),
            "error (line 1, column 2): boom\n    \tx\n    \t^"
        );
    }

    #[test]
    fn render_underlines_a_whole_token() {
        // The point of the change: a name is marked over its whole length
        // rather than at its first character.
        let err = MiruError::with_column(1, 1, "boom");
        assert_eq!(
            err.render("total = 1"),
            "error (line 1, column 1): boom\n    total = 1\n    ^^^^^"
        );
    }

    #[test]
    fn render_underlines_one_character_when_the_token_is_one_character() {
        // The common case stays exactly as it was, which is why so few existing
        // expectations moved: most errors point at an operator.
        let err = MiruError::with_column(1, 3, "boom");
        assert_eq!(
            err.render("1 + 2"),
            "error (line 1, column 3): boom\n    1 + 2\n      ^"
        );
    }

    #[test]
    fn render_falls_back_to_one_caret_at_end_of_input() {
        // Eof covers no text, so its recorded span is genuinely zero long. Every
        // "found end of input" error would underline nothing without the floor.
        let err = MiruError::with_column(1, 8, "boom");
        assert_eq!(
            err.render("let x ="),
            "error (line 1, column 8): boom\n    let x =\n           ^"
        );
    }

    #[test]
    fn render_falls_back_to_one_caret_when_the_source_does_not_lex() {
        // A lexer error points at a character rather than a token, so there is
        // no token to measure and one caret is the right answer.
        let err = MiruError::with_column(1, 1, "unexpected character '@'");
        assert_eq!(
            err.render("@"),
            "error (line 1, column 1): unexpected character '@'\n    @\n    ^"
        );
    }

    #[test]
    fn render_falls_back_to_one_caret_inside_a_token() {
        // Column 3 is the middle of `total`, not the start of anything, so
        // nothing matches and the underline stays a single caret.
        let err = MiruError::with_column(1, 3, "boom");
        assert_eq!(
            err.render("total"),
            "error (line 1, column 3): boom\n    total\n      ^"
        );
    }

    #[test]
    fn render_measures_a_token_in_characters_rather_than_bytes() {
        // Three multi-byte characters in quotes are five characters and eleven
        // bytes. Measuring in bytes would run the underline off the line.
        let err = MiruError::with_column(1, 1, "boom");
        assert_eq!(
            err.render("\"\u{4e2d}\u{4e2d}\u{4e2d}\" + 1"),
            "error (line 1, column 1): boom\n    \"\u{4e2d}\u{4e2d}\u{4e2d}\" + 1\n    ^^^^^"
        );
    }

    #[test]
    fn an_error_from_another_file_names_it_and_draws_no_caret() {
        // The position belongs to the named file, and `render` is holding the
        // importing file's text. Drawing a line from it would point at
        // something unrelated with total confidence, so it does not.
        let mut err = MiruError::with_column(3, 5, "undefined variable 'x'");
        err.file = Some("./math.miru".to_string());
        assert_eq!(
            err.render("let a = 1\nlet b = 2\nlet c = 3\n"),
            "error (./math.miru, line 3, column 5): undefined variable 'x'"
        );
    }

    #[test]
    fn a_file_is_named_whatever_position_is_known() {
        let mut err = MiruError::new(7, "boom");
        err.file = Some("./m.miru".to_string());
        assert_eq!(err.to_string(), "error (./m.miru, line 7): boom");

        let mut bare = MiruError::with_column(0, 0, "boom");
        bare.file = Some("./m.miru".to_string());
        assert_eq!(bare.to_string(), "error (./m.miru): boom");
    }

    #[test]
    fn render_without_a_column_is_one_line() {
        let err = MiruError::new(3, "oops");
        assert_eq!(err.render("a\nb\nc"), "error (line 3): oops");
    }

    #[test]
    fn print_joins_arguments_with_spaces() {
        assert_eq!(out("print(1, \"two\", true)"), "1 two true\n");
    }

    #[test]
    fn len_of_strings_and_arrays() {
        assert_eq!(
            out("print(len(\"hello\"))\nprint(len([1, 2, 3]))"),
            "5\n3\n"
        );
    }

    #[test]
    fn type_names() {
        assert_eq!(
            out("print(type(1), type(1.5), type(\"a\"), type([]), type(nil))"),
            "int float string array nil\n"
        );
    }

    #[test]
    fn range_builds_a_half_open_interval() {
        assert_eq!(out("print(range(3))"), "[0, 1, 2]\n");
        assert_eq!(out("print(range(2, 5))"), "[2, 3, 4]\n");
    }

    #[test]
    fn push_appends_in_place() {
        assert_eq!(out("let a = [1]\npush(a, 2)\nprint(a)"), "[1, 2]\n");
    }

    #[test]
    fn str_converts_and_concatenates() {
        assert_eq!(out("print(str(42) + \"!\")"), "42!\n");
    }

    #[test]
    fn map_builtins() {
        let source = "let m = {\"b\": 2, \"a\": 1}\nprint(keys(m))\nprint(values(m))\nprint(has(m, \"a\"))\nprint(has(m, \"z\"))\nprint(len(m))";
        assert_eq!(out(source), "[\"a\", \"b\"]\n[1, 2]\ntrue\nfalse\n2\n");
    }

    const EXAMPLES: [&str; 6] = [
        include_str!("../examples/greet.miru"),
        include_str!("../examples/fib.miru"),
        include_str!("../examples/fizzbuzz.miru"),
        include_str!("../examples/contacts.miru"),
        include_str!("../examples/greeter.miru"),
        include_str!("../examples/transform.miru"),
    ];

    #[test]
    fn format_source_is_idempotent_on_examples() {
        for source in EXAMPLES {
            let once = format_source(source).expect("formats");
            let twice = format_source(&once).expect("reformats");
            assert_eq!(once, twice, "formatting is not idempotent");
        }
    }

    #[test]
    fn format_source_does_not_change_behavior() {
        // Reprinting must never change what a program does. Every example runs
        // deterministically (greeter reads from an empty input and says goodbye).
        for source in EXAMPLES {
            let formatted = format_source(source).expect("formats");
            assert_eq!(
                run_capture(source).expect("source runs"),
                run_capture(&formatted).expect("formatted runs"),
                "formatting changed program behavior"
            );
        }
    }

    #[test]
    fn format_source_preserves_comments() {
        let source = include_str!("../examples/contacts.miru");
        let formatted = format_source(source).expect("formats");
        assert!(formatted.contains("// Build a small phone book"));
        assert!(formatted.contains("// Add a new entry and update an existing one."));
        assert!(formatted.contains("// Look up a name, guarding against a missing one."));
    }
}