fixed more leaks

This commit is contained in:
nora 2021-12-30 20:07:27 +01:00
parent 1ad5ea7477
commit e58e6e3dc4
6 changed files with 31 additions and 38 deletions

View file

@ -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),

View file

@ -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<str>),
String,
Array,
Object(HashMap<NewSym, Value>),
}

View file

@ -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<usize> {
fn lookup_inner(env: &Env, name: &Ident) -> Option<usize> {
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(),
);

View file

@ -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: <filename>")
}

View file

@ -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(

View file

@ -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<str>;