Lines
100 %
Functions
Branches
use super::*;
use crate::ast::Fraction;
use crate::ast::Number;
use crate::compiler::{CompileMode, Compiler};
use crate::error::Error;
use crate::reader::Reader;
fn unit(symbols: &mut SymbolTable, source: &str) -> crate::error::Result<Vec<u8>> {
let program = Reader::parse(source).expect("parses");
Compiler::new().compile_with_mode(&program, symbols, CompileMode::Eval)
}
fn after_first_unit() -> SymbolTable {
let mut symbols = SymbolTable::with_builtins();
unit(&mut symbols, "(defvar v 1) (setf v 2)").expect("compiles");
symbols.take_unit_slots();
symbols
fn value_of(symbols: &SymbolTable, name: &str) -> Option<Expr> {
symbols.lookup(name).and_then(|s| s.value().cloned())
#[test]
fn a_carried_constant_is_the_value_of_the_global() {
let mut symbols = after_first_unit();
let seven = Expr::Number(Number::Index(7));
symbols.carry("V", Carried::Constant(seven.clone()));
assert_eq!(value_of(&symbols, "V"), Some(seven.clone()));
assert_eq!(
.lookup("V")
.and_then(|s| s.expansion_value().cloned()),
Some(seven)
);
fn a_carried_constant_is_read_by_the_next_unit() {
symbols.carry(
"V",
Carried::Constant(Expr::Number(Number::Scalar(Fraction::from_integer(2)))),
let (_, ty) = Compiler::new()
.compile_eval_with_type(&Reader::parse("(/ v 4/1)").expect("parses"), &mut symbols)
.expect("compiles");
assert_eq!(ty, Some(crate::ast::WasmType::Ratio));
fn an_opaque_global_names_its_type_when_read() {
symbols.carry("V", Carried::Opaque("closure".to_string()));
assert_eq!(value_of(&symbols, "V"), None);
let err = unit(&mut symbols, "v").expect_err("v has no value");
assert!(
matches!(&err, Error::Compile(msg) if msg == "V: its closure value does not survive the form"),
"{err:?}"
fn a_constant_revives_an_opaque_global() {
symbols.carry("V", Carried::Constant(Expr::T));
assert_eq!(value_of(&symbols, "V"), Some(Expr::T));
unit(&mut symbols, "v").expect("v is read again");
fn carrying_a_name_the_table_does_not_hold_changes_nothing() {
symbols.carry("MISSING", Carried::Constant(Expr::T));
assert_eq!(value_of(&symbols, "MISSING"), None);
fn declaration_after(units: &[&str], name: &str, stored: bool) -> (bool, bool) {
for source in units {
unit(&mut symbols, source).expect("compiles");
unit(&mut symbols, "(defvar v 1)").expect("compiles");
symbols.restore_declaration(name, stored);
(symbols.is_special(name), symbols.is_created_global(name))
fn an_unrun_defvar_of_a_stored_global_leaves_it_created_and_special() {
assert_eq!(declaration_after(&["(setf v 5)"], "V", true), (true, true));
assert_eq!(declaration_after(&[], "V", true), (true, true));
fn an_unrun_defvar_of_an_unstored_new_name_leaves_it_undeclared() {
assert_eq!(declaration_after(&[], "V", false), (false, false));
fn an_unrun_defvar_of_a_special_leaves_it_special() {
declaration_after(&["(defvar v 5)"], "V", true),
(true, false)
fn an_unbound_declaration_keeps_the_value_the_unit_started_with() {
unit(&mut symbols, "(defvar v 5)").expect("compiles");
unit(&mut symbols, "(defvar v 6)").expect("compiles");
symbols.declare_unbound("V");
value_of(&symbols, "V"),
Some(Expr::Number(Number::Index(5)))
fn an_unbound_declaration_of_a_new_name_has_no_value() {
unit(&mut symbols, "(defvar v)").expect("compiles");
fn an_unbound_declaration_over_a_held_value_declares_a_created_global_special() {
unit(&mut symbols, "(setf v 5)").expect("compiles");
symbols.carry("V", Carried::Constant(Expr::Number(Number::Index(5))));
assert!(symbols.is_special("V"));
assert!(!symbols.is_created_global("V"));
fn closure_list_source() -> Expr {
let lambda = Expr::Lambda(
crate::ast::LambdaParams::simple(vec!["X".to_string()]),
Box::new(Expr::List(vec![
Expr::symbol("+"),
Expr::symbol("X"),
Expr::Number(Number::Index(1)),
])),
Expr::List(vec![
Expr::symbol("LET"),
Expr::List(vec![Expr::List(vec![Expr::symbol("CARRY-0"), lambda])]),
Expr::List(vec![Expr::symbol("LIST"), Expr::symbol("CARRY-0")]),
])
fn only_a_let_form_is_a_closure_list_source() {
assert!(is_list_source(&closure_list_source()));
assert!(!is_list_source(&Expr::List(vec![Expr::symbol("LIST")])));
assert!(!is_list_source(&Expr::T));
fn a_carried_value_is_a_constant_unless_opaque() {
assert_eq!(Carried::Constant(Expr::T).into_constant(), Some(Expr::T));
assert_eq!(Carried::Opaque("list".to_string()).into_constant(), None);
fn a_carried_closure_list_source_is_read_through_a_home() {
symbols.carry("V", Carried::Constant(closure_list_source()));
.compile_eval_with_type(
&Reader::parse("(funcall (car v) 4)").expect("parses"),
&mut symbols,
)
assert_eq!(ty, Some(crate::ast::WasmType::I32));
fn a_unit_storing_a_carried_closure_list_source_starts_it_lost() {
unit(&mut symbols, "(setf v (list 1)) (list 2)").expect("compiles");
let slots = symbols.take_unit_slots();
slots
.iter()
.any(|slot| slot.name == "V" && slot.cell.is_some())