Lines
100 %
Functions
Branches
use super::{compound_setf_call, expand_macro_places};
use crate::ast::{Expr, LambdaParams};
use crate::reader::Reader;
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
fn form(source: &str) -> Expr {
Reader::parse_expr(source).expect("form parses")
}
fn table() -> SymbolTable {
let mut symbols = SymbolTable::with_builtins_for_wasm();
let place = Expr::Lambda(
LambdaParams::simple(vec!["P".to_string()]),
Box::new(form("`(pt-x ,p)")),
);
symbols.define(Symbol::new("PX", SymbolKind::Macro).with_function(place));
symbols
#[test]
fn a_macro_place_is_replaced_by_its_expansion() {
let args = [form("(px q)"), form("(px 1)")];
let expanded = expand_macro_places(&table(), &args).expect("expands");
assert_eq!(expanded, vec![form("(pt-x q)"), form("(px 1)")]);
fn a_plain_place_is_kept() {
let args = [form("(pt-x q)"), form("3"), form("n"), form("4")];
assert_eq!(expanded, args.to_vec());
fn with_setter() -> SymbolTable {
let mut symbols = table();
let setter = Expr::Lambda(LambdaParams::simple(Vec::new()), Box::new(Expr::Nil));
symbols.define(Symbol::new("(SETF PT-X)", SymbolKind::Function).with_function(setter));
fn a_compound_place_stores_through_its_setter_with_the_value_last() {
let call = compound_setf_call(&with_setter(), &form("(pt-x (car l))"), &form("v"))
.expect("a setter exists");
assert_eq!(
call,
Some(Expr::List(vec![
Expr::Symbol("(SETF PT-X)".to_string()),
form("(car l)"),
form("v"),
]))
fn a_symbol_place_has_no_setter_call() {
let call = compound_setf_call(&with_setter(), &form("n"), &form("v")).expect("no error");
assert_eq!(call, None);
fn a_compound_place_without_a_setter_is_an_error() {
let err =
compound_setf_call(&with_setter(), &form("(pt-y q)"), &form("v")).expect_err("no setter");
err.to_string(),
"Compile error: No setf method defined for accessor 'PT-Y'"
fn a_place_headed_by_a_non_symbol_is_an_error() {
let err = compound_setf_call(&with_setter(), &form("(1 q)"), &form("v")).expect_err("no head");
assert!(err.to_string().contains("expected function call"), "{err}");