plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
ac22df2
— Peter John
2026-11-10T12:15:25+05:30
Codegen: a Unit inline if as a statement leaves nothing on the stack
compiler/wasm-codegen/src/expr.rs
CHANGED
|
@@ -286,6 +286,27 @@ fn compileExprNode(
|
|
|
286
286
|
},
|
|
287
287
|
}
|
|
288
288
|
}
|
|
289
|
+
// `if c then f() else g()` with Unit branches: nothing is left on the stack, and a
|
|
290
|
+
// branch that does leave a value (a call whose result is ignored) drops it.
|
|
291
|
+
ast::Expr { kind: ast::ExprKind::InlineIf(t), .. } if matches!(inferLocalType(expr, ctx), PlumType::TUnit) => {
|
|
292
|
+
compileBoolConditionAsI32(&t.condition, body, ctx, state)?;
|
|
293
|
+
Instruction::If(BlockType::Empty).encode(body);
|
|
294
|
+
let _tg = TailDepth::enter(&ctx.tail_depth);
|
|
295
|
+
for (i, branch) in [&t.then, &t.else_].into_iter().enumerate() {
|
|
296
|
+
if i == 1 {
|
|
297
|
+
Instruction::Else.encode(body);
|
|
298
|
+
}
|
|
299
|
+
if isUnitLiteral(branch, ctx) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
let has_result = exprHasResult(branch, ctx);
|
|
303
|
+
compileExpr(branch, body, ctx, state)?;
|
|
304
|
+
if has_result {
|
|
305
|
+
emitDrops(&inferLocalType(branch, ctx), body);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
Instruction::End.encode(body);
|
|
309
|
+
}
|
|
289
310
|
ast::Expr { kind: ast::ExprKind::InlineIf(t), .. } => {
|
|
290
311
|
let result_vt = plumTypeToValtype(&inferLocalType(expr, ctx));
|
|
291
312
|
compileBoolConditionAsI32(&t.condition, body, ctx, state)?;
|
compiler/wasm-codegen/src/matching.rs
CHANGED
|
@@ -15,6 +15,8 @@ pub(crate) fn exprHasResult(expr: &ast::Expr, ctx: &LocalCtx) -> bool {
|
|
|
15
15
|
{
|
|
16
16
|
false
|
|
17
17
|
}
|
|
18
|
+
// a Unit `if c then f() else g()` leaves nothing (see its arm in `compileExpr`)
|
|
19
|
+
ast::Expr { kind: ast::ExprKind::InlineIf(_), .. } => !matches!(inferLocalType(expr, ctx), PlumType::TUnit),
|
|
18
20
|
ast::Expr { kind: ast::ExprKind::FnCall(call), .. } => {
|
|
19
21
|
if ctx.locals.contains_key(&call.name)
|
|
20
22
|
&& matches!(
|
tests/core/unit_inline_if.plum
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# `if c then f() else g()` with Unit branches, as a statement: it once compiled to invalid
|
|
2
|
+
# wasm (a block typed as a value with nothing on the stack).
|
|
3
|
+
import std/Int
|
|
4
|
+
import std/Bool
|
|
5
|
+
import std/Str
|
|
6
|
+
import std/Slice
|
|
7
|
+
|
|
8
|
+
fun a(s: Slice[Int], v: Int) -> Unit =
|
|
9
|
+
if v > 0
|
|
10
|
+
s.push(v)
|
|
11
|
+
|
|
12
|
+
fun b(s: Slice[Int], v: Int) -> Unit =
|
|
13
|
+
s.push(v + 1)
|
|
14
|
+
|
|
15
|
+
fun step(s: Slice[Int], v: Int) -> Slice[Int] =
|
|
16
|
+
if s.isEmpty() then a(s, v) else b(s, v)
|
|
17
|
+
s
|
|
18
|
+
|
|
19
|
+
# the inline `if` is the fn's last statement
|
|
20
|
+
fun last(s: Slice[Int], v: Int) -> Unit =
|
|
21
|
+
if v > 10 then a(s, v) else b(s, v)
|
|
22
|
+
|
|
23
|
+
# a method call on one side, a free fn on the other
|
|
24
|
+
fun mixed(s: Slice[Int], v: Int) -> Unit =
|
|
25
|
+
if v > 10 then s.push(v) else b(s, v)
|
|
26
|
+
if v > 10 then b(s, v) else s.push(0)
|
|
27
|
+
|
|
28
|
+
test "a Unit inline if as a statement"
|
|
29
|
+
s := step(Slice[Int](), 3)
|
|
30
|
+
assert s.length() == 1
|
|
31
|
+
assert s.at(0) == 3
|
|
32
|
+
t := step(s, 5)
|
|
33
|
+
assert t.length() == 2
|
|
34
|
+
assert t.at(1) == 6
|
|
35
|
+
|
|
36
|
+
test "a Unit inline if as a fn's last statement"
|
|
37
|
+
s := Slice[Int]()
|
|
38
|
+
last(s, 20)
|
|
39
|
+
last(s, 2)
|
|
40
|
+
assert s.length() == 2
|
|
41
|
+
assert s.at(0) == 20
|
|
42
|
+
assert s.at(1) == 3
|
|
43
|
+
|
|
44
|
+
test "a Unit inline if with a method call and a free fn"
|
|
45
|
+
s := Slice[Int]()
|
|
46
|
+
mixed(s, 20)
|
|
47
|
+
mixed(s, 1)
|
|
48
|
+
assert s.length() == 4
|
|
49
|
+
assert s.at(0) == 20
|
|
50
|
+
assert s.at(1) == 21
|
|
51
|
+
assert s.at(2) == 2
|
|
52
|
+
assert s.at(3) == 0
|
|
53
|
+
|
|
54
|
+
test "a Unit inline if inside a loop body"
|
|
55
|
+
s := Slice[Int]()
|
|
56
|
+
for i := range 4
|
|
57
|
+
if i % 2 == 0 then a(s, i + 1) else b(s, i * 10)
|
|
58
|
+
assert s.length() == 4
|
|
59
|
+
assert s.at(0) == 1
|
|
60
|
+
assert s.at(1) == 11
|
|
61
|
+
assert s.at(3) == 31
|