luxc 0.8.3

A small teaching language that runs anywhere and transpiles to Rust, Swift, and Go
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
//! The Rust backend: emit real Rust source.
//!
//! Rust is the closest match to lux's shape — enums with values, `Option` and
//! `Result`, exhaustive `match`, value semantics — so most of the work is
//! cosmetic: `func` becomes `fn`, lux's lowercase enum cases become PascalCase
//! variants, camelCase names become snake_case. The one real wrinkle is that
//! Rust *moves* a non-`Copy` value when you pass it, while lux copies, so a
//! named place argument is cloned at the call site to preserve lux's semantics.

use crate::ast::*;

use super::{
    Ty, Types, bin_prec, escape, format_float, indent, op_str, rust_ident, to_pascal, to_snake,
    ty_from_ann,
};

struct Gen {
    t: Types,
    out: String,
    indent: usize,
    /// `readLine()` reads from the shared stdin handle the same way each call,
    /// which is a few lines; emit it once as a helper when the program asks.
    uses_read_line: bool,
    /// `run` needs the built-in `Output` struct and a helper that spawns a
    /// command, emitted once when the program reaches for it.
    uses_run: bool,
}

/// Reading one line, returning `None` at end of input — the helper `readLine()`
/// lowers to. Pulled out so a loop over input reads as one clean call.
const READ_LINE_HELPER: &str = "\
fn read_line() -> Option<String> {
    let mut line = String::new();
    match std::io::stdin().read_line(&mut line) {
        Ok(0) | Err(_) => None,
        Ok(_) => Some(line.trim_end_matches(['\\n', '\\r']).to_string()),
    }
}
";

/// The built-in `Output` struct and the helper `run` lowers to. Rust's
/// `Command` mirrors lux's shape closely: a launch failure is the `Err`, and the
/// exit code rides inside `Output` on success. The child's input is closed off.
const RUN_HELPER: &str = "\
#[derive(Debug, Clone, PartialEq)]
struct Output {
    status: i64,
    stdout: String,
    stderr: String,
}

fn run(program: String, args: Vec<String>) -> Result<Output, String> {
    match std::process::Command::new(&program)
        .args(&args)
        .stdin(std::process::Stdio::null())
        .output()
    {
        Ok(out) => Ok(Output {
            status: out.status.code().unwrap_or(-1) as i64,
            stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
            stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
        }),
        Err(e) => Err(e.to_string()),
    }
}
";

/// Translate a whole program to Rust source text.
pub fn to_rust(program: &[Stmt]) -> String {
    let mut g = Gen {
        t: Types::new(program),
        out: String::new(),
        indent: 0,
        uses_read_line: false,
        uses_run: false,
    };

    for stmt in program {
        if let Stmt::Struct { name, fields, .. } = stmt {
            g.emit_struct(name, fields);
        }
    }
    for stmt in program {
        if let Stmt::Enum { name, variants, .. } = stmt {
            g.emit_enum(name, variants);
        }
    }
    for stmt in program {
        if let Stmt::Func {
            name,
            params,
            ret,
            body,
            ..
        } = stmt
        {
            g.emit_func(name, params, ret.as_ref(), body);
        }
    }

    g.line("fn main() {".into());
    g.indent += 1;
    g.t.push_scope();
    for stmt in program {
        if !matches!(
            stmt,
            Stmt::Struct { .. } | Stmt::Enum { .. } | Stmt::Func { .. }
        ) {
            g.emit_stmt(stmt);
        }
    }
    g.t.pop_scope();
    g.indent -= 1;
    g.line("}".into());

    let mut preamble = String::new();
    if g.uses_run {
        preamble.push_str(RUN_HELPER);
        preamble.push('\n');
    }
    if g.uses_read_line {
        preamble.push_str(READ_LINE_HELPER);
        preamble.push('\n');
    }
    format!("{}{}", preamble, g.out)
}

