Lines
58.45 %
Functions
15.56 %
Branches
100 %
use tracing::debug;
use crate::ast::Expr;
use crate::error::{Error, Result};
use crate::runtime::{SymbolKind, SymbolTable};
use super::super::context::CompileContext;
use super::super::emit::FunctionEmitter;
use super::super::expr::{compile_expr, eval_value, format_expr, resolve_arg};
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 resolved = resolve_arg(symbols, &args[0])?;
match &resolved {
Expr::Quote(inner) => compile_expr(ctx, emit, symbols, inner),
_ => compile_expr(ctx, emit, symbols, &resolved),
pub(super) fn compile_describe(
let result = describe(symbols, args)?;
pub(super) fn compile_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
name: "compile".to_string(),
let name = match &args[0] {
Expr::Quote(inner) => match inner.as_ref() {
Expr::Symbol(s) => s,
_ => {
return Err(Error::Compile(
"compile: argument must be a quoted symbol".to_string(),
));
},
Expr::Symbol(_) => {
Expr::Symbol(s) => {
return compile_form_with_name(symbols, s);
};
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::Quote(Box::new(Expr::Symbol(name.to_string()))))
pub(super) fn eval_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
let resolved = eval_value(symbols, &args[0])?;
debug!(expr = ?resolved, "compiling eval");
Expr::Quote(inner) => eval_value(symbols, inner),
_ => eval_value(symbols, &resolved),
pub(super) fn describe(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
name: "DESCRIBE".to_string(),
Expr::Symbol(s) => s.clone(),
"DESCRIBE: argument must be a symbol".to_string(),
.lookup(&name)
.ok_or_else(|| Error::UndefinedSymbol(name.clone()))?;
let mut lines = Vec::new();
let kind_str = match sym.kind() {
SymbolKind::Macro => "a macro",
_ if sym.function().is_some() => "a function",
SymbolKind::Variable => "a variable",
SymbolKind::Operator => "an operator",
SymbolKind::Native => "a native function",
SymbolKind::SpecialForm => "a special form",
SymbolKind::Function => "a function",
lines.push(format!("{name} is {kind_str}"));
if let Some(func) = sym.function() {
lines.push(format!(" Lambda: {}", format_expr(func)));
if let Some(val) = sym.value() {
let type_name = match val {
Expr::Nil | Expr::Bool(false) => "Nil",
Expr::Bool(true) => "Bool",
Expr::Number(_) => "Number",
Expr::String(_) => "String",
Expr::Symbol(_) => "Symbol",
Expr::Lambda(_, _) => "Lambda",
_ => "Compound",
lines.push(format!(" Value: {} ({})", format_expr(val), type_name));
if let Some(doc) = sym.doc() {
lines.push(format!(" Documentation: \"{doc}\""));
Ok(Expr::String(lines.join("\n")))