lisette-emit 0.1.20

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
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
use syntax::ast::Expression;

macro_rules! write_line {
    ($dst:expr, $($arg:tt)*) => {
        { use std::fmt::Write as _; writeln!($dst, $($arg)*).unwrap() }
    };
}
pub(crate) use write_line;

// -- Generic helpers -------------------------------------------------------

pub(crate) fn receiver_name(type_name: &str) -> String {
    type_name
        .trim_start_matches('*')
        .split('[')
        .next()
        .unwrap_or(type_name)
        .chars()
        .next()
        .unwrap_or('x')
        .to_lowercase()
        .to_string()
}

/// Check if emitted Go output references `var` as a standalone identifier
/// (not as a substring of another identifier like `p` in `tmp_1`).
pub(crate) fn output_references_var(output: &str, var: &str) -> bool {
    let var_bytes = var.as_bytes();
    let out_bytes = output.as_bytes();
    let mut start = 0;
    while start + var.len() <= output.len() {
        if let Some(position) = output[start..].find(var) {
            let abs = start + position;
            let before_ok = abs == 0 || {
                let c = out_bytes[abs - 1];
                !c.is_ascii_alphanumeric() && c != b'_'
            };
            let after = abs + var_bytes.len();
            let after_ok = after >= out_bytes.len() || {
                let c = out_bytes[after];
                !c.is_ascii_alphanumeric() && c != b'_'
            };
            if before_ok && after_ok {
                return true;
            }
            start = abs + 1;
        } else {
            break;
        }
    }
    false
}

/// Group consecutive parameters with the same Go type: `a int, b int` → `a, b int`.
pub(crate) fn group_params(params: &[(String, String)]) -> String {
    if params.is_empty() {
        return String::new();
    }
    if params.len() == 1 {
        return format!("{} {}", params[0].0, params[0].1);
    }
    let mut parts: Vec<String> = Vec::new();
    let mut names: Vec<&str> = vec![&params[0].0];
    let mut current_ty = &params[0].1;

    for param in &params[1..] {
        if param.1 == *current_ty {
            names.push(&param.0);
        } else {
            parts.push(format!("{} {}", names.join(", "), current_ty));
            names.clear();
            names.push(&param.0);
            current_ty = &param.1;
        }
    }
    parts.push(format!("{} {}", names.join(", "), current_ty));
    parts.join(", ")
}

/// Try to negate a simple comparison by flipping its operator.
/// Returns `None` for compound expressions (`&&`/`||`) or non-comparisons.
/// Used by unary-not emission and let-else condition negation.
pub(crate) fn try_flip_comparison(expression: &str) -> Option<String> {
    if expression.contains(" && ") || expression.contains(" || ") {
        return None;
    }
    for (op, flipped) in [
        (" == ", " != "),
        (" != ", " == "),
        (" <= ", " > "),
        (" >= ", " < "),
        (" < ", " >= "),
        (" > ", " <= "),
    ] {
        if let Some(position) = expression.find(op) {
            let lhs = &expression[..position];
            let rhs = &expression[position + op.len()..];
            return Some(format!("{}{}{}", lhs, flipped, rhs));
        }
    }
    None
}

pub(crate) fn requires_temp_var(expression: &Expression) -> bool {
    matches!(
        expression,
        Expression::If { .. }
            | Expression::IfLet { .. }
            | Expression::Match { .. }
            | Expression::Block { .. }
            | Expression::Loop { .. }
            | Expression::Propagate { .. }
            | Expression::TryBlock { .. }
            | Expression::Select { .. }
    )
}

/// Whether an expression contains a function call (i.e. is side-effectful).
/// Temp-lifted forms (if/match/block) return false — after emission they're
/// just variable names.
pub(crate) fn contains_call(expression: &Expression) -> bool {
    match expression.unwrap_parens() {
        Expression::Call { .. } => true,
        Expression::Binary { left, right, .. } => contains_call(left) || contains_call(right),
        Expression::Unary { expression, .. }
        | Expression::DotAccess { expression, .. }
        | Expression::Cast { expression, .. }
        | Expression::Reference { expression, .. } => contains_call(expression),
        Expression::IndexedAccess {
            expression, index, ..
        } => contains_call(expression) || contains_call(index),
        Expression::Tuple { elements, .. } => elements.iter().any(contains_call),
        e if requires_temp_var(e) => false,
        _ => false,
    }
}

// -- Eval-order staging ----------------------------------------------------

pub(crate) fn is_order_sensitive(expression: &Expression) -> bool {
    !matches!(
        expression.unwrap_parens(),
        Expression::Literal { .. } | Expression::Identifier { .. }
    )
}

