Lines
98.08 %
Functions
81.82 %
Branches
100 %
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! The syntactic scan that decides whether a BLOCK may still fold: a macro is
//! never judged in the wrong environment, an exit that cannot run does not
//! stop the fold, and a constant-list tail never folds away an effect.
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Fraction, Number, Value};
fn eval(src: &str) -> Result<Value, String> {
let mut interp = Interpreter::new(false).map_err(|e| e.to_string())?;
interp
.eval(src)
.map_err(|e| e.to_string())?
.into_iter()
.next_back()
.ok_or_else(|| format!("{src:?} produced no value"))
}
fn index(n: i64) -> Value {
Value::Number(Number::Index(n))
fn ratio(numer: i64, denom: i64) -> Value {
Value::Number(Number::Scalar(Fraction::new(numer, denom)))
/// The macro reads `flag`'s init, nil, at expansion time, as in CL; the
/// enclosing LET's dynamic binding exists only at run time.
#[test]
fn a_macro_reads_the_init_of_a_dynamically_bound_special() {
let macros = "(defvar flag nil) (defmacro ex () (if flag '(return-from b 7) 0))";
let block = "(block b (let ((flag t)) (dolist (i (list 1)) (ex))) 0)";
assert_eq!(eval(&format!("{macros} (+ 1 {block})")), Ok(index(1)));
assert_eq!(eval(&format!("{macros} {block}")), Ok(index(0)));
/// A macro whose expansion cannot depend on the environment is still seen
/// through, so a block using one keeps folding.
fn a_template_macro_without_an_exit_still_folds() {
for src in [
"(+ 1/2 (block b (when nil (return-from b 7/1)) 1/1))",
"(+ 1/2 (block b (unless t (return-from b 7/1)) 1/1))",
"(defmacro m () 1/1) (+ 1/2 (block b (m) 1/1))",
] {
assert_eq!(eval(src), Ok(ratio(3, 2)), "{src}");
fn an_exit_that_cannot_run_keeps_the_tail_a_literal() {
"(+ 1/2 (block b (if nil (return-from b 7/1)) 1/1))",
"(+ 1/2 (block b (if t 1/1 (return-from b 7/1)) 1/1))",
"(+ 1/2 (block b (lambda () (return-from b 7/1)) 1/1))",
"(+ 1/2 (block b (let () (lambda () (return-from b 7/1)) 1/1)))",
fn a_called_lambda_exit_still_arrives() {
assert_eq!(
eval("(+ 1 (block b (funcall (lambda () (return-from b 5))) 0))"),
Ok(index(6))
);
/// A constant-list tail is a `$pair` chain like any list (plan 0014 Stage
/// 1), so the block's exit types it and the body's store runs. An exit whose
/// value is a raised condition is still refused: the condition's exit is
/// typed as a value (ISS-030, Stage 3).
fn a_constant_list_tail_never_folds_away_an_effect() {
eval(
"(defvar x (entity-count)) \
(length (block b (dolist (i (list 1)) (setf x 7) (return-from b (list 9))) (list 1 2))) \
x"
),
Ok(index(7))
let src =
"(length (block b (dolist (i (list 1)) (return-from b (error 'boom \"bad\"))) (list 1 2)))";
assert!(eval(src).is_err(), "{src}: {:?}", eval(src));