diff --git a/src/ast.rs b/src/ast.rs index db13900..6c3cb4b 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -4,12 +4,12 @@ //! All AST nodes are bump allocated into the lifetime `'ast` use crate::errors::Span; -use crate::value::Symbol; +use crate::value::AstSymbol; use bumpalo::collections::Vec; #[derive(Debug, PartialEq, Eq, Hash)] pub struct Ident<'ast> { - pub sym: Symbol<'ast>, + pub sym: AstSymbol<'ast>, pub span: Span, } @@ -112,7 +112,7 @@ impl Expr<'_> { #[derive(Debug, PartialEq)] pub enum Literal<'ast> { - String(String, Span), + String(&'ast str, Span), Number(f64, Span), Array(Vec<'ast, Expr<'ast>>, Span), Object(Span), diff --git a/src/bytecode.rs b/src/bytecode.rs index af8d2ee..8f8a1bf 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -2,9 +2,7 @@ use crate::errors::Span; use crate::value::{HashMap, NewSym}; -use bumpalo::boxed::Box; use bumpalo::collections::Vec; -use std::rc::Rc; #[derive(Debug)] pub struct FnBlock<'bc> { @@ -14,15 +12,14 @@ pub struct FnBlock<'bc> { pub arity: u8, } -// todo: this should be copy in the end tbh -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum Instr<'bc> { /// Store the current value on the stack to the stack location with the local offset `usize` Store(usize), /// Load the variable value from the local offset `usize` onto the stack Load(usize), /// Push a value onto the stack - PushVal(Box<'bc, Value>), + PushVal(&'bc Value), /// Negate the top value on the stack. Only works with numbers and booleans Neg, BinAdd, @@ -48,7 +45,7 @@ pub enum Value { Null, Bool(bool), Num(f64), - String(Rc), + String, Array, Object(HashMap), } diff --git a/src/compile.rs b/src/compile.rs index 248e77a..b6bd115 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -7,7 +7,6 @@ use crate::ast::{ use crate::bytecode::{FnBlock, Instr, Value}; use crate::errors::{CompilerError, Span}; use crate::value::HashMap; -use bumpalo::boxed::Box; use bumpalo::collections::Vec; use bumpalo::Bump; use std::cell::RefCell; @@ -24,7 +23,7 @@ struct Env<'ast> { impl Env<'_> { fn lookup_local(&self, name: &Ident) -> CResult { fn lookup_inner(env: &Env, name: &Ident) -> Option { - env.locals.get(name.sym.as_str()).copied().or_else(|| { + env.locals.get(name.sym).copied().or_else(|| { env.outer .as_ref() .map(|outer| lookup_inner(&outer.borrow(), name)) @@ -112,7 +111,7 @@ impl<'ast, 'bc> Compiler<'ast, 'bc> { self.env .borrow_mut() .locals - .insert(declaration.name.sym.as_str(), stack_pos); + .insert(declaration.name.sym, stack_pos); Ok(()) } @@ -196,7 +195,7 @@ impl<'ast, 'bc> Compiler<'ast, 'bc> { fn compile_expr_literal(&mut self, lit: &Literal) -> CResult<()> { let value = match lit { - Literal::String(str, _) => Value::String(str.clone().into()), + Literal::String(_str, _) => Value::String, Literal::Number(num, _) => Value::Num(*num), Literal::Array(vec, _) => { if vec.is_empty() { @@ -211,7 +210,7 @@ impl<'ast, 'bc> Compiler<'ast, 'bc> { }; self.push_instr( - Instr::PushVal(Box::new_in(value, self.bump)), + Instr::PushVal(self.bump.alloc(value)), StackChange::Grow, lit.span(), ); diff --git a/src/main.rs b/src/main.rs index 6aa1cf4..0c8ecd0 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 Ok("fn main() {}") { - // Ok(contents) => { - dilaria::run_program("fn main() {}"); - // } - // Err(err) => { - // eprintln!("{}", err); - // } - // } + match std::fs::read_to_string(filename) { + Ok(contents) => { + dilaria::run_program(&contents); + } + Err(err) => { + eprintln!("{}", err); + } + } } else { eprintln!("Usage: ") } diff --git a/src/parse.rs b/src/parse.rs index a2104a9..72dc9ee 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -527,7 +527,10 @@ where let next = self.next().ok_or_else(|| CompilerError::eof("primary"))?; let return_expr = match next.kind { - TokenKind::String(literal) => Ok(Expr::Literal(Literal::String(literal, next.span))), + TokenKind::String(literal) => Ok(Expr::Literal(Literal::String( + self.bump.alloc_str(&literal), + next.span, + ))), TokenKind::Number(literal) => Ok(Expr::Literal(Literal::Number(literal, next.span))), TokenKind::False => Ok(Expr::Literal(Literal::Boolean(false, next.span))), TokenKind::True => Ok(Expr::Literal(Literal::Boolean(true, next.span))), @@ -539,13 +542,10 @@ where let _ = self.expect(TokenKind::ParenC)?; Ok(expr) } - TokenKind::Ident(name) => { - let name_owned = bumpalo::collections::String::from_str_in(name, self.bump); - Ok(Expr::Ident(Ident { - sym: name_owned, - span: next.span, - })) - } + TokenKind::Ident(name) => Ok(Expr::Ident(Ident { + sym: self.bump.alloc_str(name), + span: next.span, + })), TokenKind::Error(error) => Err(*error), _ => Err(CompilerError::new( next.span, @@ -563,13 +563,10 @@ where .next() .ok_or_else(|| CompilerError::eof("identifier"))?; let return_expr = match kind { - TokenKind::Ident(name) => { - let name_owned = bumpalo::collections::String::from_str_in(name, self.bump); - Ok(Ident { - sym: name_owned, - span, - }) - } + TokenKind::Ident(name) => Ok(Ident { + sym: self.bump.alloc_str(name), + span, + }), TokenKind::Error(error) => Err(*error), _ => { return Err(CompilerError::new( diff --git a/src/value.rs b/src/value.rs index 7a97119..fd684c7 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<'ast> = bumpalo::collections::String<'ast>; +pub type AstSymbol<'ast> = &'ast str; /// here is the actual interning or something pub type NewSym = Gc;