Lines
100 %
Functions
Branches
use super::*;
use crate::ast::LambdaParams;
fn lambda(params: &[&str]) -> Expr {
let names = params.iter().map(ToString::to_string).collect();
Expr::Lambda(LambdaParams::simple(names), Box::new(Expr::Nil))
}
#[test]
fn a_closure_of_another_arity_names_both_signatures() {
let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
let sig = ctx
.intern_closure_signature(&[WasmType::Ratio], WasmType::Ratio)
.expect("sig");
let err = cell_store_error(&ctx, "F", &lambda(&["a", "b"]), WasmType::Closure(sig));
assert_eq!(
err.to_string(),
"Compile error: F: store of a 2-parameter closure into a closure (ratio) -> ratio cell"
);
fn a_closure_place_is_described_by_its_signature() {
.intern_closure_signature(&[], WasmType::Ratio)
let stored = Expr::WasmLocal(3, WasmType::Closure(sig));
let err = cell_store_error(&ctx, "F", &stored, WasmType::I32);
"Compile error: F: store of a closure () -> ratio into an Index cell"
fn a_number_into_a_closure_cell_names_the_closure() {
let err = cell_store_error(
&ctx,
"F",
&Expr::Number(Number::Index(5.into())),
WasmType::Closure(sig),
"Compile error: F: store of a number into a closure () -> ratio cell"
fn an_untyped_value_says_so() {
let ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
let stored = Expr::WasmRuntime(WasmType::AnyRef);
"Compile error: F: store of a value of unknown type into an Index cell"
fn a_closure_place_of_another_signature_names_both_types() {
let stored = ctx
.intern_closure_signature(&[WasmType::I32], WasmType::Ratio)
let cell = ctx
&Expr::WasmLocal(3, WasmType::Closure(stored)),
WasmType::Closure(cell),
"Compile error: F: store of a closure (i32) -> ratio into a closure (ratio) -> ratio cell"
fn a_lambda_of_the_same_arity_is_another_signature() {
.intern_closure_signature(&[WasmType::Ratio, WasmType::Ratio], WasmType::Ratio)
let err = cell_store_error(&ctx, "F", &lambda(&["a", "b"]), WasmType::Closure(cell));
"Compile error: F: store of a closure of another signature into a closure (ratio, ratio) -> ratio cell"
fn parameter_count_counts_every_section() {
let mut params = LambdaParams::simple(vec!["a".to_string()]);
params.optional.push(("b".to_string(), None));
params.rest = Some("r".to_string());
assert_eq!(parameter_count(¶ms), 3);
fn compile_eval(source: &str) -> Result<()> {
let program = crate::reader::Reader::parse(source).expect("parses");
let mut symbols = SymbolTable::with_builtins();
crate::compiler::Compiler::new()
.compile_eval_with_type(&program, &mut symbols)
.map(drop)
fn a_store_that_fails_for_another_reason_reports_that_reason() {
let err = compile_eval("(defvar n 0) (setf n (entity-count)) n").expect_err("no import");
assert!(err.to_string().contains("is not registered"), "{err}");
fn a_store_of_another_kind_into_a_closure_cell_is_the_cell_error() {
let err = compile_eval("(defvar f (lambda () 1)) (setf f 5) f").expect_err("refused");
assert!(err.to_string().contains("cell"), "{err}");
fn a_list_of_closures_stored_into_a_global_rides_an_any_home() {
compile_eval("(defvar l nil) (setf l (list (lambda () 1))) (length l)").expect("compiles");
fn compiled_type_of(source: &str) -> Option<WasmType> {
.expect("compiles")
.1
fn a_store_is_typed_by_what_codegen_pushed() {
let source = "(defvar g nil) (defun mk () (list 1 2)) (setf g (mk)) (car g)";
assert_eq!(compiled_type_of(source), Some(WasmType::I32));
fn a_store_that_reads_its_own_narrow_home_is_typed_at_the_init() {
let source = "(defvar total 0/1) (defun add (a) (setf total (+ total a))) (add 1/2) total";
assert_eq!(compiled_type_of(source), Some(WasmType::Ratio));
fn a_store_that_adds_a_scalar_to_an_index_home_is_a_mix() {
let err =
compile_eval("(defvar total 0) (defun add (a) (setf total (+ total a))) (add 1/2) total")
.expect_err("a mix, CL 1/2");
assert!(
err.to_string()
.contains("+ cannot mix an index (count) with a scalar"),
"{err}"