This commit is contained in:
2024-10-18 16:52:12 -04:00
parent b15a40c4d9
commit 14a4fb1ff9
22 changed files with 1672 additions and 77 deletions
+28
View File
@@ -0,0 +1,28 @@
use super::{Body, Ident, Keyword, Node, Parsable, ParseResult, ParserErrors, Symbol, TokenCursor};
use std::fmt::Debug;
pub struct Function {
pub name: Node<Ident>,
pub body: Node<Body>,
}
impl Parsable for Function {
fn parse(cursor: &mut TokenCursor, errors: &mut ParserErrors) -> ParseResult<Self> {
cursor.expect_kw(Keyword::Fn)?;
let name = Node::parse(cursor, errors)?;
cursor.expect_sym(Symbol::OpenParen)?;
cursor.expect_sym(Symbol::CloseParen)?;
Node::parse(cursor, errors).map(|body| Self { name, body })
}
}
impl Debug for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("fn ")?;
self.name.fmt(f)?;
f.write_str("() ")?;
self.body.fmt(f)?;
Ok(())
}
}