lisette-emit 0.2.5

Little language inspired by Rust that compiles to 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
//! Pattern-site owner for non-match emission.
//!
//! `decision_tree` builds runtime checks and bindings from a single pattern;
//! `MatchEmitPlan` (via `tree_emitter`) is the match-construct pipeline. This
//! module sits between them for every other site that contains a pattern.
//!
//! Callers describe the construct, this module owns the pattern mechanics:
//! subject materialization, root assertions, refutable condition assembly,
//! binding emission, and or-pattern scope policy.

use syntax::ast::{Binding, Expression, MatchArm, Pattern, TypedPattern};
use syntax::types::Type;

use crate::Emitter;
use crate::control_flow::branching::wrap_if_struct_literal;
use crate::expressions::context::ExpressionContext;
use crate::names::go_name;
use crate::patterns::decision_tree::{
    self, apply_refutable_root_assertion, apply_root_assertion, compose_refutable_condition,
    emit_tree_assignments, emit_tree_bindings, render_condition,
};
use crate::placement::BodyPlace;
use crate::utils::{DiscardGuard, try_flip_comparison};
use crate::write_line;

/// How a pattern's subject value reaches the site.
pub(crate) enum PatternSubject<'a> {
    /// Subject already lives in a Go variable named by the caller.
    Existing { var: String },
    /// Pattern-site decides whether to inline the scrutinee identifier
    /// (when safe) or hoist it into a fresh temp with the given hint.
    Expression {
        scrutinee: &'a Expression,
        pattern: &'a Pattern,
        temp_hint: Option<&'a str>,
    },
}

impl<'a> PatternSubject<'a> {
    pub(crate) fn for_value(var: impl Into<String>) -> Self {
        Self::Existing { var: var.into() }
    }

    pub(crate) fn expression(
        scrutinee: &'a Expression,
        pattern: &'a Pattern,
        temp_hint: Option<&'a str>,
    ) -> Self {
        Self::Expression {
            scrutinee,
            pattern,
            temp_hint,
        }
    }
}

struct ResolvedSubject {
    var: String,
    guard: Option<DiscardGuard>,
}

