Refactor bound node types for clarity

Introduce generic `CompilerPhase` trait to unify different stages of the
bound AST. Rename `LocalSlot` to `VirtualId` and introduce `StackOffset`
for clearer distinction between compile-time and run-time addressing.
Update type aliases and implementations to reflect these changes.
This commit is contained in:
Michael Schimmel
2026-03-13 19:20:44 +01:00
parent d035da9d91
commit e5d82ee2b6
15 changed files with 403 additions and 458 deletions
+6 -29
View File
@@ -1,31 +1,7 @@
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, LocalSlot, Node};
use crate::ast::types::StaticType;
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, ExecNode, Node, RuntimeMetadata, StackOffset, VirtualId};
use std::collections::HashMap;
use std::fmt::Debug;
use std::rc::Rc;
#[derive(Clone)]
pub struct RuntimeMetadata {
pub ty: StaticType,
pub is_tail: bool,
/// The analyzed node, containing metrics and a link to the original TypedNode.
pub original: Rc<AnalyzedNode>,
pub stack_size: u32,
}
impl Debug for RuntimeMetadata {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Metadata")
.field("ty", &self.ty)
.field("is_tail", &self.is_tail)
.field("stack_size", &self.stack_size)
.finish()
}
}
/// The ExecNode is the AST used by the VM. It carries TCO flags and links to metrics.
pub type ExecNode = Node<RuntimeMetadata>;
struct StackAllocator {
mapping: HashMap<u32, u32>,
next_slot: u32,
@@ -39,19 +15,20 @@ impl StackAllocator {
}
}
fn map_slot(&mut self, slot: LocalSlot) -> LocalSlot {
fn map_slot(&mut self, slot: VirtualId) -> StackOffset {
let entry = self.mapping.entry(slot.0).or_insert_with(|| {
let s = self.next_slot;
self.next_slot += 1;
s
});
LocalSlot(*entry)
StackOffset(*entry)
}
fn map_address(&mut self, addr: Address) -> Address {
fn map_address(&mut self, addr: Address<VirtualId>) -> Address<StackOffset> {
match addr {
Address::Local(slot) => Address::Local(self.map_slot(slot)),
other => other,
Address::Upvalue(idx) => Address::Upvalue(idx),
Address::Global(idx) => Address::Global(idx),
}
}
}