Extract scoping

This commit is contained in:
numzero 2025-04-19 12:54:15 +03:00
parent 6dbe397ebf
commit d9b86f430f

View File

@ -66,6 +66,12 @@ mod scope {
scope: HashMap<ast::Ident, Ident>, scope: HashMap<ast::Ident, Ident>,
} }
#[derive(Debug, Clone, Default)]
pub struct ScopeInfo {
pub locals: Vec<ast::Ident>,
pub upvalues: Vec<(ast::Ident, Ident)>,
}
impl Scope { impl Scope {
pub fn new_toplevel() -> Self { pub fn new_toplevel() -> Self {
Self { Self {
@ -77,6 +83,13 @@ mod scope {
} }
} }
pub fn finish(self) -> ScopeInfo {
ScopeInfo {
locals: self.locals,
upvalues: self.upvalues,
}
}
pub fn lookup(&self, name: &ast::Ident) -> Option<Ident> { pub fn lookup(&self, name: &ast::Ident) -> Option<Ident> {
self.scope.get(name).copied() self.scope.get(name).copied()
} }
@ -210,11 +223,12 @@ fn build_function(parent: &BuildContext<'_>, code: &ast::Function) -> Function {
} }
} }
} }
let scope = scope.scope.into_inner().finish();
Function { Function {
name: code.name.clone(), name: code.name.clone(),
args: code.args.clone(), args: code.args.clone(),
locals: vec![], locals: scope.locals,
upvalues: vec![], upvalues: scope.upvalues,
body, body,
ret: Box::new(Expression::Constant(ast::Constant::Nil)), ret: Box::new(Expression::Constant(ast::Constant::Nil)),
} }