miruscriptx 2.1.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
//! The arithmetic, comparison, and indexing rules at the heart of the language.
//!
//! Keeping them here rather than inline in the virtual machine means numeric
//! promotion, overflow checks, and index bounds are each defined exactly once.
//! Every function returns the bare error message on failure; the VM attaches
//! the source position from the chunk's position table.

use std::cmp::Ordering;
use std::rc::Rc;

use crate::ast::{BinaryOp, UnaryOp};
use crate::value::Value;

/// A pair of numbers promoted to a common representation for arithmetic.
enum Num {
    Ints(i64, i64),
    Floats(f64, f64),
}

/// The message for using a caught error as if it were an ordinary value.
///
/// Reported where the misuse is written rather than where the error happened,
/// because the misuse is the mistake: the program already had the error in
/// hand and did something else with it. The original position stays on the
/// value, reachable through its fields, so nothing is lost by pointing here.
pub fn unhandled(value: &Value) -> Option<String> {
    match value {
        Value::Error(error) => Some(format!("unhandled error: {}", error.message)),
        _ => None,
    }
}

/// Apply a unary operator to a value.
pub fn unary(op: UnaryOp, value: Value) -> Result<Value, String> {
    // Checked here rather than in each rule below, which matters most for
    // `Not`: it answers for every value through `is_truthy` and so would
    // otherwise quietly report an error as `false`.
    if let Some(message) = unhandled(&value) {
        return Err(message);
    }
    match op {
        UnaryOp::Negate => match value {
            Value::Int(n) => n
                .checked_neg()
                .map(Value::Int)
                .ok_or_else(|| "integer overflow while negating".to_string()),
            Value::Float(f) => Ok(Value::Float(-f)),
            other => Err(format!("cannot negate a {}", other.type_name())),
        },
        UnaryOp::Not => Ok(Value::Bool(!value.is_truthy())),
    }
}

/// Apply a binary operator to two values.
pub fn binary(op: BinaryOp, left: Value, right: Value) -> Result<Value, String> {
    // Once, here, rather than in each rule. `Equal` and `NotEqual` are the ones
    // that need it: they answer for every pair through `Value::equals`, so
    // without this an error would compare `false` against everything and look
    // like an ordinary value that simply did not match.
    //
    // The VM's integer fast path has already declined by the time this runs, so
    // no arithmetic between two integers reaches it.
    if let Some(message) = unhandled(&left).or_else(|| unhandled(&right)) {
        return Err(message);
    }
    match op {
        BinaryOp::Add => add(left, right),
        BinaryOp::Subtract => match numeric_pair(&left, &right, "subtract")? {
            Num::Ints(a, b) => a
                .checked_sub(b)
                .map(Value::Int)
                .ok_or_else(|| "integer overflow in subtraction".to_string()),
            Num::Floats(a, b) => Ok(Value::Float(a - b)),
        },
        BinaryOp::Multiply => match numeric_pair(&left, &right, "multiply")? {
            Num::Ints(a, b) => a
                .checked_mul(b)
                .map(Value::Int)
                .ok_or_else(|| "integer overflow in multiplication".to_string()),
            Num::Floats(a, b) => Ok(Value::Float(a * b)),
        },
        BinaryOp::Divide => match numeric_pair(&left, &right, "divide")? {
            Num::Ints(_, 0) => Err("division by zero".to_string()),
            Num::Ints(a, b) => a
                .checked_div(b)
                .map(Value::Int)
                .ok_or_else(|| "integer overflow in division".to_string()),
            Num::Floats(a, b) => {
                if b == 0.0 {
                    Err("division by zero".to_string())
                } else {
                    Ok(Value::Float(a / b))
                }
            }
        },
        BinaryOp::Modulo => match numeric_pair(&left, &right, "take the modulo of")? {
            Num::Ints(_, 0) => Err("modulo by zero".to_string()),
            Num::Ints(a, b) => a
                .checked_rem(b)
                .map(Value::Int)
                .ok_or_else(|| "integer overflow in modulo".to_string()),
            Num::Floats(a, b) => {
                if b == 0.0 {
                    Err("modulo by zero".to_string())
                } else {
                    Ok(Value::Float(a % b))
                }
            }
        },
        BinaryOp::Equal => Ok(Value::Bool(left.equals(&right)?)),
        BinaryOp::NotEqual => Ok(Value::Bool(!left.equals(&right)?)),
        BinaryOp::Less => Ok(Value::Bool(ordering(left, right)? == Ordering::Less)),
        BinaryOp::Greater => Ok(Value::Bool(ordering(left, right)? == Ordering::Greater)),
        BinaryOp::LessEqual => Ok(Value::Bool(ordering(left, right)? != Ordering::Greater)),
        BinaryOp::GreaterEqual => Ok(Value::Bool(ordering(left, right)? != Ordering::Less)),
    }
}