/// Result of emitting a sub-expression to a separate buffer.
/// `setup` contains any statements the emitter produced (temp vars, etc.).
/// `value` is the final expression string.
pub(crate) struct Staged {
    pub setup: String,
    pub value: String,
    /// Whether the emitted value contains a call (side-effectful).
    /// Detected via `value.contains('(')` on the Go output string.
    pub has_side_effects: bool,
}

impl Staged {
    pub(crate) fn new(setup: String, value: String) -> Self {
        let has_side_effects = value.contains('(');
        Self {
            setup,
            value,
            has_side_effects,
        }
    }
}

/// Guard that snapshots the output length and inserts `_ = var\n` on `finish()`
/// if the variable was never referenced in the output emitted since creation.
pub(crate) struct DiscardGuard {
    pre_len: usize,
    var: String,
}

impl DiscardGuard {
    pub(crate) fn new(output: &str, var: &str) -> Self {
        Self {
            pre_len: output.len(),
            var: var.to_string(),
        }
    }

    pub(crate) fn finish(self, output: &mut String) {
        discard_if_unused(output, self.pre_len, &self.var);
    }
}

fn discard_if_unused(output: &mut String, pre_len: usize, var: &str) {
    if !output_references_var(&output[pre_len..], var) {
        output.insert_str(pre_len, &format!("_ = {}\n", var));
    }
}

// -- Peephole optimization passes ------------------------------------------

/// Run all peephole optimization passes on a region of emitted Go output.
///
/// When `temp_var` is `Some`, uses the given variable name for return inlining.
/// When `None`, auto-discovers `var X T ... return X` patterns.
pub(crate) fn optimize_region(output: &mut String, pre_len: usize, temp_var: Option<&str>) {
    let discovered;
    let resolved = match temp_var {
        Some(v) => Some(v),
        None => {
            discovered = find_inlinable_var(&output[pre_len..]);
            discovered.as_deref()
        }
    };
    if let Some(v) = resolved {
        inline_returns(output, pre_len, v);
    }
    strip_redundant_else(output, pre_len);
    strip_bare_blocks(output, pre_len);
    inline_wrapper_alias(output, pre_len);
    inline_trivial_bindings(output, pre_len);
}

pub(crate) fn optimize_function_body(output: &mut String) {
    optimize_region(output, 0, None);
}

/// Collapse single-use bindings: `VAR := EXPR\n{return VAR|TARGET = VAR|_ = VAR}\n`
/// becomes `{return EXPR|TARGET = EXPR|_ = EXPR}\n`.
///
/// Only collapses when VAR appears nowhere else in the region (true single-use).
/// Pattern bindings are always pure field accesses, so reordering is safe.
pub(crate) fn inline_trivial_bindings(output: &mut String, pre_len: usize) {
    let region = &output[pre_len..];
    let lines: Vec<&str> = region.lines().collect();

    let mut result = String::with_capacity(region.len());
    let mut i = 0;
    let mut changed = false;

    while i < lines.len() {
        if i + 1 < lines.len()
            && let Some((var, expression)) = parse_binding(lines[i])
            && let Some(collapsed) = try_inline_binding(lines[i + 1], var, expression)
        {
            let used_elsewhere = lines
                .iter()
                .enumerate()
                .any(|(j, line)| j != i && j != i + 1 && output_references_var(line, var));

            if !used_elsewhere {
                result.push_str(&collapsed);
                result.push('\n');
                i += 2;
                changed = true;
                continue;
            }
        }
        result.push_str(lines[i]);
        result.push('\n');
        i += 1;
    }

    if changed {
        output.truncate(pre_len);
        output.push_str(&result);
    }
}

/// Check if the output ends with a diverging statement (break/continue/return/panic).
pub(crate) fn output_ends_with_diverge(output: &str) -> bool {
    output
        .trim_end()
        .lines()
        .next_back()
        .is_some_and(is_diverge_line)
}

fn is_go_identifier(s: &str) -> bool {
    !s.is_empty() && s.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'_')
}

fn find_matching_close(lines: &[impl AsRef<str>], start: usize) -> Option<usize> {
    let mut depth: i32 = 1;
    let mut j = start + 1;
    while j < lines.len() {
        let l = lines[j].as_ref();
        let opens = l.chars().filter(|&c| c == '{').count() as i32;
        let closes = l.chars().filter(|&c| c == '}').count() as i32;
        depth += opens - closes;
        if depth == 0 {
            return Some(j);
        }
        j += 1;
    }
    None
}