/// A lux type as Rust source text.
fn ty_text(t: &Ty) -> String {
    match t {
        Ty::Int => "i64".into(),
        Ty::Float => "f64".into(),
        Ty::Str => "String".into(),
        Ty::Bool => "bool".into(),
        Ty::Array(t) => format!("Vec<{}>", ty_text(t)),
        Ty::User(n) => n.clone(),
        Ty::Option(t) => format!("Option<{}>", ty_text(t)),
        Ty::Result(a, b) => format!("Result<{}, {}>", ty_text(a), ty_text(b)),
        Ty::Range => "std::ops::Range<i64>".into(),
        Ty::Unit => "()".into(),
        Ty::Unknown => "_".into(),
    }
}

/// The natural empty value for a `var` that was declared without one.
fn zero(t: &Ty) -> String {
    match t {
        Ty::Int => "0".into(),
        Ty::Float => "0.0".into(),
        Ty::Bool => "false".into(),
        Ty::Str => "String::new()".into(),
        Ty::Array(_) => "Vec::new()".into(),
        Ty::Option(_) => "None".into(),
        _ => "Default::default()".into(),
    }
}

impl Gen {
    fn line(&mut self, s: String) {
        self.out.push_str(&indent(self.indent));
        self.out.push_str(&s);
        self.out.push('\n');
    }

    fn blank(&mut self) {
        self.out.push('\n');
    }

    // --- declarations ------------------------------------------------------

    fn emit_struct(&mut self, name: &str, fields: &[FieldDef]) {
        self.line("#[derive(Debug, Clone, PartialEq)]".into());
        self.line(format!("struct {} {{", name));
        for f in fields {
            self.line(format!(
                "    {}: {},",
                to_snake(&f.name),
                ty_text(&ty_from_ann(&f.ty))
            ));
        }
        self.line("}".into());
        self.blank();
    }

    fn emit_enum(&mut self, name: &str, variants: &[VariantDef]) {
        self.line("#[derive(Debug, Clone, PartialEq)]".into());
        self.line(format!("enum {} {{", name));
        for v in variants {
            if v.fields.is_empty() {
                self.line(format!("    {},", to_pascal(&v.name)));
            } else {
                let tys: Vec<String> = v
                    .fields
                    .iter()
                    .map(|f| ty_text(&ty_from_ann(&f.ty)))
                    .collect();
                self.line(format!("    {}({}),", to_pascal(&v.name), tys.join(", ")));
            }
        }
        self.line("}".into());
        self.blank();
    }

    fn emit_func(&mut self, name: &str, params: &[Param], ret: Option<&TypeAnn>, body: &[Stmt]) {
        let ps: Vec<String> = params
            .iter()
            .map(|p| {
                format!(
                    "{}: {}",
                    rust_ident(&to_snake(&p.name)),
                    ty_text(&ty_from_ann(&p.ty))
                )
            })
            .collect();
        let r = ret
            .map(|t| format!(" -> {}", ty_text(&ty_from_ann(t))))
            .unwrap_or_default();
        self.line(format!(
            "fn {}({}){} {{",
            rust_ident(&to_snake(name)),
            ps.join(", "),
            r
        ));
        self.indent += 1;
        self.t.push_scope();
        for p in params {
            self.t.declare(p.name.clone(), ty_from_ann(&p.ty));
        }
        for stmt in body {
            self.emit_stmt(stmt);
        }
        self.t.pop_scope();
        self.indent -= 1;
        self.line("}".into());
        self.blank();
    }

    // --- statements --------------------------------------------------------

