Lines
93.33 %
Functions
50 %
Branches
100 %
//! `SYMBOLP`, `KEYWORDP` and `SYMBOL-NAME`. `nil` and `t` are symbols. A
//! static operand folds; a runtime `SymbolRef` or `Bool` has its answer from
//! its type, and an `AnyRef` is tested for its shape (`nil` is `ref.null`, `t`
//! the `$t` singleton, a symbol a `$symbol`).
use super::NativeSpec;
use super::comparison::bool_result;
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{
NilRepr, compile_for_stack, describe_operand, eval_value, fold_call, nil_repr,
};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use wasm_encoder::BlockType;
pub(super) const NATIVES: &[NativeSpec] = &[
NativeSpec {
name: "SYMBOLP",
eval: symbolp,
stack: Some(compile_symbolp_to_stack),
effect: None,
},
name: "KEYWORDP",
eval: keywordp,
stack: Some(compile_keywordp_to_stack),
name: "SYMBOL-NAME",
eval: symbol_name,
stack: Some(compile_symbol_name_to_stack),
];
/// What an evaluated operand is, as far as the symbol natives can tell.
enum Operand {
Symbol { name: String, keyword: bool },
Other,
Runtime(WasmType),
}
fn symbol_operand(name: &str, keyword: bool) -> Operand {
Operand::Symbol {
name: name.to_string(),
keyword,
fn classify(value: &Expr) -> Operand {
if let Some(ty) = value.wasm_type() {
return Operand::Runtime(ty);
match value {
Expr::Nil => symbol_operand("NIL", false),
Expr::T => symbol_operand("T", false),
Expr::Symbol(name) => symbol_operand(name, false),
Expr::Keyword(name) => symbol_operand(name, true),
Expr::Datum(inner) | Expr::Quote(inner) => match inner.as_ref() {
_ => Operand::Other,
fn check_arity(name: &str, args: &[Expr]) -> Result<()> {
if args.len() == 1 {
return Ok(());
Err(Error::Arity {
expected: 1,
actual: args.len(),
})
fn operand(symbols: &mut SymbolTable, name: &str, args: &[Expr]) -> Result<Operand> {
check_arity(name, args)?;
eval_value(symbols, &args[0]).map(|value| classify(&value))
fn symbolp(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
Ok(match operand(symbols, "SYMBOLP", args)? {
Operand::Symbol { .. } => bool_result(true),
Operand::Other => bool_result(false),
Operand::Runtime(_) => Expr::WasmRuntime(WasmType::Bool),
fn keywordp(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
Ok(match operand(symbols, "KEYWORDP", args)? {
Operand::Symbol { keyword, .. } => bool_result(keyword),
fn symbol_name(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
match operand(symbols, "SYMBOL-NAME", args)? {
Operand::Symbol { name, .. } => Ok(Expr::String(name)),
Operand::Runtime(ty) if names_a_symbol(ty) => Ok(Expr::WasmRuntime(WasmType::StringRef)),
Operand::Runtime(ty) => Err(not_a_symbol(&ty.to_string())),
Operand::Other => Err(not_a_symbol("a non-symbol value")),
fn names_a_symbol(ty: WasmType) -> bool {
matches!(ty, WasmType::Bool | WasmType::SymbolRef | WasmType::AnyRef)
fn not_a_symbol(got: &str) -> Error {
Error::Compile(format!("SYMBOL-NAME expects a symbol, got {got}"))
fn compile_symbolp_to_stack(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<WasmType> {
check_arity("SYMBOLP", args)?;
if let Some(folded) = fold_call(symbols, "SYMBOLP", args)? {
emit.i32_const(i32::from(!folded.is_nil()));
return Ok(WasmType::Bool);
let ty = compile_for_stack(ctx, emit, symbols, &args[0])?;
emit_symbolp(ctx, emit, ty)?;
Ok(WasmType::Bool)
fn emit_symbolp(ctx: &mut CompileContext, emit: &mut FunctionEmitter, ty: WasmType) -> Result<()> {
match ty {
WasmType::Bool | WasmType::SymbolRef => emit_constant(emit, true),
WasmType::AnyRef => {
let local = ctx.alloc_local(ty)?;
emit.local_tee(local);
emit.ref_test(ctx.ids.ty_symbol);
emit.local_get(local);
emit.ref_cast_eq();
emit.global_get(ctx.ids.global_t);
emit.ref_eq();
emit.i32_or();
_ => match nil_repr(&ty) {
NilRepr::Null => emit.ref_is_null(),
NilRepr::Never | NilRepr::I32Zero => emit_constant(emit, false),
Ok(())
/// Replaces the operand on top of the stack with a constant truth value.
fn emit_constant(emit: &mut FunctionEmitter, value: bool) {
emit.drop_value();
emit.i32_const(i32::from(value));
fn compile_keywordp_to_stack(
check_arity("KEYWORDP", args)?;
if let Some(folded) = fold_call(symbols, "KEYWORDP", args)? {
emit_keywordp(ctx, emit, ty)?;
fn emit_keywordp(ctx: &mut CompileContext, emit: &mut FunctionEmitter, ty: WasmType) -> Result<()> {
WasmType::SymbolRef => emit.call(ctx.ids.symbol_keyword_p),
emit.if_block(BlockType::Result(wasm_encoder::ValType::I32));
emit.ref_cast_nullable(ctx.ids.ty_symbol);
emit.call(ctx.ids.symbol_keyword_p);
emit.else_block();
emit.i32_const(0);
emit.block_end()?;
_ => emit_constant(emit, false),
fn compile_symbol_name_to_stack(
check_arity("SYMBOL-NAME", args)?;
if let Some(Expr::String(name)) = fold_call(symbols, "SYMBOL-NAME", args)? {
return compile_for_stack(ctx, emit, symbols, &Expr::String(name));
if !names_a_symbol(ty) {
let got = describe_operand(symbols, &args[0], ty);
emit.reject_if_live(not_a_symbol(&got))?;
return Ok(WasmType::StringRef);
emit_symbol_name(ctx, emit, symbols, ty)?;
Ok(WasmType::StringRef)
fn emit_symbol_name(
ty: WasmType,
) -> Result<()> {
let string = BlockType::Result(ctx.string_ref());
WasmType::Bool => {
emit.if_block(string);
push_name(ctx, emit, symbols, "T")?;
push_name(ctx, emit, symbols, "NIL")?;
emit.block_end()
WasmType::SymbolRef => {
emit.ref_is_null();
emit.call(ctx.ids.symbol_designator);
_ => emit_any_symbol_name(ctx, emit, symbols, ty, string),
/// An `AnyRef` operand: `nil`, `t` or a `$symbol` names itself, anything else
/// throws a catchable `TYPE-ERROR`.
fn emit_any_symbol_name(
string: BlockType,
emit.call(ctx.ids.not_a_symbol);
emit.unreachable();
fn push_name(
name: &str,
compile_for_stack(ctx, emit, symbols, &Expr::String(name.to_string())).map(drop)
#[cfg(test)]
#[path = "symbol_tests.rs"]
mod tests;