hexicon-compiler 0.1.0

The Hexicon compiler: source text to a program image
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
//! The syntax tree: what the parser makes of the tokens, and what the
//! code generator reads.
//!
//! The tree follows the reference compiler's. That matters because the
//! byte code is generated node by node, and the choice of instructions
//! depends on what kind of node each operand is.

/// A local variable or parameter: an index into the program's list of
/// them.
pub(crate) type LocalId = usize;
/// A table of local variables for one scope.
pub(crate) type TabId = usize;

/// A value known when the program is compiled.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Value {
    Nil,
    True,
    Int(i32),
    /// A string, as its bytes of UTF-8.
    SStr(Vec<u8>),
    /// The address of a function: an index into the program's list of
    /// functions.
    FuncPtr(usize),
    /// A list all of whose elements are constants.
    List(Vec<ConstVal>),
    /// An object, by its number.
    Obj(u32),
    /// A property, by the number it has while compiling.
    Prop(u16),
    /// A value of an enumeration, by its number.
    Enum(u32),
    /// A number that is not an integer, as its text, which is the
    /// source's own or what arithmetic on constants wrote out.
    /// `promoted` says it is an integer too large to be one, which
    /// goes back to being an integer if arithmetic brings it in range.
    Float {
        text: Vec<u8>,
        promoted: bool,
    },
    /// A regular expression (`R'...'`), as its text.
    ReStr(Vec<u8>),
}

/// A property of an object written in an expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct InlineProp {
    /// An index into the program's properties.
    pub prop: usize,
    pub value: InlineValue,
    pub file: usize,
    pub line: u32,
}

/// What such a property is given.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum InlineValue {
    /// A method: an anonymous method, which can use the locals of the
    /// code the object is written in.
    Method(Expr),
    /// A value, and the anonymous method that works it out each time
    /// the property is asked for. If the value turns out to be a
    /// constant, it is given outright and the method is not wanted.
    Wrapped(Expr, Expr),
    /// A value worked out once, when the object is made: one marked
    /// `static`, or an object written inside this one.
    Eval(Expr),
}

/// A constant in an expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ConstVal {
    pub value: Value,
    /// The value comes from a question about the program being compiled
    /// (`defined(x)`), not from the source saying it outright. A
    /// condition that is constant for that reason is not worth a warning.
    pub ctc: bool,
    /// The elements of the list this constant used to be. The reference
    /// folds some operators by overwriting the value of the left
    /// operand in place, and when that operand is a list, what is left
    /// is a list that claims to be `true` or `nil`: everything that
    /// asks for its value sees the truth value, but the code generated
    /// for it pushes the list. `[1] == [1]` is `[1]`.
    pub list_node: Option<Vec<ConstVal>>,
}

impl ConstVal {
    pub(crate) fn new(value: Value) -> Self {
        Self {
            value,
            ctc: false,
            list_node: None,
        }
    }

    /// The value as a condition. Only nil and zero are false.
    pub(crate) fn is_true(&self) -> bool {
        match &self.value {
            Value::Nil | Value::Int(0) => false,
            Value::Float { .. } => !crate::fold::big_of(&self.value).is_some_and(|big| big.zero),
            _ => true,
        }
    }

    /// Replace the value by true or nil. The reference changes the
    /// value in place, so the note of where it came from stays.
    pub(crate) fn set_bool(&mut self, value: bool) {
        let new = if value { Value::True } else { Value::Nil };
        if let Value::List(elements) = std::mem::replace(&mut self.value, new) {
            self.list_node = Some(elements);
        }
    }

    /// Whether the value is sure to be true or nil when the program
    /// runs.
    pub(crate) fn is_bool(&self) -> bool {
        matches!(self.value, Value::Nil | Value::True) && self.list_node.is_none()
    }
}

/// An operator with one operand.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum UnOp {
    Not,
    /// Make a value true or nil. The parser adds this where `&&` or `||`
    /// is folded away but its result must still be a truth value.
    Boolize,
    BNot,
    Pos,
    Neg,
}

/// An operator with two operands.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BinOp {
    Comma,
    Or,
    And,
    BOr,
    BAnd,
    BXor,
    Eq,
    Ne,
    Gt,
    Ge,
    Lt,
    Le,
    Shl,
    AShr,
    LShr,
    Mul,
    Div,
    Mod,
    Sub,
    Add,
}

