Lines
100 %
Functions
Branches
//! `APROPOS` — search the symbol table for names containing a
//! substring (case-insensitive), return a sorted quoted list of
//! matching symbols. Powers `nomisync-help` completion in emacs and
//! the REPL's tab-completion.
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{compile_expr, format_expr};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::compile_static_result_for_stack;
use super::folded::folded_form;
fn folded_needle(symbols: &mut SymbolTable, arg: &Expr) -> Result<String> {
match folded_form(symbols, "APROPOS", arg)? {
Expr::String(s) => Ok(s),
Expr::Symbol(s) | Expr::Keyword(s) => Ok(s.trim_start_matches(':').to_string()),
Expr::Nil => Ok("NIL".to_string()),
other => Err(Error::Compile(format!(
"APROPOS: argument must be a string or symbol, got {}",
format_expr(&other)
))),
}
pub(super) fn apropos(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
if args.len() != 1 {
return Err(Error::Arity {
name: "APROPOS".to_string(),
expected: 1,
actual: args.len(),
});
let needle = folded_needle(symbols, &args[0])?;
let needle_uc = needle.to_uppercase();
let mut matches: Vec<String> = symbols
.iter()
.filter(|(name, _)| name.to_uppercase().contains(&needle_uc))
.map(|(name, _)| name.clone())
.collect();
matches.sort();
let elems = matches.into_iter().map(Expr::Symbol).collect();
Ok(Expr::quoted_list(elems))
pub(super) fn compile_apropos(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<()> {
let result = apropos(symbols, args)?;
compile_expr(ctx, emit, symbols, &result)
pub(super) fn compile_apropos_for_stack(
) -> Result<WasmType> {
compile_static_result_for_stack(ctx, emit, symbols, &result)