vox-lang 0.4.4

A systems level compiler for Vox (sentence based code)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
use crate::errors::SourceLocation;

#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq)]
pub enum Type {
    Integer,
    Float,
    String,
    Boolean,
    List(Box<Type>),
    Map(Box<Type>), // key/value collection (tag 5); inner type is the value type, keys are always text
    Buffer,
    File,
    Time,
    Timer,
    Value,
    // A user-defined thing (plan 310): the payload is the thing's name as
    // written in `A thing called <name> has ...`. Layout, size, and field
    // offsets are resolved from the `ThingDef` registry at compile time;
    // the type itself carries only the name.
    Thing(String),
    Void,
    Unknown,
}

/// One user-defined composite type, as written in a definition construct
/// (plan 310 §1). Built by the parser and carried on the `Program` so the
/// analyzer and codegen can compute layout without re-parsing.
///
/// Function members take no storage - they are the type's declared
/// callable API (the manifest, plan 310 §4), so `fields` and `members` are
/// deliberately separate lists: everything sensitive to layout (size,
/// offsets, copying, printing, equality) reads `fields` alone.
///
/// `allow(dead_code)`: definition parsing lands ahead of the declaration,
/// field-access, and codegen work that reads the registry, so these fields
/// are written but not yet read. Same treatment as `Type` and the other
/// ahead-of-consumer shapes in this file.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ThingDef {
    pub name: String,
    /// Data fields only, in definition order (which is layout order).
    pub fields: Vec<FieldDef>,
    /// Manifest function-member names, in definition order.
    pub members: Vec<String>,
    /// 1-based source line of the `A thing called <name> has` opener, so a
    /// later duplicate definition can point back at this one.
    pub line: usize,
}

/// One data field of a `ThingDef`. `allow(dead_code)` for the same reason
/// as `ThingDef`: written by the parser, read once layout work lands.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct FieldDef {
    pub name: String,
    /// A builtin type noun, or `Type::Thing(name)` for a nested thing.
    pub field_type: Type,
    /// The literal written after `is`, when the field declares a default.
    /// `None` means the field takes its type's zero/empty value.
    pub default: Option<Expr>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum FlagValueType {
    Boolean,
    Number,
    Text,
}

#[derive(Debug, Clone, PartialEq)]
pub enum FileMode {
    Reading,
    Writing,
    Appending,
}

#[derive(Debug, Clone)]
pub enum Expr {
    IntegerLit(i64),
    FloatLit(f64),
    StringLit(String),
    BoolLit(bool),
    // The nothing/null literal (stage 1e3, tag 6). Unit variant: the
    // payload is always 0 and the tag is always TAG_NOTHING (6), so it
    // carries no data. Spelled `nothing`, `null`, or `nil` in source.
    NothingLit,
    Identifier(String),
    
    BinaryOp {
        left: Box<Expr>,
        op: BinaryOperator,
        right: Box<Expr>,
    },
    
    UnaryOp {
        op: UnaryOperator,
        operand: Box<Expr>,
    },
    
    Range {
        start: Box<Expr>,
        end: Box<Expr>,
        inclusive: bool,
    },
    
    PropertyCheck {
        value: Box<Expr>,
        property: Property,
    },

    // Runtime type predicate: `item is a text` / `is a number` / etc.
    // `type_noun` is constrained by the parser to Integer/Float/String/Boolean.
    // Negation (`is not a text`) wraps this in `UnaryOp { Not, .. }`.
    TypeCheck {
        value: Box<Expr>,
        type_noun: Type,
    },

    FunctionCall {
        name: String,
        args: Vec<Expr>,
    },
    
    ListLit {
        elements: Vec<Expr>,
    },

    // Map literal: {"key": value, ...}. Each pair is (key_expr, value_expr);
    // keys must be text. JSON-object syntax (stage 1e2, tag 5).
    MapLit {
        pairs: Vec<(Expr, Expr)>,
    },

    #[allow(dead_code)]
    ListAccess {
        list: Box<Expr>,
        index: Box<Expr>,
    },
    
    // Property access: buffer's size, buffer's capacity
    PropertyAccess {
        object: String,
        property: ObjectProperty,
    },