    fn emit_stmt(&mut self, stmt: &Stmt) {
        match stmt {
            Stmt::Let {
                name, ty, value, ..
            } => self.emit_binding(name, ty.as_ref(), value, false),
            Stmt::Var {
                name,
                ty,
                value: Some(value),
                ..
            } => self.emit_binding(name, ty.as_ref(), value, true),
            Stmt::Var {
                name,
                ty: Some(ann),
                value: None,
                ..
            } => {
                let vty = ty_from_ann(ann);
                let z = zero(&vty);
                let snake = rust_ident(&to_snake(name));
                self.t.declare(name.clone(), vty.clone());
                self.line(format!("let mut {}: {} = {};", snake, ty_text(&vty), z));
            }
            Stmt::Var { value: None, .. } => {} // a var with neither type nor value can't occur
            Stmt::Assign {
                name, op, value, ..
            } => self.emit_assign(name, *op, value),
            Stmt::Return { value, .. } => match value {
                Some(v) => {
                    let e = self.emit_expr(v);
                    self.line(format!("return {};", e));
                }
                None => self.line("return;".into()),
            },
            Stmt::If {
                cond,
                then_body,
                else_body,
                ..
            } => self.emit_if(cond, then_body, else_body.as_deref()),
            Stmt::While { cond, body, .. } => {
                let c = self.emit_expr(cond);
                self.line(format!("while {} {{", c));
                self.block(body);
                self.line("}".into());
            }
            Stmt::For {
                var, iter, body, ..
            } => self.emit_for(var, iter, body),
            Stmt::Expr(e) => {
                let s = self.emit_expr(e);
                self.line(format!("{};", s));
            }
            // Declarations are hoisted to module scope in the top-level pass.
            Stmt::Func {
                name,
                params,
                ret,
                body,
                ..
            } => self.emit_func(name, params, ret.as_ref(), body),
            Stmt::Struct { .. } | Stmt::Enum { .. } => {}
        }
    }

    /// A block that owns its own scope, indented one level.
    fn block(&mut self, body: &[Stmt]) {
        self.indent += 1;
        self.t.push_scope();
        for stmt in body {
            self.emit_stmt(stmt);
        }
        self.t.pop_scope();
        self.indent -= 1;
    }

    fn emit_binding(&mut self, name: &str, ann: Option<&TypeAnn>, value: &Expr, mutable: bool) {
        let snake = rust_ident(&to_snake(name));
        let vty = ann
            .map(ty_from_ann)
            .unwrap_or_else(|| self.t.type_of(value));
        // A bare `none` (or anything that leaves its type open) carries no type
        // of its own, so when the source named one, write it down for Rust.
        let value_open = self.t.type_of(value).has_unknown();
        let annotate = !vty.has_unknown() && ((ann.is_some() && value_open) || vty.has_int());
        let kw = if mutable { "let mut" } else { "let" };
        let expr = self.emit_expr(value);
        if annotate {
            self.line(format!("{} {}: {} = {};", kw, snake, ty_text(&vty), expr));
        } else {
            self.line(format!("{} {} = {};", kw, snake, expr));
        }
        self.t.declare(name.to_string(), vty);
    }

    fn emit_assign(&mut self, name: &str, op: AssignOp, value: &Expr) {
        let snake = rust_ident(&to_snake(name));
        let lty = self.t.lookup(name);
        match op {
            AssignOp::Set => {
                let e = self.emit_expr(value);
                self.line(format!("{} = {};", snake, e));
            }
            AssignOp::Add => match lty {
                Ty::Str => {
                    // lux `+=` on a string appends text.
                    if let Expr::Str(s, _) = value {
                        self.line(format!("{}.push_str(\"{}\");", snake, escape(s)));
                    } else {
                        let e = self.emit_expr(value);
                        self.line(format!("{}.push_str(&{});", snake, e));
                    }
                }
                Ty::Array(_) => {
                    // lux `+=` on an array appends one element.
                    let e = self.emit_expr(value);
                    self.line(format!("{}.push({});", snake, e));
                }
                _ => {
                    let e = self.emit_expr(value);
                    self.line(format!("{} += {};", snake, e));
                }
            },
            AssignOp::Sub => {
                let e = self.emit_expr(value);
                self.line(format!("{} -= {};", snake, e));
            }
        }
    }

