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:
+16
-16
@@ -1,5 +1,5 @@
|
||||
use crate::ast::compiler::bound_nodes::{
|
||||
Address, BoundKind, DeclarationKind, GlobalIdx, LocalSlot, Node, UpvalueIdx,
|
||||
Address, BoundKind, BoundPhase, DeclarationKind, GlobalIdx, Node, UpvalueIdx, VirtualId,
|
||||
};
|
||||
use crate::ast::diagnostics::Diagnostics;
|
||||
use crate::ast::nodes::{Symbol, SyntaxKind, SyntaxNode};
|
||||
@@ -9,7 +9,7 @@ use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LocalInfo {
|
||||
pub addr: Address,
|
||||
pub addr: Address<VirtualId>,
|
||||
pub identity: Identity,
|
||||
pub _ty: StaticType,
|
||||
pub purity: Purity,
|
||||
@@ -38,7 +38,7 @@ struct FunctionCompiler {
|
||||
identity: Identity,
|
||||
scopes: Vec<CompilerScope>,
|
||||
slot_count: u32,
|
||||
upvalues: Vec<Address>,
|
||||
upvalues: Vec<Address<VirtualId>>,
|
||||
}
|
||||
|
||||
impl FunctionCompiler {
|
||||
@@ -65,7 +65,7 @@ impl FunctionCompiler {
|
||||
}
|
||||
}
|
||||
|
||||
fn define_variable(&mut self, name: &Symbol, identity: Identity) -> Result<Address, String> {
|
||||
fn define_variable(&mut self, name: &Symbol, identity: Identity) -> Result<Address<VirtualId>, String> {
|
||||
let current_scope = self.scopes.last_mut().unwrap();
|
||||
if current_scope.locals.contains_key(name) {
|
||||
return Err(format!(
|
||||
@@ -74,7 +74,7 @@ impl FunctionCompiler {
|
||||
));
|
||||
}
|
||||
|
||||
let slot = LocalSlot(self.slot_count);
|
||||
let slot = VirtualId(self.slot_count);
|
||||
current_scope.locals.insert(
|
||||
name.clone(),
|
||||
LocalInfo {
|
||||
@@ -97,7 +97,7 @@ impl FunctionCompiler {
|
||||
None
|
||||
}
|
||||
|
||||
fn add_upvalue(&mut self, addr: Address) -> UpvalueIdx {
|
||||
fn add_upvalue(&mut self, addr: Address<VirtualId>) -> UpvalueIdx {
|
||||
if let Some(idx) = self.upvalues.iter().position(|&a| a == addr) {
|
||||
return UpvalueIdx(idx as u32);
|
||||
}
|
||||
@@ -114,7 +114,7 @@ pub enum ExprContext {
|
||||
}
|
||||
|
||||
pub type BindingResult = (
|
||||
Node,
|
||||
Node<BoundPhase>,
|
||||
HashMap<Identity, Vec<Identity>>,
|
||||
Vec<CompilerScope>,
|
||||
u32,
|
||||
@@ -179,7 +179,7 @@ impl Binder {
|
||||
identity: Identity,
|
||||
_kind: crate::ast::compiler::bound_nodes::DeclarationKind,
|
||||
diag: &mut Diagnostics,
|
||||
) -> Option<Address> {
|
||||
) -> Option<Address<VirtualId>> {
|
||||
let current_fn_idx = self.functions.len() - 1;
|
||||
|
||||
if current_fn_idx == 0 {
|
||||
@@ -211,7 +211,7 @@ impl Binder {
|
||||
node: &SyntaxNode,
|
||||
ctx: ExprContext,
|
||||
diag: &mut Diagnostics,
|
||||
) -> Node {
|
||||
) -> Node<BoundPhase> {
|
||||
match &node.kind {
|
||||
SyntaxKind::Nop => self.make_node(node.identity.clone(), BoundKind::Nop),
|
||||
SyntaxKind::Constant(v) => {
|
||||
@@ -361,7 +361,7 @@ impl Binder {
|
||||
let body_bound = self.bind(body.as_ref(), ExprContext::Expression, diag);
|
||||
let compiled_fn = self.functions.pop().unwrap();
|
||||
|
||||
fn count_params(node: &Node) -> Option<u32> {
|
||||
fn count_params(node: &Node<BoundPhase>) -> Option<u32> {
|
||||
match &node.kind {
|
||||
BoundKind::Define {
|
||||
kind: DeclarationKind::Parameter,
|
||||
@@ -520,9 +520,9 @@ impl Binder {
|
||||
);
|
||||
self.make_node(node.identity.clone(), BoundKind::Error)
|
||||
}
|
||||
SyntaxKind::Error => crate::ast::compiler::bound_nodes::Node {
|
||||
SyntaxKind::Error => Node {
|
||||
identity: node.identity.clone(),
|
||||
kind: crate::ast::compiler::bound_nodes::BoundKind::Error,
|
||||
kind: BoundKind::Error,
|
||||
ty: (),
|
||||
},
|
||||
}
|
||||
@@ -533,7 +533,7 @@ impl Binder {
|
||||
sym: &Symbol,
|
||||
diag: &mut Diagnostics,
|
||||
identity: &Identity,
|
||||
) -> Option<Address> {
|
||||
) -> Option<Address<VirtualId>> {
|
||||
let current_fn_idx = self.functions.len() - 1;
|
||||
|
||||
// 1. Try local in current function
|
||||
@@ -603,7 +603,7 @@ impl Binder {
|
||||
node: &SyntaxNode,
|
||||
kind: DeclarationKind,
|
||||
diag: &mut Diagnostics,
|
||||
) -> Node {
|
||||
) -> Node<BoundPhase> {
|
||||
match &node.kind {
|
||||
SyntaxKind::Identifier(sym) => {
|
||||
if let Some(addr) = self.declare_variable(sym, node.identity.clone(), kind, diag) {
|
||||
@@ -647,7 +647,7 @@ impl Binder {
|
||||
&mut self,
|
||||
node: &SyntaxNode,
|
||||
diag: &mut Diagnostics,
|
||||
) -> Node {
|
||||
) -> Node<BoundPhase> {
|
||||
match &node.kind {
|
||||
SyntaxKind::Identifier(sym) => {
|
||||
if let Some(addr) = self.resolve_variable(sym, diag, &node.identity) {
|
||||
@@ -684,7 +684,7 @@ impl Binder {
|
||||
}
|
||||
}
|
||||
|
||||
fn make_node(&self, identity: Identity, kind: BoundKind<()>) -> Node {
|
||||
fn make_node(&self, identity: Identity, kind: BoundKind<BoundPhase>) -> Node<BoundPhase> {
|
||||
Node {
|
||||
identity,
|
||||
kind,
|
||||
|
||||
Reference in New Issue
Block a user