    // A field of a user-defined thing (plan 310 §3): `origin's x`, and
    // through nesting to any depth, `route's leg's start's x`. `base` is the
    // thing variable's name; `path` is the field names in order, outermost
    // first. Every step is a compile-time offset, so the whole chain folds
    // into one `base_address + constant` - there is no pointer chase and no
    // runtime failure path (unlike list element access).
    ThingField {
        base: String,
        path: Vec<String>,
    },

    // Map key access: person's "name". `map` is the variable name (like
    // PropertyAccess.object); `key` is an expression that evaluates to a
    // text value (usually a StringLit). Tag of the found value travels in
    // r11 at codegen, mirroring ElementAccess. (stage 1e2, tag 5)
    MapAccess {
        map: String,
        key: Box<Expr>,
    },

    // The last error value
    #[allow(dead_code)]
    LastError,
    
    // Command-line arguments
    ArgumentCount,
    ArgumentAt {
        index: Box<Expr>,
    },
    ArgumentName,       // argv[0] - program name
    ArgumentFirst,      // argv[1] - first user arg
    ArgumentSecond,     // argv[2] - second user arg
    ArgumentLast,       // last user argument (or program name if no args)
    ArgumentEmpty,      // true if argc <= 1 (no user args)
    ArgumentAll,        // all user arguments as a list (argv[1..])
    ArgumentRaw,        // raw user arguments as a list (argv[1..], unfiltered)
    ArgumentHas {
        value: Box<Expr>,
    },
    
    // Inline substitution: expr treating "X" as "Y"
    TreatingAs {
        value: Box<Expr>,
        match_value: Box<Expr>,
        replacement: Box<Expr>,
    },
    
    // Environment variables
    EnvironmentVariable {
        name: Box<Expr>,
    },
    EnvironmentVariableCount,
    EnvironmentVariableAt {
        index: Box<Expr>,
    },
    EnvironmentVariableExists {
        name: Box<Expr>,
    },
    EnvironmentVariableFirst,   // first env var
    EnvironmentVariableLast,    // last env var
    EnvironmentVariableEmpty,   // true if no env vars
    
    // Time expressions
    CurrentTime,                // current time value
    Fork,                       // fork() - 0 in child, child pid in parent, negative on error
    ReapChild {                 // wait4() - reap a child process, returns its pid (or -1 on error)
        pid: Option<Box<Expr>>, // None = any child (pid -1); Some(expr) = a specific pid
        no_hang: bool,          // plan 311: true = WNOHANG (non-blocking); false = blocking
    },
    // plan 311: the raw wait4 status word from the most recent successful reap.
    // -1 sentinel before any reap. Decoding lives in lib/process.vox, not here.
    ReapedStatus,
    
    // Type casting
    Cast {
        value: Box<Expr>,
        target_type: Type,
        radix: u32, // base for string->integer casts (2, 8, 10, or 16); ignored otherwise
    },
    
    // Duration cast (timer's duration in seconds)
    DurationCast {
        value: Box<Expr>,
        unit: TimeUnit,
    },
    
    // Byte access: byte N of buffer
    ByteAccess {
        buffer: Box<Expr>,
        index: Box<Expr>,
    },

    // Element access: element N of list
    ElementAccess {
        list: Box<Expr>,
        index: Box<Expr>,
    },

    // Format string: "Hello {name}, you are {age} years old"
    FormatString {
        parts: Vec<FormatPart>,
    },

    // File availability check: path is available
    FileAvailable {
        path: Box<Expr>,
    },
}

#[derive(Debug, Clone)]
pub enum FormatPart {
    Literal(String),
    Variable { name: String, format: Option<String> },
    Expression { expr: Box<Expr>, format: Option<String> },
}

#[derive(Debug, Clone)]
pub enum TimeUnit {
    Seconds,
    Milliseconds,
}

#[derive(Debug, Clone)]
pub enum BinaryOperator {
    Add, Subtract, Multiply, Divide, Modulo,
    Equal, NotEqual, Greater, Less, GreaterEqual, LessEqual,
    And, Or,
    // Bitwise operators
    BitAnd, BitOr, BitXor, ShiftLeft, ShiftRight,
}

#[derive(Debug, Clone)]
pub enum UnaryOperator {
    Negate,
    Not,
}

#[derive(Debug, Clone)]
pub enum Property {
    Even,
    Odd,
    Positive,
    Negative,
    Zero,
    Empty,
}

#[derive(Debug, Clone)]
pub enum ObjectProperty {
    // Buffer properties
    Size,      // buffer's size (current length)
    Capacity,  // buffer's capacity (max size)
    Empty,     // buffer's empty (size == 0)
    Full,      // buffer's full (size == capacity)
    