/// Addition doubles as concatenation when both operands are strings, or both
/// are arrays.
fn add(left: Value, right: Value) -> Result<Value, String> {
    if let (Value::Str(a), Value::Str(b)) = (&left, &right) {
        let mut joined = String::with_capacity(a.len() + b.len());
        joined.push_str(a);
        joined.push_str(b);
        return Ok(Value::Str(Rc::new(joined)));
    }
    if let (Value::Array(a), Value::Array(b)) = (&left, &right) {
        // A new array, never either operand extended in place. `push` is the
        // spelling for growing an array you already have; this one is an
        // expression, and an expression that quietly modified one of its
        // operands would make `let joined = front + back` change `front`.
        //
        // Borrowed separately, and `a + a` is why. One `borrow_mut` and one
        // `borrow` of the same array would panic at runtime, so both are shared
        // borrows and the result is built from scratch.
        let (a, b) = (a.borrow(), b.borrow());
        let mut joined = Vec::with_capacity(a.len() + b.len());
        joined.extend(a.iter().cloned());
        joined.extend(b.iter().cloned());
        return Ok(Value::array(joined));
    }
    match numeric_pair(&left, &right, "add")? {
        Num::Ints(a, b) => a
            .checked_add(b)
            .map(Value::Int)
            .ok_or_else(|| "integer overflow in addition".to_string()),
        Num::Floats(a, b) => Ok(Value::Float(a + b)),
    }
}

/// Promote two operands to a common numeric type, or fail with a message naming
/// the attempted operation.
fn numeric_pair(left: &Value, right: &Value, verb: &str) -> Result<Num, String> {
    match (left, right) {
        (Value::Int(a), Value::Int(b)) => Ok(Num::Ints(*a, *b)),
        (Value::Int(a), Value::Float(b)) => Ok(Num::Floats(*a as f64, *b)),
        (Value::Float(a), Value::Int(b)) => Ok(Num::Floats(*a, *b as f64)),
        (Value::Float(a), Value::Float(b)) => Ok(Num::Floats(*a, *b)),
        _ => Err(format!(
            "cannot {} a {} and a {}",
            verb,
            left.type_name(),
            right.type_name()
        )),
    }
}

/// Turn an index that may count from the end into a position in `0..len`.
///
/// A negative index counts back from the end: `-1` is the last element and
/// `-len` is the first. `None` means it lands outside the sequence either way.
///
/// **This is where every negative index in the language is decided**, so that
/// `a[-1]`, `s[-1]`, and `insert(a, -1, v)` cannot come to disagree about what
/// `-1` means. `slice` is the one caller that does not use it, because `slice`
/// clamps rather than refusing and has to keep doing so.
///
/// The addition is checked because `n` can be `i64::MIN`, and the comparison
/// goes through `i64` rather than casting to `usize` because a cast would turn
/// a negative that fell short into an enormous positive.
pub fn resolve_index(n: i64, len: usize) -> Option<usize> {
    let from_start = if n < 0 { n.checked_add(len as i64)? } else { n };
    if from_start < 0 || from_start >= len as i64 {
        return None;
    }
    Some(from_start as usize)
}

/// Validate an array index and give the position it names.
///
/// The message names the index **as the program wrote it**, so `a[-5]` on three
/// elements says `-5` rather than the position it worked out to. The number in
/// the message is then a number the reader can find on the line.
pub fn array_index(index: &Value, len: usize) -> Result<usize, String> {
    if let Some(message) = unhandled(index) {
        return Err(message);
    }
    let Value::Int(n) = index else {
        return Err(format!(
            "array index must be an int, not a {}",
            index.type_name()
        ));
    };
    resolve_index(*n, len)
        .ok_or_else(|| format!("index {n} is out of range for an array of length {len}"))
}

