use super::*; pub struct Func { args: Vec, ret: Option, body: Expr, span: Span, } impl Node for Func { fn parse(ctx: &mut ParseCtx) -> Result { ctx.expect(Token::OpenParen)?; let args = ctx.list(Token::Comma, Token::CloseParen)?; let mut ret = None; if ctx.next_if(Token::Arrow) { ret = Some(ctx.parse()?); } let body = Expr::body(ctx)?; Ok(Self { args, ret, body, span: ctx.span(), }) } fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result { write!(f, "fn")?; write!(f, "(")?; if let Some((last, rest)) = self.args.split_last() { for arg in rest { write!(f, "{}, ", arg.dsp(ctx))?; } write!(f, "{}", last.dsp(ctx))?; } write!(f, ") ")?; if let Some(ret) = &self.ret { write!(f, "-> {} ", ret.dsp(ctx))?; } self.body.fmt_body(f, ctx)?; Ok(()) } } impl Func { pub fn ends_with_block(&self) -> bool { self.body.ends_with_block() } }