parse struct initialization

main
NotAFile 2022-04-05 14:52:56 +02:00
parent b6c6a23ca5
commit c0be718bbc
2 changed files with 37 additions and 1 deletions

View File

@ -223,6 +223,7 @@ impl Context {
todo!("expression blocks not representable in typed ir yet") todo!("expression blocks not representable in typed ir yet")
} }
}, },
Expression::StructInit(_) => todo!("structure initialization"),
}; };
Ok(t_expr) Ok(t_expr)
} }

View File

@ -6,7 +6,7 @@ use nom::{
branch::alt, branch::alt,
combinator::{map, opt}, combinator::{map, opt},
multi::separated_list0, multi::separated_list0,
sequence::{delimited, tuple}, sequence::{delimited, separated_pair, tuple},
}; };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -27,6 +27,12 @@ pub struct Call<'a> {
pub args: Vec<Expression<'a>>, pub args: Vec<Expression<'a>>,
} }
#[derive(Debug, Clone)]
pub struct StructInit<'a> {
pub name: Span<'a>,
pub args: Vec<(Token<'a>, Expression<'a>)>,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum BinOpKind { pub enum BinOpKind {
And, And,
@ -60,6 +66,7 @@ pub enum Expression<'a> {
UnOp(Box<UnOp<'a>>), UnOp(Box<UnOp<'a>>),
BinOp(Box<BinOp<'a>>), BinOp(Box<BinOp<'a>>),
Call(Box<Call<'a>>), Call(Box<Call<'a>>),
StructInit(Box<StructInit<'a>>),
BlockExpr(Box<BlockExpr<'a>>), BlockExpr(Box<BlockExpr<'a>>),
} }
@ -67,6 +74,9 @@ pub enum Expression<'a> {
fn atom(input: TokenSpan) -> IResult<TokenSpan, Expression> { fn atom(input: TokenSpan) -> IResult<TokenSpan, Expression> {
alt(( alt((
map(call_item, |call| Expression::Call(Box::new(call))), map(call_item, |call| Expression::Call(Box::new(call))),
map(struct_init_item, |sinit| {
Expression::StructInit(Box::new(sinit))
}),
map(token(tk::Ident), |it| { map(token(tk::Ident), |it| {
Expression::Path(it.span().fragment()) Expression::Path(it.span().fragment())
}), }),
@ -146,6 +156,26 @@ pub fn call_item(input: TokenSpan) -> IResult<TokenSpan, Call> {
)(input) )(input)
} }
pub fn struct_init_item(input: TokenSpan) -> IResult<TokenSpan, StructInit> {
map(
tuple((
token(tk::Ident),
delimited(
token(tk::LBrace),
separated_list0(
token(tk::Comma),
separated_pair(token(tk::Ident), token(tk::Colon), expression),
),
token(tk::RBrace),
),
)),
|(name, args)| StructInit {
name: name.span(),
args,
},
)(input)
}
pub fn expression(input: TokenSpan) -> IResult<TokenSpan, Expression> { pub fn expression(input: TokenSpan) -> IResult<TokenSpan, Expression> {
alt(( alt((
bitop, bitop,
@ -200,4 +230,9 @@ mod test {
fn test_call() { fn test_call() {
fullexpr(TokenSpan::new(&tok("a()"))).unwrap(); fullexpr(TokenSpan::new(&tok("a()"))).unwrap();
} }
#[test]
fn test_struct_init() {
fullexpr(TokenSpan::new(&tok("a{}"))).unwrap();
}
} }