diff --git a/src/ast.rs b/src/ast.rs index 752640c..db13900 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -8,8 +8,8 @@ use crate::value::Symbol; use bumpalo::collections::Vec; #[derive(Debug, PartialEq, Eq, Hash)] -pub struct Ident { - pub sym: Symbol, +pub struct Ident<'ast> { + pub sym: Symbol<'ast>, pub span: Span, } @@ -40,7 +40,7 @@ pub enum Stmt<'ast> { #[derive(Debug, PartialEq)] pub struct Declaration<'ast> { pub span: Span, - pub name: Ident, + pub name: Ident<'ast>, pub init: Expr<'ast>, } @@ -54,8 +54,8 @@ pub struct Assignment<'ast> { #[derive(Debug, PartialEq)] pub struct FnDecl<'ast> { pub span: Span, - pub name: Ident, - pub params: Vec<'ast, Ident>, + pub name: Ident<'ast>, + pub params: Vec<'ast, Ident<'ast>>, pub body: Block<'ast>, } @@ -91,7 +91,7 @@ pub struct WhileStmt<'ast> { #[derive(Debug, PartialEq)] pub enum Expr<'ast> { - Ident(Ident), + Ident(Ident<'ast>), Literal(Literal<'ast>), UnaryOp(&'ast UnaryOp<'ast>), BinaryOp(&'ast BinaryOp<'ast>), @@ -180,6 +180,6 @@ pub struct Call<'ast> { #[derive(Debug, PartialEq)] pub enum CallKind<'ast> { - Field(Ident), + Field(Ident<'ast>), Fn(Vec<'ast, Expr<'ast>>), } diff --git a/src/bytecode.rs b/src/bytecode.rs index f795a31..af8d2ee 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -1,7 +1,7 @@ //! The bytecode that is executed in the vm use crate::errors::Span; -use crate::value::{HashMap, Symbol}; +use crate::value::{HashMap, NewSym}; use bumpalo::boxed::Box; use bumpalo::collections::Vec; use std::rc::Rc; @@ -50,5 +50,5 @@ pub enum Value { Num(f64), String(Rc), Array, - Object(HashMap), + Object(HashMap), } diff --git a/src/compile.rs b/src/compile.rs index 741b17e..248e77a 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -6,7 +6,7 @@ use crate::ast::{ }; use crate::bytecode::{FnBlock, Instr, Value}; use crate::errors::{CompilerError, Span}; -use crate::value::{HashMap, Symbol}; +use crate::value::HashMap; use bumpalo::boxed::Box; use bumpalo::collections::Vec; use bumpalo::Bump; @@ -16,15 +16,15 @@ use std::rc::Rc; type CResult = Result; #[derive(Debug, Default)] -struct Env { - locals: HashMap, - outer: Option>>, +struct Env<'ast> { + locals: HashMap<&'ast str, usize>, + outer: Option>>>, } -impl Env { +impl Env<'_> { fn lookup_local(&self, name: &Ident) -> CResult { fn lookup_inner(env: &Env, name: &Ident) -> Option { - env.locals.get(&name.sym).copied().or_else(|| { + env.locals.get(name.sym.as_str()).copied().or_else(|| { env.outer .as_ref() .map(|outer| lookup_inner(&outer.borrow(), name)) @@ -46,12 +46,12 @@ impl Env { } #[derive(Debug)] -struct Compiler<'bc> { +struct Compiler<'ast, 'bc> { blocks: Vec<'bc, FnBlock<'bc>>, current_block: usize, bump: &'bc Bump, /// the current local variables that are in scope, only needed for compiling - env: Rc>, + env: Rc>>, } pub fn compile<'bc>( @@ -70,8 +70,8 @@ pub fn compile<'bc>( Ok(compiler.blocks) } -impl<'bc> Compiler<'bc> { - fn compile(&mut self, ast: &Program) -> CResult<()> { +impl<'ast, 'bc> Compiler<'ast, 'bc> { + fn compile(&mut self, ast: &'ast Program<'ast>) -> CResult<()> { let global_block = FnBlock { code: Vec::new_in(self.bump), stack_sizes: Vec::new_in(self.bump), @@ -84,7 +84,7 @@ impl<'bc> Compiler<'bc> { Ok(()) } - fn compile_stmts(&mut self, stmts: &[Stmt]) -> CResult<()> { + fn compile_stmts(&mut self, stmts: &'ast [Stmt]) -> CResult<()> { for stmt in stmts { match stmt { Stmt::Declaration(inner) => self.compile_declaration(inner), @@ -104,7 +104,7 @@ impl<'bc> Compiler<'bc> { Ok(()) } - fn compile_declaration(&mut self, declaration: &Declaration) -> CResult<()> { + fn compile_declaration(&mut self, declaration: &'ast Declaration<'ast>) -> CResult<()> { // Compile the expression, the result of the expression will be the last thing left on the stack self.compile_expr(&declaration.init)?; // Now just remember that the value at this stack location is this variable name @@ -112,7 +112,7 @@ impl<'bc> Compiler<'bc> { self.env .borrow_mut() .locals - .insert(declaration.name.sym.clone(), stack_pos); + .insert(declaration.name.sym.as_str(), stack_pos); Ok(()) } @@ -167,7 +167,7 @@ impl<'bc> Compiler<'bc> { Ok(()) } - fn compile_block(&mut self, block: &Block) -> CResult<()> { + fn compile_block(&mut self, block: &'ast Block) -> CResult<()> { let next_env = Env::new_inner(self.env.clone()); self.env = next_env; diff --git a/src/main.rs b/src/main.rs index 0c8ecd0..6aa1cf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ fn main() { if let Some(filename) = std::env::args().nth(1) { - match std::fs::read_to_string(filename) { - Ok(contents) => { - dilaria::run_program(&contents); - } - Err(err) => { - eprintln!("{}", err); - } - } + // match Ok("fn main() {}") { + // Ok(contents) => { + dilaria::run_program("fn main() {}"); + // } + // Err(err) => { + // eprintln!("{}", err); + // } + // } } else { eprintln!("Usage: ") } diff --git a/src/parse.rs b/src/parse.rs index bf26f1f..a2104a9 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -189,7 +189,7 @@ where })) } - fn fn_args(&mut self) -> ParseResult> { + fn fn_args(&mut self) -> ParseResult>> { enter_parse!(self); self.expect(TokenKind::ParenO)?; @@ -540,7 +540,7 @@ where Ok(expr) } TokenKind::Ident(name) => { - let name_owned = name.to_owned(); + let name_owned = bumpalo::collections::String::from_str_in(name, self.bump); Ok(Expr::Ident(Ident { sym: name_owned, span: next.span, @@ -556,7 +556,7 @@ where return_expr } - fn ident(&mut self) -> ParseResult { + fn ident(&mut self) -> ParseResult> { enter_parse!(self); let Token { kind, span } = self @@ -564,7 +564,7 @@ where .ok_or_else(|| CompilerError::eof("identifier"))?; let return_expr = match kind { TokenKind::Ident(name) => { - let name_owned = name.to_owned(); + let name_owned = bumpalo::collections::String::from_str_in(name, self.bump); Ok(Ident { sym: name_owned, span, diff --git a/src/value.rs b/src/value.rs index 34db50c..7a97119 100644 --- a/src/value.rs +++ b/src/value.rs @@ -6,7 +6,7 @@ use std::ops::Deref; use std::ptr::NonNull; /// imagine interning or something here -pub type Symbol = String; +pub type Symbol<'ast> = bumpalo::collections::String<'ast>; /// here is the actual interning or something pub type NewSym = Gc;