mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-16 10:25:02 +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;
|
use bumpalo::collections::Vec;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Ident {
|
pub struct Ident<'ast> {
|
||||||
pub sym: Symbol,
|
pub sym: Symbol<'ast>,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,7 +40,7 @@ pub enum Stmt<'ast> {
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Declaration<'ast> {
|
pub struct Declaration<'ast> {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub name: Ident,
|
pub name: Ident<'ast>,
|
||||||
pub init: Expr<'ast>,
|
pub init: Expr<'ast>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,8 +54,8 @@ pub struct Assignment<'ast> {
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct FnDecl<'ast> {
|
pub struct FnDecl<'ast> {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub name: Ident,
|
pub name: Ident<'ast>,
|
||||||
pub params: Vec<'ast, Ident>,
|
pub params: Vec<'ast, Ident<'ast>>,
|
||||||
pub body: Block<'ast>,
|
pub body: Block<'ast>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,7 +91,7 @@ pub struct WhileStmt<'ast> {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Expr<'ast> {
|
pub enum Expr<'ast> {
|
||||||
Ident(Ident),
|
Ident(Ident<'ast>),
|
||||||
Literal(Literal<'ast>),
|
Literal(Literal<'ast>),
|
||||||
UnaryOp(&'ast UnaryOp<'ast>),
|
UnaryOp(&'ast UnaryOp<'ast>),
|
||||||
BinaryOp(&'ast BinaryOp<'ast>),
|
BinaryOp(&'ast BinaryOp<'ast>),
|
||||||
|
|
@ -180,6 +180,6 @@ pub struct Call<'ast> {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum CallKind<'ast> {
|
pub enum CallKind<'ast> {
|
||||||
Field(Ident),
|
Field(Ident<'ast>),
|
||||||
Fn(Vec<'ast, Expr<'ast>>),
|
Fn(Vec<'ast, Expr<'ast>>),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
//! The bytecode that is executed in the vm
|
//! The bytecode that is executed in the vm
|
||||||
|
|
||||||
use crate::errors::Span;
|
use crate::errors::Span;
|
||||||
use crate::value::{HashMap, Symbol};
|
use crate::value::{HashMap, NewSym};
|
||||||
use bumpalo::boxed::Box;
|
use bumpalo::boxed::Box;
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
@ -50,5 +50,5 @@ pub enum Value {
|
||||||
Num(f64),
|
Num(f64),
|
||||||
String(Rc<str>),
|
String(Rc<str>),
|
||||||
Array,
|
Array,
|
||||||
Object(HashMap<Symbol, Value>),
|
Object(HashMap<NewSym, Value>),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ 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, Symbol};
|
use crate::value::HashMap;
|
||||||
use bumpalo::boxed::Box;
|
use bumpalo::boxed::Box;
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
|
|
@ -16,15 +16,15 @@ use std::rc::Rc;
|
||||||
type CResult<T> = Result<T, CompilerError>;
|
type CResult<T> = Result<T, CompilerError>;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Env {
|
struct Env<'ast> {
|
||||||
locals: HashMap<Symbol, usize>,
|
locals: HashMap<&'ast str, usize>,
|
||||||
outer: Option<Rc<RefCell<Env>>>,
|
outer: Option<Rc<RefCell<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).copied().or_else(|| {
|
env.locals.get(name.sym.as_str()).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))
|
||||||
|
|
@ -46,12 +46,12 @@ impl Env {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Compiler<'bc> {
|
struct Compiler<'ast, 'bc> {
|
||||||
blocks: Vec<'bc, FnBlock<'bc>>,
|
blocks: Vec<'bc, FnBlock<'bc>>,
|
||||||
current_block: usize,
|
current_block: usize,
|
||||||
bump: &'bc Bump,
|
bump: &'bc Bump,
|
||||||
/// the current local variables that are in scope, only needed for compiling
|
/// 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>(
|
pub fn compile<'bc>(
|
||||||
|
|
@ -70,8 +70,8 @@ pub fn compile<'bc>(
|
||||||
Ok(compiler.blocks)
|
Ok(compiler.blocks)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'bc> Compiler<'bc> {
|
impl<'ast, 'bc> Compiler<'ast, 'bc> {
|
||||||
fn compile(&mut self, ast: &Program) -> CResult<()> {
|
fn compile(&mut self, ast: &'ast Program<'ast>) -> CResult<()> {
|
||||||
let global_block = FnBlock {
|
let global_block = FnBlock {
|
||||||
code: Vec::new_in(self.bump),
|
code: Vec::new_in(self.bump),
|
||||||
stack_sizes: Vec::new_in(self.bump),
|
stack_sizes: Vec::new_in(self.bump),
|
||||||
|
|
@ -84,7 +84,7 @@ impl<'bc> Compiler<'bc> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_stmts(&mut self, stmts: &[Stmt]) -> CResult<()> {
|
fn compile_stmts(&mut self, stmts: &'ast [Stmt]) -> CResult<()> {
|
||||||
for stmt in stmts {
|
for stmt in stmts {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::Declaration(inner) => self.compile_declaration(inner),
|
Stmt::Declaration(inner) => self.compile_declaration(inner),
|
||||||
|
|
@ -104,7 +104,7 @@ impl<'bc> Compiler<'bc> {
|
||||||
Ok(())
|
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
|
// Compile the expression, the result of the expression will be the last thing left on the stack
|
||||||
self.compile_expr(&declaration.init)?;
|
self.compile_expr(&declaration.init)?;
|
||||||
// Now just remember that the value at this stack location is this variable name
|
// Now just remember that the value at this stack location is this variable name
|
||||||
|
|
@ -112,7 +112,7 @@ impl<'bc> Compiler<'bc> {
|
||||||
self.env
|
self.env
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.locals
|
.locals
|
||||||
.insert(declaration.name.sym.clone(), stack_pos);
|
.insert(declaration.name.sym.as_str(), stack_pos);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,7 +167,7 @@ impl<'bc> Compiler<'bc> {
|
||||||
Ok(())
|
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());
|
let next_env = Env::new_inner(self.env.clone());
|
||||||
self.env = next_env;
|
self.env = next_env;
|
||||||
|
|
||||||
|
|
|
||||||
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 std::fs::read_to_string(filename) {
|
// match Ok("fn main() {}") {
|
||||||
Ok(contents) => {
|
// Ok(contents) => {
|
||||||
dilaria::run_program(&contents);
|
dilaria::run_program("fn main() {}");
|
||||||
}
|
// }
|
||||||
Err(err) => {
|
// Err(err) => {
|
||||||
eprintln!("{}", err);
|
// eprintln!("{}", err);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Usage: <filename>")
|
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);
|
enter_parse!(self);
|
||||||
|
|
||||||
self.expect(TokenKind::ParenO)?;
|
self.expect(TokenKind::ParenO)?;
|
||||||
|
|
@ -540,7 +540,7 @@ where
|
||||||
Ok(expr)
|
Ok(expr)
|
||||||
}
|
}
|
||||||
TokenKind::Ident(name) => {
|
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 {
|
Ok(Expr::Ident(Ident {
|
||||||
sym: name_owned,
|
sym: name_owned,
|
||||||
span: next.span,
|
span: next.span,
|
||||||
|
|
@ -556,7 +556,7 @@ where
|
||||||
return_expr
|
return_expr
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ident(&mut self) -> ParseResult<Ident> {
|
fn ident(&mut self) -> ParseResult<Ident<'ast>> {
|
||||||
enter_parse!(self);
|
enter_parse!(self);
|
||||||
|
|
||||||
let Token { kind, span } = self
|
let Token { kind, span } = self
|
||||||
|
|
@ -564,7 +564,7 @@ where
|
||||||
.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) => {
|
||||||
let name_owned = name.to_owned();
|
let name_owned = bumpalo::collections::String::from_str_in(name, self.bump);
|
||||||
Ok(Ident {
|
Ok(Ident {
|
||||||
sym: name_owned,
|
sym: name_owned,
|
||||||
span,
|
span,
|
||||||
|
|
|
||||||
|
|
@ -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 = String;
|
pub type Symbol<'ast> = bumpalo::collections::String<'ast>;
|
||||||
|
|
||||||
/// 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