Lines
98.91 %
Functions
100 %
Branches
use super::*;
use crate::ast::Number;
use crate::ast::{ClosureSigId, LambdaParams, WasmType};
use crate::reader::Reader;
use crate::runtime::{Symbol, SymbolKind};
fn forms(source: &str) -> Vec<Expr> {
Reader::parse(source)
.map(|program| program.exprs)
.unwrap_or_default()
}
fn targets(source: &str) -> Vec<String> {
collect_setf_targets(&forms(source))
.into_iter()
.map(|(name, _)| name)
.collect()
#[test]
fn a_lambda_parameter_shadows_the_outer_variable() {
assert!(targets("(lambda (x) (setf x 1))").is_empty());
assert_eq!(targets("(lambda (y) (setf x 1))"), ["X"]);
fn a_binders_init_runs_outside_its_own_scope() {
assert_eq!(targets("(let ((x (setf x 1))) (setf x 2))"), ["X"]);
assert!(targets("(let ((x 0)) (setf x 2))").is_empty());
assert!(targets("(let* ((x 0) (y (setf x 1))) y)").is_empty());
assert_eq!(targets("(do ((x (setf x 1) (setf x 2))) (t))"), ["X"]);
assert!(targets("(do ((x 0 (setf x 2))) (t))").is_empty());
assert_eq!(targets("(dolist (x (setf x 1)) (setf x 2))"), ["X"]);
fn a_lambda_default_sees_only_the_parameters_before_it() {
assert_eq!(
targets("(lambda (&optional (x (setf y 1)) (y 2)) x)"),
["Y"]
);
assert!(targets("(lambda (&optional (y 2) (x (setf y 1))) x)").is_empty());
fn a_case_key_or_clause_name_is_not_a_form() {
assert!(targets("(case k (setf y 1))").is_empty());
assert!(targets("(handler-case (f) (setf (e) y))").is_empty());
assert_eq!(targets("(case k (a (setf y 1)))"), ["Y"]);
assert_eq!(targets("(cond ((setf y 1) 2))"), ["Y"]);
assert_eq!(targets("(do () ((setf y 1) (setf z 2)))"), ["Y", "Z"]);
fn code_forms_skip_names_keys_and_codes() {
let shown = |source: &str| {
let parsed = forms(source);
let Some(Expr::List(elems)) = parsed.first() else {
return Vec::new();
};
code_forms(elems).cloned().collect::<Vec<_>>()
assert_eq!(shown("(cond (a b) (c))"), forms("a b c"));
assert_eq!(shown("(case k (1 a b) ((2) c))"), forms("k a b c"));
assert_eq!(shown("(handler-case x (e (v) a))"), forms("x a"));
assert_eq!(shown("(labels ((f (p) a b)) c)"), forms("a b c"));
assert_eq!(shown("(do () (m a) b)"), forms("m a b"));
assert_eq!(shown("(do ((i 0 s)) (m a) b)"), forms("0 s m a b"));
assert_eq!(shown("(defun f (p (q d)) a)"), forms("d a"));
fn code_mapped_rewrites_only_the_code_positions() {
let mapped = |source: &str| {
let mark = forms("x").into_iter().next().unwrap_or(Expr::Nil);
code_mapped(elems, &mut |form| {
Ok(Expr::List(vec![mark.clone(), form.clone()]))
})
assert_eq!(mapped("(case k (1 a))"), forms("(x case) (x k) (1 (x a))"));
mapped("(defmacro m (p (q d)) a)"),
forms("(x defmacro) m (p (q (x d))) (x a)")
mapped("(labels ((f (p) a)) b)"),
forms("(x labels) ((f (p) (x a))) (x b)")
fn quoted_data_assigns_nothing() {
assert!(targets("(quote (setf x 1))").is_empty());
assert!(targets("'(setf x 1)").is_empty());
fn assignment_queries_share_the_scoped_walk() {
let body = forms("(setf x 1) (lambda (x) (setf x 2)) (setf y 3)");
assert!(assigns(&body, "X"));
setf_rhs_of(&body, "X"),
[&Expr::Number(Number::Index(1.into()))]
assert!(!assigns(&forms("(lambda (x) (setf x 2))"), "X"));
fn element(source: &str) -> Option<PairElement> {
let symbols = SymbolTable::with_builtins_for_wasm();
forms(source)
.first()
.and_then(|form| step_pair_element(form, &symbols))
fn a_native_cons_or_list_builds_its_element() {
assert_eq!(element("(cons \"a\" r)"), Some(PairElement::StringRef));
assert_eq!(element("(list \"a\" \"b\")"), Some(PairElement::StringRef));
assert_eq!(element("(list t \"b\")"), Some(PairElement::AnyRef));
fn a_list_inside_the_stored_value_is_not_the_stored_value() {
assert_eq!(element("(block c (dolist (y (list \"a\")) y) 0)"), None);
fn quoted_and_lambda_subtrees_build_nothing() {
assert_eq!(element("(car (list (quote (cons \"a\" 2))))"), None);
assert_eq!(element("(lambda () (cons \"a\" r))"), None);
fn disagreeing_branch_arms_widen() {
element("(if c (cons \"a\" r) (list t))"),
Some(PairElement::AnyRef)
element("(if c (cons \"a\" r) r)"),
Some(PairElement::StringRef)
element("(cond (a (cons \"a\" r)) (t (cons t r)))"),
fn identity_lambda() -> Expr {
Expr::Lambda(
LambdaParams::simple(vec!["X".to_string()]),
Box::new(Expr::Symbol("X".to_string())),
)
fn a_user_cons_builds_nothing() {
let mut symbols = SymbolTable::with_builtins_for_wasm();
symbols.define(
crate::runtime::Symbol::new("CONS", SymbolKind::Function).with_function(identity_lambda()),
let form = &forms("(cons \"a\" r)")[0];
assert_eq!(step_pair_element(form, &symbols), None);
fn a_defun_name_stays_user_callable_over_a_closure_local() {
let defun =
crate::runtime::Symbol::new("F", SymbolKind::Function).with_function(identity_lambda());
symbols.define(defun.clone());
assert!(names_user_callable("F", &symbols));
symbols.define(defun.with_value(Expr::WasmLocal(3, WasmType::Closure(ClosureSigId(7)))));
fn a_runtime_list_item_rides_a_cell_as_any() {
crate::runtime::Symbol::new("ACC", SymbolKind::Variable)
.with_value(Expr::WasmRuntime(WasmType::PairRef(PairElement::I32))),
let form = &forms("(list acc 1)")[0];
assert_eq!(step_pair_element(form, &symbols), Some(PairElement::AnyRef));
fn symbols_with_f(body: Expr) -> SymbolTable {
crate::runtime::Symbol::new("F", SymbolKind::Function).with_function(Expr::Lambda(
Box::new(body),
)),
symbols
fn a_user_function_car_is_typed_by_its_result() {
let symbols = symbols_with_f(Expr::Symbol("X".to_string()));
let form = &forms("(cons (f \"a\") r)")[0];
step_pair_element(form, &symbols),
fn a_non_terminating_user_function_car_builds_nothing() {
let symbols = symbols_with_f(forms("(f x)").remove(0));
let form = &forms("(cons (f 1) r)")[0];
fn element_with_closure(source: &str, body: Expr) -> Option<PairElement> {
let closure = Expr::Lambda(LambdaParams::simple(Vec::new()), Box::new(body));
symbols.define(Symbol::new("F", SymbolKind::Variable).with_value(closure));
fn a_funcall_of_a_closure_variable_builds_the_closures_element() {
let five = Expr::Number(Number::Index(5));
element_with_closure("(cons (funcall f) r)", five),
Some(PairElement::I32)
element_with_closure("(cons (funcall f) r)", Expr::String("s".to_string())),
fn a_funcall_of_an_unbound_variable_builds_nothing() {
assert_eq!(element("(cons (funcall f) r)"), None);
fn a_call_head_that_is_not_a_symbol_is_scanned() {
assert_eq!(targets("((lambda () (setf y 1)))"), ["Y"]);