/// A kind of assignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AsiOp {
    Simple,
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    BAnd,
    BOr,
    BXor,
    Shl,
    AShr,
    LShr,
    PreInc,
    PreDec,
    PostInc,
    PostDec,
    /// Not an assignment to the target itself but to an element of it:
    /// the target is what is being indexed in `target[i] = value`.
    Idx,
}

impl AsiOp {
    /// The operator as messages name it.
    pub(crate) fn text(self) -> &'static str {
        match self {
            Self::Simple | Self::Idx => "=",
            Self::Add => "+=",
            Self::Sub => "-=",
            Self::Mul => "*=",
            Self::Div => "/=",
            Self::Mod => "%=",
            Self::BAnd => "&=",
            Self::BOr => "|=",
            Self::BXor => "^=",
            Self::Shl => "<<=",
            Self::AShr => ">>=",
            Self::LShr => ">>>=",
            Self::PreInc | Self::PostInc => "++",
            Self::PreDec | Self::PostDec => "--",
        }
    }
}

/// One argument of a call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Arg {
    pub expr: Expr,
    /// The argument is a list to be passed as that many arguments
    /// (`f(lst...)`).
    pub varargs: bool,
    /// The argument is passed by name (`f(count: 3)`), and this is the
    /// name.
    pub name: Option<Vec<u8>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Expr {
    Const(ConstVal),
    /// A name that is not a local variable. What it is cannot be known
    /// until the whole program has been read.
    Sym(Vec<u8>),
    Local(LocalId),
    Unary(UnOp, Box<Expr>),
    Binary(BinOp, Box<Expr>, Box<Expr>),
    /// An assignment of any kind but `++` and `--`.
    Assign(AsiOp, Box<Expr>, Box<Expr>),
    /// `++` or `--`, before or after.
    IncDec(AsiOp, Box<Expr>),
    /// `a ? b : c`
    If(Box<Expr>, Box<Expr>, Box<Expr>),
    /// `a ?? b`
    IfNil(Box<Expr>, Box<Expr>),
    /// A call, with its arguments in the order the source has them.
    Call(Box<Expr>, Vec<Arg>),
    /// `replaced`: in a function defined with `modify`, the function
    /// as it was before.
    Replaced,
    /// `delegated target`, which is always the left side of a `.`: the
    /// method is the target's, but runs with this method's `self`.
    Delegated(Box<Expr>),
    /// `argcount`: how many arguments the function was called with.
    ArgCount,
    /// `inherited<Type, *, ...>`, which is always what is called: in a
    /// version of a multi-method, the version for these types.
    InhTypes(Vec<Option<Vec<u8>>>, bool),
    /// An object written in an expression (`object: Class { ... }`),
    /// which is made each time the expression is evaluated.
    InlineObj {
        superclasses: Vec<Vec<u8>>,
        props: Vec<InlineProp>,
    },
    /// A list with an element that is not a constant.
    List(Vec<Expr>),
    /// A list of keys and values (`[key -> value, * -> default]`): a
    /// lookup table made, when the program runs, from the list inside,
    /// which has the keys and values in turn and the default last.
    Lookup(Box<Expr>),
    /// `a[b]`
    Index(Box<Expr>, Box<Expr>),
    /// A string in double quotes: text to display.
    DStr(Vec<u8>),
    /// An expression embedded in a string in double quotes: its value
    /// is displayed.
    DStrEmbed(Box<Expr>),
    /// `<<one of>>` in a string: the alternatives. The flag is for a
    /// string in double quotes.
    OneOf(bool, Vec<Expr>, Option<u32>),
    /// `self`
    SelfRef,
    /// An anonymous function: its body, as an index into the program's
    /// bodies, and whether calling it gives a value.
    AnonFunc {
        body: usize,
        has_retval: bool,
    },
    /// `x is in (a, b)`, or with `not` set `x not in (a, b)`. `hit` is
    /// set when constants were worked out and the left side was found
    /// among them: the test is then known to succeed if nothing else
    /// stops it.
    IsIn {
        left: Box<Expr>,
        args: Vec<Expr>,
        not: bool,
        hit: bool,
    },
    /// `x in collection` or `x in from..to step by`, among the
    /// initializers of a `for`.
    ForIn(Box<ForIn>),
    /// `&x`: the address of what a name stands for.
    Addr(Box<Expr>),
    /// `new Class(args)`, or `new transient Class(args)`. What is made
    /// is the operand, which is a call if there are arguments.
    New(Box<Expr>, bool),
    /// `inherited`, which is only ever the left side of a member
    /// expression: the method being overridden, in whichever
    /// superclass has it.
    Inh,
    /// `inherited Class`: the same, in a named superclass.
    InhClass(Vec<u8>),
    /// Something about the method that is running: 1 for
    /// `targetprop`, 2 for `targetobj`, 3 for `definingobj`, 4 for
    /// `invokee`.
    CtxEle(u8),
    /// `obj.prop`, or with arguments `obj.prop(args)`. The property is
    /// a name, or with `prop_is_expr` an expression in parentheses
    /// whose value says which property.
    Member {
        obj: Box<Expr>,
        prop: Box<Expr>,
        prop_is_expr: bool,
        args: Option<Vec<Arg>>,
    },
}

