1
//! `COMPILE` and `EVAL` reflective forms.
2
//!
3
//! `COMPILE` resolves a quoted symbol to its function definition and
4
//! returns a quoted symbol marking it as compiled (the marker is what
5
//! the next pipeline stage picks up; today the marker just round-
6
//! trips). `EVAL` resolves its argument and re-runs the evaluator on
7
//! the inner form.
8

            
9
use tracing::debug;
10

            
11
use crate::ast::{Expr, WasmType};
12
use crate::compiler::context::CompileContext;
13
use crate::compiler::emit::FunctionEmitter;
14
use crate::compiler::expr::{compile_expr, compile_for_stack, eval_value};
15
use crate::error::{Error, Result};
16
use crate::runtime::SymbolTable;
17

            
18
use super::compile_static_result_for_stack;
19
use super::folded::{folded_eval_form, folded_symbol};
20

            
21
1046
pub(super) fn compile_compile_form(
22
1046
    ctx: &mut CompileContext,
23
1046
    emit: &mut FunctionEmitter,
24
1046
    symbols: &mut SymbolTable,
25
1046
    args: &[Expr],
26
1046
) -> Result<()> {
27
1046
    let result = compile_form(symbols, args)?;
28
420
    compile_expr(ctx, emit, symbols, &result)
29
1046
}
30

            
31
1752
pub(super) fn compile_eval_form(
32
1752
    ctx: &mut CompileContext,
33
1752
    emit: &mut FunctionEmitter,
34
1752
    symbols: &mut SymbolTable,
35
1752
    args: &[Expr],
36
1752
) -> Result<()> {
37
1752
    if args.len() != 1 {
38
210
        return Err(Error::Arity {
39
210
            name: "eval".to_string(),
40
210
            expected: 1,
41
210
            actual: args.len(),
42
210
        });
43
1542
    }
44
1542
    let form = folded_eval_form(symbols, &args[0])?;
45
1500
    compile_expr(ctx, emit, symbols, &form)
46
1752
}
47

            
48
1063
pub(super) fn compile_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
49
1063
    if args.len() != 1 {
50
208
        return Err(Error::Arity {
51
208
            name: "compile".to_string(),
52
208
            expected: 1,
53
208
            actual: args.len(),
54
208
        });
55
855
    }
56
855
    let name = folded_symbol(symbols, "COMPILE", &args[0])?;
57
645
    compile_form_with_name(symbols, &name)
58
1063
}
59

            
60
645
pub(super) fn compile_form_with_name(symbols: &mut SymbolTable, name: &str) -> Result<Expr> {
61
645
    debug!(function = %name, "compiling compile");
62
645
    let sym = symbols
63
645
        .lookup(name)
64
208
        .ok_or_else(|| Error::UndefinedSymbol(name.to_string()))?;
65
437
    if sym.function().is_none() {
66
        return Err(Error::Compile(format!(
67
            "compile: '{name}' is not a function"
68
        )));
69
437
    }
70
437
    Ok(Expr::quoted(Expr::Symbol(name.to_string())))
71
645
}
72

            
73
547
pub(super) fn eval_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
74
547
    if args.len() != 1 {
75
3
        return Err(Error::Arity {
76
3
            name: "eval".to_string(),
77
3
            expected: 1,
78
3
            actual: args.len(),
79
3
        });
80
544
    }
81
544
    let form = folded_eval_form(symbols, &args[0])?;
82
476
    debug!(expr = ?form, "compiling eval");
83
476
    eval_value(symbols, &form)
84
547
}
85

            
86
11
pub(super) fn compile_compile_form_for_stack(
87
11
    ctx: &mut CompileContext,
88
11
    emit: &mut FunctionEmitter,
89
11
    symbols: &mut SymbolTable,
90
11
    args: &[Expr],
91
11
) -> Result<WasmType> {
92
11
    let result = compile_form(symbols, args)?;
93
11
    compile_static_result_for_stack(ctx, emit, symbols, &result)
94
11
}
95

            
96
313
pub(super) fn compile_eval_form_for_stack(
97
313
    ctx: &mut CompileContext,
98
313
    emit: &mut FunctionEmitter,
99
313
    symbols: &mut SymbolTable,
100
313
    args: &[Expr],
101
313
) -> Result<WasmType> {
102
313
    if args.len() != 1 {
103
2
        return Err(Error::Arity {
104
2
            name: "eval".to_string(),
105
2
            expected: 1,
106
2
            actual: args.len(),
107
2
        });
108
311
    }
109
311
    let form = folded_eval_form(symbols, &args[0])?;
110
260
    compile_for_stack(ctx, emit, symbols, &form)
111
313
}