    // File properties
    Descriptor,  // file's descriptor (fd number)
    Modified,    // file's modified (mtime)
    Accessed,    // file's accessed (atime)
    Permissions, // file's permissions (mode bits)
    Readable,    // file's readable
    Writable,    // file's writable
    
    // List properties
    First,     // list's first item
    Last,      // list's last item

    // Map properties (stage 1e2)
    Keys,      // map's keys   -> a list of key texts (insertion order)
    Values,    // map's values -> a list of values with their tags (insertion order)
    
    // Number properties
    Absolute,  // number's absolute value
    Sign,      // number's sign (-1, 0, 1)
    Even,      // number's even
    Odd,       // number's odd
    Positive,  // number's positive
    Negative,  // number's negative
    Zero,      // number's zero
    
    // Time properties
    Hour,      // time's hour (0-23)
    Minute,    // time's minute (0-59)
    Second,    // time's second (0-59)
    Day,       // time's day (1-31)
    Month,     // time's month (1-12)
    Year,      // time's year
    Unix,      // time's unix timestamp
    
    // Timer properties
    Duration,   // timer's duration
    Elapsed,    // timer's elapsed time
    StartTime,  // timer's start time
    EndTime,    // timer's end time
    Running,    // timer's running status

    // Universal property: every variable reports its type as text.
    Type,       // x's type -> "Number (static)" / "Text (dynamic)" etc.
}

#[derive(Debug, Clone)]
pub enum Statement {
    Print {
        value: Expr,
        without_newline: bool,
    },
    
    VarDecl {
        name: String,
        var_type: Option<Type>,
        value: Option<Expr>,
    },

    // A user-defined thing definition (plan 310 §1). Declares a type, not a
    // variable: it allocates nothing and emits no code. It stays in the
    // statement stream so a definition keeps its source position relative to
    // the uses that must follow it.
    ThingDecl(ThingDef),

    // Write to a field of a user-defined thing (plan 310 §3): `Set origin's x
    // to 3.`, the bare `origin's x is 3.`, and `increment origin's x.` (which
    // the parser desugars into this statement with a `+ 1` value, since the
    // target is an offset, not a name). The read counterpart is
    // `Expr::ThingField`, and `base`/`path` mean exactly the same there.
    SetThingField {
        base: String,
        path: Vec<String>,
        value: Expr,
    },

    FlagSchemaDecl {
        name: String,
        short: String,
        long: String,
        value_type: FlagValueType,
        required: bool,
        default: Option<Expr>,
    },

    ParseFlags,
    
    Assignment {
        name: String,
        value: Expr,
    },

    // In-place cast/retype of a `value` variable: `<name> is a <type>.`.
    // Statement position only; the same phrase in condition position is a
    // TypeCheck predicate.
    ValueRetype {
        name: String,
        target_type: Type,
    },

    If {
        condition: Expr,
        then_block: Vec<Statement>,
        else_if_blocks: Vec<(Expr, Vec<Statement>)>,
        else_block: Option<Vec<Statement>>,
    },
    
    While {
        condition: Expr,
        body: Vec<Statement>,
    },
    
    ForRange {
        variable: String,
        range: Expr,
        body: Vec<Statement>,
    },
    
    ForEach {
        variable: String,
        collection: Expr,
        body: Vec<Statement>,
    },
    
    Repeat {
        count: Expr,
        body: Vec<Statement>,
    },
    
    Break,
    Continue,
    
    Exit {
        code: Expr,
    },
    
    Return {
        value: Option<Expr>,
        // The type written in `Return a <type>, ...`, when present. Carried
        // on the statement itself (rather than only being consulted where
        // the statement is parsed) so that whichever code assembles the
        // enclosing function's `FunctionDef.return_type` can read it back
        // regardless of where in the body this Return sits.
        declared_type: Option<Type>,
    },
    
    FunctionDef {
        name: String,
        params: Vec<(String, Type)>,
        #[allow(dead_code)]
        return_type: Type,
        body: Vec<Statement>,
        // Set when a blank line (paragraph break) force-closed this
        // function's body early, per the "blank line closes all open
        // clauses" rule. Consulted by the analyzer to explain otherwise
        // confusing errors in the top-level statements that follow.
        body_ended_early: Option<SourceLocation>,
        // Set to the location of a body-level `Return` (one that isn't the
        // function's first statement - "Gate B") when IT closed the body,
        // rather than a blank line. Statements written after it in source
        // are silently promoted to top-level entry code (plan 318 §2) - if
        // one of them is itself a `Return`, the analyzer's "Return is only
        // valid inside a function" error consults this to explain why.
        body_ended_via_return: Option<SourceLocation>,
    },
    