/// Peephole: rewrite `var TEMP TYPE\n...TEMP = VALUE...\nreturn TEMP\n`
/// into `...return VALUE...\n`, inlining returns into each branch.
///
/// The emitter has no indentation (gofmt handles it), so `TEMP = ` appears
/// at line start even inside switch cases and if/else branches.
fn inline_returns(output: &mut String, pre_len: usize, temp_var: &str) {
    let region = &output[pre_len..];
    let var_line_prefix = format!("var {} ", temp_var);
    let assign_prefix = format!("{} = ", temp_var);
    let return_line = format!("return {}", temp_var);

    let lines: Vec<&str> = region.lines().collect();

    let Some(var_idx) = lines.iter().position(|l| l.starts_with(&var_line_prefix)) else {
        return;
    };

    if lines.last().is_none_or(|l| *l != return_line.as_str()) {
        return;
    }

    let mut result = String::with_capacity(region.len());
    let mut dead_labels: Vec<&str> = Vec::new();

    for line in &lines[..var_idx] {
        result.push_str(line);
        result.push('\n');
    }

    let body = &lines[var_idx + 1..lines.len() - 1];
    let mut i = 0;
    while i < body.len() {
        if body[i].starts_with(&assign_prefix) {
            result.push_str("return ");
            result.push_str(&body[i][assign_prefix.len()..]);
            result.push('\n');
            if i + 1 < body.len() && (body[i + 1] == "break" || body[i + 1].starts_with("break ")) {
                if let Some(label) = body[i + 1].strip_prefix("break ") {
                    dead_labels.push(label);
                }
                i += 1;
            }
        } else {
            result.push_str(body[i]);
            result.push('\n');
        }
        i += 1;
    }

    if !dead_labels.is_empty() {
        let cleaned = result
            .lines()
            .filter(|l| {
                if let Some(label) = l.strip_suffix(':') {
                    !dead_labels.contains(&label)
                        || result.lines().any(|rl| rl == format!("break {}", label))
                } else {
                    true
                }
            })
            .collect::<Vec<_>>()
            .join("\n");
        result.clear();
        result.push_str(&cleaned);
        result.push('\n');
    }

    output.truncate(pre_len);
    output.push_str(&result);
}

/// Check whether the region has a `var X T; ... X = expression; ... return X` pattern
/// that can be safely inlined. Returns the variable name if so.
fn find_inlinable_var(region: &str) -> Option<String> {
    let lines: Vec<&str> = region.lines().collect();

    // Last line must be `return VAR` where VAR is a simple identifier.
    let last = *lines.last()?;
    let var_name = last.strip_prefix("return ")?;
    if !is_go_identifier(var_name) {
        return None;
    }

    // Must have a corresponding `var VAR TYPE` declaration.
    let var_line_prefix = format!("var {} ", var_name);
    if !lines.iter().any(|l| l.starts_with(&var_line_prefix)) {
        return None;
    }

    // Must have at least one `VAR = ` assignment (not just initialization in var decl).
    let assign_prefix = format!("{} = ", var_name);
    if !lines.iter().any(|l| l.starts_with(&assign_prefix)) {
        return None;
    }

    // VAR must only appear in `var VAR`, `VAR = `, and `return VAR` lines.
    let var_read_elsewhere = lines.iter().any(|l| {
        !l.starts_with(&var_line_prefix)
            && !l.starts_with(&assign_prefix)
            && *l != last
            && output_references_var(l, var_name)
    });
    if var_read_elsewhere {
        return None;
    }

    for (i, line) in lines.iter().enumerate() {
        if !line.starts_with(&assign_prefix) {
            continue;
        }
        let mut j = i + 1;
        while j < lines.len() && lines[j].starts_with('}') {
            j += 1;
        }
        if j >= lines.len() {
            continue;
        }
        let next = lines[j];
        if next == "break"
            || next.starts_with("break ")
            || next.starts_with(&assign_prefix)
            || next == last
        {
            continue;
        }
        return None;
    }

    Some(var_name.to_owned())
}

/// Parse a short variable declaration: `VAR := EXPR` → `(VAR, EXPR)`.
fn parse_binding(line: &str) -> Option<(&str, &str)> {
    let idx = line.find(" := ")?;
    let var = &line[..idx];
    if !is_go_identifier(var) {
        return None;
    }
    let expression = &line[idx + 4..];
    Some((var, expression))
}

/// Try to collapse the next line with a single-use binding variable.
fn try_inline_binding(next_line: &str, var: &str, expression: &str) -> Option<String> {
    if let Some(rest) = next_line.strip_prefix("return ")
        && rest == var
    {
        return Some(format!("return {}", expression));
    }
    if let Some(rest) = next_line.strip_prefix("_ = ")
        && rest == var
    {
        return Some(format!("_ = {}", expression));
    }
    if let Some(eq_position) = next_line.find(" = ")
        && !next_line.contains(":=")
    {
        let target = &next_line[..eq_position];
        let value = &next_line[eq_position + 3..];
        if value == var && target != var {
            return Some(format!("{} = {}", target, expression));
        }
    }
    None
}

