Lines
97.06 %
Functions
100 %
Branches
use super::*;
use crate::compiler::expr::format_form;
use crate::reader::Reader;
fn parse(source: &str) -> Expr {
Reader::parse(source).expect("parses").exprs.remove(0)
}
fn source(fields: &[(&str, WasmType)]) -> ClosureSource {
let fields = fields
.iter()
.map(|(name, ty)| (name.to_string(), *ty))
.collect();
ClosureSource::new(
&LambdaParams::simple(vec!["X".to_string()]),
&parse("(* x r)"),
fields,
)
fn lambda_text(lambda: &Expr) -> String {
match lambda {
Expr::Lambda(params, body) => format!("{:?} {}", params.required, format_form(body)),
other => panic!("not a lambda: {other:?}"),
#[test]
fn a_closure_with_nothing_captured_is_its_lambda() {
let lambda = source(&[]).lambda(Vec::new());
assert_eq!(lambda_text(&lambda), r#"["X"] (* X R)"#);
fn the_values_of_the_captured_variables_are_bound_around_the_body() {
let lambda = source(&[("R", WasmType::Ratio)]).lambda(vec![parse("1/10")]);
assert_eq!(lambda_text(&lambda), r#"["X"] (LET ((R 1/10)) (* X R))"#);
fn a_site_names_a_source_of_the_last_unit_only() {
let mut symbols = SymbolTable::new();
assert_eq!(symbols.closure_source(0), None);
symbols.finish_unit(vec![source(&[])]);
assert_eq!(symbols.closure_source(0), Some(&source(&[])));
assert_eq!(symbols.closure_source(1), None);
assert_eq!(symbols.closure_source(-1), None);