/// A variable in a `for` that takes each of a set of values in turn.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ForIn {
    /// The elements of a collection. The local holds the iterator.
    Coll {
        lval: Expr,
        expr: Expr,
        iter_local: LocalId,
    },
    /// The integers of a range. The locals hold the end and the step,
    /// where those are not constants.
    Range {
        lval: Expr,
        from: Expr,
        to: Expr,
        step: Option<Expr>,
        to_local: Option<LocalId>,
        step_local: Option<LocalId>,
    },
}

impl Expr {
    /// The `in` clauses among a `for`'s initializers, in order.
    pub(crate) fn for_ins<'e>(&'e self, found: &mut Vec<&'e ForIn>) {
        match self {
            Self::Binary(BinOp::Comma, left, right) => {
                left.for_ins(found);
                right.for_ins(found);
            }
            Self::ForIn(for_in) => found.push(for_in),
            _ => {}
        }
    }

    pub(crate) fn constant(value: Value) -> Self {
        Self::Const(ConstVal::new(value))
    }

    pub(crate) fn as_const(&self) -> Option<&ConstVal> {
        match self {
            Self::Const(value) => Some(value),
            _ => None,
        }
    }

    pub(crate) fn is_const(&self) -> bool {
        self.as_const().is_some()
    }

    pub(crate) fn is_const_int(&self, int: i32) -> bool {
        matches!(self.as_const(), Some(ConstVal { value: Value::Int(value), .. }) if *value == int)
    }

    /// Whether the value is sure to be true or nil.
    pub(crate) fn is_bool(&self) -> bool {
        match self {
            Self::Const(value) => value.is_bool(),
            Self::Unary(UnOp::Not, _) | Self::IsIn { .. } => true,
            Self::Binary(op, _, right) => match op {
                BinOp::Comma => right.is_bool(),
                BinOp::Eq
                | BinOp::Ne
                | BinOp::And
                | BinOp::Or
                | BinOp::Gt
                | BinOp::Ge
                | BinOp::Lt
                | BinOp::Le => true,
                _ => false,
            },
            _ => false,
        }
    }

    /// Whether the expression can be assigned to, as far as the parser
    /// can tell.
    pub(crate) fn check_lvalue(&self) -> bool {
        matches!(
            self,
            Self::Sym(_) | Self::Local(_) | Self::Index(..) | Self::Member { args: None, .. }
        )
    }

    /// A list of these elements: a constant if they all are.
    pub(crate) fn list(elements: Vec<Expr>) -> Self {
        if elements.iter().all(Self::is_const) {
            let values = elements
                .into_iter()
                .map(|element| match element {
                    Self::Const(value) => value,
                    _ => unreachable!("checked to be constants"),
                })
                .collect();
            Self::constant(Value::List(values))
        } else {
            Self::List(elements)
        }
    }

    /// How many elements a constant list has, if that is what this is.
    pub(crate) fn const_list_len(&self) -> Option<usize> {
        match self {
            Self::Const(ConstVal {
                value: Value::List(elements),
                ..
            }) => Some(elements.len()),
            _ => None,
        }
    }
}