/// Validate an index against a string and give the character it names.
///
/// **Characters, not bytes**, matching `len`, `find`, `slice`, and
/// `split(s, "")`. That was settled long before indexing existed: the library
/// counts characters everywhere, so this had only to follow rather than choose,
/// which is why `s[i]` needed no new decision about what a string is made of.
///
/// The refusals are [`array_index`]'s, in its words with "string" for "array".
/// Indexing an array and indexing a string are one mistake made two ways, and a
/// program should not have to learn two vocabularies for it.
///
/// **A negative index counts back from the end**, so `s[-1]` is the last
/// character. Version 1 refused it, and 1.9's note here said the refusal was
/// kept so that a later release could give it this meaning: a refusal can
/// become a meaning and a meaning cannot become a refusal. This is that
/// release.
///
/// Finding the character costs a walk from the front, since a character index
/// is not a byte offset. A loop over every character is therefore quadratic and
/// `split(s, "")` remains the way to walk a long string once.
pub fn string_index(index: &Value, s: &str) -> Result<String, String> {
    if let Some(message) = unhandled(index) {
        return Err(message);
    }
    let Value::Int(n) = index else {
        return Err(format!(
            "string index must be an int, not a {}",
            index.type_name()
        ));
    };
    // The length is counted first rather than after a failure, because a
    // negative index needs it to resolve at all.
    let len = s.chars().count();
    let wanted = resolve_index(*n, len)
        .ok_or_else(|| format!("index {n} is out of range for a string of length {len}"))?;
    match s.chars().nth(wanted) {
        Some(found) => Ok(found.to_string()),
        // Unreachable: `resolve_index` has already held `wanted` below `len`,
        // and `len` is this string's own character count.
        None => Err(format!(
            "index {n} is out of range for a string of length {len}"
        )),
    }
}

/// Validate a map key without copying it: it must be a string.
///
/// Reading a map only needs to look the key up, and `BTreeMap<String, _>::get`
/// takes a `&str` through `Borrow`. Use this there. [`map_key`] copies, which a
/// read does not need and an insert does.
pub fn map_key_ref(index: &Value) -> Result<&str, String> {
    match index {
        Value::Str(s) => Ok(s.as_str()),
        Value::Error(error) => Err(format!("unhandled error: {}", error.message)),
        other => Err(format!(
            "map key must be a string, not a {}",
            other.type_name()
        )),
    }
}

/// Validate a map key and copy it, for the paths that store it.
pub fn map_key(index: &Value) -> Result<String, String> {
    map_key_ref(index).map(str::to_string)
}

