Lines
100 %
Functions
Branches
//! The evaluator's control transfer (0014 P14): inside a macro body a
//! RETURN-FROM reached for certain raises `Error::Transfer`, the BLOCK it
//! names catches it, and every other evaluation keeps the runtime placeholder.
use crate::ast::Expr;
use crate::compiler::expr::eval_value;
use crate::error::Error;
use crate::reader::Reader;
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
fn form(source: &str) -> Expr {
Reader::parse(source)
.ok()
.and_then(|program| program.exprs.into_iter().next())
.unwrap_or(Expr::Nil)
}
fn macro_body_table() -> SymbolTable {
let mut symbols = SymbolTable::with_builtins_for_wasm();
symbols.mark_macro_expansion();
symbols.define(Symbol::new("X", SymbolKind::Variable).with_value(Expr::Nil));
symbols
fn number(value: i64) -> Expr {
form(&value.to_string())
#[test]
fn a_block_catches_the_transfer_that_names_it() {
let mut symbols = macro_body_table();
let value = eval_value(&mut symbols, &form("(block c (return-from c 5) 0)"));
assert!(matches!(value, Ok(ref v) if *v == number(5)), "{value:?}");
fn the_inner_block_of_the_same_name_catches_first() {
let value = eval_value(
&mut symbols,
&form("(block c (+ 1 (block c (return-from c 5))))"),
);
assert!(matches!(value, Ok(ref v) if *v == number(6)), "{value:?}");
fn a_transfer_naming_another_block_propagates() {
let value = eval_value(&mut symbols, &form("(block c (return-from d 5) 0)"));
assert!(
matches!(value, Err(Error::Transfer { ref block, .. }) if block == "D"),
"{value:?}"
fn an_outer_block_catches_a_transfer_through_an_inner_one() {
&form("(block d (block c (return-from d 9)) 0)"),
assert!(matches!(value, Ok(ref v) if *v == number(9)), "{value:?}");
fn a_constant_test_branch_is_reached_unconditionally() {
&form("(block c (if (= 1 1) (return-from c 5) 0) 9)"),
fn unwind_protect_cleanup_runs_when_a_transfer_passes_through() {
&form("(block d (block c (unwind-protect (return-from c 5) (return-from d 9))) 0)"),
fn outside_a_macro_body_the_exit_stays_a_runtime_placeholder() {
let value = eval_value(&mut symbols, &form("(return-from c 5)"));
matches!(value, Ok(ref v) if v.is_wasm_runtime()),
fn under_a_runtime_branch_the_exit_stays_a_runtime_placeholder() {
assert!(symbols.enter_runtime_branch().is_ok());
fn a_block_whose_exit_is_not_taken_has_its_body_value() {
let value = eval_value(&mut symbols, &form("(block b (if x (return-from b 1)) 2)"));
assert!(matches!(value, Ok(ref v) if *v == number(2)), "{value:?}");
fn a_dolist_exit_that_is_not_taken_leaves_the_body_value() {
&form("(block b (dolist (i (list 1 2)) (if x (return-from b i))) 7)"),
assert!(matches!(value, Ok(ref v) if *v == number(7)), "{value:?}");
fn under_a_runtime_branch_a_block_with_an_exit_stays_runtime() {