run-rs 0.6.6

Run a subset of Rust as an interpreted script
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
//! Operators and pattern binding for the VM.

use num_traits::AsPrimitive;
use std::cmp::Ordering;
use std::slice::from_ref;

use anyhow::{Result, anyhow, bail};

use super::bytecode::{BinKind, UnKind};
use super::bytecode::{PLit, PPat};
use super::numeric::{
    IntWidth, float_arith, i64_arith, int_arith, int_bit, int_neg, int_not, int_shift, u64_arith,
    unify,
};
use super::shared::{duration_arith, usize_i64};
use super::std_bridge::{duration_from_value, make_duration};
use super::value::Value;

pub(super) fn apply_bin(op: BinKind, l: &Value, r: &Value) -> Result<Value> {
    use BinKind::{
        Add, BitAnd, BitOr, BitXor, Div, Eq, Ge, Gt, Le, Lt, Mul, Ne, Rem, Shl, Shr, Sub,
    };
    Ok(match op {
        Add | Sub | Mul | Div | Rem => return arith(op, l, r),
        Eq => Value::Bool(l.eq_value(r)),
        Ne => Value::Bool(!l.eq_value(r)),
        Lt => Value::Bool(partial_compare(l, r)? == Some(Ordering::Less)),
        Le => Value::Bool(matches!(
            partial_compare(l, r)?,
            Some(Ordering::Less | Ordering::Equal)
        )),
        Gt => Value::Bool(partial_compare(l, r)? == Some(Ordering::Greater)),
        Ge => Value::Bool(matches!(
            partial_compare(l, r)?,
            Some(Ordering::Greater | Ordering::Equal)
        )),
        BitAnd | BitOr | BitXor => bit_bin(op, l, r)?,
        Shl | Shr => shift_bin(op, l, r)?,
    })
}

pub(super) fn apply_bin_imm(op: BinKind, l: &Value, imm: i64) -> Result<Value> {
    apply_bin(op, l, &Value::Int(imm))
}

pub(super) fn cmp_test(op: BinKind, l: &Value, r: &Value) -> Result<bool> {
    use BinKind::{Eq, Ge, Gt, Le, Lt, Ne};
    Ok(match op {
        Eq => l.eq_value(r),
        Ne => !l.eq_value(r),
        Lt => partial_compare(l, r)? == Some(Ordering::Less),
        Le => matches!(
            partial_compare(l, r)?,
            Some(Ordering::Less | Ordering::Equal)
        ),
        Gt => partial_compare(l, r)? == Some(Ordering::Greater),
        Ge => matches!(
            partial_compare(l, r)?,
            Some(Ordering::Greater | Ordering::Equal)
        ),
        _ => unreachable!("compare jump carries a non-comparison operator"),
    })
}

pub(super) fn cmp_test_imm(op: BinKind, l: &Value, imm: i64) -> Result<bool> {
    cmp_test(op, l, &Value::Int(imm))
}

