This commit is contained in:
2026-04-17 01:49:43 -04:00
parent e5ae506a84
commit 2f91e454dd
16 changed files with 268 additions and 401 deletions
+10 -16
View File
@@ -1,12 +1,12 @@
use super::*;
pub struct Func {
args: Vec<Id<Param>>,
ret: Option<Id<Type>>,
body: Id<Expr>,
args: Vec<Parsed<Param>>,
ret: Option<Parsed<Type>>,
body: Parsed<Expr>,
}
impl Parsable for Func {
impl Node for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?;
@@ -14,16 +14,10 @@ impl Parsable for Func {
if ctx.next_if(Token::Arrow) {
ret = Some(ctx.parse()?);
}
let body = if ret.is_some() {
ctx.parse_with(Expr::block)
} else {
ctx.parse()
}?;
let body = Expr::body(ctx)?;
Ok(Self { args, ret, body })
}
}
impl FmtNode for Func {
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "fn(")?;
if let Some((last, rest)) = self.args.split_last() {
@@ -33,16 +27,16 @@ impl FmtNode for Func {
write!(f, "{}", last.dsp(ctx))?;
}
write!(f, ") ")?;
if let Some(ret) = self.ret {
if let Some(ret) = &self.ret {
write!(f, "-> {} ", ret.dsp(ctx))?;
}
self.body.fmt(f, ctx)?;
self.body.fmt_body(f, ctx)?;
Ok(())
}
}
impl Id<Func> {
pub fn ends_with_block(&self, nodes: &Nodes) -> bool {
nodes[self].body.ends_with_block(nodes)
impl Func {
pub fn ends_with_block(&self) -> bool {
self.body.ends_with_block()
}
}