mirror of
https://github.com/edg-l/edlang.git
synced 2024-11-22 07:58:24 +00:00
struct init parsing
This commit is contained in:
parent
79c1243f0d
commit
604dcd33c0
|
@ -1,3 +1,5 @@
|
|||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
pub use edlang_span::Span;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
|
@ -167,6 +169,7 @@ pub struct Struct {
|
|||
pub enum Expression {
|
||||
Value(ValueExpr),
|
||||
FnCall(FnCallExpr),
|
||||
StructInit(StructInitExpr),
|
||||
Unary(UnaryOp, Box<Self>),
|
||||
Binary(Box<Self>, BinaryOp, Box<Self>),
|
||||
Deref(Box<Self>),
|
||||
|
@ -183,6 +186,19 @@ pub enum ValueExpr {
|
|||
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)]
|
||||
pub struct FnCallExpr {
|
||||
pub name: Ident,
|
||||
|
|
|
@ -491,6 +491,7 @@ fn find_expr_type(builder: &mut BodyBuilder, info: &ast::Expression) -> Option<T
|
|||
}
|
||||
ast::Expression::Deref(_) => todo!(),
|
||||
ast::Expression::AsRef(_, _) => todo!(),
|
||||
ast::Expression::StructInit(_) => todo!(),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -556,6 +557,7 @@ fn lower_expr(
|
|||
|
||||
(value, ty)
|
||||
}
|
||||
ast::Expression::StructInit(_) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -212,6 +212,13 @@ pub(crate) LetStmt: ast::LetStmt = {
|
|||
value,
|
||||
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 = {
|
||||
|
@ -221,6 +228,12 @@ pub(crate) AssignStmt: ast::AssignStmt = {
|
|||
deref_times: deref.len(),
|
||||
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 = {
|
||||
|
@ -272,6 +285,7 @@ pub(crate) Term: ast::Expression = {
|
|||
#[precedence(level="0")]
|
||||
<ValueExpr> => ast::Expression::Value(<>),
|
||||
<FnCallExpr> => ast::Expression::FnCall(<>),
|
||||
"(" <StructInitExpr> ")" => ast::Expression::StructInit(<>),
|
||||
#[precedence(level="2")] #[assoc(side="left")]
|
||||
"(" <Expression> ")",
|
||||
}
|
||||
|
@ -355,6 +369,25 @@ pub(crate) ValueExpr: ast::ValueExpr = {
|
|||
<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 = {
|
||||
"&" => ast::RefType::Not,
|
||||
"&" "mut" => ast::RefType::Mut,
|
||||
|
|
Loading…
Reference in a new issue