Lines
100 %
Functions
Branches
use super::NATIVES;
use crate::ast::{Expr, PairElement, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::eval_value;
use crate::error::{Error, Result};
use crate::reader::Reader;
use crate::runtime::SymbolTable;
fn spec(name: &str) -> &'static crate::compiler::native::NativeSpec {
NATIVES
.iter()
.find(|spec| spec.name == name)
.expect("registered search native")
}
fn args(sources: &[&str]) -> Result<Vec<Expr>> {
sources.iter().map(|s| Reader::parse_expr(s)).collect()
fn eval(name: &str, sources: &[&str]) -> Result<Expr> {
let mut symbols = SymbolTable::with_builtins();
(spec(name).eval)(&mut symbols, &args(sources)?)
fn value(source: &str) -> Expr {
eval_value(&mut symbols, &Reader::parse_expr(source).unwrap()).unwrap()
fn lower(name: &str, sources: &[&str]) -> Result<WasmType> {
let mut ctx = CompileContext::new_with_host_fns(&[])?;
let mut emit = FunctionEmitter::new();
let stack = spec(name).stack.expect("search natives lower to the stack");
let ty = stack(&mut ctx, &mut emit, &mut symbols, &args(sources)?)?;
emit.finish(&[])?;
Ok(ty)
#[test]
fn getf_folds_a_constant_plist() {
assert_eq!(eval("GETF", &["'(:a 1 :b 2)", ":b"]).unwrap(), value("2"));
fn getf_answers_the_default_or_nil_for_a_missing_key() {
assert_eq!(eval("GETF", &["'(:a 1)", ":z", "9"]).unwrap(), value("9"));
assert_eq!(eval("GETF", &["'(:a 1)", ":z"]).unwrap(), Expr::Nil);
fn getf_keys_are_eql_so_a_keyword_is_not_its_symbol() {
assert_eq!(eval("GETF", &["'(:a 1)", "'a"]).unwrap(), Expr::Nil);
fn getf_of_a_trailing_key_without_a_value_is_nil() {
assert_eq!(eval("GETF", &["'(:a 1 :b)", ":b"]).unwrap(), Expr::Nil);
fn member_folds_to_the_tail() {
assert_eq!(
eval("MEMBER", &["'b", "'(a b c)"]).unwrap(),
value("'(b c)")
);
assert_eq!(eval("MEMBER", &["'z", "'(a b c)"]).unwrap(), Expr::Nil);
fn assoc_folds_to_the_first_matching_entry() {
eval("ASSOC", &["'b", "'((a 1) (b 2) (b 3))"]).unwrap(),
value("'(b 2)")
assert_eq!(eval("ASSOC", &["'z", "'((a 1))"]).unwrap(), Expr::Nil);
fn assoc_skips_nil_entries_and_refuses_other_atoms() {
eval("ASSOC", &["'b", "'(nil (b 2))"]).unwrap(),
assert!(matches!(
eval("ASSOC", &["'b", "'(1 (b 2))"]),
Err(Error::Compile(ref m)) if m.contains("alist")
));
fn a_non_list_is_a_compile_error() {
for name in ["GETF", "MEMBER", "ASSOC"] {
let sources: &[&str] = if name == "GETF" {
&["1", ":a"]
} else {
&["'a", "1"]
};
assert!(
matches!(eval(name, sources), Err(Error::Compile(ref m)) if m.contains(name)),
"{name}"
fn test_and_key_arguments_are_refused() {
for name in ["MEMBER", "ASSOC"] {
for keyword in [":test", ":key", ":test-not"] {
let err = eval(name, &["'a", "'(a)", keyword, "'eql"]);
matches!(err, Err(Error::Compile(ref m)) if m.contains("not supported")),
"{name} {keyword}"
fn a_wrong_argument_count_is_an_arity_error() {
eval("GETF", &["'(:a 1)"]),
Err(Error::Arity { expected: 2, .. })
eval("GETF", &["'(:a 1)", ":a", "1", "2"]),
Err(Error::Arity { .. })
eval(name, &["'a"]),
fn a_runtime_operand_answers_the_type_of_the_walk() {
let runtime = Expr::WasmRuntime(WasmType::PairRef(PairElement::AnyRef));
let key = Reader::parse_expr(":a").unwrap();
let got = (spec("GETF").eval)(&mut symbols, &[runtime.clone(), key]).unwrap();
assert_eq!(got, Expr::WasmRuntime(WasmType::AnyRef));
let item = Reader::parse_expr("'a").unwrap();
let tail = (spec("MEMBER").eval)(&mut symbols, &[item, runtime]).unwrap();
tail,
Expr::WasmRuntime(WasmType::PairRef(PairElement::AnyRef))
fn constant_operands_lower_to_the_folded_value() {
lower("GETF", &["'(:a 1 :b 2)", ":b"]).unwrap(),
WasmType::I32
lower("MEMBER", &["'b", "'(a b c)"]).unwrap(),
WasmType::PairRef(PairElement::Symbol)
lower("ASSOC", &["'b", "'((a 1) (b 2))"]).unwrap(),
WasmType::PairRef(PairElement::AnyRef)
fn a_runtime_operand_lowers_to_the_walk() {
lower("MEMBER", &["(progn (print 1) 'b)", "'(a b c)"]).unwrap(),
lower("GETF", &["'(:a 1)", "(progn (print 1) :a)"]).unwrap(),
WasmType::AnyRef
fn expansion_text(search: super::Search, sources: &[&str], any: bool) -> String {
crate::compiler::expr::format_expr(&super::expansion(search, &args(sources).unwrap(), any))
fn an_any_list_operand_is_coerced_to_a_list_once() {
let any = expansion_text(super::Search::Getf, &["p", ":k"], true);
assert!(any.contains("APPEND"), "{any}");
let typed = expansion_text(super::Search::Getf, &["p", ":k"], false);
assert!(!typed.contains("APPEND"), "{typed}");
fn only_the_list_operand_is_coerced_for_assoc() {
let text = expansion_text(super::Search::Assoc, &["k", "alist"], true);
assert_eq!(text.matches("APPEND").count(), 1, "{text}");
assert!(text.contains("(APPEND nil ALIST)"), "{text}");
fn list_is_any_detects_only_an_any_operand() {
let quoted = args(&["'(1 2)", "1"]).unwrap();
let probed = super::probe_args(&symbols, "ed).unwrap();
assert!(!super::list_is_any(super::Search::Member, &probed));
symbols.define(
crate::runtime::Symbol::new("P", crate::runtime::SymbolKind::Variable)
.with_value(Expr::WasmRuntime(WasmType::AnyRef)),
let any = args(&["1", "p"]).unwrap();
let probed = super::probe_args(&symbols, &any).unwrap();
assert!(super::list_is_any(super::Search::Member, &probed));
fn refuse_kind_mix_refuses_a_key_of_another_kind_than_a_constant_candidate() {
let symbols = SymbolTable::with_builtins();
for (search, sources) in [
(super::Search::Member, ["1", "'(1/1 \"a\")"]),
(super::Search::Assoc, ["1", "'((1/1 2/1))"]),
(super::Search::Getf, ["'(1/1 \"a\")", "1"]),
] {
matches!(
super::refuse_kind_mix(
search,
&super::probe_args(&symbols, &args(&sources).unwrap()).unwrap()
),
Err(Error::Dimension(_))
"{sources:?}"
fn refuse_kind_mix_passes_a_key_of_the_candidates_kind() {
let same_kind = args(&["1/2", "'(1/2 \"a\")"]).unwrap();
let probed = super::probe_args(&symbols, &same_kind).unwrap();
assert!(super::refuse_kind_mix(super::Search::Member, &probed).is_ok());
fn constant_searches_across_kinds_are_refused_on_both_surfaces() {
for (name, sources) in [
("MEMBER", ["1", "'(1/1 \"a\")"]),
("ASSOC", ["1", "'((1/1 \"a\"))"]),
("GETF", ["'(1/1 \"a\")", "1"]),
matches!(eval(name, &sources), Err(Error::Dimension(_))),
matches!(lower(name, &sources), Err(Error::Dimension(_))),