    fn emit_if(&mut self, cond: &Expr, then_body: &[Stmt], mut els: Option<&[Stmt]>) {
        let c = self.emit_expr(cond);
        self.line(format!("if {} {{", c));
        self.block(then_body);
        loop {
            match els {
                None => {
                    self.line("}".into());
                    break;
                }
                // A lone nested `if` is an `else if` — chain it on one line.
                Some(e) if e.len() == 1 && matches!(e[0], Stmt::If { .. }) => {
                    if let Stmt::If {
                        cond,
                        then_body,
                        else_body,
                        ..
                    } = &e[0]
                    {
                        let c = self.emit_expr(cond);
                        self.line(format!("}} else if {} {{", c));
                        self.block(then_body);
                        els = else_body.as_deref();
                    }
                }
                Some(e) => {
                    self.line("} else {".into());
                    self.block(e);
                    self.line("}".into());
                    break;
                }
            }
        }
    }

    fn emit_for(&mut self, var: &str, iter: &Expr, body: &[Stmt]) {
        let svar = to_snake(var);
        let (iter_str, elem_ty) = match self.t.type_of(iter) {
            Ty::Range => (self.emit_expr(iter), Ty::Int),
            Ty::Array(t) => {
                let base = self.emit_expr(iter);
                // Borrow the array and clone each element, so the loop never
                // consumes what it walks and the body gets owned values.
                (format!("{}.iter().cloned()", base), *t)
            }
            _ => (self.emit_expr(iter), Ty::Unknown),
        };
        self.line(format!("for {} in {} {{", svar, iter_str));
        self.indent += 1;
        self.t.push_scope();
        self.t.declare(var.to_string(), elem_ty);
        for stmt in body {
            self.emit_stmt(stmt);
        }
        self.t.pop_scope();
        self.indent -= 1;
        self.line("}".into());
    }

    // --- expressions -------------------------------------------------------

    fn emit_expr(&mut self, e: &Expr) -> String {
        match e {
            Expr::Int(n, _) => n.to_string(),
            Expr::Float(f, _) => format_float(*f),
            Expr::Str(s, _) => format!("\"{}\".to_string()", escape(s)),
            Expr::Bool(b, _) => b.to_string(),
            Expr::Ident(name, _) => {
                if name == "none" {
                    "None".to_string()
                } else {
                    rust_ident(&to_snake(name))
                }
            }
            Expr::Array(els, _) => {
                let parts: Vec<String> = els.iter().map(|x| self.emit_expr(x)).collect();
                format!("vec![{}]", parts.join(", "))
            }
            Expr::Unary { op, rhs, .. } => {
                // Unary binds tighter than any binary operator, so a binary
                // operand needs parentheses: `-(a + b)`, not `-a + b`.
                let r = if matches!(**rhs, Expr::Binary { .. }) {
                    let inner = self.emit_expr(rhs);
                    format!("({})", inner)
                } else {
                    self.emit_expr(rhs)
                };
                match op {
                    UnOp::Neg => format!("-{}", r),
                    UnOp::Not => format!("!{}", r),
                }
            }
            Expr::Binary { op, lhs, rhs, .. } => {
                if *op == BinOp::Add && self.t.type_of(lhs) == Ty::Str {
                    let l = self.display_arg(lhs);
                    let r = self.display_arg(rhs);
                    format!("format!(\"{{}}{{}}\", {}, {})", l, r)
                } else {
                    let p = bin_prec(*op);
                    let l = self.emit_child(lhs, p, false);
                    let r = self.emit_child(rhs, p, true);
                    format!("{} {} {}", l, op_str(*op), r)
                }
            }
            Expr::Index { base, index, .. } => {
                let b = self.emit_expr(base);
                let idx = if let Expr::Int(n, _) = **index {
                    n.to_string()
                } else {
                    let e = self.emit_expr(index);
                    format!("({}) as usize", e)
                };
                format!("{}[{}]", b, idx)
            }
            Expr::Range { start, end, .. } => {
                let s = self.emit_expr(start);
                let e = self.emit_expr(end);
                format!("{}..{}", s, e)
            }
            Expr::Call { name, args, .. } => self.emit_call(name, args),
            Expr::StructLit { name, fields, .. } => {
                let parts: Vec<String> = fields
                    .iter()
                    .map(|(k, v)| {
                        let val = self.emit_expr(v);
                        format!("{}: {}", to_snake(k), val)
                    })
                    .collect();
                format!("{} {{ {} }}", name, parts.join(", "))
            }
            Expr::EnumLit {
                enum_name,
                variant,
                fields,
                ..
            } => self.emit_enum_lit(enum_name, variant, fields),
            Expr::Field { base, field, .. } => {
                // `Shape.dot` parses as a field access but is a payload-less
                // enum case — emit it as construction.
                if let Expr::Ident(n, _) = &**base
                    && let Some(variants) = self.t.env.enums.get(n)
                    && variants.iter().any(|v| v.name == *field)
                {
                    return format!("{}::{}", n, to_pascal(field));
                }
                let b = self.emit_expr(base);
                format!("{}.{}", b, to_snake(field))
            }
            Expr::Match {
                scrutinee, arms, ..
            } => self.emit_match(scrutinee, arms),
        }
    }