fn arith(op: BinKind, l: &Value, r: &Value) -> Result<Value> {
    // Same-type numbers first, they dominate hot loops. The remaining
    // patterns are disjoint from these two, so the order change is safe.
    if let (Value::Int(a), Value::Int(b)) = (l, r) {
        return Ok(Value::Int(i64_arith(op, *a, *b)?));
    }
    // A 64-bit unsigned pair, or one with a bare literal beside it, computes
    // natively instead of through the i128 pipeline. Counting and checksum
    // loops on `u64` and `usize` live here.
    if let Value::IntW(a, wa @ (IntWidth::U64 | IntWidth::USize)) = l {
        let rhs = match r {
            Value::IntW(b, wb) if wa == wb => Some(b.cast_unsigned()),
            Value::Int(b) if *b >= 0 => Some(b.cast_unsigned()),
            _ => None,
        };
        if let Some(y) = rhs {
            let x = a.cast_unsigned();
            let out = u64_arith(op, x, y)?;
            return Ok(Value::IntW(out.cast_signed(), *wa));
        }
    }
    if let (Value::Float(a), Value::Float(b)) = (l, r) {
        return Ok(Value::Float(float_arith(op, *a, *b)));
    }
    if let (BinKind::Add, Value::Str(a), Value::Str(b)) = (op, l, r) {
        let mut out = String::with_capacity(a.len() + b.len());
        out.push_str(a);
        out.push_str(b);
        return Ok(Value::str(out));
    }
    // Only a struct can be a Duration, so the discriminant check keeps the
    // probe off every numeric op.
    if matches!(l, Value::Struct(_))
        && let (Some(a), Some(b)) = (duration_from_value(l), duration_from_value(r))
    {
        return Ok(make_duration(duration_arith(op, a, b)?));
    }
    if let Some(width) = big_operands(l, r) {
        let (a, b) = (big_bits(l), big_bits(r));
        return Ok(Value::Big(
            super::numeric::big_arith(op, width, a, b)?,
            width,
        ));
    }
    if let (Some((a, wa)), Some((b, wb))) = (l.int_parts(), r.int_parts()) {
        let width = unify(wa, wb)?;
        return Ok(Value::int_of_width(int_arith(op, width, a, b)?, width));
    }
    match float_pair(l, r)? {
        FloatPair::F64(x, y) => Ok(Value::Float(float_arith(op, x, y))),
        FloatPair::F32(x, y) => Ok(Value::F32(float_arith(op, x, y))),
    }
}

/// An untagged f64 next to an f32 is a bare
/// literal that is f32 in the source types.
enum FloatPair {
    F64(f64, f64),
    F32(f32, f32),
}

fn float_pair(l: &Value, r: &Value) -> Result<FloatPair> {
    Ok(match (l, r) {
        (Value::F32(a), Value::F32(b)) => FloatPair::F32(*a, *b),
        (Value::F32(a), Value::Float(b)) => FloatPair::F32(*a, AsPrimitive::<f32>::as_(*b)),
        (Value::Float(a), Value::F32(b)) => FloatPair::F32(AsPrimitive::<f32>::as_(*a), *b),
        (a, b) => FloatPair::F64(to_float(a)?, to_float(b)?),
    })
}

fn bit_bin(op: BinKind, l: &Value, r: &Value) -> Result<Value> {
    if let (Value::Int(a), Value::Int(b)) = (l, r) {
        let f = bit_i64(op);
        return Ok(Value::Int(f(*a, *b)));
    }
    if let (Value::Bool(a), Value::Bool(b)) = (l, r) {
        let f = bit_i64(op);
        return Ok(Value::Bool(f(i64::from(*a), i64::from(*b)) != 0));
    }
    if let Some(width) = big_operands(l, r) {
        let (a, b) = (big_bits(l), big_bits(r));
        return Ok(Value::Big(
            super::numeric::big_arith(op, width, a, b)?,
            width,
        ));
    }
    if let (Some((a, wa)), Some((b, wb))) = (l.int_parts(), r.int_parts()) {
        let width = unify(wa, wb)?;
        return Ok(Value::int_of_width(int_bit(op, a, b)?, width));
    }
    bail!("bitwise operators need integers")
}

fn bit_i64(op: BinKind) -> fn(i64, i64) -> i64 {
    match op {
        BinKind::BitAnd => |a, b| a & b,
        BinKind::BitOr => |a, b| a | b,
        _ => |a, b| a ^ b,
    }
}

/// `<<` and `>>`, the amount only supplies the count and the result keeps
/// the shifted side's width.
fn shift_bin(op: BinKind, l: &Value, r: &Value) -> Result<Value> {
    if let Value::Big(a, w) = l {
        let Some((amount, _)) = r.int_parts() else {
            bail!("shift operators need integers");
        };
        return Ok(Value::Big(int_shift(op, *w, *a, amount)?, *w));
    }
    let (Some((a, wa)), Some((b, _))) = (l.int_parts(), r.int_parts()) else {
        bail!("shift operators need integers");
    };
    Ok(Value::int_of_width(int_shift(op, wa, a, b)?, wa))
}

pub(super) fn compare_values(l: &Value, r: &Value) -> Result<Ordering> {
    partial_compare(l, r)?.ok_or_else(|| anyhow!("cannot order NaN"))
}

/// `PartialOrd` semantics: a NaN operand makes every ordered
/// comparison false, exactly like compiled Rust. Contexts that need a total
/// order, sorting for example, go through `compare_values` and reject NaN.
fn partial_compare(l: &Value, r: &Value) -> Result<Option<Ordering>> {
    Ok(match (l, r) {
        (Value::Int(a), Value::Int(b)) => Some(a.cmp(b)),
        (Value::Big(a, wa), Value::Big(b, _)) => Some(if *wa == super::numeric::IntWidth::U128 {
            a.cast_unsigned().cmp(&b.cast_unsigned())
        } else {
            a.cmp(b)
        }),
        (Value::Big(..), Value::Int(_)) | (Value::Int(_), Value::Big(..)) => {
            match (l.int_parts(), r.int_parts()) {
                (Some((a, _)), Some((b, _))) => Some(a.cmp(&b)),
                // The big side is a u128 past the i128 range, larger than
                // any i64 the other side can hold.
                (None, _) => Some(Ordering::Greater),
                (_, None) => Some(Ordering::Less),
            }
        }
        (Value::IntW(..), Value::Int(_) | Value::IntW(..)) | (Value::Int(_), Value::IntW(..)) => {
            let (a, _) = l.int_parts().unwrap();
            let (b, _) = r.int_parts().unwrap();
            Some(a.cmp(&b))
        }
        (Value::Float(a), Value::Float(b)) => a.partial_cmp(b),
        (Value::F32(a), Value::F32(b)) => a.partial_cmp(b),
        (Value::F32(a), Value::Float(b)) => a.partial_cmp(&AsPrimitive::<f32>::as_(*b)),
        (Value::Float(a), Value::F32(b)) => AsPrimitive::<f32>::as_(*a).partial_cmp(b),
        (Value::Int(a), Value::Float(b)) => AsPrimitive::<f64>::as_(*a).partial_cmp(b),
        (Value::Float(a), Value::Int(b)) => a.partial_cmp(&AsPrimitive::<f64>::as_(*b)),
        (Value::Str(a), Value::Str(b)) => Some(a.as_ref().cmp(b.as_ref())),
        (Value::Char(a), Value::Char(b)) => Some(a.cmp(b)),
        (Value::Bool(a), Value::Bool(b)) => Some(a.cmp(b)),
        // Sequences order lexicographically, the first differing element
        // deciding and a prefix ordering before what extends it. Tuples order
        // the same way, field by field.
        (Value::Vec(a), Value::Vec(b)) | (Value::Tuple(a), Value::Tuple(b)) => {
            // Two separate statements on purpose. In one statement both lock
            // guards live to the end of it, so ordering a value against its
            // own clone locks the same mutex twice and deadlocks.
            let a = a.lock().clone();
            let b = b.lock().clone();
            let mut order = None;
            for (left, right) in a.iter().zip(b.iter()) {
                match partial_compare(left, right)? {
                    Some(Ordering::Equal) => {}
                    other => {
                        order = Some(other);
                        break;
                    }
                }
            }
            match order {
                Some(decided) => decided,
                None => Some(a.len().cmp(&b.len())),
            }
        }
        // `Option` and `Result` derive their order from the variant order in
        // the declaration, so `None` sorts before any `Some` and `Ok` before
        // any `Err`, and two of the same variant compare by payload.
        (
            Value::Enum {
                enum_name: left_enum,
                variant: left_variant,
                data: left_data,
            },
            Value::Enum {
                enum_name: right_enum,
                variant: right_variant,
                data: right_data,
            },
        ) if left_enum == right_enum => {
            let rank = |variant: &str| match variant {
                "None" | "Ok" => 0,
                _ => 1,
            };
            match rank(left_variant).cmp(&rank(right_variant)) {
                Ordering::Equal => {
                    // Snapshots, not held guards: comparing a value with its
                    // own clone sees the same storage on both sides.
                    let left_data = left_data.lock().clone();
                    let right_data = right_data.lock().clone();
                    let mut order = None;
                    for (left, right) in left_data.iter().zip(right_data.iter()) {
                        match partial_compare(left, right)? {
                            Some(Ordering::Equal) => {}
                            other => {
                                order = Some(other);
                                break;
                            }
                        }
                    }
                    match order {
                        Some(decided) => decided,
                        None => Some(left_data.len().cmp(&right_data.len())),
                    }
                }
                decided => Some(decided),
            }
        }
        (a, b) => bail!("cannot compare {} and {}", a.type_name(), b.type_name()),
    })
}

fn to_float(v: &Value) -> Result<f64> {
    match v {
        Value::Int(i) => Ok(AsPrimitive::<f64>::as_(*i)),
        Value::Float(f) => Ok(*f),
        other => bail!("expected a number, got {}", other.type_name()),
    }
}

pub(super) fn apply_un(op: UnKind, v: &Value) -> Result<Value> {
    Ok(match (op, v) {
        (UnKind::Neg, Value::Int(i)) => Value::Int(
            i.checked_neg()
                .ok_or_else(|| anyhow!("attempt to negate with overflow"))?,
        ),
        (UnKind::Neg, Value::IntW(v, w)) => Value::int_of_width(int_neg(*w, w.decode(*v))?, *w),
        (UnKind::Neg, Value::Big(v, w)) => Value::Big(int_neg(*w, *v)?, *w),
        (UnKind::Neg, Value::Float(f)) => Value::Float(-*f),
        (UnKind::Neg, Value::F32(f)) => Value::F32(-*f),
        (UnKind::Not, Value::Bool(b)) => Value::Bool(!*b),
        (UnKind::Not, Value::Int(i)) => Value::Int(!*i),
        (UnKind::Not, Value::IntW(v, w)) => Value::int_of_width(int_not(*w, w.decode(*v)), *w),
        (UnKind::Not, Value::Big(v, w)) => Value::Big(int_not(*w, *v), *w),
        (op, v) => bail!("cannot apply {:?} to {}", op, v.type_name()),
    })
}

/// Whether a `serde_json::Value` variant pattern like `Value::String(s)`
/// matches the shape of the value, since decoded json is held as plain values.
fn json_variant_kind_matches(name: Option<&str>, val: &Value) -> bool {
    matches!(
        (name, val),
        (Some("String"), Value::Str(_))
            | (Some("Number"), Value::Int(_) | Value::Float(_))
            | (Some("Bool"), Value::Bool(_))
            | (Some("Array"), Value::Vec(_))
            | (Some("Object"), Value::Map(..))
    )
}

pub(super) fn try_bind(pat: &PPat, val: &Value, define: &mut dyn FnMut(&str, Value)) -> bool {
    match pat {
        PPat::Wild | PPat::Rest => true,
        PPat::Ident { name, sub } => {
            if let Some(s) = sub
                && !try_bind(s, val, define)
            {
                return false;
            }
            define(name, val.clone());
            true
        }
        PPat::Lit(l) => plit_eq(l, val),
        PPat::Tuple(elems) => match val {
            Value::Tuple(items) => bind_seq(elems, &items.lock(), define),
            Value::Unit if elems.is_empty() => true,
            _ => false,
        },
        PPat::TupleStruct { name, elems } => match val {
            Value::Enum { variant, data, .. } => {
                let payload = data.lock().clone();
                name.as_deref() == Some(&**variant) && bind_seq(elems, &payload, define)
            }
            Value::Struct(st) => {
                let vals: Vec<Value> = st.values.lock().clone();
                bind_seq(elems, &vals, define)
            }
            // Matches the pre-unwrapped Some rule in ops.rs, see the note there.
            Value::Unit => false,
            other => {
                if json_variant_kind_matches(name.as_deref(), other) {
                    bind_seq(elems, from_ref(other), define)
                } else {
                    name.as_deref() == Some("Some") && bind_seq(elems, from_ref(other), define)
                }
            }
        },
        PPat::Path { name } => match val {
            Value::Enum {
                enum_name, variant, ..
            } => {
                name.as_deref() == Some(&**variant)
                    // A json null is Option::None here, so `Value::Null` matches it.
                    || (name.as_deref() == Some("Null")
                        && &**enum_name == "Option"
                        && &**variant == "None")
            }
            _ => false,
        },
        PPat::Struct { name, fields } => {
            let Value::Struct(st) = val else {
                return false;
            };
            if let Some(pn) = name
                && pn.as_str() != super::resolver::bare(st.name())
            {
                return false;
            }
            for (key, fp) in fields {
                match st.get(key) {
                    Some(v) => {
                        if !try_bind(fp, &v, define) {
                            return false;
                        }
                    }
                    None => return false,
                }
            }
            true
        }
        PPat::Or(cases) => cases.iter().any(|c| try_bind(c, val, define)),
        PPat::Slice(elems) => match val {
            Value::Vec(items) => bind_seq(elems, &items.lock(), define),
            _ => false,
        },
        PPat::Range { lo, hi, inclusive } => {
            range_matches(lo.as_ref(), hi.as_ref(), *inclusive, |l| {
                endpoint_cmp(l, val)
            })
        }
        PPat::Unsupported => false,
    }
}

