mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-16 10:25:02 +01:00
fixed more leaks
This commit is contained in:
parent
1ad5ea7477
commit
e58e6e3dc4
6 changed files with 31 additions and 38 deletions
|
|
@ -4,12 +4,12 @@
|
||||||
//! All AST nodes are bump allocated into the lifetime `'ast`
|
//! All AST nodes are bump allocated into the lifetime `'ast`
|
||||||
|
|
||||||
use crate::errors::Span;
|
use crate::errors::Span;
|
||||||
use crate::value::Symbol;
|
use crate::value::AstSymbol;
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Ident<'ast> {
|
pub struct Ident<'ast> {
|
||||||
pub sym: Symbol<'ast>,
|
pub sym: AstSymbol<'ast>,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,7 +112,7 @@ impl Expr<'_> {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Literal<'ast> {
|
pub enum Literal<'ast> {
|
||||||
String(String, Span),
|
String(&'ast str, Span),
|
||||||
Number(f64, Span),
|
Number(f64, Span),
|
||||||
Array(Vec<'ast, Expr<'ast>>, Span),
|
Array(Vec<'ast, Expr<'ast>>, Span),
|
||||||
Object(Span),
|
Object(Span),
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
use crate::errors::Span;
|
use crate::errors::Span;
|
||||||
use crate::value::{HashMap, NewSym};
|
use crate::value::{HashMap, NewSym};
|
||||||
use bumpalo::boxed::Box;
|
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FnBlock<'bc> {
|
pub struct FnBlock<'bc> {
|
||||||
|
|
@ -14,15 +12,14 @@ pub struct FnBlock<'bc> {
|
||||||
pub arity: u8,
|
pub arity: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: this should be copy in the end tbh
|
#[derive(Debug, Clone, Copy)]
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Instr<'bc> {
|
pub enum Instr<'bc> {
|
||||||
/// Store the current value on the stack to the stack location with the local offset `usize`
|
/// Store the current value on the stack to the stack location with the local offset `usize`
|
||||||
Store(usize),
|
Store(usize),
|
||||||
/// Load the variable value from the local offset `usize` onto the stack
|
/// Load the variable value from the local offset `usize` onto the stack
|
||||||
Load(usize),
|
Load(usize),
|
||||||
/// Push a value onto the stack
|
/// 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
|
/// Negate the top value on the stack. Only works with numbers and booleans
|
||||||
Neg,
|
Neg,
|
||||||
BinAdd,
|
BinAdd,
|
||||||
|
|
@ -48,7 +45,7 @@ pub enum Value {
|
||||||
Null,
|
Null,
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
Num(f64),
|
Num(f64),
|
||||||
String(Rc<str>),
|
String,
|
||||||
Array,
|
Array,
|
||||||
Object(HashMap<NewSym, Value>),
|
Object(HashMap<NewSym, Value>),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ use crate::ast::{
|
||||||
use crate::bytecode::{FnBlock, Instr, Value};
|
use crate::bytecode::{FnBlock, Instr, Value};
|
||||||
use crate::errors::{CompilerError, Span};
|
use crate::errors::{CompilerError, Span};
|
||||||
use crate::value::HashMap;
|
use crate::value::HashMap;
|
||||||
use bumpalo::boxed::Box;
|
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
|
@ -24,7 +23,7 @@ struct Env<'ast> {
|
||||||
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.as_str()).copied().or_else(|| {
|
env.locals.get(name.sym).copied().or_else(|| {
|
||||||
env.outer
|
env.outer
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|outer| lookup_inner(&outer.borrow(), name))
|
.map(|outer| lookup_inner(&outer.borrow(), name))
|
||||||
|
|
@ -112,7 +111,7 @@ impl<'ast, 'bc> Compiler<'ast, 'bc> {
|
||||||
self.env
|
self.env
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.locals
|
.locals
|
||||||
.insert(declaration.name.sym.as_str(), stack_pos);
|
.insert(declaration.name.sym, stack_pos);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -196,7 +195,7 @@ impl<'ast, 'bc> Compiler<'ast, 'bc> {
|
||||||
|
|
||||||
fn compile_expr_literal(&mut self, lit: &Literal) -> CResult<()> {
|
fn compile_expr_literal(&mut self, lit: &Literal) -> CResult<()> {
|
||||||
let value = match lit {
|
let value = match lit {
|
||||||
Literal::String(str, _) => Value::String(str.clone().into()),
|
Literal::String(_str, _) => Value::String,
|
||||||
Literal::Number(num, _) => Value::Num(*num),
|
Literal::Number(num, _) => Value::Num(*num),
|
||||||
Literal::Array(vec, _) => {
|
Literal::Array(vec, _) => {
|
||||||
if vec.is_empty() {
|
if vec.is_empty() {
|
||||||
|
|
@ -211,7 +210,7 @@ impl<'ast, 'bc> Compiler<'ast, 'bc> {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.push_instr(
|
self.push_instr(
|
||||||
Instr::PushVal(Box::new_in(value, self.bump)),
|
Instr::PushVal(self.bump.alloc(value)),
|
||||||
StackChange::Grow,
|
StackChange::Grow,
|
||||||
lit.span(),
|
lit.span(),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
16
src/main.rs
16
src/main.rs
|
|
@ -1,13 +1,13 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
if let Some(filename) = std::env::args().nth(1) {
|
if let Some(filename) = std::env::args().nth(1) {
|
||||||
// match Ok("fn main() {}") {
|
match std::fs::read_to_string(filename) {
|
||||||
// Ok(contents) => {
|
Ok(contents) => {
|
||||||
dilaria::run_program("fn main() {}");
|
dilaria::run_program(&contents);
|
||||||
// }
|
}
|
||||||
// Err(err) => {
|
Err(err) => {
|
||||||
// eprintln!("{}", err);
|
eprintln!("{}", err);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Usage: <filename>")
|
eprintln!("Usage: <filename>")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
27
src/parse.rs
27
src/parse.rs
|
|
@ -527,7 +527,10 @@ where
|
||||||
|
|
||||||
let next = self.next().ok_or_else(|| CompilerError::eof("primary"))?;
|
let next = self.next().ok_or_else(|| CompilerError::eof("primary"))?;
|
||||||
let return_expr = match next.kind {
|
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::Number(literal) => Ok(Expr::Literal(Literal::Number(literal, next.span))),
|
||||||
TokenKind::False => Ok(Expr::Literal(Literal::Boolean(false, next.span))),
|
TokenKind::False => Ok(Expr::Literal(Literal::Boolean(false, next.span))),
|
||||||
TokenKind::True => Ok(Expr::Literal(Literal::Boolean(true, next.span))),
|
TokenKind::True => Ok(Expr::Literal(Literal::Boolean(true, next.span))),
|
||||||
|
|
@ -539,13 +542,10 @@ where
|
||||||
let _ = self.expect(TokenKind::ParenC)?;
|
let _ = self.expect(TokenKind::ParenC)?;
|
||||||
Ok(expr)
|
Ok(expr)
|
||||||
}
|
}
|
||||||
TokenKind::Ident(name) => {
|
TokenKind::Ident(name) => Ok(Expr::Ident(Ident {
|
||||||
let name_owned = bumpalo::collections::String::from_str_in(name, self.bump);
|
sym: self.bump.alloc_str(name),
|
||||||
Ok(Expr::Ident(Ident {
|
span: next.span,
|
||||||
sym: name_owned,
|
})),
|
||||||
span: next.span,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
TokenKind::Error(error) => Err(*error),
|
TokenKind::Error(error) => Err(*error),
|
||||||
_ => Err(CompilerError::new(
|
_ => Err(CompilerError::new(
|
||||||
next.span,
|
next.span,
|
||||||
|
|
@ -563,13 +563,10 @@ where
|
||||||
.next()
|
.next()
|
||||||
.ok_or_else(|| CompilerError::eof("identifier"))?;
|
.ok_or_else(|| CompilerError::eof("identifier"))?;
|
||||||
let return_expr = match kind {
|
let return_expr = match kind {
|
||||||
TokenKind::Ident(name) => {
|
TokenKind::Ident(name) => Ok(Ident {
|
||||||
let name_owned = bumpalo::collections::String::from_str_in(name, self.bump);
|
sym: self.bump.alloc_str(name),
|
||||||
Ok(Ident {
|
span,
|
||||||
sym: name_owned,
|
}),
|
||||||
span,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
TokenKind::Error(error) => Err(*error),
|
TokenKind::Error(error) => Err(*error),
|
||||||
_ => {
|
_ => {
|
||||||
return Err(CompilerError::new(
|
return Err(CompilerError::new(
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use std::ops::Deref;
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
|
|
||||||
/// imagine interning or something here
|
/// 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
|
/// here is the actual interning or something
|
||||||
pub type NewSym = Gc<str>;
|
pub type NewSym = Gc<str>;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue