Lines
99.1 %
Functions
100 %
Branches
//! A fold-safe form folds to its datum; anything else is compiled.
use super::*;
#[test]
fn a_fold_safe_form_folds_to_its_datum() {
let mut symbols = with_constant("C", "'(1 2)");
assert_eq!(fold_checked(&mut symbols, "(+ 1 2)"), Some(form("3")));
assert_eq!(fold_checked(&mut symbols, "(length c)"), Some(form("2")));
assert_eq!(
fold_checked(&mut symbols, "(if (< 1 2) \"a\" \"b\")"),
Some(form("\"a\""))
);
fold_checked(&mut symbols, "(cons 0 c)"),
Some(datum("'(0 1 2)"))
fold_checked(&mut symbols, "(upcase-string \"ab\")"),
Some(form("\"AB\""))
assert_eq!(fold_checked(&mut symbols, "(car c)"), Some(form("1")));
assert_eq!(fold_checked(&mut symbols, "(cdr c)"), Some(datum("'(2)")));
fold_checked(&mut symbols, "(reverse c)"),
Some(datum("'(2 1)"))
fold_checked(&mut symbols, "(append c c)"),
Some(datum("'(1 2 1 2)"))
assert_eq!(fold_checked(&mut symbols, "(pair? c)"), Some(form("t")));
}
fn a_selector_of_a_constant_symbol_list_folds_to_the_quoted_symbol() {
let mut symbols = table();
let quoted = Some(datum("'a"));
let folded = fold_call(&mut symbols, "CAR", &[form("'(a b)")]);
assert_eq!(folded.ok().flatten(), quoted);
let folded = fold_value(&mut symbols, &form("(car '(a b))"));
fn a_call_that_is_not_fold_safe_or_errs_is_not_folded() {
let mut symbols = with_runtime("G");
for (head, arg) in [
("CAR", "g"),
("CAR", "(progn (setf z 1) '(1))"),
("CAR", "1"),
("NOT", "g"),
("MAP", "'(1)"),
] {
let folded = fold_call(&mut symbols, head, &[form(arg)]);
assert_eq!(folded.ok().flatten(), None, "({head} {arg})");
fn an_operation_error_on_constants_means_compile_it() {
for source in [
"(/ 1 0)",
"(mod 1 0)",
"(* 1073741823 1073741823 1073741823)",
"(length 'g)",
"(length 1)",
"(car 1)",
"(cdr 1)",
"(reverse 1)",
"(append '(1) 2)",
"(let ((x 'g)) (length x))",
"(let* ((x (quote g))) (length x))",
"(dolist (x '(g)) (length x))",
"(+ 1 \"a\")",
"(cons 1 2 3)",
"(undefined-name 1)",
assert_eq!(fold_checked(&mut symbols, source), None, "{source}");
fn only_a_fold_safe_division_by_zero_raises_division() {
let symbols = table();
"(/ 1/2 0/1)",
"(+ 1 (/ 1 0))",
"(progn (/ 1 0))",
assert!(raises_division(&symbols, &form(source)), "{source}");
"(/ 4 2)",
"(progn (print \"x\") (/ 1 0))",
"(error 'x \"m\")",
assert!(!raises_division(&symbols, &form(source)), "{source}");
fn a_resource_error_propagates() {
for _ in 0..crate::compiler::budget::MAX_ELAB_WORK {
if symbols.charge_work(1).is_err() {
break;
assert!(matches!(
fold_value(&mut symbols, &form("(+ 1 2)")),
Err(Error::Resource(_))
));
/// The DOLIST half of obligation (f): the element `G` of `'(g)` is bound as
/// the datum `G`, so the body's `(length x)` is LENGTH of a symbol — an
/// error, not `g`'s length — and the loop variable is restored on that
/// error path, an outer binding of the same name intact.
fn dolist_binds_an_extracted_symbol_as_data_and_restores_on_error() {
let body = form("(dolist (x '(g)) (length x))");
assert!(eval_value(&mut symbols, &body).is_err());
assert!(symbols.lookup("X").is_none());
let mut outer = with_runtime("G");
outer.define(Symbol::new("X", SymbolKind::Variable).with_value(form("9")));
assert!(eval_value(&mut outer, &body).is_err());
assert_eq!(outer.lookup("X").and_then(Symbol::value), Some(&form("9")));
fn let_binds_a_quoted_symbol_as_data() {
"(let ((x (quote g))) (length x))",
assert!(eval_value(&mut symbols, &form(source)).is_err(), "{source}");