/// Order a range endpoint against a value of the same type. `None` for a type
/// mismatch, which makes the range not match.
fn endpoint_cmp(literal: &PLit, value: &Value) -> Option<Ordering> {
    match (literal, value) {
        (PLit::Int(a), Value::Int(b)) => Some(a.cmp(b)),
        (PLit::Int(a), Value::IntW(..)) => {
            let (b, _) = value.int_parts()?;
            Some(i128::from(*a).cmp(&b))
        }
        (PLit::Float(a), Value::Float(b)) => a.partial_cmp(b),
        (PLit::Float(a), Value::F32(b)) => AsPrimitive::<f32>::as_(*a).partial_cmp(b),
        (PLit::Char(a), Value::Char(b)) => Some(a.cmp(b)),
        _ => None,
    }
}

/// Where a value being bound by reference lives, so the binding can anchor
/// to that storage. A slotless value binds as a plain borrow wrapper when
/// it is a composite, or as a copy when it is a scalar.
enum BindSlot {
    None,
    Elem(super::value::List, usize),
    Field(std::sync::Arc<super::value::StructData>, usize),
}

/// Define bindings for a pattern that already matched a `&mut` scrutinee.
/// Every binding anchors to the matched value's own storage where one
/// exists, so `*x += 1` and `v.push(..)` through the binding land in the
/// borrowed place. Runs after `try_bind` said the pattern matches, and must
/// walk the same shapes.
fn bind_refs(pat: &PPat, val: &Value, slot: BindSlot, define: &mut dyn FnMut(&str, Value)) {
    match pat {
        PPat::Ident { name, sub } => {
            let bound = match &slot {
                BindSlot::Elem(list, i) => Value::Ref(std::sync::Arc::new(
                    super::value::ValueRef::vec_element(list.clone(), *i),
                )),
                BindSlot::Field(data, i) => Value::Ref(std::sync::Arc::new(
                    super::value::ValueRef::struct_field(data.clone(), *i),
                )),
                BindSlot::None => match val {
                    Value::Vec(_)
                    | Value::Map(..)
                    | Value::Tuple(_)
                    | Value::Struct(_)
                    | Value::Enum { .. } => Value::Ref(std::sync::Arc::new(
                        super::value::ValueRef::borrowed(val.clone()),
                    )),
                    other => other.clone(),
                },
            };
            define(name, bound);
            if let Some(s) = sub {
                bind_refs(s, val, slot, define);
            }
        }
        PPat::Tuple(elems) => {
            if let Value::Tuple(items) = val {
                bind_refs_seq(elems, items, define);
            }
        }
        PPat::TupleStruct { elems, .. } => match val {
            Value::Enum { data, .. } => bind_refs_seq(elems, data, define),
            Value::Struct(st) => {
                let vals: Vec<Value> = st.values.lock().clone();
                for (i, (p, v)) in elems.iter().zip(vals.iter()).enumerate() {
                    bind_refs(p, v, BindSlot::Field(st.clone(), i), define);
                }
            }
            // The pre-unwrapped Some shapes bind the value itself.
            other => {
                if let Some(p) = elems.first() {
                    bind_refs(p, other, BindSlot::None, define);
                }
            }
        },
        PPat::Struct { fields, .. } => {
            if let Value::Struct(st) = val {
                let vals: Vec<Value> = st.values.lock().clone();
                for (fname, p) in fields {
                    if let Some(i) = st.shape.slot(fname) {
                        bind_refs(p, &vals[i], BindSlot::Field(st.clone(), i), define);
                    }
                }
            }
        }
        PPat::Or(alts) => {
            // The first alternative that matches is the one whose bindings
            // are live, the same choice `try_bind` made.
            for alt in alts {
                if try_bind(alt, val, &mut |_, _| {}) {
                    bind_refs(alt, val, slot, define);
                    return;
                }
            }
        }
        PPat::Slice(elems) => {
            if let Value::Vec(items) = val {
                bind_refs_seq(elems, items, define);
            }
        }
        PPat::Wild
        | PPat::Rest
        | PPat::Lit(_)
        | PPat::Path { .. }
        | PPat::Range { .. }
        | PPat::Unsupported => {}
    }
}

