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