remove 'ast lifetime from compiler

This commit is contained in:
nora 2022-01-04 22:03:20 +01:00
parent 5699ab190a
commit 1a77e710d5

View file

@ -17,12 +17,12 @@ use std::rc::Rc;
type CResult<T = ()> = Result<T, CompilerError>; type CResult<T = ()> = Result<T, CompilerError>;
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct Env<'ast> { struct Env {
locals: HashMap<Symbol, usize>, locals: HashMap<Symbol, usize>,
outer: Option<Rc<RefCell<Env<'ast>>>>, outer: Option<Rc<RefCell<Env>>>,
} }
impl Env<'_> { impl Env {
fn lookup_local(&self, name: &Ident) -> CResult<usize> { fn lookup_local(&self, name: &Ident) -> CResult<usize> {
fn lookup_inner(env: &Env, name: &Ident) -> Option<usize> { fn lookup_inner(env: &Env, name: &Ident) -> Option<usize> {
env.locals.get(&name.sym).copied().or_else(|| { env.locals.get(&name.sym).copied().or_else(|| {
@ -49,12 +49,12 @@ impl Env<'_> {
} }
#[derive(Debug)] #[derive(Debug)]
struct Compiler<'ast, 'bc, 'gc> { struct Compiler<'bc, 'gc> {
blocks: Vec<'bc, FnBlock<'bc>>, blocks: Vec<'bc, FnBlock<'bc>>,
current_block: usize, current_block: usize,
bump: &'bc Bump, bump: &'bc Bump,
/// the current local variables that are in scope, only needed for compiling /// the current local variables that are in scope, only needed for compiling
env: Rc<RefCell<Env<'ast>>>, env: Rc<RefCell<Env>>,
rt: &'gc mut RtAlloc, rt: &'gc mut RtAlloc,
} }
@ -76,8 +76,8 @@ pub fn compile<'ast, 'bc, 'gc>(
Ok(compiler.blocks) Ok(compiler.blocks)
} }
impl<'ast, 'bc, 'gc> Compiler<'ast, 'bc, 'gc> { impl<'bc, 'gc> Compiler<'bc, 'gc> {
fn compile(&mut self, ast: &'ast Program<'ast>) -> CResult { fn compile(&mut self, ast: &Program) -> CResult {
let global_block = FnBlock { let global_block = FnBlock {
code: Vec::new_in(self.bump), code: Vec::new_in(self.bump),
stack_sizes: Vec::new_in(self.bump), stack_sizes: Vec::new_in(self.bump),
@ -94,7 +94,7 @@ impl<'ast, 'bc, 'gc> Compiler<'ast, 'bc, 'gc> {
Ok(()) Ok(())
} }
fn compile_stmts(&mut self, stmts: &'ast [Stmt]) -> CResult { fn compile_stmts(&mut self, stmts: &[Stmt]) -> CResult {
for stmt in stmts { for stmt in stmts {
match stmt { match stmt {
Stmt::Declaration(inner) => self.compile_declaration(inner), Stmt::Declaration(inner) => self.compile_declaration(inner),
@ -114,7 +114,7 @@ impl<'ast, 'bc, 'gc> Compiler<'ast, 'bc, 'gc> {
Ok(()) Ok(())
} }
fn compile_declaration(&mut self, declaration: &'ast Declaration<'ast>) -> CResult { fn compile_declaration(&mut self, declaration: &Declaration) -> CResult {
// Compile the expression, the result of the expression will be the last thing left on the stack // Compile the expression, the result of the expression will be the last thing left on the stack
self.compile_expr(&declaration.init)?; self.compile_expr(&declaration.init)?;
// Now just remember that the value at this stack location is this variable name // Now just remember that the value at this stack location is this variable name
@ -149,7 +149,7 @@ impl<'ast, 'bc, 'gc> Compiler<'ast, 'bc, 'gc> {
todo!() todo!()
} }
fn compile_if(&mut self, if_stmt: &'ast IfStmt) -> CResult { fn compile_if(&mut self, if_stmt: &IfStmt) -> CResult {
/* /*
0 PushVal (true) 0 PushVal (true)
1 JumpCond (2) 1 JumpCond (2)
@ -193,7 +193,7 @@ impl<'ast, 'bc, 'gc> Compiler<'ast, 'bc, 'gc> {
Ok(()) Ok(())
} }
fn compile_loop(&mut self, ast_block: &'ast Block, span: Span) -> CResult { fn compile_loop(&mut self, ast_block: &Block, span: Span) -> CResult {
/* /*
>0 // do things >0 // do things
1 JMP (-2), 1 JMP (-2),
@ -209,7 +209,7 @@ impl<'ast, 'bc, 'gc> Compiler<'ast, 'bc, 'gc> {
Ok(()) Ok(())
} }
fn compile_while(&mut self, while_stmt: &'ast WhileStmt) -> CResult { fn compile_while(&mut self, while_stmt: &WhileStmt) -> CResult {
/* /*
>0 PushVal (true) >0 PushVal (true)
1 JmpFalse (2) 1 JmpFalse (2)
@ -251,7 +251,7 @@ impl<'ast, 'bc, 'gc> Compiler<'ast, 'bc, 'gc> {
Ok(()) Ok(())
} }
fn compile_block(&mut self, block: &'ast Block) -> CResult { fn compile_block(&mut self, block: &Block) -> CResult {
let next_env = Env::new_inner(self.env.clone()); let next_env = Env::new_inner(self.env.clone());
self.env = next_env; self.env = next_env;