    /// Emit a binary operand, wrapping it in parentheses only when its operator
    /// binds more loosely than the parent's. The right operand of a left-
    /// associative operator also needs them at equal precedence (`a - (b - c)`).
    fn emit_child(&mut self, e: &Expr, parent: u8, is_right: bool) -> String {
        let s = self.emit_expr(e);
        if let Expr::Binary { op, .. } = e {
            let p = bin_prec(*op);
            let wrap = if is_right { p <= parent } else { p < parent };
            if wrap {
                return format!("({})", s);
            }
        }
        s
    }

    /// A user-function argument. lux passes values by copy and the caller keeps
    /// its own, so a named value of a non-`Copy` type is cloned at the call
    /// site to preserve that — exactly what lux does under the hood.
    fn emit_call_arg(&mut self, a: &Expr) -> String {
        let clone = !is_copy(&self.t.type_of(a)) && is_place(a);
        let s = self.emit_expr(a);
        if clone { format!("{}.clone()", s) } else { s }
    }

    /// An argument in `print` or string concatenation, where a bare string
    /// literal can stay a clean `&str` instead of an owned `String`.
    fn display_arg(&mut self, e: &Expr) -> String {
        if let Expr::Str(s, _) = e {
            format!("\"{}\"", escape(s))
        } else {
            self.emit_expr(e)
        }
    }

    /// `print` and `eprint` differ only in the macro they reach for — one writes
    /// stdout, the other stderr — so they share how arguments become a format.
    fn println_call(&mut self, mac: &str, args: &[Expr]) -> String {
        let mut fmt = String::new();
        let mut parts = Vec::new();
        for (i, a) in args.iter().enumerate() {
            if i > 0 {
                fmt.push(' ');
            }
            // `{:?}` on an f64 keeps the decimal point (`9.0`), matching how lux
            // prints floats; plain scalars use `{}`.
            let ty = self.t.type_of(a);
            fmt.push_str(if ty == Ty::Float || !ty.is_scalar() {
                "{:?}"
            } else {
                "{}"
            });
            parts.push(self.display_arg(a));
        }
        if parts.is_empty() {
            format!("{}!()", mac)
        } else {
            format!("{}!(\"{}\", {})", mac, fmt, parts.join(", "))
        }
    }

