Lines
96.47 %
Functions
87.5 %
Branches
100 %
//! `COMPILE` and `EVAL` reflective forms.
//!
//! `COMPILE` resolves a quoted symbol to its function definition and
//! returns a quoted symbol marking it as compiled (the marker is what
//! the next pipeline stage picks up; today the marker just round-
//! trips). `EVAL` resolves its argument and re-runs the evaluator on
//! the inner form.
use tracing::debug;
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{compile_expr, compile_for_stack, eval_value};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::compile_static_result_for_stack;
use super::folded::{folded_eval_form, folded_symbol};
pub(super) fn compile_compile_form(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<()> {
let result = compile_form(symbols, args)?;
compile_expr(ctx, emit, symbols, &result)
}
pub(super) fn compile_eval_form(
if args.len() != 1 {
return Err(Error::Arity {
name: "eval".to_string(),
expected: 1,
actual: args.len(),
});
let form = folded_eval_form(symbols, &args[0])?;
compile_expr(ctx, emit, symbols, &form)
pub(super) fn compile_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
name: "compile".to_string(),
let name = folded_symbol(symbols, "COMPILE", &args[0])?;
compile_form_with_name(symbols, &name)
pub(super) fn compile_form_with_name(symbols: &mut SymbolTable, name: &str) -> Result<Expr> {
debug!(function = %name, "compiling compile");
let sym = symbols
.lookup(name)
.ok_or_else(|| Error::UndefinedSymbol(name.to_string()))?;
if sym.function().is_none() {
return Err(Error::Compile(format!(
"compile: '{name}' is not a function"
)));
Ok(Expr::quoted(Expr::Symbol(name.to_string())))
pub(super) fn eval_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
debug!(expr = ?form, "compiling eval");
eval_value(symbols, &form)
pub(super) fn compile_compile_form_for_stack(
) -> Result<WasmType> {
compile_static_result_for_stack(ctx, emit, symbols, &result)
pub(super) fn compile_eval_form_for_stack(
compile_for_stack(ctx, emit, symbols, &form)