nodes + dyn stuff which is dumb and I'ma replace

This commit is contained in:
2024-10-07 13:53:33 -04:00
parent e5aea8b24e
commit ca52443f81
13 changed files with 408 additions and 125 deletions
+23 -8
View File
@@ -6,34 +6,37 @@ mod error;
mod expr;
mod token;
mod val;
mod node;
pub use body::*;
pub use cursor::*;
pub use error::*;
pub use expr::*;
pub use val::*;
pub use node::*;
use token::*;
#[derive(Debug)]
pub struct Module {
functions: Vec<Function>,
functions: Vec<Node<Function>>,
}
#[derive(Clone)]
pub struct Function {
pub name: String,
pub body: Body,
pub body: Node<Body>,
}
impl Module {
pub fn parse(cursor: &mut TokenCursor) -> Result<Self, ParserError> {
impl Parsable for Module {
fn parse(cursor: &mut TokenCursor) -> Result<Self, ParserError> {
let mut functions = Vec::new();
loop {
let Some(next) = cursor.peek() else {
return Ok(Self { functions });
};
if next.is_keyword(Keyword::Fn) {
functions.push(Function::parse(cursor)?);
functions.push(Node::parse(cursor));
} else {
return Err(ParserError::unexpected_token(next, "fn"));
}
@@ -41,13 +44,13 @@ impl Module {
}
}
impl Function {
pub fn parse(cursor: &mut TokenCursor) -> Result<Self, ParserError> {
impl Parsable for Function {
fn parse(cursor: &mut TokenCursor) -> Result<Self, ParserError> {
cursor.expect_kw(Keyword::Fn)?;
let name = cursor.expect_ident()?;
cursor.expect_sym(Symbol::OpenParen)?;
cursor.expect_sym(Symbol::CloseParen)?;
let body = Body::parse(cursor)?;
let body = Node::parse(cursor);
Ok(Self { name, body })
}
}
@@ -61,3 +64,15 @@ impl Debug for Function {
Ok(())
}
}
impl NodeContainer for Module {
fn children(&self) -> Vec<Node<Box<dyn NodeContainer>>> {
self.functions.iter().map(|f| f.containerr()).collect()
}
}
impl NodeContainer for Function {
fn children(&self) -> Vec<Node<Box<dyn NodeContainer>>> {
vec![self.body.containerr()]
}
}