/// Order two values: integers and strings compare directly, mixed numbers are
/// promoted, and NaN is rejected.
fn ordering(left: Value, right: Value) -> Result<Ordering, String> {
    match (&left, &right) {
        (Value::Int(a), Value::Int(b)) => Ok(a.cmp(b)),
        (Value::Str(a), Value::Str(b)) => Ok(a.cmp(b)),
        _ => match numeric_pair(&left, &right, "compare")? {
            Num::Ints(a, b) => Ok(a.cmp(&b)),
            Num::Floats(a, b) => a
                .partial_cmp(&b)
                .ok_or_else(|| "cannot compare with NaN".to_string()),
        },
    }
}

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

    fn caught_error() -> Value {
        Value::Error(Rc::new(crate::MiruError::with_column(
            1,
            9,
            "division by zero",
        )))
    }

    #[test]
    fn every_operator_refuses_a_caught_error_as_an_operand() {
        // The two that need saying out loud, because neither reports a type
        // error for anything: `!` answers for every value through is_truthy,
        // and `==` answers for every pair through Value::equals. Left to
        // themselves they would call an error `false` and "not equal to
        // anything", which reads exactly like an ordinary value.
        assert_eq!(
            unary(UnaryOp::Not, caught_error()).err().unwrap(),
            "unhandled error: division by zero"
        );
        assert_eq!(
            binary(BinaryOp::Equal, caught_error(), Value::Int(1))
                .err()
                .unwrap(),
            "unhandled error: division by zero"
        );

        // And the ordinary ones, on either side.
        for op in [
            BinaryOp::Add,
            BinaryOp::Subtract,
            BinaryOp::Multiply,
            BinaryOp::Divide,
            BinaryOp::Modulo,
            BinaryOp::NotEqual,
            BinaryOp::Less,
            BinaryOp::Greater,
            BinaryOp::LessEqual,
            BinaryOp::GreaterEqual,
        ] {
            assert_eq!(
                binary(op, caught_error(), Value::Int(1)).err().unwrap(),
                "unhandled error: division by zero",
                "{op:?} accepted an error on the left"
            );
            assert_eq!(
                binary(op, Value::Int(1), caught_error()).err().unwrap(),
                "unhandled error: division by zero",
                "{op:?} accepted an error on the right"
            );
        }
        assert_eq!(
            unary(UnaryOp::Negate, caught_error()).err().unwrap(),
            "unhandled error: division by zero"
        );
    }

    #[test]
    fn a_caught_error_is_not_an_index_or_a_key() {
        assert_eq!(
            array_index(&caught_error(), 3).err().unwrap(),
            "unhandled error: division by zero"
        );
        assert_eq!(
            map_key(&caught_error()).err().unwrap(),
            "unhandled error: division by zero"
        );
    }

    #[test]
    fn arithmetic_promotes_and_concatenates() {
        assert!(binary(BinaryOp::Add, Value::Int(1), Value::Int(2))
            .unwrap()
            .equals(&Value::Int(3))
            .unwrap());
        assert!(binary(BinaryOp::Add, Value::Int(1), Value::Float(2.0))
            .unwrap()
            .equals(&Value::Float(3.0))
            .unwrap());
        let joined = binary(
            BinaryOp::Add,
            Value::Str(Rc::new("a".to_string())),
            Value::Str(Rc::new("b".to_string())),
        )
        .unwrap();
        assert!(joined
            .equals(&Value::Str(Rc::new("ab".to_string())))
            .unwrap());
    }

    #[test]
    fn adding_two_arrays_joins_them() {
        let joined = binary(
            BinaryOp::Add,
            Value::array(vec![Value::Int(1), Value::Int(2)]),
            Value::array(vec![Value::Int(3)]),
        )
        .unwrap();
        assert!(joined
            .equals(&Value::array(vec![
                Value::Int(1),
                Value::Int(2),
                Value::Int(3)
            ]))
            .unwrap());
    }

    /// **The result is new, and neither operand is touched.**
    ///
    /// `+` is an expression. One that grew its left operand in place would make
    /// `let joined = front + back` quietly change `front`, which is the kind of
    /// thing a program only finds out about several lines later. `push` is the
    /// spelling for growing an array you already have.
    #[test]
    fn adding_arrays_leaves_both_operands_alone() {
        let front = Value::array(vec![Value::Int(1)]);
        let back = Value::array(vec![Value::Int(2)]);
        let joined = binary(BinaryOp::Add, front.clone(), back.clone()).unwrap();

        let Value::Array(items) = &joined else {
            panic!("adding two arrays gave a {}", joined.type_name());
        };
        items.borrow_mut().push(Value::Int(3));

        assert!(front.equals(&Value::array(vec![Value::Int(1)])).unwrap());
        assert!(back.equals(&Value::array(vec![Value::Int(2)])).unwrap());
    }

    /// An array added to itself. Two `borrow`s of one `RefCell`, which is fine,
    /// where a `borrow` and a `borrow_mut` would panic, so this is the case
    /// that would catch an implementation that tried to extend in place.
    #[test]
    fn an_array_can_be_added_to_itself() {
        let a = Value::array(vec![Value::Int(1)]);
        let joined = binary(BinaryOp::Add, a.clone(), a).unwrap();
        assert!(joined
            .equals(&Value::array(vec![Value::Int(1), Value::Int(1)]))
            .unwrap());
    }

    /// Only array with array. Mixing one with anything else stays the error it
    /// has always been, so nothing that used to fail now quietly succeeds.
    #[test]
    fn an_array_still_cannot_be_added_to_anything_else() {
        assert_eq!(
            binary(BinaryOp::Add, Value::array(vec![]), Value::Int(1))
                .err()
                .unwrap(),
            "cannot add a array and a int"
        );
        assert_eq!(
            binary(
                BinaryOp::Add,
                Value::Str(Rc::new("a".to_string())),
                Value::array(vec![])
            )
            .err()
            .unwrap(),
            "cannot add a string and a array"
        );
    }

    #[test]
    fn division_and_modulo_by_zero_are_errors() {
        assert_eq!(
            binary(BinaryOp::Divide, Value::Int(1), Value::Int(0))
                .err()
                .unwrap(),
            "division by zero"
        );
        assert_eq!(
            binary(BinaryOp::Modulo, Value::Int(1), Value::Int(0))
                .err()
                .unwrap(),
            "modulo by zero"
        );
    }

    #[test]
    fn integer_overflow_is_reported() {
        assert_eq!(
            binary(BinaryOp::Add, Value::Int(i64::MAX), Value::Int(1))
                .err()
                .unwrap(),
            "integer overflow in addition"
        );
        assert_eq!(
            unary(UnaryOp::Negate, Value::Int(i64::MIN)).err().unwrap(),
            "integer overflow while negating"
        );
    }

    #[test]
    fn comparisons_and_type_errors() {
        assert!(binary(BinaryOp::Less, Value::Int(1), Value::Int(2))
            .unwrap()
            .equals(&Value::Bool(true))
            .unwrap());
        assert_eq!(
            binary(BinaryOp::Add, Value::Int(1), Value::Bool(true))
                .err()
                .unwrap(),
            "cannot add a int and a bool"
        );
    }

    #[test]
    fn not_and_negate() {
        assert!(unary(UnaryOp::Not, Value::Nil)
            .unwrap()
            .equals(&Value::Bool(true))
            .unwrap());
        assert!(unary(UnaryOp::Negate, Value::Int(5))
            .unwrap()
            .equals(&Value::Int(-5))
            .unwrap());
    }
}