Lines
100 %
Functions
Branches
use super::*;
use crate::ast::WasmType;
use crate::compiler::expr::format_form;
use crate::reader::Reader;
use crate::runtime::{Symbol, SymbolKind};
fn parse(source: &str) -> Expr {
Reader::parse(source).expect("parses").exprs.remove(0)
}
fn scope() -> SymbolTable {
let mut symbols = SymbolTable::new();
let constants = [("R", "2"), ("S", "1/3")];
for (name, value) in constants {
symbols.define(Symbol::new(name, SymbolKind::Variable).with_value(parse(value)));
let identity = Expr::Lambda(
LambdaParams::simple(vec!["Y".to_string()]),
Box::new(Expr::symbol("Y")),
);
symbols.define(Symbol::new("F", SymbolKind::Variable).with_value(identity));
symbols.define(
Symbol::new("N", SymbolKind::Variable).with_value(Expr::WasmRuntime(WasmType::I32)),
symbols
fn closed(params: &[&str], body: &str) -> String {
let params = LambdaParams::simple(params.iter().map(ToString::to_string).collect());
format_form(&close_over_constants(&scope(), ¶ms, parse(body)).expect("closes"))
#[test]
fn the_constants_a_body_reads_are_bound_around_it() {
assert_eq!(closed(&["X"], "(* x r)"), "(LET ((R 2)) (* X R))");
assert_eq!(
closed(&["X"], "(* x r s)"),
"(LET ((R 2) (S 1/3)) (* X R S))"
fn a_constant_the_body_does_not_read_is_not_bound() {
assert_eq!(closed(&["X"], "(* x 5)"), "(* X 5)");
fn a_parameter_shadows_the_constant_of_its_name() {
assert_eq!(closed(&["R"], "(* r 5)"), "(* R 5)");
fn a_constant_the_body_stores_to_is_left_free() {
assert_eq!(closed(&[], "(setf r 3)"), "(SETF R 3)");
fn a_constant_lambda_is_bound_like_any_other() {
closed(&["X"], "(funcall f x)"),
"(LET ((F (LAMBDA (Y) Y))) (FUNCALL F X))"
fn a_runtime_variable_is_not_a_constant() {
assert_eq!(closed(&[], "n"), "N");
fn a_constant_holding_a_runtime_value_cannot_be_bound() {
let mut symbols = scope();
let runtime = Expr::List(vec![Expr::WasmLocal(3, WasmType::I32)]);
symbols.define(Symbol::new("B", SymbolKind::Variable).with_value(runtime));
let params = LambdaParams::simple(vec![]);
assert!(reads_unbound_constant(&symbols, ¶ms, &parse("b")));
assert!(!reads_unbound_constant(&symbols, ¶ms, &parse("r")));
fn a_symbol_or_list_constant_is_bound_quoted() {
symbols.define(Symbol::new("Q", SymbolKind::Variable).with_value(parse("Y")));
symbols.define(Symbol::new("L", SymbolKind::Variable).with_value(parse("(+ y 1)")));
let closed = close_over_constants(&symbols, ¶ms, parse("(list q l)")).expect("closes");
format_form(&closed),
"(LET ((L '(+ Y 1)) (Q 'Y)) (LIST Q L))"
fn a_macro_time_lambda_refuses_a_name_the_caller_binds() {
symbols.mark_macro_expansion();
let err = close_over_constants(&symbols, ¶ms, parse("r")).expect_err("caller lexical");
assert!(matches!(err, Error::UndefinedSymbol(name) if name == "R"));
let err = close_over_constants(&symbols, ¶ms, parse("zz")).expect_err("unbound");
assert!(matches!(err, Error::UndefinedSymbol(name) if name == "ZZ"));