/// The element half of `bind_refs`: anchor each pattern to its element slot,
/// with the same head-and-tail split around a `..` that `bind_seq` uses.
fn bind_refs_seq(pats: &[PPat], list: &super::value::List, define: &mut dyn FnMut(&str, Value)) {
    let vals: Vec<Value> = list.lock().clone();
    if pats.iter().any(|p| matches!(p, PPat::Rest)) {
        let head = pats.iter().take_while(|p| !matches!(p, PPat::Rest)).count();
        for (i, p) in pats.iter().take(head).enumerate() {
            if let Some(v) = vals.get(i) {
                bind_refs(p, v, BindSlot::Elem(list.clone(), i), define);
            }
        }
        let tail = &pats[head + 1..];
        for (j, p) in tail.iter().enumerate() {
            let Some(i) = (vals.len() - tail.len()).checked_add(j) else {
                continue;
            };
            if let Some(v) = vals.get(i) {
                bind_refs(p, v, BindSlot::Elem(list.clone(), i), define);
            }
        }
        return;
    }
    for (i, (p, v)) in pats.iter().zip(vals.iter()).enumerate() {
        bind_refs(p, v, BindSlot::Elem(list.clone(), i), define);
    }
}

/// Entry for the VM: bindings for a matched `&mut` scrutinee.
pub(super) fn bind_pattern_refs(pat: &PPat, val: &Value, define: &mut dyn FnMut(&str, Value)) {
    bind_refs(pat, val, BindSlot::None, define);
}

fn bind_seq(pats: &[PPat], vals: &[Value], define: &mut dyn FnMut(&str, Value)) -> bool {
    if pats.iter().any(|p| matches!(p, PPat::Rest)) {
        let head = pats.iter().take_while(|p| !matches!(p, PPat::Rest)).count();
        for (p, v) in pats.iter().take(head).zip(vals.iter()) {
            if !try_bind(p, v, define) {
                return false;
            }
        }
        for (p, v) in pats.iter().skip(head + 1).zip(vals.iter().rev()) {
            if !try_bind(p, v, define) {
                return false;
            }
        }
        return true;
    }
    pats.len() == vals.len()
        && pats
            .iter()
            .zip(vals.iter())
            .all(|(p, v)| try_bind(p, v, define))
}

fn plit_eq(l: &PLit, val: &Value) -> bool {
    match (l, val) {
        (PLit::Int(a), Value::Int(b)) => a == b,
        (PLit::Int(a), Value::IntW(..)) => val.int_parts().map(|(v, _)| v) == Some(i128::from(*a)),
        (PLit::Float(a), Value::Float(b)) => a == b,
        (PLit::Float(a), Value::F32(b)) => AsPrimitive::<f32>::as_(*a) == *b,
        (PLit::Bool(a), Value::Bool(b)) => a == b,
        (PLit::Str(a), Value::Str(b)) => a.as_str() == b.as_ref(),
        (PLit::Char(a), Value::Char(b)) => a == b,
        _ => false,
    }
}

