This commit is contained in:
numzero 2025-04-19 12:15:15 +03:00
parent 819d6ec3fc
commit b0e201fccb

View File

@ -1,4 +1,4 @@
use std::{cell::RefCell, collections::HashMap}; use std::cell::RefCell;
use crate::ast; use crate::ast;
@ -53,15 +53,21 @@ struct Call {
args: Vec<Expression>, args: Vec<Expression>,
} }
#[derive(Debug, Clone, Default)] mod scope {
struct Scope { use std::collections::HashMap;
use super::Ident;
use crate::ast;
#[derive(Debug, Clone, Default)]
pub struct Scope {
locals: Vec<ast::Ident>, locals: Vec<ast::Ident>,
upvalues: Vec<(ast::Ident, Ident)>, upvalues: Vec<(ast::Ident, Ident)>,
scope: HashMap<ast::Ident, Ident>, scope: HashMap<ast::Ident, Ident>,
} }
impl Scope { impl Scope {
fn new_toplevel() -> Self { pub fn new_toplevel() -> Self {
Self { Self {
locals: vec!["_ENV".to_string()], locals: vec!["_ENV".to_string()],
upvalues: vec![], upvalues: vec![],
@ -71,7 +77,7 @@ impl Scope {
} }
} }
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()
} }
@ -80,22 +86,24 @@ impl Scope {
ident ident
} }
fn add_arg(&mut self, name: &ast::Ident, index: usize) { pub fn add_arg(&mut self, name: &ast::Ident, index: usize) {
self.scope.insert(name.clone(), Ident::Argument(index)); self.scope.insert(name.clone(), Ident::Argument(index));
} }
fn add_local(&mut self, name: &ast::Ident) -> Ident { pub fn add_local(&mut self, name: &ast::Ident) -> Ident {
let index = self.locals.len(); let index = self.locals.len();
self.locals.push(name.clone()); self.locals.push(name.clone());
self.add(name, Ident::Local(index)) self.add(name, Ident::Local(index))
} }
fn add_upvalue(&mut self, name: &ast::Ident, up_ident: Ident) -> Ident { pub 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));
self.add(name, Ident::Upvalue(index)) self.add(name, Ident::Upvalue(index))
} }
}
} }
use scope::Scope;
#[derive(Debug)] #[derive(Debug)]
struct BuildContext<'a> { struct BuildContext<'a> {