struct init parsing

This commit is contained in:
Edgar 2024-02-18 09:27:39 +01:00
parent 79c1243f0d
commit 604dcd33c0
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
3 changed files with 51 additions and 0 deletions

View file

@ -1,3 +1,5 @@
use std::collections::{BTreeMap, HashMap};
pub use edlang_span::Span; pub use edlang_span::Span;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -167,6 +169,7 @@ pub struct Struct {
pub enum Expression { pub enum Expression {
Value(ValueExpr), Value(ValueExpr),
FnCall(FnCallExpr), FnCall(FnCallExpr),
StructInit(StructInitExpr),
Unary(UnaryOp, Box<Self>), Unary(UnaryOp, Box<Self>),
Binary(Box<Self>, BinaryOp, Box<Self>), Binary(Box<Self>, BinaryOp, Box<Self>),
Deref(Box<Self>), Deref(Box<Self>),
@ -183,6 +186,19 @@ pub enum ValueExpr {
Path(PathExpr), Path(PathExpr),
} }
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StructInitField {
pub value: Expression,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StructInitExpr {
pub name: Ident,
pub fields: BTreeMap<Ident, StructInitField>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FnCallExpr { pub struct FnCallExpr {
pub name: Ident, pub name: Ident,

View file

@ -491,6 +491,7 @@ fn find_expr_type(builder: &mut BodyBuilder, info: &ast::Expression) -> Option<T
} }
ast::Expression::Deref(_) => todo!(), ast::Expression::Deref(_) => todo!(),
ast::Expression::AsRef(_, _) => todo!(), ast::Expression::AsRef(_, _) => todo!(),
ast::Expression::StructInit(_) => todo!(),
}) })
} }
@ -556,6 +557,7 @@ fn lower_expr(
(value, ty) (value, ty)
} }
ast::Expression::StructInit(_) => todo!(),
} }
} }

View file

@ -212,6 +212,13 @@ pub(crate) LetStmt: ast::LetStmt = {
value, value,
span: ast::Span::new(lo, hi), span: ast::Span::new(lo, hi),
}, },
<lo:@L> "let" <is_mut:"mut"?> <name:Ident> ":" <target_type:Type> "=" <value:StructInitExpr> <hi:@R> => ast::LetStmt {
is_mut: is_mut.is_some(),
name,
r#type: target_type,
value: ast::Expression::StructInit(value),
span: ast::Span::new(lo, hi),
},
} }
pub(crate) AssignStmt: ast::AssignStmt = { pub(crate) AssignStmt: ast::AssignStmt = {
@ -221,6 +228,12 @@ pub(crate) AssignStmt: ast::AssignStmt = {
deref_times: deref.len(), deref_times: deref.len(),
span: ast::Span::new(lo, hi), span: ast::Span::new(lo, hi),
}, },
<lo:@L> <deref:"*"*> <name:PathExpr> "=" <value:StructInitExpr> <hi:@R> => ast::AssignStmt {
name,
value: ast::Expression::StructInit(value),
deref_times: deref.len(),
span: ast::Span::new(lo, hi),
},
} }
pub(crate) ReturnStmt: ast::ReturnStmt = { pub(crate) ReturnStmt: ast::ReturnStmt = {
@ -272,6 +285,7 @@ pub(crate) Term: ast::Expression = {
#[precedence(level="0")] #[precedence(level="0")]
<ValueExpr> => ast::Expression::Value(<>), <ValueExpr> => ast::Expression::Value(<>),
<FnCallExpr> => ast::Expression::FnCall(<>), <FnCallExpr> => ast::Expression::FnCall(<>),
"(" <StructInitExpr> ")" => ast::Expression::StructInit(<>),
#[precedence(level="2")] #[assoc(side="left")] #[precedence(level="2")] #[assoc(side="left")]
"(" <Expression> ")", "(" <Expression> ")",
} }
@ -355,6 +369,25 @@ pub(crate) ValueExpr: ast::ValueExpr = {
<PathExpr> => ast::ValueExpr::Path(<>), <PathExpr> => ast::ValueExpr::Path(<>),
} }
pub(crate) StructInitField: (ast::Ident, ast::StructInitField) = {
<lo:@L> <name:Ident> ":" <value:Expression> <hi:@R> => (name, ast::StructInitField {
value,
span: ast::Span::new(lo, hi),
}),
<lo:@L> <name:Ident> ":" <value:StructInitExpr> <hi:@R> => (name, ast::StructInitField {
value: ast::Expression::StructInit(value),
span: ast::Span::new(lo, hi),
})
}
pub(crate) StructInitExpr: ast::StructInitExpr = {
<lo:@L> <name:Ident> "{" <fields:Comma<StructInitField>> "}" <hi:@R> => ast::StructInitExpr {
name,
fields: fields.into_iter().collect(),
span: ast::Span::new(lo, hi),
}
}
pub(crate) RefType: ast::RefType = { pub(crate) RefType: ast::RefType = {
"&" => ast::RefType::Not, "&" => ast::RefType::Not,
"&" "mut" => ast::RefType::Mut, "&" "mut" => ast::RefType::Mut,