Lines
99.75 %
Functions
98.21 %
Branches
100 %
use super::entry::{Symbol, SymbolKind};
use super::table::SymbolTable;
use crate::ast::Expr;
pub(super) fn variable(name: &str) -> Symbol {
Symbol::new(name, SymbolKind::Variable).with_value(Expr::T)
}
pub(super) fn function_symbol(name: &str) -> Symbol {
Symbol::new(name, SymbolKind::Function).with_function(Expr::T)
pub(super) fn reader_of(name: &str, read: &str) -> Symbol {
reader_of_kind(name, read, SymbolKind::Function)
pub(super) fn macro_reader_of(name: &str, read: &str) -> Symbol {
reader_of_kind(name, read, SymbolKind::Macro)
fn reader_of_kind(name: &str, read: &str, kind: SymbolKind) -> Symbol {
let body = Expr::Symbol(read.to_string());
Symbol::new(name, kind).with_function(Expr::Lambda(
crate::ast::LambdaParams::simple(Vec::new()),
Box::new(body),
))
pub(super) fn definition_of(table: &SymbolTable, name: &str) -> Option<u32> {
table.lookup(name).map(Symbol::function_definition)
pub(super) fn value_definition_of(table: &SymbolTable, name: &str) -> Option<u32> {
table.lookup(name).map(Symbol::value_definition)
#[test]
fn define_gives_each_function_a_distinct_definition_id() {
let mut table = SymbolTable::new();
table.define(function_symbol("walk"));
let first = definition_of(&table, "walk");
let mut shadowed = table.clone();
shadowed.define(function_symbol("walk"));
assert_ne!(first, Some(0));
assert_ne!(definition_of(&shadowed, "walk"), first);
fn define_keeps_the_id_of_a_restored_symbol() {
let saved = table.lookup("walk").cloned();
let before = definition_of(&table, "walk");
table.define(function_symbol("other"));
table.define(saved.expect("walk was defined"));
assert_eq!(definition_of(&table, "walk"), before);
fn redefining_a_function_assigns_a_fresh_id() {
table.define(function_symbol("f"));
let before = definition_of(&table, "f");
assert_ne!(definition_of(&table, "f"), before);
fn redefining_a_macro_makes_it_a_function() {
table.define(macro_reader_of("m", "x"));
assert!(matches!(table.resolve_head("m"), super::Head::Macro(..)));
table.define(reader_of("m", "y"));
assert!(matches!(table.resolve_head("m"), super::Head::Function(..)));
fn clones_share_one_id_allocator() {
let mut other = table.clone();
table.define(function_symbol("a"));
other.define(function_symbol("b"));
assert_ne!(definition_of(&table, "a"), definition_of(&other, "b"));
fn test_symbol_creation() {
let sym = Symbol::new("foo", SymbolKind::Variable);
assert_eq!(sym.name(), "foo");
assert_eq!(sym.kind(), SymbolKind::Variable);
assert!(sym.value().is_none());
fn test_symbol_with_value() {
let sym = Symbol::new("x", SymbolKind::Variable).with_value(Expr::T);
assert!(sym.value().is_some());
assert_eq!(sym.value(), Some(&Expr::T));
fn test_symbol_properties() {
let mut sym = Symbol::new("test", SymbolKind::Function);
sym.set_property("doc", Expr::String("A test function".into()));
sym.set_property("pure", Expr::T);
assert_eq!(
sym.get_property("doc"),
Some(&Expr::String("A test function".into()))
);
assert_eq!(sym.get_property("pure"), Some(&Expr::T));
assert!(sym.get_property("nonexistent").is_none());
fn test_symbol_table_builtins() {
let table = SymbolTable::with_builtins();
assert!(table.contains("+"));
assert_eq!(table.lookup("+").unwrap().kind(), SymbolKind::Operator);
assert!(table.contains("IF"));
assert_eq!(table.lookup("IF").unwrap().kind(), SymbolKind::SpecialForm);
assert!(table.contains("CAR"));
assert_eq!(table.lookup("CAR").unwrap().kind(), SymbolKind::Native);
assert!(table.contains("EVAL"));
table.lookup("EVAL").unwrap().kind(),
SymbolKind::SpecialForm
assert!(table.contains("DEBUG"));
assert_eq!(table.lookup("DEBUG").unwrap().kind(), SymbolKind::Native);
fn test_symbol_table_define_lookup() {
table.define(Symbol::new("my-func", SymbolKind::Function));
assert!(table.contains("my-func"));
assert!(!table.contains("undefined"));
let sym = table.lookup("my-func").unwrap();
assert_eq!(sym.kind(), SymbolKind::Function);
// ADR-0029 strict resolution: a namespaced symbol keys by its canonical
// `NS:NAME` string and does NOT alias the bare `NAME`. There is no ambient
// current namespace and no cross-namespace fallback.
fn qualified_symbol_keys_separately_from_bare() {
table.define(Symbol::new("FOO:BAR", SymbolKind::Function));
assert!(table.contains("FOO:BAR"));
// Strict: the qualified key does not create a bare alias.
assert!(!table.contains("BAR"));
assert!(table.lookup("FOO:BAR").is_some());
assert!(table.lookup("BAR").is_none());
fn lookup_qualified_builds_canonical_key() {
table.define(Symbol::new("FINANCE:ADD-MONEY", SymbolKind::Function));
assert!(
table
.lookup_qualified(Some("FINANCE"), "ADD-MONEY")
.is_some()
// An unqualified lookup of the same base name does not reach the namespaced entry.
assert!(table.lookup_qualified(None, "ADD-MONEY").is_none());
fn builtins_remain_global_unqualified() {
// Global builtins keep bare keys; a namespaced lookup of the same base misses.
assert!(!table.contains("MATH:+"));
assert!(table.lookup_qualified(Some("MATH"), "+").is_none());
fn universal_prelude_loaded_but_not_host_layer() {
// ADR-0029 two-layer prelude: the universal math:* / list:* helpers are
// present in every table; the host-fn-dependent split:* helpers are NOT
// (they load only on the rpc Session path, after host fns are registered).
assert!(table.contains("MATH:SQUARE"));
assert!(table.contains("LIST:SECOND"));
assert!(!table.contains("SPLIT:LIST-FOR-TRANSACTION"));
// The pure-wasm table loads the same universal layer.
let wasm_table = SymbolTable::with_builtins_for_wasm();
assert!(wasm_table.contains("MATH:SQUARE"));
assert!(!wasm_table.contains("SPLIT:LIST-FOR-TRANSACTION"));
/// Fable review (Phase 3): `exit_macro_expansion` used to hide a mismatched
/// enter/exit pair with `saturating_sub`. It is now a checked subtraction
/// returning a structured `Error::Compile` — the `Result` IS the contract,
/// so no `#[spec(requires: ...)]` shadows it with a panic under
/// `--cfg anodized_panic`.
fn exit_macro_expansion_at_depth_zero_is_a_compile_error_not_a_panic() {
let mut symbols = SymbolTable::with_builtins();
let err = symbols
.exit_macro_expansion()
.expect_err("exit_macro_expansion() with shared_recursion_depth 0 must error");
err.to_string()
.contains("exit_shared_recursion() called with shared_recursion_depth already 0")
/// A balanced enter/exit pair still succeeds.
fn exit_macro_expansion_after_enter_succeeds() {
symbols.enter_macro_expansion().expect("enter succeeds");
symbols
.expect("balanced exit must succeed");
fn exit_runtime_guard_releases_the_shared_level_even_on_a_branch_underflow() {
let mut symbols = SymbolTable::new();
symbols.enter_shared_recursion().expect("enter succeeds");
.exit_runtime_guard()
.expect_err("a branch underflow must error");
assert!(err.to_string().contains("exit_runtime_branch()"));
assert!(symbols.exit_shared_recursion().is_err());
fn a_runtime_branch_marks_the_table_until_it_exits() {
symbols.enter_runtime_branch().expect("enter succeeds");
assert!(symbols.is_under_any_runtime_guard());
symbols.exit_runtime_branch().expect("exit succeeds");
assert!(!symbols.is_under_any_runtime_guard());
assert!(symbols.exit_runtime_branch().is_err());
/// A scope frame shadows without copying: the shadowed binding returns when
/// the frame closes, a name the frame introduced leaves, and a plain
/// definition made inside the frame outlives it (0014 Stage 2, ISS-039).
fn a_scope_frame_restores_what_it_shadowed_and_keeps_definitions() {
table.define(Symbol::new("X", SymbolKind::Variable).with_value(Expr::Nil));
table.push_scope();
table.define_scoped(Symbol::new("X", SymbolKind::Variable).with_value(Expr::T));
table.define_scoped(Symbol::new("Y", SymbolKind::Variable).with_value(Expr::T));
table.define(Symbol::new("F", SymbolKind::Function).with_value(Expr::T));
assert_eq!(table.lookup("X").and_then(Symbol::value), Some(&Expr::T));
table.pop_scope().expect("a frame is open");
assert_eq!(table.lookup("X").and_then(Symbol::value), Some(&Expr::Nil));
assert!(table.lookup("Y").is_none());
assert!(table.lookup("F").is_some());
assert!(table.pop_scope().is_err());
fn a_runtime_value_is_recorded_only_on_a_macro_expansion_table() {
symbols.note_runtime_value();
assert!(!symbols.saw_runtime_value());
symbols.mark_macro_expansion();
assert!(symbols.saw_runtime_value());
fn a_clones_runtime_note_is_seen_by_the_original() {
let mut clone = symbols.clone();
clone.note_runtime_value();
fn a_fresh_expansion_does_not_inherit_a_previous_note() {
let mut first = SymbolTable::with_builtins();
first.mark_macro_expansion();
first.note_runtime_value();
let mut second = first.clone();
second.mark_macro_expansion();
assert!(!second.saw_runtime_value());
assert!(first.saw_runtime_value());
fn a_nested_expansions_note_does_not_reach_the_outer_one() {
let mut outer = SymbolTable::with_builtins();
outer.mark_macro_expansion();
let mut inner = outer.clone();
inner.mark_macro_expansion();
inner.note_runtime_value();
assert!(!outer.saw_runtime_value());
fn a_speculative_clones_note_is_not_seen_by_the_original() {
let mut probe = symbols.speculative_clone();
probe.note_runtime_value();
assert!(probe.saw_runtime_value());
fn a_speculative_clone_of_an_expansion_still_records_notes() {
let probe = symbols.speculative_clone();
assert!(probe.is_macro_expansion());
fn defining_does_not_bump_the_epoch_but_noting_a_redefinition_does() {
let start = table.redefinition_epoch();
assert_eq!(table.redefinition_epoch(), start);
table.note_redefinition();
assert_eq!(table.redefinition_epoch(), start + 1);
fn note_redefinition_is_visible_through_clones() {
let table = SymbolTable::new();
let clone = table.clone();
clone.note_redefinition();
fn a_function_body_table_and_its_clones_know_they_are_in_a_body() {
assert!(table.function_owner().is_none());
table.enter_function_body(0);
assert!(table.function_owner().is_some());
assert!(table.clone().function_owner().is_some());
fn hidden_names_are_unique_across_clones() {
let names = [
table.hidden_name("mk", "a"),
clone.hidden_name("mk", "a"),
];
assert_ne!(names[0], names[1]);
assert_ne!(names[1], names[2]);
assert_ne!(names[0], names[2]);
fn a_hidden_name_tells_its_factory_and_parameter() {
let name = SymbolTable::new().hidden_name("MK", "A");
assert_eq!(SymbolTable::hidden_origin(&name), Some(("MK", "A")));
assert!(SymbolTable::is_hidden_name(&name));
assert_eq!(SymbolTable::hidden_origin("A"), None);
fn within_shared_recursion_charges_one_level_and_releases_it() {
let seen = symbols
.within_shared_recursion(|inner| Ok(inner.shared_recursion_depth))
.expect("below the ceiling");
assert_eq!(seen, 1);
assert_eq!(symbols.shared_recursion_depth, 0);
fn within_shared_recursion_releases_the_level_when_the_run_fails() {
.within_shared_recursion(|_| Err::<(), _>(crate::error::Error::Compile("boom".to_string())))
.expect_err("the run fails");
assert!(matches!(err, crate::error::Error::Compile(_)), "{err}");
fn within_shared_recursion_is_refused_at_the_ceiling() {
for _ in 0..crate::runtime::MAX_SHARED_RECURSION_DEPTH {
symbols.enter_shared_recursion().expect("below the ceiling");
.within_shared_recursion(|_| Ok(()))
.expect_err("ceiling reached");
assert!(matches!(err, crate::error::Error::Resource(_)), "{err}");
fn a_designator_of_a_global_function_is_not_special() {
table.define(reader_of("f", "x"));
assert_eq!(table.designated_function("f").expect("designated"), None);
fn a_designator_names_the_alias_of_the_global_a_labels_function_hides() {
let global = reader_of("f", "x");
let expected = global.function().cloned();
table.define(global);
table.define(reader_of("f", "y").local_function());
let alias = table
.designated_function("f")
.expect("designated")
.expect("alias");
table.lookup(&alias).and_then(|s| s.function().cloned()),
expected
table.clone().designated_function("f").expect("designated"),
Some(alias)
fn a_global_survives_a_variable_between_it_and_the_labels_function() {
table.define(Symbol::new("f", SymbolKind::Variable));
fn a_designator_naming_only_a_labels_function_is_undefined() {
let err = table.designated_function("f").unwrap_err();
assert_eq!(err.to_string(), "Undefined symbol: f");
fn a_global_macro_is_not_the_function_a_labels_function_hides() {
table.define(macro_reader_of("f", "x"));
fn a_variable_does_not_hide_a_macro_in_operator_position() {
table.define(macro_reader_of("m", "y"));
table.define(variable("m"));
assert!(table.lookup("m").is_some_and(Symbol::is_variable));
fn a_labels_function_over_a_macro_gives_it_back_when_its_scope_closes() {
table.define_scoped(reader_of("m", "y").local_function());
table.define_scoped(variable("m"));
fn chain_of(depth: usize) -> Expr {
(0..depth).fold(Expr::Nil, |tail, _| Expr::cons(Expr::Nil, tail))
fn open_expansions_compose_up_to_the_ceiling() {
let ceiling = crate::compiler::datum::MAX_ELAB_DEPTH;
symbols.enter_macro_expansion().expect("outer");
.compose_expansion(&chain_of(10), &[])
.expect("first expansion");
symbols.enter_macro_expansion().expect("inner");
.compose_expansion(&chain_of(ceiling - 10), &[])
.expect("exactly at the ceiling");
.compose_expansion(&chain_of(ceiling - 9), &[])
.expect_err("one level past the ceiling");
matches!(&err, crate::error::Error::Resource(m) if m.contains("nests deeper")),
"{err}"
fn an_expansion_is_charged_only_for_the_depth_it_adds_to_its_arguments() {
symbols.enter_macro_expansion().expect("expansion");
let arg = chain_of(ceiling - 1);
.compose_expansion(
&Expr::cons(arg.clone(), Expr::Nil),
std::slice::from_ref(&arg),
)
.expect("a wrapper around a deep argument adds one level");
fn leaving_an_expansion_releases_its_depth() {
symbols.enter_macro_expansion().expect("enter");
.compose_expansion(&chain_of(100), &[])
.expect("compose");
symbols.exit_macro_expansion().expect("exit");
symbols.enter_macro_expansion().expect("sibling");
.expect("a sibling starts from the outer depth");