Lines
92.46 %
Functions
92.37 %
Branches
100 %
use std::collections::HashMap;
use crate::ast::Expr;
use super::homes::HomeKey;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolKind {
/// Built-in operators like +, -, *, /
Operator,
/// Native functions implemented in the runtime
Native,
/// User-defined functions
Function,
/// Variables and constants
Variable,
/// Special forms like if, let, define, lambda
SpecialForm,
/// User-defined macros
Macro,
}
impl SymbolKind {
/// A native, operator or special form: a name no program may bind.
pub(super) fn is_builtin(self) -> bool {
matches!(self, Self::Native | Self::Operator | Self::SpecialForm)
/// The variable cell of a name.
#[derive(Debug, Clone, Default, PartialEq)]
pub(super) struct Variable {
pub(super) value: Option<Expr>,
/// The constant init of a global that lives in a wasm global: the only
/// value a macro body reads while expanding (ISS-131).
pub(super) expansion_value: Option<Expr>,
/// A loop variable bound as constant data by a statically unrolled loop
/// (0014 P11): a store to it is a structured error, never a rebinding.
pub(super) loop_constant: bool,
/// Copied into a closure's environment: a later store would not reach
/// the closure's copy (ISS-001), so it is a structured error.
pub(super) captured: bool,
/// Table-assigned id of this binding (0 = unassigned): a lexical's tells a
/// call site's rebinding from the binding a function was defined under.
pub(super) definition: u32,
/// The home of an assigned binder's local, which its stores write into.
pub(super) home: Option<HomeKey>,
/// The function cell of a name: a function, macro, native or special form.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct Function {
/// Never `Variable`.
pub(super) kind: SymbolKind,
pub(super) lambda: Option<Expr>,
/// The BLOCK labels visible where this function was defined: an inlined
/// call sees only those and the body's own (ISS-179).
pub(super) block_depth: usize,
/// The lexical variables visible where this function was defined, which
/// its body sees; empty for a top-level function.
pub(super) lexical_env: Vec<(String, u32)>,
/// The bindings of that environment its body reads and never assigns,
/// which it keeps after their binder ended.
pub(super) lexical_values: Vec<Symbol>,
/// A LABELS function: it exists only inside its form.
pub(super) local: bool,
/// Table-assigned id (0 = unassigned): a cache keyed on it never serves a
/// superseded body.
impl Function {
pub(super) fn new(kind: SymbolKind) -> Self {
Self {
kind,
lambda: None,
block_depth: 0,
lexical_env: Vec::new(),
lexical_values: Vec::new(),
local: false,
definition: 0,
/// A name with its two cells: the variable it names and the function it names.
pub struct Symbol {
pub(super) name: String,
pub(super) doc: Option<String>,
pub(super) properties: HashMap<String, Expr>,
pub(super) value: Option<Variable>,
pub(super) function: Option<Function>,
impl Symbol {
/// A variable gets a value cell, any other kind a function cell.
pub fn new(name: impl Into<String>, kind: SymbolKind) -> Self {
let mut symbol = Self::bare(name);
match kind {
SymbolKind::Variable => symbol.value = Some(Variable::default()),
kind => symbol.function = Some(Function::new(kind)),
symbol
pub(super) fn bare(name: impl Into<String>) -> Self {
name: name.into(),
doc: None,
properties: HashMap::new(),
value: None,
function: None,
pub(super) fn from_value(name: impl Into<String>, cell: Variable) -> Self {
value: Some(cell),
..Self::bare(name)
pub(super) fn from_function(name: impl Into<String>, cell: Function) -> Self {
function: Some(cell),
pub(super) fn is_bare(&self) -> bool {
self.value.is_none() && self.function.is_none()
/// Writes the cells, doc and properties `new` carries over this symbol's.
pub(super) fn absorb(&mut self, new: Symbol) {
self.value = new.value.or(self.value.take());
self.function = new.function.or(self.function.take());
self.doc = new.doc.or(self.doc.take());
self.properties.extend(new.properties);
/// Whether the name is a variable: a value cell that is not the stand-in
/// a host fn keeps in its own.
#[must_use]
pub fn is_variable(&self) -> bool {
self.value.is_some() && !self.kind().is_builtin()
/// Whether the name is a LABELS function.
pub fn is_local_function(&self) -> bool {
self.function
.as_ref()
.is_some_and(|function| function.local)
/// Marks this binding as a statically unrolled loop's variable.
pub fn loop_constant(mut self) -> Self {
self.value.get_or_insert_default().loop_constant = true;
self
/// Whether this binding is a statically unrolled loop's variable.
pub fn is_loop_constant(&self) -> bool {
self.value.as_ref().is_some_and(|cell| cell.loop_constant)
/// Marks this binding as copied into a closure's environment.
pub fn mark_captured(&mut self) {
self.value.get_or_insert_default().captured = true;
/// Whether a closure holds a copy of this binding.
pub fn is_captured(&self) -> bool {
self.value.as_ref().is_some_and(|cell| cell.captured)
/// Records how many BLOCK labels were visible at the definition.
pub fn set_block_depth(&mut self, depth: usize) {
if let Some(function) = self.function.as_mut() {
function.block_depth = depth;
/// The BLOCK labels visible at the definition.
pub fn block_depth(&self) -> usize {
.map_or(0, |function| function.block_depth)
/// Records the lexical variables visible at the definition.
pub fn set_lexical_env(&mut self, env: Vec<(String, u32)>) {
function.lexical_env = env;
/// The lexical variables visible at the definition.
pub fn lexical_env(&self) -> &[(String, u32)] {
.map_or(&[], |function| function.lexical_env.as_slice())
/// Records the lexical bindings the body of this function keeps.
pub fn set_lexical_values(&mut self, values: Vec<Symbol>) {
function.lexical_values = values;
/// Drops the runtime values from the lexical bindings this function kept.
pub(crate) fn forget_runtime_lexicals(&mut self) {
let Some(function) = self.function.as_mut() else {
return;
};
for binding in &mut function.lexical_values {
if binding.value().is_some_and(Expr::is_wasm_runtime) {
binding.clear_value();
pub fn with_value(mut self, value: Expr) -> Self {
self.value.get_or_insert_default().value = Some(value);
pub fn name(&self) -> &str {
&self.name
/// The kind of the function cell, `Variable` for a name without one.
pub fn kind(&self) -> SymbolKind {
.map_or(SymbolKind::Variable, |function| function.kind)
pub fn value(&self) -> Option<&Expr> {
self.value.as_ref()?.value.as_ref()
pub fn set_value(&mut self, value: Expr) {
let cell = self.value.get_or_insert_default();
cell.value = Some(value);
cell.expansion_value = None;
pub(crate) fn clear_value(&mut self) {
if let Some(cell) = self.value.as_mut() {
cell.value = None;
pub fn expansion_value(&self) -> Option<&Expr> {
self.value.as_ref()?.expansion_value.as_ref()
pub fn set_expansion_value(&mut self, value: Expr) {
self.value.get_or_insert_default().expansion_value = Some(value);
pub(crate) fn clear_expansion_value(&mut self) {
/// Gives the name a function cell holding `func`, of kind `Function`
/// unless it already has one.
pub fn with_function(mut self, func: Expr) -> Self {
.get_or_insert_with(|| Function::new(SymbolKind::Function))
.lambda = Some(func);
pub fn function(&self) -> Option<&Expr> {
self.function.as_ref()?.lambda.as_ref()
/// Marks this function as bound by a LABELS form.
pub fn local_function(mut self) -> Self {
function.local = true;
/// Keeps the binding identity of the variable this one re-materialises.
pub fn with_definition(mut self, id: u32) -> Self {
self.value.get_or_insert_default().definition = id;
/// Gives the variable the home its stores record into.
pub(crate) fn with_home(mut self, home: Option<HomeKey>) -> Self {
self.value.get_or_insert_default().home = home;
/// The home of the binder this variable is, when it has one.
pub(crate) fn home(&self) -> Option<&HomeKey> {
self.value.as_ref()?.home.as_ref()
/// The id of the variable binding, 0 without one.
pub fn value_definition(&self) -> u32 {
self.value.as_ref().map_or(0, |cell| cell.definition)
/// The id of the function binding, 0 without one.
pub fn function_definition(&self) -> u32 {
self.function.as_ref().map_or(0, |cell| cell.definition)
/// Whether either cell carries the binding id `id`.
pub(super) fn has_definition(&self, id: u32) -> bool {
self.value_definition() == id || self.function_definition() == id
pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
self.doc = Some(doc.into());
pub fn doc(&self) -> Option<&str> {
self.doc.as_deref()
pub fn set_doc(&mut self, doc: impl Into<String>) {
pub fn get_property(&self, key: &str) -> Option<&Expr> {
self.properties.get(key)
pub fn set_property(&mut self, key: impl Into<String>, value: Expr) {
self.properties.insert(key.into(), value);
pub fn remove_property(&mut self, key: &str) -> Option<Expr> {
self.properties.remove(key)
pub fn properties(&self) -> &HashMap<String, Expr> {
&self.properties