    FunctionCall {
        name: String,
        args: Vec<Expr>,
    },
    
    Allocate {
        name: String,
        size: Expr,
    },
    
    Free {
        name: String,
    },
    
    Increment {
        name: String,
    },
    
    Decrement {
        name: String,
    },
    
    // File I/O statements
    BufferDecl {
        name: String,
        size: Expr,
    },
    
    // Set byte N of buffer to value (1-indexed)
    ByteSet {
        buffer: String,
        index: Expr,
        value: Expr,
    },
    
    // Set element N of list to value (1-indexed)
    ElementSet {
        list: String,
        index: Expr,
        value: Expr,
    },

    // Set map's "<key>" to value: insert or replace. The map may reallocate
    // on growth; codegen stores the returned pointer back into the variable
    // (mirroring ListAppend). (stage 1e2, tag 5)
    MapSet {
        map: String,
        key: Expr,
        value: Expr,
    },
    
    // Append value to list
    ListAppend {
        list: String,
        value: Expr,
    },

    // Copy buffer contents into another buffer (clobber destination)
    BufferCopy {
        source: Expr,
        destination: String,
    },

    // Clear buffer contents (set length to zero, preserve capacity)
    BufferClear {
        name: String,
    },
    
    FileOpen {
        name: String,
        path: Expr,
        mode: FileMode,
    },
    
    FileRead {
        source: String,      // file name or "stdin"
        buffer: String,
    },

    FileReadLine {
        source: String,      // file name or "stdin"
        buffer: String,
    },

    FileSeekLine {
        file: String,
        line: Expr,          // 1-indexed line number
    },

    FileSeekByte {
        file: String,
        byte: Expr,          // 1-indexed byte position
    },
    
    FileWrite {
        file: String,
        value: Expr,
    },
    
    FileWriteNewline {
        file: String,
    },
    
    FileClose {
        file: String,
    },
    
    FileDelete {
        path: Expr,
    },

    Rmdir {
        path: Expr,
    },
    
    // Error handling - actions are comma-separated within the sentence
    OnError {
        actions: Vec<Statement>,
    },
    
    // Buffer resize
    BufferResize {
        name: String,
        new_size: Expr,
    },
    
    // Library declaration (for library authors)
    LibraryDecl {
        name: String,
        version: String,
    },
    
    // See/import statement (for library users)
    See {
        path: String,
        lib_name: Option<String>,
        lib_version: Option<String>,
    },
    
    // Time and Timer statements
    TimerDecl {
        name: String,
    },
    
    TimerStart {
        name: String,
    },
    
    TimerStop {
        name: String,
    },
    
    Wait {
        duration: Expr,
        unit: TimeUnit,
    },
    
    GetTime {
        into: String,
    },

    // Filesystem operations
    Mkdir {
        path: Expr,
    },

    Chdir {
        path: Expr,
    },

    Symlink {
        target: Expr,
        linkpath: Expr,
    },

    // Create device node: mknod(path, mode, dev)
    Mknod {
        path: Expr,
        node_type: DeviceNodeType,
        major: Expr,
        minor: Expr,
    },

    Mount {
        source: Expr,
        target: Expr,
        fstype: Expr,
        options: Option<Expr>,
    },

    Unmount {
        target: Expr,
        /// true = MNT_DETACH (lazy unmount, succeeds even while busy)
        lazy: bool,
    },

    /// reboot(2) with LINUX_REBOOT_CMD_POWER_OFF (syncs filesystems first)
    Shutdown,

    /// reboot(2) with LINUX_REBOOT_CMD_RESTART (syncs filesystems first)
    Reboot,

    /// reboot(2) with LINUX_REBOOT_CMD_HALT (syncs filesystems first)
    Halt,

    PivotRoot {
        new_root: Expr,
        put_old: Expr,
    },

    // execve(path, argv, envp) - argv is built as [path, args..., NULL]
    // envp is always the process's own inherited environment (_envp)
    Execute {
        path: Expr,
        args: Expr, // expected to be an Expr::ListLit
    },