/// An integer operand, for range bounds and sequence indexes.
pub(super) fn int_of(v: &Value) -> Result<i64> {
    match v {
        Value::Int(i) => Ok(*i),
        Value::IntW(..) => v
            .untag_int()
            .ok_or_else(|| anyhow!("integer out of the i64 range")),
        Value::Big(..) => match v.int_parts() {
            Some((n, _)) => i64::try_from(n).map_err(|_| anyhow!("integer out of the i64 range")),
            None => bail!("integer out of the i64 range"),
        },
        _ => bail!("range bound must be an integer"),
    }
}

/// The shared 128-bit width of two operands when either is a `Value::Big`.
/// The untagged side is a bare literal adopting the big side's width.
fn big_operands(l: &Value, r: &Value) -> Option<super::numeric::IntWidth> {
    match (l, r) {
        (Value::Big(_, w), Value::Big(..) | Value::Int(_)) | (Value::Int(_), Value::Big(_, w)) => {
            Some(*w)
        }
        _ => None,
    }
}

/// An operand's raw 128-bit image for the big paths: a `Big` as stored, an
/// untagged literal widened.
fn big_bits(v: &Value) -> i128 {
    match v {
        Value::Big(bits, _) => *bits,
        Value::Int(i) => i128::from(*i),
        _ => 0,
    }
}

// -- indexing and `?` ------------------------------------------------------

pub(super) fn index(recv: &Value, key: &Value) -> Result<Value> {
    if let Value::Range {
        start,
        end,
        inclusive,
    } = key
    {
        return slice_value(recv, *start, *end, *inclusive);
    }
    match recv {
        Value::Vec(items) => {
            let i = usize::try_from(int_of(key)?)?;
            let items = items.lock();
            items.get(i).cloned().ok_or_else(|| {
                anyhow::anyhow!(
                    "index out of bounds: the len is {} but the index is {i}",
                    items.len()
                )
            })
        }
        Value::Map(m, _) => {
            let k = key
                .as_key()
                .ok_or_else(|| anyhow::anyhow!("invalid map key"))?;
            m.lock()
                .get(&k)
                .cloned()
                .ok_or_else(|| anyhow::anyhow!("no entry found for key"))
        }
        Value::Str(s) => {
            let i = usize::try_from(int_of(key)?)?;
            s.chars().nth(i).map(Value::Char).ok_or_else(|| {
                anyhow::anyhow!(
                    "index out of bounds: the len is {} but the index is {i}",
                    s.chars().count()
                )
            })
        }
        // `caps[1]` and `caps["name"]` on a capture set.
        Value::Native(h) => super::regex_bridge::capture_index(h, key),
        _ => bail!("cannot index {}", recv.type_name()),
    }
}

/// A range key resolved against a length. The messages are the exact texts
/// debug Rust panics with, checked in declaration order: inverted range
/// here, out of bounds and a string's char boundary at the use site.
fn range_bounds(len: usize, start: i64, end: i64, inclusive: bool) -> Result<(usize, usize)> {
    if start < 0 {
        bail!("negative slice start {start}");
    }
    let end = if end == i64::MAX {
        usize_i64(len)
    } else if inclusive {
        end + 1
    } else {
        end
    };
    if end < start {
        bail!("slice index starts at {start} but ends at {end}");
    }
    Ok((usize::try_from(start)?, usize::try_from(end)?))
}

/// The debug Rust panic text for a byte range off a char boundary.
fn char_boundary_error(s: &str, a: usize, b: usize) -> anyhow::Error {
    let (side, bad) = if s.is_char_boundary(a) {
        ("end", b)
    } else {
        ("start", a)
    };
    let mut at = bad;
    while at > 0 && !s.is_char_boundary(at) {
        at -= 1;
    }
    let ch = s[at..].chars().next().unwrap_or('\u{FFFD}');
    anyhow!(
        "{side} byte index {bad} is not a char boundary; it is inside {ch:?} (bytes {at}..{} of string)",
        at + ch.len_utf8()
    )
}

