mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-14 17:35:03 +01:00
improve ast symbol handling
This commit is contained in:
parent
145d63e755
commit
1ad5ea7477
6 changed files with 36 additions and 36 deletions
14
src/ast.rs
14
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>>),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<str>),
|
||||
Array,
|
||||
Object(HashMap<Symbol, Value>),
|
||||
Object(HashMap<NewSym, Value>),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<T> = Result<T, CompilerError>;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Env {
|
||||
locals: HashMap<Symbol, usize>,
|
||||
outer: Option<Rc<RefCell<Env>>>,
|
||||
struct Env<'ast> {
|
||||
locals: HashMap<&'ast str, usize>,
|
||||
outer: Option<Rc<RefCell<Env<'ast>>>>,
|
||||
}
|
||||
|
||||
impl Env {
|
||||
impl Env<'_> {
|
||||
fn lookup_local(&self, name: &Ident) -> CResult<usize> {
|
||||
fn lookup_inner(env: &Env, name: &Ident) -> Option<usize> {
|
||||
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<RefCell<Env>>,
|
||||
env: Rc<RefCell<Env<'ast>>>,
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
|||
16
src/main.rs
16
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: <filename>")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -189,7 +189,7 @@ where
|
|||
}))
|
||||
}
|
||||
|
||||
fn fn_args(&mut self) -> ParseResult<Vec<'ast, Ident>> {
|
||||
fn fn_args(&mut self) -> ParseResult<Vec<'ast, Ident<'ast>>> {
|
||||
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<Ident> {
|
||||
fn ident(&mut self) -> ParseResult<Ident<'ast>> {
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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<str>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue