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