/// Whether the body `lines[start..end]` declares a `var :=` whose name is also
/// referenced anywhere outside that range. Used by block-flattening passes to
/// avoid moving an inner `var :=` into the same Go scope as an outer one
/// (which would trip "no new variables on left side of :=").
fn body_var_conflicts_outside<S: AsRef<str>>(lines: &[S], start: usize, end: usize) -> bool {
    lines[start..end].iter().any(|l| {
        let Some(idx) = l.as_ref().find(" := ") else {
            return false;
        };
        let var = &l.as_ref()[..idx];
        is_go_identifier(var)
            && lines
                .iter()
                .enumerate()
                .any(|(k, ol)| (k < start || k >= end) && output_references_var(ol.as_ref(), var))
    })
}

/// After `inline_returns` converts assignments to returns, `} else {`
/// wrappers become redundant when the if-branch already diverges.
/// This pass strips them: `<diverge>\n} else {\n<body>\n}\n` → `<diverge>\n}\n<body>\n`.
fn strip_redundant_else(output: &mut String, pre_len: usize) {
    loop {
        let lines: Vec<String> = output[pre_len..].lines().map(String::from).collect();
        let mut result: Vec<&str> = Vec::with_capacity(lines.len());
        let mut changed = false;
        let mut i = 0;

        while i < lines.len() {
            if lines[i] == "} else {" && result.last().is_some_and(|l| is_diverge_line(l)) {
                let close = find_matching_close(&lines, i);
                let safe_to_strip =
                    close.is_some_and(|j| !body_var_conflicts_outside(&lines, i + 1, j));

                if safe_to_strip {
                    result.push("}");
                    changed = true;
                    let close = close.unwrap();
                    for line in &lines[i + 1..close] {
                        result.push(line);
                    }
                    i = close + 1;
                    continue;
                }
            }
            result.push(&lines[i]);
            i += 1;
        }

        if !changed {
            break;
        }

        output.truncate(pre_len);
        for line in &result {
            output.push_str(line);
            output.push('\n');
        }
    }
}

fn is_diverge_line(line: &str) -> bool {
    let trimmed = line.trim();
    trimmed == "break"
        || trimmed.starts_with("break ")
        || trimmed == "continue"
        || trimmed.starts_with("continue ")
        || trimmed == "return"
        || trimmed.starts_with("return ")
        || trimmed.starts_with("panic(")
}

fn strip_bare_blocks(output: &mut String, pre_len: usize) {
    let lines: Vec<&str> = output[pre_len..].lines().collect();
    let mut result: Vec<&str> = Vec::with_capacity(lines.len());
    let mut changed = false;
    let mut i = 0;

    while i < lines.len() {
        if lines[i] == "{"
            && let Some(j) = find_matching_close(&lines, i)
            && j > i + 1
            && lines[j] == "}"
            && is_diverge_line(lines[j - 1])
            && !body_var_conflicts_outside(&lines, i + 1, j)
        {
            for line in &lines[i + 1..j] {
                result.push(line);
            }
            changed = true;
            i = j + 1;
            continue;
        }
        result.push(lines[i]);
        i += 1;
    }

    if changed {
        let mut new = String::with_capacity(output.len());
        new.push_str(&output[..pre_len]);
        for line in &result {
            new.push_str(line);
            new.push('\n');
        }
        *output = new;
    }
}

/// Peephole: when a Go interop wrapper temp is immediately aliased
/// (`var tmp T; ... tmp = V ...; alias := tmp`), rename tmp → alias
/// throughout and remove the alias line.
fn inline_wrapper_alias(output: &mut String, pre_len: usize) {
    let lines: Vec<String> = output[pre_len..].lines().map(String::from).collect();

    for (alias_idx, line) in lines.iter().enumerate() {
        let Some((alias, temp)) = parse_binding(line) else {
            continue;
        };
        let var_prefix = format!("var {} ", temp);
        let Some(var_idx) = lines[..alias_idx]
            .iter()
            .position(|l| l.starts_with(&var_prefix))
        else {
            continue;
        };

        let assign_prefix = format!("{} = ", temp);
        let temp_used_elsewhere = lines.iter().enumerate().any(|(j, l)| {
            j != var_idx
                && j != alias_idx
                && !l.starts_with(&assign_prefix)
                && output_references_var(l, temp)
        });
        if temp_used_elsewhere {
            continue;
        }

        let mut result = String::with_capacity(output.len() - pre_len);
        for (j, l) in lines.iter().enumerate() {
            if j == var_idx {
                result.push_str(&format!("var {} {}", alias, &l[var_prefix.len()..]));
                result.push('\n');
            } else if j == alias_idx {
                // Remove the alias line.
            } else if l.starts_with(&assign_prefix) {
                result.push_str(&format!("{} = {}", alias, &l[assign_prefix.len()..]));
                result.push('\n');
            } else {
                result.push_str(l);
                result.push('\n');
            }
        }

        output.truncate(pre_len);
        output.push_str(&result);
        return;
    }
}