Lines
99.3 %
Functions
100 %
Branches
//! The per-head lemma over the whitelist and the macroexpand heads.
use super::*;
/// The per-head lemma: with constant operands — well-formed and malformed
/// alike — a whitelisted head's eval answers a datum or an error, never a
/// placeholder, and the table is unchanged either way.
#[test]
fn whitelisted_heads_answer_a_datum_or_an_error_and_keep_the_table() {
let mut symbols = with_constant("C", "'(1 2)");
for source in [
"(block b 1)",
"(block)",
"(if t 1 2)",
"(if)",
"(progn)",
"(progn 1 2)",
"(let ((x 1)) x)",
"(let x)",
"(let* ((x 1) (y x)) y)",
"(dolist (x c) x)",
"(dolist (x 1) x)",
"(dolist x)",
"(and 1 nil)",
"(or nil 2)",
"(quote (1 . 2))",
"(quote)",
"(list 1 'a \"s\")",
"(cons 1 c)",
"(cons 1)",
"(length c)",
"(length 1)",
"(car c)",
"(car nil)",
"(car 1)",
"(car)",
"(cdr c)",
"(cdr (quote (1 . 2)))",
"(cdr 1)",
"(cdr c c)",
"(reverse c)",
"(reverse nil)",
"(reverse 1)",
"(reverse)",
"(append c c)",
"(append)",
"(append c 1)",
"(append 1 c)",
"(pair? c)",
"(pair? nil)",
"(pair? 1)",
"(pair?)",
"(not nil)",
"(not)",
"(null? nil)",
"(null? c)",
"(null?)",
"(= 1 1)",
"(= 1 \"a\")",
"(/= 1 2)",
"(< 1 2 3)",
"(< 'a 1)",
"(> 2 1)",
"(<= 1 1)",
"(>= 1 2)",
"(+ 1 1/2)",
"(+ 1 \"a\")",
"(- 1)",
"(-)",
"(* 2 3)",
"(* 1000000000 1000000000 1000000000)",
"(/ 3 2)",
"(/ 1 0)",
"(/)",
"(mod 7 2)",
"(mod 1 0)",
"(mod 1)",
"(upcase-string \"a\")",
"(upcase-string 1)",
] {
let before = strip_charge(&symbols.state_digest());
let answer = eval_value(&mut symbols, &form(source));
assert_eq!(before, strip_charge(&symbols.state_digest()), "{source}");
match answer {
Ok(value) => assert!(is_constant_datum(&value), "{source}: {value:?}"),
Err(Error::Resource(_)) => panic!("{source}: resource error"),
Err(_) => {}
}
/// A macro body's store to anything outside its private frame is refused
/// (ADR-0034:233); a store to its own parameter or LET variable is allowed.
fn a_macro_body_may_not_mutate_a_global() {
let mut symbols = with_constant("G", "0");
symbols.define(
Symbol::new("BUMP", SymbolKind::Macro).with_function(Expr::Lambda(
crate::ast::LambdaParams::simple(vec!["V".to_string()]),
Box::new(form(
"(progn (setf v 1) (let ((w 0)) (setf w 2)) (setf g 1))",
)),
);
let err = eval_value(&mut symbols, &form("(bump 0)")).expect_err("the global store is refused");
assert!(err.to_string().contains("may not mutate a global"), "{err}");
Symbol::new("LOCAL", SymbolKind::Macro).with_function(Expr::Lambda(
Box::new(form("(progn (setf v 1) (let ((w 0)) (setf w 2) w))")),
assert_eq!(
eval_value(&mut symbols, &form("(local 0)")).ok(),
Some(form("2"))
/// `MACROEXPAND-1` / `MACROEXPAND` expand in a private clone: the answer is a
/// constant datum or an error and the table's bindings are untouched, even
/// for a macro body that prints, stores or reads a global.
fn macroexpand_heads_answer_a_datum_or_an_error_and_keep_the_bindings() {
for (name, params, body) in [
("TWICE", vec!["X"], "`(+ ,x ,x)"),
("NOISY", vec!["X"], "(progn (print 99) `(+ ,x 1))"),
("LOCAL", vec!["V"], "(progn (setf v 1) `(quote ,v))"),
("READG", vec![], "`(+ ,g 1)"),
Symbol::new(name, SymbolKind::Macro).with_function(Expr::Lambda(
crate::ast::LambdaParams::simple(params.into_iter().map(String::from).collect()),
Box::new(form(body)),
"(macroexpand-1 '(when t 1))",
"(macroexpand '(when t 1))",
"(car (macroexpand-1 '(when t 1)))",
"(car (macroexpand '(unless t 1)))",
"(macroexpand-1 '(twice 3))",
"(macroexpand '(twice 3))",
"(macroexpand-1 '(noisy 3))",
"(macroexpand '(local 3))",
"(macroexpand-1 '(readg))",
"(macroexpand-1 '(twice))",
"(macroexpand-1)",
"(macroexpand 1 2)",
"(macroexpand-1 1)",
"(macroexpand '(car '(1)))",
let folded = fold_value(&mut symbols, &form(source));
if let Ok(Some(value)) = folded {
assert!(is_constant_datum(&value), "{source}: {value:?}")
fold_value(&mut symbols, &form("(car (macroexpand-1 '(when t 1)))")).ok(),
Some(Some(datum("'if")))