work
This commit is contained in:
@@ -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))?;
|
||||
|
||||
Reference in New Issue
Block a user