    fn emit_call(&mut self, name: &str, args: &[Expr]) -> String {
        match name {
            "print" => self.println_call("println", args),
            "eprint" => self.println_call("eprintln", args),
            // Each fallible call turns the target's native error into a string, so
            // the lux source stays `Result<_, string>` on this side too.
            "readFile" => {
                let p = self.emit_call_arg(&args[0]);
                format!("std::fs::read_to_string({}).map_err(|e| e.to_string())", p)
            }
            "writeFile" => {
                let p = self.emit_call_arg(&args[0]);
                let c = self.emit_call_arg(&args[1]);
                format!("std::fs::write({}, {}).map_err(|e| e.to_string())", p, c)
            }
            "args" => "std::env::args().collect::<Vec<String>>()".to_string(),
            "readLine" => {
                self.uses_read_line = true;
                "read_line()".to_string()
            }
            "run" => {
                self.uses_run = true;
                let p = self.emit_call_arg(&args[0]);
                let a = self.emit_call_arg(&args[1]);
                format!("run({}, {})", p, a)
            }
            "string" => {
                // `{:?}` keeps a whole float's decimal point, the way lux's
                // `string(2.0)` yields "2.0" rather than "2".
                let is_float = self.t.type_of(&args[0]) == Ty::Float;
                let e = self.emit_expr(&args[0]);
                if is_float {
                    format!("format!(\"{{:?}}\", {})", e)
                } else {
                    format!("({}).to_string()", e)
                }
            }
            "int" => {
                let inner = self.t.type_of(&args[0]);
                let e = self.emit_expr(&args[0]);
                match inner {
                    Ty::Int => e,
                    _ => format!("({}) as i64", e),
                }
            }
            "float" => {
                let inner = self.t.type_of(&args[0]);
                let e = self.emit_expr(&args[0]);
                match inner {
                    Ty::Float => e,
                    _ => format!("({}) as f64", e),
                }
            }
            // `.ok()` turns parse's Result into the Option lux returns.
            "parseInt" => {
                let e = self.emit_call_arg(&args[0]);
                format!("{}.trim().parse::<i64>().ok()", e)
            }
            "parseFloat" => {
                let e = self.emit_call_arg(&args[0]);
                format!("{}.trim().parse::<f64>().ok()", e)
            }
            "length" => {
                let inner = self.t.type_of(&args[0]);
                let e = self.emit_expr(&args[0]);
                if inner == Ty::Str {
                    format!("({}).chars().count() as i64", e)
                } else {
                    format!("({}).len() as i64", e)
                }
            }
            "some" => {
                let e = self.emit_expr(&args[0]);
                format!("Some({})", e)
            }
            "ok" => {
                let e = self.emit_expr(&args[0]);
                format!("Ok({})", e)
            }
            "err" => {
                let e = self.emit_expr(&args[0]);
                format!("Err({})", e)
            }
            _ => {
                let parts: Vec<String> = args.iter().map(|a| self.emit_call_arg(a)).collect();
                format!("{}({})", rust_ident(&to_snake(name)), parts.join(", "))
            }
        }
    }

    fn emit_enum_lit(
        &mut self,
        enum_name: &str,
        variant: &str,
        fields: &[(String, Expr)],
    ) -> String {
        // Tuple variants are positional, so emit the values in the order the
        // enum declared its fields, not the order they were written.
        let order: Option<Vec<String>> = self.t.env.enums.get(enum_name).and_then(|variants| {
            variants
                .iter()
                .find(|v| v.name == variant)
                .map(|v| v.fields.iter().map(|f| f.name.clone()).collect())
        });
        let args: Vec<String> = match order {
            Some(names) => names
                .iter()
                .map(|fname| {
                    let expr = fields.iter().find(|(k, _)| k == fname).map(|(_, e)| e);
                    match expr {
                        Some(e) => self.emit_expr(e),
                        None => "()".to_string(),
                    }
                })
                .collect(),
            None => fields.iter().map(|(_, e)| self.emit_expr(e)).collect(),
        };
        if args.is_empty() {
            format!("{}::{}", enum_name, to_pascal(variant))
        } else {
            format!("{}::{}({})", enum_name, to_pascal(variant), args.join(", "))
        }
    }

