This commit is contained in:
numzero 2025-04-19 12:13:38 +03:00
parent b8477148b6
commit 819d6ec3fc

View File

@ -55,6 +55,7 @@ struct Call {
#[derive(Debug, Clone, Default)]
struct Scope {
locals: Vec<ast::Ident>,
upvalues: Vec<(ast::Ident, Ident)>,
scope: HashMap<ast::Ident, Ident>,
}
@ -62,8 +63,9 @@ struct Scope {
impl Scope {
fn new_toplevel() -> Self {
Self {
upvalues: vec![("_ENV".to_string(), Ident::Local(0))],
scope: [("_ENV".to_string(), Ident::Upvalue(0))]
locals: vec!["_ENV".to_string()],
upvalues: vec![],
scope: [("_ENV".to_string(), Ident::Local(0))]
.into_iter()
.collect(),
}
@ -73,12 +75,25 @@ impl Scope {
self.scope.get(name).copied()
}
fn add(&mut self, name: &ast::Ident, ident: Ident) -> Ident {
self.scope.insert(name.clone(), ident);
ident
}
fn add_arg(&mut self, name: &ast::Ident, index: usize) {
self.scope.insert(name.clone(), Ident::Argument(index));
}
fn add_local(&mut self, name: &ast::Ident) -> Ident {
let index = self.locals.len();
self.locals.push(name.clone());
self.add(name, Ident::Local(index))
}
fn add_upvalue(&mut self, name: &ast::Ident, up_ident: Ident) -> Ident {
let index = self.upvalues.len();
self.upvalues.push((name.clone(), up_ident));
let ident = Ident::Upvalue(index);
self.scope.insert(name.clone(), ident);
ident
self.add(name, Ident::Upvalue(index))
}
}
@ -105,9 +120,8 @@ fn build_in_scope(parent: &BuildContext<'_>, code: ast::Function) -> Function {
parent: Some(parent),
scope: Default::default(),
};
if false {
build_in_scope(&scope, code.clone());
build_in_scope(&scope, code.clone());
for (index, name) in code.args.iter().enumerate() {
scope.scope.borrow_mut().add_arg(name, index);
}
todo!()
}