fn slice_value(base: &Value, start: i64, end: i64, inclusive: bool) -> Result<Value> {
    match base {
        Value::Vec(items) => {
            let items = items.lock();
            let (a, b) = range_bounds(items.len(), start, end, inclusive)?;
            if b > items.len() {
                bail!(
                    "range end index {b} out of range for slice of length {}",
                    items.len()
                );
            }
            Ok(Value::vec(items[a..b].to_vec()))
        }
        Value::Str(s) => {
            let (a, b) = range_bounds(s.len(), start, end, inclusive)?;
            if b > s.len() {
                bail!(
                    "end byte index {b} is out of bounds for string of length {}",
                    s.len()
                );
            }
            match s.get(a..b) {
                Some(sub) => Ok(Value::str(sub.to_string())),
                None => Err(char_boundary_error(s, a, b)),
            }
        }
        other => bail!("cannot slice {}", other.type_name()),
    }
}

/// The writeback of a mutating method called on a string slice, like
/// `s[2..].make_ascii_uppercase()`. The slice arrived as a copied temporary,
/// so the mutated bytes are spliced back into the base string here.
pub(super) fn splice_str(
    s: &str,
    start: i64,
    end: i64,
    inclusive: bool,
    val: &Value,
) -> Result<String> {
    let Value::Str(new) = val else {
        bail!("cannot write {} back into a string slice", val.type_name());
    };
    let (a, b) = range_bounds(s.len(), start, end, inclusive)?;
    if b > s.len() {
        bail!(
            "end byte index {b} is out of bounds for string of length {}",
            s.len()
        );
    }
    if s.get(a..b).is_none() {
        return Err(char_boundary_error(s, a, b));
    }
    let mut out = s.to_string();
    out.replace_range(a..b, new);
    Ok(out)
}

pub(super) fn set_index(recv: &Value, key: &Value, v: Value) -> Result<()> {
    match recv {
        Value::Vec(items) => {
            let i = usize::try_from(int_of(key)?)?;
            let mut items = items.lock();
            if i >= items.len() {
                bail!(
                    "index out of bounds: the len is {} but the index is {i}",
                    items.len()
                );
            }
            items[i] = v;
        }
        Value::Map(m, _) => {
            let k = key
                .as_key()
                .ok_or_else(|| anyhow::anyhow!("invalid map key"))?;
            m.lock().insert(k, v);
        }
        _ => bail!("cannot index {}", recv.type_name()),
    }
    Ok(())
}

pub(super) fn eval_try(v: Value) -> Result<Value, Value> {
    match v {
        Value::Enum {
            enum_name,
            variant,
            data,
        } => match (&*enum_name, &*variant) {
            ("Result", "Ok") | ("Option", "Some") => {
                Ok(data.lock().first().cloned().unwrap_or(Value::Unit))
            }
            ("Result", "Err") => {
                let inner = data.lock().first().cloned().unwrap_or(Value::Unit);
                Err(Value::err(inner))
            }
            ("Option", "None") => Err(Value::none()),
            // Any other value acts as its own Some, matching eval_try in
            // eval.rs, see the comment there.
            _ => Ok(Value::Enum {
                enum_name,
                variant,
                data,
            }),
        },
        other => Ok(other),
    }
}

/// Whether a scrutinee lands inside a `lo..hi` pattern, given a comparator
/// against each bound.
pub(super) fn range_matches<L>(
    lo: Option<&L>,
    hi: Option<&L>,
    inclusive: bool,
    cmp: impl Fn(&L) -> Option<Ordering>,
) -> bool {
    if let Some(l) = lo {
        match cmp(l) {
            Some(Ordering::Less | Ordering::Equal) => {}
            _ => return false,
        }
    }
    if let Some(h) = hi {
        match cmp(h) {
            Some(Ordering::Greater) => {}
            Some(Ordering::Equal) if inclusive => {}
            _ => return false,
        }
    }
    true
}