/// A statement, and where in the source it starts.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Stmt {
    pub kind: StmtKind,
    pub file: usize,
    pub line: u32,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum StmtKind {
    /// Nothing: what an empty block holds.
    Null,
    /// The body of the constructor the compiler gives an object with
    /// several superclasses and no constructor of its own: it passes
    /// its arguments to each superclass's constructor. These are the
    /// superclasses' numbers.
    ImplicitCtor(Vec<u32>),
    /// The code of a multi-method itself, which the compiler writes:
    /// a call to the library with the arguments and the function, an
    /// index into the program's functions.
    MmStub(usize),
    /// The code that tells the library of each version of each
    /// multi-method, before the program starts.
    MmInit,
    /// The body of the code that works out a property's value before
    /// the program starts (`prop = static expr`): it sets the property
    /// of `self`, given as an index into the program's properties, to
    /// the value, and returns the value.
    StaticPropInit(Expr, usize),
    /// A block.
    Comp {
        stmts: Vec<Stmt>,
        /// The table of locals in force in the block.
        symtab: Option<TabId>,
        /// Where it ends: where the parser was when it made the block,
        /// at the closing brace if it has one.
        end_file: usize,
        end_line: u32,
    },
    Expr(Expr),
    If {
        cond: Expr,
        then_part: Option<Box<Stmt>>,
        else_part: Option<Box<Stmt>>,
    },
    Return(Option<Expr>),
    For {
        init: Option<Expr>,
        cond: Option<Expr>,
        reinit: Option<Expr>,
        body: Option<Box<Stmt>>,
        symtab: Option<TabId>,
        id: usize,
    },
    While {
        cond: Expr,
        body: Option<Box<Stmt>>,
        id: usize,
    },
    /// `foreach (x in collection) body`. The local holds the iterator.
    Foreach {
        iter: Expr,
        coll: Expr,
        body: Option<Box<Stmt>>,
        symtab: Option<TabId>,
        id: usize,
        iter_local: LocalId,
    },
    DoWhile {
        body: Option<Box<Stmt>>,
        cond: Expr,
        id: usize,
        /// Where the `while` part ends, which has code of its own.
        while_file: usize,
        while_line: u32,
    },
    Break(Option<Vec<u8>>),
    Continue(Option<Vec<u8>>),
    Switch {
        expr: Expr,
        body: Box<Stmt>,
        /// How many `case` labels the parser counted in the body.
        case_cnt: usize,
        has_default: bool,
        id: usize,
    },
    Case {
        expr: Expr,
        stm: Option<Box<Stmt>>,
    },
    Default(Option<Box<Stmt>>),
    /// A statement with a label on it.
    Label {
        /// The label, as an index into [`CodeBody::labels`]. There is
        /// none when the name was already taken by another statement.
        label: Option<usize>,
        stm: Option<Box<Stmt>>,
        id: usize,
    },
    Goto(Vec<u8>),
    Try {
        id: usize,
        body: Option<Box<Stmt>>,
        catches: Vec<Catch>,
        finally: Option<Box<Finally>>,
    },
    Throw(Expr),
}

/// A statement that has other statements inside it and that matters to
/// a jump out of them or into them: a loop, a `switch`, a labeled
/// statement, a `try`, or a `finally` block. Each has a number of its
/// own within the program.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Enclosure {
    pub id: usize,
    pub is_finally: bool,
}

/// A `catch` clause of a `try`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Catch {
    /// The name of the class of exception it catches.
    pub class: Vec<u8>,
    /// The local the exception is put in.
    pub var: LocalId,
    pub body: Option<Box<Stmt>>,
    pub symtab: Option<TabId>,
    pub file: usize,
    pub line: u32,
}

/// The `finally` clause of a `try`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Finally {
    pub id: usize,
    pub body: Option<Box<Stmt>>,
    /// The locals that hold an exception while the block runs, and the
    /// place to go back to when it is done.
    pub exc_local: usize,
    pub jsr_local: usize,
    pub file: usize,
    pub line: u32,
    /// Where the block ends, if it has one.
    pub end: Option<(usize, u32)>,
}

/// A label in a function, for `goto`, `break`, and `continue`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LabelSym {
    pub name: Vec<u8>,
    /// How the label is used: the `GOTO`, `BREAK`, and `CONT` bits of
    /// [`flow`], set as the uses are found.
    pub flags: std::cell::Cell<u32>,
    /// Where the labeled statement is.
    pub file: usize,
    pub line: u32,
    /// The statements the labeled statement is inside, as the parser
    /// saw it, outermost first and ending with the labeled statement
    /// itself. (The parser takes a `catch` block to be outside its
    /// `try`; the code generator takes it to be inside.)
    pub within: Vec<Enclosure>,
}

impl Stmt {
    /// Where the statement ends, as the reference takes it: a block's
    /// end, and for any other statement where it starts.
    pub(crate) fn end_pos(&self) -> (usize, u32) {
        match &self.kind {
            StmtKind::Comp {
                end_file, end_line, ..
            } => (*end_file, *end_line),
            _ => (self.file, self.line),
        }
    }

