This commit is contained in:
2026-04-17 18:51:12 -04:00
parent 2f91e454dd
commit b3f77076d4
20 changed files with 244 additions and 192 deletions
+25 -5
View File
@@ -1,13 +1,23 @@
use super::*;
pub struct Func {
args: Vec<Parsed<Param>>,
ret: Option<Parsed<Type>>,
body: Parsed<Expr>,
args: Vec<Param>,
name: Option<Ident>,
ret: Option<Type>,
body: Expr,
span: Span,
}
impl Node for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut name = None;
if let Token::Ident(ident) = ctx.expect_peek()? {
// yucky
let ident = ident.to_string();
ctx.next();
let ident = ctx.ident(ident);
name = Some(ident);
}
ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?;
let mut ret = None;
@@ -15,11 +25,21 @@ impl Node for Func {
ret = Some(ctx.parse()?);
}
let body = Expr::body(ctx)?;
Ok(Self { args, ret, body })
Ok(Self {
args,
ret,
body,
name,
span: ctx.span(),
})
}
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "fn(")?;
write!(f, "fn")?;
if let Some(name) = &self.name {
write!(f, " {name}")?;
}
write!(f, "(")?;
if let Some((last, rest)) = self.args.split_last() {
for arg in rest {
write!(f, "{}, ", arg.dsp(ctx))?;