impl Emitter<'_> {
    fn resolve_pattern_subject(
        &mut self,
        output: &mut String,
        subject: PatternSubject<'_>,
    ) -> ResolvedSubject {
        match subject {
            PatternSubject::Existing { var } => ResolvedSubject { var, guard: None },
            PatternSubject::Expression {
                scrutinee,
                pattern,
                temp_hint,
            } => {
                if let Expression::Identifier { value, .. } = scrutinee
                    && !value.contains('.')
                    && !Self::pattern_binds_name(pattern, value)
                {
                    let var = self.scope.resolve_or_escape(value);
                    return ResolvedSubject { var, guard: None };
                }
                let var = self.fresh_var(temp_hint);
                self.declare(&var);
                let expression = self.emit_value(output, scrutinee, ExpressionContext::value());
                write_line!(output, "{} := {}", var, expression);
                let guard = DiscardGuard::new(output, &var);
                ResolvedSubject {
                    var,
                    guard: Some(guard),
                }
            }
        }
    }

    pub(crate) fn emit_irrefutable_pattern_site(
        &mut self,
        output: &mut String,
        subject: PatternSubject<'_>,
        pattern: &Pattern,
        typed: Option<&TypedPattern>,
        subject_ty: &Type,
    ) {
        let resolved = self.resolve_pattern_subject(output, subject);
        let info = decision_tree::collect_pattern_info(self, pattern, typed, subject_ty);
        self.requirements.apply_effects(&info.effects);
        let effective = apply_root_assertion(self, output, &info, &resolved.var);
        emit_tree_bindings(self, output, &info.bindings, &effective);
        if let Some(guard) = resolved.guard {
            guard.finish(output);
        }
    }

    pub(crate) fn emit_let_else_pattern_site(
        &mut self,
        output: &mut String,
        pattern: &Pattern,
        typed: Option<&TypedPattern>,
        binding_ty: &Type,
        scrutinee: &Expression,
        else_block: &Expression,
    ) {
        let value_ty = scrutinee.get_type();
        let resolved = self.resolve_pattern_subject(
            output,
            PatternSubject::expression(scrutinee, pattern, Some("subject")),
        );

        if let Pattern::Or { patterns, .. } = pattern {
            self.emit_let_else_or_pattern(
                output,
                pattern,
                patterns,
                typed,
                binding_ty,
                &resolved.var,
                &value_ty,
                else_block,
            );
        } else {
            self.emit_let_else_single_pattern(
                output,
                pattern,
                typed,
                &resolved.var,
                &value_ty,
                else_block,
            );
        }

        if let Some(guard) = resolved.guard {
            guard.finish(output);
        }
    }

    pub(crate) fn emit_while_let(
        &mut self,
        output: &mut String,
        pattern: &Pattern,
        typed: Option<&TypedPattern>,
        scrutinee: &Expression,
        body: &Expression,
        needs_label: bool,
    ) {
        self.set_current_loop_label_if_needed(needs_label);
        if let Some(label) = self.current_loop_label() {
            write_line!(output, "{}:", label);
        }
        output.push_str("for {\n");

        let inline_var = if let Expression::Identifier { value, .. } = scrutinee {
            let has_collision = Self::pattern_binds_name(pattern, value);
            if !has_collision && !value.contains('.') {
                Some(self.scope.resolve_or_escape(value))
            } else {
                None
            }
        } else {
            None
        };
        let subject_var = inline_var.unwrap_or_else(|| {
            let var = self.fresh_var(Some("subject"));
            let expression = self.emit_operand(output, scrutinee, ExpressionContext::value());
            write_line!(output, "{} := {}", var, expression);
            var
        });

        let scrutinee_ty = scrutinee.get_type();
        if let Pattern::Or { patterns, .. } = pattern
            && Self::pattern_has_bindings(pattern)
        {
            self.emit_while_let_or_pattern(output, patterns, &subject_var, &scrutinee_ty, body);
            return;
        }

        let info = decision_tree::collect_pattern_info(self, pattern, typed, &scrutinee_ty);
        self.requirements.apply_effects(&info.effects);
        let (effective, ok_var) = apply_refutable_root_assertion(self, output, &info, &subject_var);
        let condition = compose_refutable_condition(ok_var.as_deref(), &info.checks, &effective);
        write_line!(output, "if {} {{", condition);
        self.enter_scope();

        if !matches!(pattern, Pattern::Or { .. }) {
            emit_tree_bindings(self, output, &info.bindings, &effective);
        }

        self.emit_block(output, body);

        self.emit_while_let_break_else(output);
    }

    fn emit_let_else_single_pattern(
        &mut self,
        output: &mut String,
        pattern: &Pattern,
        typed: Option<&TypedPattern>,
        subject_var: &str,
        subject_ty: &Type,
        else_block: &Expression,
    ) {
        let info = decision_tree::collect_pattern_info(self, pattern, typed, subject_ty);
        self.requirements.apply_effects(&info.effects);

        let (effective_subject, assert_ok_var) =
            apply_refutable_root_assertion(self, output, &info, subject_var);

        if info.checks.is_empty() && assert_ok_var.is_none() {
            emit_tree_bindings(self, output, &info.bindings, &effective_subject);
            return;
        }

        let mut guard_parts: Vec<String> = Vec::new();
        if let Some(ref ok) = assert_ok_var {
            guard_parts.push(format!("!{}", ok));
        }
        if !info.checks.is_empty() {
            let condition = render_condition(&info.checks, &effective_subject);
            let single = info.checks.len() == 1;
            let negated = if single {
                negate_condition(&condition)
            } else {
                format!("!({})", condition)
            };
            guard_parts.push(wrap_if_struct_literal(negated));
        }
        let guard = guard_parts.join(" || ");
        write_line!(output, "if {} {{", guard);
        self.emit_block(output, else_block);
        output.push_str("}\n");

        emit_tree_bindings(self, output, &info.bindings, &effective_subject);
    }

    #[allow(clippy::too_many_arguments)]
    fn emit_let_else_or_pattern(
        &mut self,
        output: &mut String,
        pattern: &Pattern,
        patterns: &[Pattern],
        typed: Option<&TypedPattern>,
        binding_ty: &Type,
        subject_var: &str,
        subject_ty: &Type,
        else_block: &Expression,
    ) {
        let outer_snapshot = self.scope.binding_snapshot();

        self.emit_binding_declarations_with_type(output, pattern, binding_ty, typed);

        let pattern_snapshot = self.scope.binding_snapshot();

        let collected: Vec<_> = patterns
            .iter()
            .map(|alt| decision_tree::collect_pattern_info(self, alt, None, subject_ty))
            .collect();
        for info in &collected {
            self.requirements.apply_effects(&info.effects);
        }

        let hoisted: Vec<_> = collected
            .iter()
            .map(|info| apply_refutable_root_assertion(self, output, info, subject_var))
            .collect();

        let irrefutable_idx = collected
            .iter()
            .zip(hoisted.iter())
            .position(|(info, (_, ok_var))| info.checks.is_empty() && ok_var.is_none());
        let chain_len = irrefutable_idx.unwrap_or(collected.len());

        for (i, info) in collected.iter().take(chain_len).enumerate() {
            let (effective, ok_var) = &hoisted[i];
            let condition = compose_refutable_condition(ok_var.as_deref(), &info.checks, effective);
            if i == 0 {
                write_line!(output, "if {} {{", condition);
            } else {
                write_line!(output, "}} else if {} {{", condition);
            }

            emit_tree_assignments(self, output, &info.bindings, effective);
        }

        if let Some(idx) = irrefutable_idx {
            let info = &collected[idx];
            let (effective, _) = &hoisted[idx];
            if idx == 0 {
                emit_tree_assignments(self, output, &info.bindings, effective);
            } else {
                output.push_str("} else {\n");
                emit_tree_assignments(self, output, &info.bindings, effective);
                output.push_str("}\n");
            }
            return;
        }

        self.scope.restore_binding_snapshot(outer_snapshot);
        output.push_str("} else {\n");
        self.emit_block(output, else_block);
        output.push_str("}\n");

        self.scope.restore_binding_snapshot(pattern_snapshot);
    }

    fn emit_while_let_or_pattern(
        &mut self,
        output: &mut String,
        patterns: &[Pattern],
        subject_var: &str,
        subject_ty: &Type,
        body: &Expression,
    ) {
        let mut alternatives: Vec<_> = patterns
            .iter()
            .map(|alt| decision_tree::collect_pattern_info(self, alt, None, subject_ty))
            .collect();
        for info in &alternatives {
            self.requirements.apply_effects(&info.effects);
        }

        let unused_names: rustc_hash::FxHashSet<String> = alternatives
            .iter()
            .flat_map(|info| info.bindings.iter())
            .filter(|b| b.go_name.is_none())
            .map(|b| b.lisette_name.clone())
            .collect();
        for info in alternatives.iter_mut() {
            for binding in info.bindings.iter_mut() {
                if unused_names.contains(&binding.lisette_name) {
                    binding.go_name = None;
                }
            }
        }

        let hoisted: Vec<_> = alternatives
            .iter()
            .map(|info| apply_refutable_root_assertion(self, output, info, subject_var))
            .collect();

        for (i, info) in alternatives.iter().enumerate() {
            let (effective, ok_var) = &hoisted[i];
            let condition = compose_refutable_condition(ok_var.as_deref(), &info.checks, effective);

            self.emit_branch_header(output, &condition, false, i == 0);

            emit_tree_bindings(self, output, &info.bindings, effective);
            self.emit_block(output, body);
        }

        self.emit_while_let_break_else(output);
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn emit_select_receive_pattern_site(
        &mut self,
        output: &mut String,
        receiver_var: &str,
        pattern: &Pattern,
        typed: Option<&TypedPattern>,
        element_ty: &Type,
        body: &Expression,
        default_body: Option<&Expression>,
        place: &BodyPlace,
    ) {
        self.emit_refutable_arm(
            output,
            receiver_var,
            pattern,
            typed,
            element_ty,
            body,
            place,
            |this, output| {
                if let Some(default_body) = default_body {
                    output.push_str("} else {\n");
                    this.emit_body_to_place(output, default_body, place);
                }
            },
        );
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn emit_select_match_receive_some_site(
        &mut self,
        output: &mut String,
        case_var: &str,
        pattern: &Pattern,
        typed: Option<&TypedPattern>,
        element_ty: &Type,
        some_body: &Expression,
        match_arms: &[MatchArm],
        place: &BodyPlace,
    ) {
        self.emit_refutable_arm(
            output,
            case_var,
            pattern,
            typed,
            element_ty,
            some_body,
            place,
            |this, output| {
                output.push_str("} else {\n");
                emit_none_arm_body(this, output, match_arms, place);
            },
        );
    }

    /// Emit a refutable site whose checks gate `body`. The `failure` closure
    /// runs only on the guarded path (after the body, before the closing
    /// brace) so callers can emit `} else { ... }` for their failure continuation.
    #[allow(clippy::too_many_arguments)]
    fn emit_refutable_arm(
        &mut self,
        output: &mut String,
        subject_var: &str,
        pattern: &Pattern,
        typed: Option<&TypedPattern>,
        subject_ty: &Type,
        body: &Expression,
        place: &BodyPlace,
        failure: impl FnOnce(&mut Emitter, &mut String),
    ) {
        let info = decision_tree::collect_pattern_info(self, pattern, typed, subject_ty);
        self.requirements.apply_effects(&info.effects);
        let (effective, ok_var) = apply_refutable_root_assertion(self, output, &info, subject_var);
        if info.checks.is_empty() && ok_var.is_none() {
            emit_tree_bindings(self, output, &info.bindings, &effective);
            self.emit_body_to_place(output, body, place);
            return;
        }
        let condition = compose_refutable_condition(ok_var.as_deref(), &info.checks, &effective);
        write_line!(output, "if {} {{", condition);
        emit_tree_bindings(self, output, &info.bindings, &effective);
        self.emit_body_to_place(output, body, place);
        failure(self, output);
        output.push_str("}\n");
    }
}

pub(crate) fn emit_none_arm_body(
    emitter: &mut Emitter,
    output: &mut String,
    match_arms: &[MatchArm],
    place: &BodyPlace,
) {
    for match_arm in match_arms {
        if let Pattern::EnumVariant { identifier, .. } = &match_arm.pattern {
            let variant_name = go_name::unqualified_name(identifier);
            if variant_name == "None" {
                emitter.emit_body_to_place(output, &match_arm.expression, place);
                return;
            }
        }
    }
}

fn negate_condition(condition: &str) -> String {
    try_flip_comparison(condition).unwrap_or_else(|| format!("!({})", condition))
}

/// True when `Some(_)`-shaped (peeling any outer `as`-binding), with exactly
/// one payload field. Used by select receive arms to decide whether the
/// receive needs an `ok` check on channel closure.
pub(crate) fn is_some_pattern(pattern: &Pattern) -> bool {
    let pattern = peel_as_binding(pattern);
    if let Pattern::EnumVariant {
        identifier, fields, ..
    } = pattern
    {
        let variant_name = go_name::unqualified_name(identifier);
        return variant_name == "Some" && fields.len() == 1;
    }
    false
}

/// Peel `Some(inner)` to expose `inner`; returns the original pattern when
/// the outer is not `Some(_)`. Pairs with `is_some_pattern` for select receive.
pub(crate) fn unwrap_some_pattern(pattern: &Pattern) -> &Pattern {
    let pattern = peel_as_binding(pattern);
    if let Pattern::EnumVariant {
        identifier, fields, ..
    } = pattern
        && go_name::unqualified_name(identifier) == "Some"
        && fields.len() == 1
    {
        return &fields[0];
    }
    pattern
}

pub(crate) fn unwrap_some_typed_pattern(typed: Option<&TypedPattern>) -> Option<&TypedPattern> {
    if let Some(TypedPattern::EnumVariant {
        variant_name,
        fields,
        ..
    }) = typed
        && variant_name == "Some"
        && fields.len() == 1
    {
        return Some(&fields[0]);
    }
    None
}

fn peel_as_binding(pattern: &Pattern) -> &Pattern {
    match pattern {
        Pattern::AsBinding { pattern, .. } => pattern.as_ref(),
        p => p,
    }
}

impl Emitter<'_> {
    /// Map a `Some(pattern)` payload to a case-variable name and whether the
    /// payload needs decision-tree destructuring inside the arm body (rather
    /// than being bound directly by the `case v := <-ch:` header).
    pub(crate) fn classify_receive_var_pattern(&mut self, pattern: &Pattern) -> (String, bool) {
        match pattern {
            Pattern::WildCard { .. } => ("_".to_string(), false),
            Pattern::Identifier { identifier, .. } => {
                let Some(go_name) = self.go_name_for_binding(pattern) else {
                    return ("_".to_string(), false);
                };
                if self.scope.resolve_binding(identifier).is_some() {
                    return (self.fresh_var(Some("recv")), true);
                }
                (self.scope.bind(identifier, go_name), false)
            }
            _ => (self.fresh_var(Some("recv")), true),
        }
    }

    /// Emit a for-loop element destructure. Owns the header form decision:
    /// when the pattern destructures nothing, emit `for range xs`; otherwise
    /// capture into a fresh `item` var and route through the irrefutable site.
    pub(crate) fn emit_for_loop_pattern_site(
        &mut self,
        output: &mut String,
        binding: &Binding,
        iter_expression: &str,
        is_channel: bool,
        body: &Expression,
    ) {
        if !Self::pattern_has_bindings(&binding.pattern) {
            write_line!(output, "for range {} {{", iter_expression);
            self.emit_block(output, body);
            output.push_str("}\n");
            return;
        }
        let item_var = self.fresh_var(Some("item"));
        if is_channel {
            write_line!(output, "for {} := range {} {{", item_var, iter_expression);
        } else {
            write_line!(
                output,
                "for _, {} := range {} {{",
                item_var,
                iter_expression
            );
        }
        let guard = DiscardGuard::new(output, &item_var);
        self.emit_irrefutable_pattern_site(
            output,
            PatternSubject::for_value(item_var),
            &binding.pattern,
            binding.typed_pattern.as_ref(),
            &binding.ty,
        );
        self.emit_block(output, body);
        guard.finish(output);
        output.push_str("}\n");
    }
}