    // kill(2): "Send signal <N> to process <pid>." / "... to child <pid>."
    // rdi = pid, rsi = signal. Sets _last_error on failure, clears on success.
    SendSignal {
        signal: Expr,
        pid: Expr,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceNodeType {
    Character, // 'c' - requires CAP_MKNOD/root on real hardware
    Block,     // 'b' - requires CAP_MKNOD/root on real hardware
    Fifo,      // 'p' - named pipe, no special privilege required
}

#[derive(Debug, Clone)]
pub struct Program {
    pub statements: Vec<Statement>,
    pub uses_heap: bool,
    pub uses_strings: bool,
    pub uses_io: bool,
    pub uses_args: bool,
    /// Every thing defined in this program, in definition order. The parser
    /// fills this from its own registry after a successful parse; consumers
    /// look layout up here rather than walking the statement list.
    pub things: Vec<ThingDef>,
}

impl Program {
    pub fn new(statements: Vec<Statement>) -> Self {
        // `things` is DERIVED here, not filled in by the caller, so every
        // construction path populates it. The `--shared` driver builds a
        // Program directly from the combined statements of several inputs
        // (src/main.rs), bypassing the parser's own post-parse derivation -
        // which left `things` empty for a multi-input build, so every
        // consumer of the registry (layout, offsets, cycle checks) silently
        // saw no things at all.
        let things = statements
            .iter()
            .filter_map(|s| match s {
                Statement::ThingDecl(def) => Some(def.clone()),
                _ => None,
            })
            .collect();
        Program {
            statements,
            uses_heap: false,
            uses_strings: false,
            uses_io: false,
            uses_args: false,
            things,
        }
    }
}

/// What kind of definitely-declared name this is, so consumers can route
/// it into their type-specific tracking (buffer/list/file sets).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DefiniteDeclKind {
    Plain,
    Buffer,
    List,
    Map,
    File,
}

/// Names that are DEFINITELY declared by the time this statement sequence
/// finishes, regardless of which control-flow path ran: unconditional
/// declarations, plus - for if/otherwise chains that have an else branch -
/// the intersection of what every branch declares. A name declared in only
/// some branches is not definite (the analyzer's guard tracking owns those),
/// and loop bodies never count (they may run zero times). Function bodies
/// are their own scope and are never entered.
///
/// Shared by the analyzer (function-visible globals) and codegen (bss
/// mirror labels) so the two can never disagree about which main-line
/// declarations behave as globals.
pub fn collect_definite_decls(stmts: &[Statement]) -> std::collections::HashMap<String, DefiniteDeclKind> {
    let mut out = std::collections::HashMap::new();
    // A name whose recognised occurrences disagree on kind (e.g. `a text
    // called src is "hello".` followed later by `open ... called src`,
    // which this function would otherwise see as `Plain` then `File`) is
    // poisoned: removed from `out` and never re-added, rather than letting
    // whichever occurrence is scanned last silently win. That "last write
    // wins" used to pre-register the *later* kind (into
    // buffer_variables/list_variables/map_variables/file_variables) before
    // the analyzer's own per-statement, declaration-order-respecting
    // type-immutability check ever ran - masking the real conflict instead
    // of surfacing it (plan 294 finding 3). A poisoned name is simply
    // absent from the definite-decl map; the analyzer's linear walk still
    // sees and rejects the conflict when it reaches the second occurrence,
    // it just does so without this pre-pass having pre-judged the type.
    let mut poisoned: std::collections::HashSet<String> = std::collections::HashSet::new();

    fn record(
        out: &mut std::collections::HashMap<String, DefiniteDeclKind>,
        poisoned: &mut std::collections::HashSet<String>,
        name: &str,
        kind: DefiniteDeclKind,
    ) {
        if poisoned.contains(name) {
            return;
        }
        match out.get(name) {
            Some(existing) if *existing != kind => {
                out.remove(name);
                poisoned.insert(name.to_string());
            }
            _ => {
                out.insert(name.to_string(), kind);
            }
        }
    }

    for stmt in stmts {
        match stmt {
            Statement::VarDecl { name, var_type, .. } => {
                let kind = match var_type {
                    Some(Type::Buffer) => DefiniteDeclKind::Buffer,
                    Some(Type::List(_)) => DefiniteDeclKind::List,
                    Some(Type::Map(_)) => DefiniteDeclKind::Map,
                    _ => DefiniteDeclKind::Plain,
                };
                record(&mut out, &mut poisoned, name, kind);
            }
            Statement::BufferDecl { name, .. } => {
                record(&mut out, &mut poisoned, name, DefiniteDeclKind::Buffer);
            }
            Statement::Allocate { name, .. } | Statement::TimerDecl { name } => {
                record(&mut out, &mut poisoned, name, DefiniteDeclKind::Plain);
            }
            Statement::FileOpen { name, .. } => {
                record(&mut out, &mut poisoned, name, DefiniteDeclKind::File);
            }
            Statement::GetTime { into } => {
                record(&mut out, &mut poisoned, into, DefiniteDeclKind::Plain);
            }
            Statement::If { then_block, else_if_blocks, else_block: Some(else_block), .. } => {
                let mut definite = collect_definite_decls(then_block);
                for (_, block) in else_if_blocks {
                    let branch = collect_definite_decls(block);
                    definite.retain(|name, kind| branch.get(name) == Some(kind));
                }
                let branch = collect_definite_decls(else_block);
                definite.retain(|name, kind| branch.get(name) == Some(kind));
                for (name, kind) in definite {
                    record(&mut out, &mut poisoned, &name, kind);
                }
            }
            _ => {}
        }
    }
    out
}

/// Every typed declaration reachable in this statement sequence, at ANY
/// nesting depth, regardless of whether the path that reaches it is
/// guaranteed to run - the complement of `collect_definite_decls`.
///
/// `On error`, `While`, `for each` (both `ForRange` and `ForEach`), and
/// `Repeat` bodies are not scoped (LANGUAGE.md:526: no block scoping) -
/// a name declared in one of them is accepted everywhere after, exactly
/// like a top-level declaration, but the analyzer never proves the body
/// ran. `collect_definite_decls` correctly refuses to call such a
/// declaration definite; this function is the other half - it finds
/// EVERY declaration so codegen can tell "definitely declared" apart
/// from "declared on some path, might be skipped" and emit the type's
/// default for the latter at frame setup (docs/BUGS_FOUND.md #25,
/// plan 318 §1). `If` bodies are included too, for the same reason
/// `collect_definite_decls` recurses into them: a some-branches name
/// still needs its type known here even though its own use-after is
/// separately rejected by the analyzer's branch tracking.
///
/// Function bodies are their own scope and are never entered - same
/// rule as `collect_definite_decls`.
pub fn collect_all_typed_decls(stmts: &[Statement]) -> std::collections::HashMap<String, Type> {
    let mut out = std::collections::HashMap::new();
    let mut poisoned: std::collections::HashSet<String> = std::collections::HashSet::new();

    fn record(
        out: &mut std::collections::HashMap<String, Type>,
        poisoned: &mut std::collections::HashSet<String>,
        name: &str,
        ty: Type,
    ) {
        if poisoned.contains(name) {
            return;
        }
        match out.get(name) {
            Some(existing) if *existing != ty => {
                out.remove(name);
                poisoned.insert(name.to_string());
            }
            _ => {
                out.insert(name.to_string(), ty);
            }
        }
    }

    fn walk(
        stmts: &[Statement],
        out: &mut std::collections::HashMap<String, Type>,
        poisoned: &mut std::collections::HashSet<String>,
    ) {
        for stmt in stmts {
            match stmt {
                Statement::VarDecl { name, var_type: Some(t), .. } => {
                    record(out, poisoned, name, t.clone());
                }
                Statement::BufferDecl { name, .. } => {
                    record(out, poisoned, name, Type::Buffer);
                }
                Statement::If { then_block, else_if_blocks, else_block, .. } => {
                    walk(then_block, out, poisoned);
                    for (_, block) in else_if_blocks {
                        walk(block, out, poisoned);
                    }
                    if let Some(block) = else_block {
                        walk(block, out, poisoned);
                    }
                }
                Statement::While { body, .. }
                | Statement::ForRange { body, .. }
                | Statement::ForEach { body, .. }
                | Statement::Repeat { body, .. } => {
                    walk(body, out, poisoned);
                }
                Statement::OnError { actions } => {
                    walk(actions, out, poisoned);
                }
                Statement::FunctionDef { .. } => {}
                _ => {}
            }
        }
    }

    walk(stmts, &mut out, &mut poisoned);
    out
}