    /// Whether the statement starts with a label, a `case`, or a
    /// `default`, so that it can be reached from somewhere other than
    /// the statement before it.
    pub(crate) fn has_code_label(&self) -> bool {
        match &self.kind {
            StmtKind::Label { .. } | StmtKind::Case { .. } | StmtKind::Default(_) => true,
            StmtKind::Comp { stmts, .. } => stmts.first().is_some_and(Stmt::has_code_label),
            _ => false,
        }
    }
}

/// The body of a function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CodeBody {
    /// The outermost table of locals: the parameters, and the locals of
    /// the outermost block.
    pub lcltab: Option<TabId>,
    pub stm: Stmt,
    pub argc: usize,
    pub opt_argc: usize,
    pub varargs: bool,
    /// The parameter that collects the variable arguments in a list
    /// (`f(a, [rest])`), if there is one.
    pub varargs_list_local: Option<LocalId>,
    pub local_cnt: usize,
    /// The code is a method's: it has a `self`.
    pub self_valid: bool,
    /// Another definition has replaced this one, and no code is
    /// generated for it.
    pub replaced: bool,
    /// The labels on statements in the body.
    pub labels: std::rc::Rc<Vec<LabelSym>>,
    /// Where the opening brace is, if the body has one: a function's
    /// code starts there, for the debugger.
    pub start: Option<(usize, u32)>,
    /// Where the statement is, for the debugger, if not where it says:
    /// for the code that works out a property's value, where the
    /// reference's parser was after the value.
    pub stm_pos: Option<(usize, u32)>,
    /// Where the closing brace is.
    pub end_file: usize,
    pub end_line: u32,
    /// What the body shares with functions nested in it, and with the
    /// function it is nested in.
    pub closure: Closure,
}

/// How a body shares locals, and the object a method is running on,
/// with anonymous functions.
///
/// A local that a nested function uses does not live on the stack: it
/// is an element of a vector, the function's "context", which the
/// nested function is given when it is made. The nested function keeps
/// each context it was given in a local of its own.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Closure {
    /// The body is a function's, not a method's or an anonymous
    /// function's.
    pub is_func: bool,
    /// The function was defined with `modify`, so `replaced` means
    /// something in it: the body of the definition it modified, if
    /// that is in the source.
    pub has_replaced: bool,
    pub replaced_base: Option<usize>,
    /// The definition it modified is in another source file: the
    /// function, as an index into the program's functions.
    pub replaced_ext: Option<usize>,
    /// The function whose name still means this body, or meant it
    /// last: an index into the program's functions. A body that
    /// `modify` has put another in front of has none. While a body has
    /// one, `replaced` in it means what the function's latest `modify`
    /// says, not what it meant when the body was read.
    pub func_id: Option<usize>,
    /// The function a function's body was defined as, which stays the
    /// same when `modify` takes the name away from the body.
    pub defined_as: Option<usize>,
    /// For a function's body: how many parameters it has to be given,
    /// how many more it can be, and whether any number beyond that.
    pub func_proto: (usize, usize, bool),
    /// The body makes a context for its own locals, and keeps it in
    /// this local; and how many elements it has.
    pub has_local_ctx: bool,
    pub local_ctx_var: usize,
    pub local_ctx_arr_size: usize,
    /// The contexts the body is given, in the order it is given them:
    /// how many functions out each comes from, and the local it is
    /// kept in.
    pub ctx_levels: Vec<(usize, usize)>,
    /// The code refers to `self`, or might; and to the rest of what a
    /// method knows about how it was called.
    pub self_referenced: bool,
    pub full_ctx_referenced: bool,
    /// A function nested in the body needs the body's `self` put in its
    /// context, or all of what the method knows.
    pub local_ctx_needs_self: bool,
    pub local_ctx_needs_full: bool,
    /// It is the body of an anonymous method, which gets its `self`
    /// from whoever calls it.
    pub is_anon_method: bool,
    /// The body this one is nested in, as a number in
    /// `Program::body_refs`.
    pub enclosing: Option<usize>,
    /// The body was made for a property's value, an expression that
    /// had to be a method for the sake of a function in it.
    pub wraps_prop_expr: bool,
}

/// The ways a statement can be left.
pub(crate) mod flow {
    pub(crate) const NEXT: u32 = 0x01;
    pub(crate) const THROW: u32 = 0x02;
    pub(crate) const RET_VAL: u32 = 0x04;
    pub(crate) const RET_VOID: u32 = 0x08;
    pub(crate) const BREAK: u32 = 0x10;
    pub(crate) const CONT: u32 = 0x20;
    pub(crate) const GOTO: u32 = 0x40;
}