    fn emit_match(&mut self, scrutinee: &Expr, arms: &[MatchArm]) -> String {
        let base = self.indent;
        let ind = indent(base);
        let ind1 = indent(base + 1);
        let st = self.t.type_of(scrutinee);
        let needs_as_str = arms.iter().any(|a| matches!(a.pattern, Pattern::Str(..)));
        let scrut = if needs_as_str {
            let s = self.emit_expr(scrutinee);
            format!("{}.as_str()", s)
        } else {
            self.emit_expr(scrutinee)
        };
        let mut s = format!("match {} {{\n", scrut);
        for arm in arms {
            let pat = self.emit_pattern(&arm.pattern, &st);
            // Bring the pattern's captures into scope so the arm body types
            // correctly (a captured string should print without quotes).
            self.t.push_scope();
            self.declare_bindings(&arm.pattern, &st);
            // Arm bodies sit one level in, so a nested match nests cleanly.
            self.indent = base + 1;
            let body = self.emit_expr(&arm.body);
            self.indent = base;
            self.t.pop_scope();
            s.push_str(&format!("{}{} => {},\n", ind1, pat, body));
        }
        s.push_str(&format!("{}}}", ind));
        s
    }

    fn emit_pattern(&mut self, pat: &Pattern, st: &Ty) -> String {
        match pat {
            Pattern::Wildcard(_) => "_".to_string(),
            Pattern::Int(n, _) => n.to_string(),
            Pattern::Str(s, _) => format!("\"{}\"", escape(s)),
            Pattern::Bool(b, _) => b.to_string(),
            Pattern::Variant { name, bindings, .. } => {
                let binds: Vec<String> = bindings.iter().map(|b| to_snake(b)).collect();
                let inner = if binds.is_empty() {
                    String::new()
                } else {
                    format!("({})", binds.join(", "))
                };
                match st {
                    Ty::Option(_) => match name.as_str() {
                        "some" => format!("Some{}", paren_or_empty(&binds)),
                        _ => "None".to_string(),
                    },
                    Ty::Result(_, _) => match name.as_str() {
                        "ok" => format!("Ok{}", paren_or_empty(&binds)),
                        _ => format!("Err{}", paren_or_empty(&binds)),
                    },
                    Ty::User(en) => format!("{}::{}{}", en, to_pascal(name), inner),
                    _ => format!("{}{}", to_pascal(name), inner),
                }
            }
        }
    }

    /// Record the types of a pattern's captures in the current scope.
    fn declare_bindings(&mut self, pat: &Pattern, st: &Ty) {
        let Pattern::Variant { name, bindings, .. } = pat else {
            return;
        };
        let types: Vec<Ty> = match st {
            Ty::Option(t) if name == "some" => vec![(**t).clone()],
            Ty::Result(o, _) if name == "ok" => vec![(**o).clone()],
            Ty::Result(_, e) if name == "err" => vec![(**e).clone()],
            Ty::User(en) => self
                .t
                .env
                .enums
                .get(en)
                .and_then(|vs| vs.iter().find(|v| v.name == *name))
                .map(|v| v.fields.iter().map(|f| ty_from_ann(&f.ty)).collect())
                .unwrap_or_default(),
            _ => Vec::new(),
        };
        for (b, t) in bindings.iter().zip(types) {
            self.t.declare(b.clone(), t);
        }
    }
}

fn paren_or_empty(binds: &[String]) -> String {
    if binds.is_empty() {
        String::new()
    } else {
        format!("({})", binds.join(", "))
    }
}

/// Types that copy on use in Rust, so passing them never moves the original.
fn is_copy(t: &Ty) -> bool {
    matches!(t, Ty::Int | Ty::Float | Ty::Bool)
}

/// A "place" — a named value that could still be used after a call, as opposed
/// to a fresh temporary like a literal or another call's result.
fn is_place(e: &Expr) -> bool {
    match e {
        Expr::Ident(n, _) => n != "none",
        Expr::Field { .. } | Expr::Index { .. } => true,
        _ => false,
    }
}