1
//! `PROGN` special form. Sequencing primitive — evaluates each body
2
//! form in order, returning the last value. Empty body collapses to
3
//! `nil`.
4

            
5
use crate::ast::{Expr, WasmType};
6
use crate::compiler::context::CompileContext;
7
use crate::compiler::emit::FunctionEmitter;
8
use crate::compiler::expr::{compile_body, compile_body_for_stack, compile_nil};
9
use crate::error::Result;
10
use crate::runtime::SymbolTable;
11

            
12
14235
pub(super) fn compile_progn(
13
14235
    ctx: &mut CompileContext,
14
14235
    emit: &mut FunctionEmitter,
15
14235
    symbols: &mut SymbolTable,
16
14235
    args: &[Expr],
17
14235
) -> Result<()> {
18
14235
    if args.is_empty() {
19
6
        compile_nil(ctx, emit)?;
20
6
        return Ok(());
21
14229
    }
22
14229
    compile_body(ctx, emit, symbols, args)
23
14235
}
24

            
25
216042
pub(super) fn compile_progn_for_stack(
26
216042
    ctx: &mut CompileContext,
27
216042
    emit: &mut FunctionEmitter,
28
216042
    symbols: &mut SymbolTable,
29
216042
    args: &[Expr],
30
216042
) -> Result<WasmType> {
31
216042
    if args.is_empty() {
32
        // Empty `(progn)` ≡ nil; push the falsy i31 value but type it `Bool`
33
        // so it serializes as Nil (not Number(0)) and agrees with the eval
34
        // mirror (`progn_form` → `Expr::Nil`), keeping a runtime-IF branch
35
        // homogeneous.
36
9
        emit.i32_const(0);
37
9
        return Ok(WasmType::Bool);
38
216033
    }
39
216033
    compile_body_for_stack(ctx, emit, symbols, args)
40
216042
}
41

            
42
224391
pub(super) fn progn_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
43
224391
    if args.is_empty() {
44
229
        return Ok(Expr::Nil);
45
224162
    }
46
224162
    super::super::binding::eval_body(symbols, args)
47
224391
}