formalang 0.0.4-beta

FormaLang compiler frontend: lexer, parser, semantic analyzer, and IR lowering.
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
//! Lowering for control-flow expressions: `if`, `for`, `match`, `block`,
//! `let` and pattern destructuring.

use crate::ast::{
    self, BindingPattern, BlockStatement, Expr, Literal, ParamConvention, PrimitiveType,
};
use crate::ir::lower::IrLowerer;
use crate::ir::{IrBlockStatement, IrExpr, IrMatchArm, ResolvedType};
use std::collections::HashMap;

impl IrLowerer<'_> {
    pub(super) fn lower_if_expr(
        &mut self,
        condition: &Expr,
        then_branch: &Expr,
        else_branch: Option<&Expr>,
    ) -> IrExpr {
        let then_ir = self.lower_expr(then_branch);
        let ty = then_ir.ty().clone();
        IrExpr::If {
            condition: Box::new(self.lower_expr(condition)),
            then_branch: Box::new(then_ir),
            else_branch: else_branch.map(|e| Box::new(self.lower_expr(e))),
            ty,
            span: self.current_ir_span(),
        }
    }

    pub(super) fn lower_for_expr(
        &mut self,
        var: &crate::ast::Ident,
        collection: &Expr,
        body: &Expr,
    ) -> IrExpr {
        let collection_ir = self.lower_expr(collection);
        let bad_collection = collection_ir.ty().clone();
        // For-loops iterate `Array<T>` and `Range<T>`; both are
        // prelude-defined generic structs after the built-in unification.
        let var_ty = self.iterator_element_ty(&bad_collection).unwrap_or_else(|| {
            self.internal_error_type_if_concrete(
                &bad_collection,
                format!(
                    "for-loop collection lowered to non-iterable type {bad_collection:?}; semantic should have caught this",
                ),
            )
        });
        // Make the loop variable visible while lowering the body, so
        // references to `var` inside the body resolve to the iterator
        // element type instead of falling through to UndefinedReference.
        let mut frame = HashMap::new();
        frame.insert(var.name.clone(), (ParamConvention::Let, var_ty.clone()));
        self.local_binding_scopes.push(frame);
        let body_ir = self.lower_expr(body);
        self.local_binding_scopes.pop();
        IrExpr::For {
            var: var.name.clone(),
            var_ty,
            var_binding_id: crate::ir::BindingId(0),
            collection: Box::new(collection_ir),
            body: Box::new(body_ir.clone()),
            ty: self
                .array_of(body_ir.ty().clone())
                .unwrap_or(ResolvedType::Error),
            span: self.current_ir_span(),
        }
    }

    pub(super) fn lower_match_expr(
        &mut self,
        scrutinee: &Expr,
        arms: &[crate::ast::MatchArm],
    ) -> IrExpr {
        let scrutinee_ir = self.lower_expr(scrutinee);
        let arms_ir: Vec<IrMatchArm> = arms
            .iter()
            .map(|arm| {
                let bindings = self.extract_pattern_bindings(&arm.pattern, &scrutinee_ir);
                // Pattern bindings (e.g. `urgency` from `.high(urgency)`)
                // need to be visible to the arm body. Without this frame
                // the body lowered with the binding as an UndefinedReference.
                let mut frame = HashMap::new();
                for (name, _binding_id, ty) in &bindings {
                    frame.insert(name.clone(), (ParamConvention::Let, ty.clone()));
                }
                self.local_binding_scopes.push(frame);
                let body = self.lower_expr(&arm.body);
                self.local_binding_scopes.pop();
                IrMatchArm {
                    variant: match &arm.pattern {
                        ast::Pattern::Variant { name, .. } => name.name.clone(),
                        ast::Pattern::Wildcard => String::new(),
                    },
                    variant_idx: crate::ir::VariantIdx(0),
                    is_wildcard: matches!(&arm.pattern, ast::Pattern::Wildcard),
                    bindings,
                    body,
                }
            })
            .collect();
        let ty = arms_ir.first().map_or_else(
            || self.internal_error_type("match expression with no arms reached IR lowering".into()),
            |a| a.body.ty().clone(),
        );
        IrExpr::Match {
            scrutinee: Box::new(scrutinee_ir),
            arms: arms_ir,
            ty,
            span: self.current_ir_span(),
        }
    }

    /// Lower a `let pat = val in body` expression into a block with the
    /// binding as one or more statements. Destructuring patterns are
    /// expanded into per-field let statements so the bindings actually
    /// reach the body — previously they collapsed to a single `_let`
    /// binding.
    pub(super) fn lower_let_expr(
        &mut self,
        mutable: bool,
        pattern: &BindingPattern,
        ty: Option<&ast::Type>,
        value: &Expr,
        body: &Expr,
    ) -> IrExpr {
        let ir_value = self.lower_expr(value);
        let ir_ty = ty.map(|t| self.lower_type(t));

        let statements: Vec<IrBlockStatement> = match pattern {
            BindingPattern::Simple(ident) => vec![IrBlockStatement::Let {
                binding_id: crate::ir::BindingId(0),
                name: ident.name.clone(),
                mutable,
                ty: ir_ty,
                value: ir_value,

                span: crate::ir::IrSpan::default(),
            }],
            BindingPattern::Array { elements, .. } => {
                self.lower_let_array_destructure(elements, mutable, &ir_value)
            }
            BindingPattern::Struct { fields, .. } => {
                self.lower_let_struct_destructure(fields, mutable, &ir_value)
            }
            BindingPattern::Tuple { elements, .. } => {
                self.lower_let_tuple_destructure(elements, mutable, &ir_value)
            }
        };
        // Make the let-introduced names visible to the body, mirroring
        // `lower_block_expr`. Without this frame, `let x = ... in x` lowered
        // the body with no scope to find `x` in, and the reference fell back
        // to a stringly-typed placeholder.
        self.local_binding_scopes.push(HashMap::new());
        for s in &statements {
            if let IrBlockStatement::Let {
                name,
                mutable,
                ty,
                value,
                ..
            } = s
            {
                let resolved = ty.clone().unwrap_or_else(|| value.ty().clone());
                let convention = if *mutable {
                    ParamConvention::Mut
                } else {
                    ParamConvention::Let
                };
                if let Some(frame) = self.local_binding_scopes.last_mut() {
                    frame.insert(name.clone(), (convention, resolved));
                }
            }
        }
        let ir_body = self.lower_expr(body);
        self.local_binding_scopes.pop();
        let ty = ir_body.ty().clone();
        IrExpr::Block {
            statements,
            result: Box::new(ir_body),
            ty,
            span: self.current_ir_span(),
        }
    }

    pub(super) fn lower_block_expr(
        &mut self,
        statements: &[BlockStatement],
        result: &Expr,
    ) -> IrExpr {
        // Fresh binding-scope frame so each block `let` is visible to
        // subsequent statements and `result`, then popped so siblings
        // don't see it. Required for accurate receiver types in dispatch
        // rewriting.
        self.local_binding_scopes.push(HashMap::new());
        let mut ir_statements: Vec<IrBlockStatement> = Vec::new();
        for stmt in statements {
            for s in self.lower_block_statement(stmt) {
                if let IrBlockStatement::Let {
                    name,
                    mutable,
                    ty,
                    value,
                    ..
                } = &s
                {
                    let resolved = ty.clone().unwrap_or_else(|| value.ty().clone());
                    let convention = if *mutable {
                        crate::ast::ParamConvention::Mut
                    } else {
                        crate::ast::ParamConvention::Let
                    };
                    if let Some(frame) = self.local_binding_scopes.last_mut() {
                        frame.insert(name.clone(), (convention, resolved));
                    }
                }
                ir_statements.push(s);
            }
        }
        let ir_result = self.lower_expr(result);
        self.local_binding_scopes.pop();
        let ty = ir_result.ty().clone();
        if ir_statements.is_empty() {
            return ir_result;
        }
        IrExpr::Block {
            statements: ir_statements,
            result: Box::new(ir_result),
            ty,
            span: self.current_ir_span(),
        }
    }

    /// Lower an AST block statement to one or more IR block statements.
    pub(super) fn lower_block_statement(&mut self, stmt: &BlockStatement) -> Vec<IrBlockStatement> {
        match stmt {
            BlockStatement::Let {
                mutable,
                pattern,
                ty,
                value,
                ..
            } => {
                let ir_ty = ty.as_ref().map(|t| self.lower_type(t));
                // Thread the let's declared closure type into the value
                // lowering so an arrow-form literal (`n -> n * scale`)
                // can pick up its parameter types from the annotation
                // when the literal itself omits them.
                let saved_closure = self.expected_closure_type.take();
                if let Some(t) = &ir_ty {
                    if matches!(t, ResolvedType::Closure { .. }) {
                        self.expected_closure_type = Some(t.clone());
                    }
                }
                let ir_value = self.lower_expr(value);
                self.expected_closure_type = saved_closure;
                match pattern {
                    BindingPattern::Simple(ident) => vec![IrBlockStatement::Let {
                        binding_id: crate::ir::BindingId(0),
                        name: ident.name.clone(),
                        mutable: *mutable,
                        ty: ir_ty,
                        value: ir_value,

                        span: crate::ir::IrSpan::default(),
                    }],
                    BindingPattern::Array { elements, .. } => {
                        self.lower_let_array_destructure(elements, *mutable, &ir_value)
                    }
                    BindingPattern::Struct { fields, .. } => {
                        self.lower_let_struct_destructure(fields, *mutable, &ir_value)
                    }
                    BindingPattern::Tuple { elements, .. } => {
                        self.lower_let_tuple_destructure(elements, *mutable, &ir_value)
                    }
                }
            }
            BlockStatement::Assign { target, value, .. } => {
                vec![IrBlockStatement::Assign {
                    target: self.lower_expr(target),
                    value: self.lower_expr(value),

                    span: crate::ir::IrSpan::default(),
                }]
            }
            BlockStatement::Expr(expr) => {
                vec![IrBlockStatement::Expr(self.lower_expr(expr))]
            }
        }
    }

    fn lower_let_array_destructure(
        &mut self,
        elements: &[crate::ast::ArrayPatternElement],
        mutable: bool,
        ir_value: &IrExpr,
    ) -> Vec<IrBlockStatement> {
        let bad_recv = ir_value.ty().clone();
        let elem_ty = self.array_element_ty(&bad_recv).unwrap_or_else(|| {
            self.internal_error_type_if_concrete(
                &bad_recv,
                format!("let array-destructure receiver lowered to non-array type {bad_recv:?}"),
            )
        });
        elements
            .iter()
            .enumerate()
            .filter_map(|(i, elem)| {
                Self::extract_block_binding_name(elem).map(|name| {
                    #[expect(
                        clippy::cast_precision_loss,
                        reason = "array indices are small positions that fit in f64 mantissa"
                    )]
                    let key = IrExpr::Literal {
                        value: Literal::Number((i as f64).into()),
                        ty: ResolvedType::Primitive(PrimitiveType::I32),
                        span: self.current_ir_span(),
                    };
                    IrBlockStatement::Let {
                        binding_id: crate::ir::BindingId(0),
                        name,
                        mutable,
                        ty: Some(elem_ty.clone()),
                        value: IrExpr::DictAccess {
                            dict: Box::new(ir_value.clone()),
                            key: Box::new(key),
                            ty: elem_ty.clone(),
                            span: self.current_ir_span(),
                        },

                        span: crate::ir::IrSpan::default(),
                    }
                })
            })
            .collect()
    }

    fn lower_let_struct_destructure(
        &mut self,
        fields: &[crate::ast::StructPatternField],
        mutable: bool,
        ir_value: &IrExpr,
    ) -> Vec<IrBlockStatement> {
        fields
            .iter()
            .map(|field| {
                let field_name = field.name.name.clone();
                let binding_name = field
                    .alias
                    .as_ref()
                    .map_or_else(|| field_name.clone(), |a| a.name.clone());
                let field_ty = self.get_field_type_from_resolved(ir_value.ty(), &field_name);
                IrBlockStatement::Let {
                    binding_id: crate::ir::BindingId(0),
                    name: binding_name,
                    mutable,
                    ty: Some(field_ty.clone()),
                    value: IrExpr::FieldAccess {
                        object: Box::new(ir_value.clone()),
                        field: field_name,
                        field_idx: crate::ir::FieldIdx(0),
                        ty: field_ty,
                        span: self.current_ir_span(),
                    },

                    span: crate::ir::IrSpan::default(),
                }
            })
            .collect()
    }

    fn lower_let_tuple_destructure(
        &mut self,
        elements: &[crate::ast::BindingPattern],
        mutable: bool,
        ir_value: &IrExpr,
    ) -> Vec<IrBlockStatement> {
        let bad_tuple = ir_value.ty().clone();
        let tuple_types = if let ResolvedType::Tuple(fields) = &bad_tuple {
            fields.clone()
        } else {
            let _ = self.internal_error_type_if_concrete(
                &bad_tuple,
                format!("let tuple-destructure receiver lowered to non-tuple type {bad_tuple:?}"),
            );
            Vec::new()
        };
        // The "out-of-range" placeholder is only used if a binding index
        // overshoots the tuple's fields; we lazily build it to avoid
        // pushing a spurious error on every well-formed destructure.
        let out_of_range_ty = if elements.len() > tuple_types.len() && !tuple_types.is_empty() {
            self.internal_error_type(format!(
                "let tuple-destructure binds {} names but receiver has {} fields",
                elements.len(),
                tuple_types.len(),
            ))
        } else {
            ResolvedType::Error
        };
        elements
            .iter()
            .enumerate()
            .filter_map(|(i, elem)| {
                IrLowerer::extract_simple_binding_name(elem).map(|name| {
                    let (field_name, ty) = tuple_types.get(i).map_or_else(
                        || (i.to_string(), out_of_range_ty.clone()),
                        |(n, t)| (n.clone(), t.clone()),
                    );
                    IrBlockStatement::Let {
                        binding_id: crate::ir::BindingId(0),
                        name,
                        mutable,
                        ty: Some(ty.clone()),
                        value: IrExpr::FieldAccess {
                            object: Box::new(ir_value.clone()),
                            field: field_name,
                            field_idx: crate::ir::FieldIdx(0),
                            ty,
                            span: self.current_ir_span(),
                        },

                        span: crate::ir::IrSpan::default(),
                    }
                })
            })
            .collect()
    }

    fn extract_block_binding_name(elem: &crate::ast::ArrayPatternElement) -> Option<String> {
        match elem {
            crate::ast::ArrayPatternElement::Binding(p) => {
                IrLowerer::extract_simple_binding_name(p)
            }
            crate::ast::ArrayPatternElement::Rest(Some(ident)) => Some(ident.name.clone()),
            crate::ast::ArrayPatternElement::Rest(None)
            | crate::ast::ArrayPatternElement::Wildcard => None,
        }
    }

    pub(super) fn extract_pattern_bindings(
        &mut self,
        pattern: &ast::Pattern,
        scrutinee: &IrExpr,
    ) -> Vec<(String, crate::ir::BindingId, ResolvedType)> {
        match pattern {
            ast::Pattern::Variant { name, bindings } => {
                // Try to find variant field types from the enum
                let variant_fields = self.get_variant_fields(scrutinee.ty(), &name.name);
                let has_overflow = bindings.len() > variant_fields.len();
                // Only emit an error when the scrutinee's type is already a
                // concrete enum; if it's a TypeParam (unresolved path), the
                // overflow is a downstream artefact of the upstream gap.
                let out_of_range_ty = if has_overflow
                    && !matches!(scrutinee.ty(), ResolvedType::TypeParam(_))
                {
                    self.internal_error_type(format!(
                        "match pattern `{}` binds more names ({}) than the variant has fields ({}); semantic should have caught this",
                        name.name,
                        bindings.len(),
                        variant_fields.len(),
                    ))
                } else {
                    ResolvedType::Error
                };
                bindings
                    .iter()
                    .enumerate()
                    .map(|(i, ident)| {
                        let ty = variant_fields
                            .get(i)
                            .cloned()
                            .unwrap_or_else(|| out_of_range_ty.clone());
                        (ident.name.clone(), crate::ir::BindingId(0), ty)
                    })
                    .collect()
            }
            ast::Pattern::Wildcard => {
                // Wildcard has no bindings
                Vec::new()
            }
        }
    }
}