Skip to content

Commit

Permalink
Allow struct constructions in more places.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlamonster committed Oct 31, 2023
1 parent 6a17b79 commit 0a79521
Show file tree
Hide file tree
Showing 2 changed files with 2,727 additions and 5,961 deletions.
40 changes: 20 additions & 20 deletions compiler/src/passes/parse/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Def: Def<&'input str, Expr<&'input str>> = {
sym,
variants,
},
"fn" <sym:Ident> "(" <params:Comma<Param>> ")" <typ:("->" <Type>)?> "{" <bdy: Expr<Struct>> "}" => Def::Fn {
"fn" <sym:Ident> "(" <params:Comma<Param>> ")" <typ:("->" <Type>)?> "{" <bdy: Expr> "}" => Def::Fn {
sym,
params,
typ: typ.unwrap_or(Type::Unit),
Expand Down Expand Up @@ -138,36 +138,36 @@ Type: Type = {
// ExprCall
// ExprAtom
// Num/Bool/Ident
Expr<T> = ExprStmt<T>;
Expr = ExprStmt;

ExprStmt<T>: Expr<&'input str> = {
"let" <mutable:"mut"?> <sym:Ident> "=" <bnd:ExprLogicalOr<T>> ";" <bdy:ExprStmt<T>?> => Expr::Let {
ExprStmt: Expr<&'input str> = {
"let" <mutable:"mut"?> <sym:Ident> "=" <bnd:ExprLogicalOr<Struct>> ";" <bdy:ExprStmt?> => Expr::Let {
sym,
mutable: mutable.is_some(),
bnd: Box::new(bnd),
bdy: Box::new(bdy.unwrap_or(Expr::Lit { val: Lit::Unit })),
},
<stmt:ExprInStmt<T>> ";" <cnt:ExprStmt<T>?> => Expr::Seq {
<stmt:ExprInStmt> ";" <cnt:ExprStmt?> => Expr::Seq {
stmt: Box::new(stmt),
cnt: Box::new(cnt.unwrap_or(Expr::Lit { val: Lit::Unit })),
},
ExprInStmt<T>,
ExprInStmt,
}

ExprInStmt<T>: Expr<&'input str> = {
<sym:Ident> "=" <bnd:ExprLogicalOr<T>> => Expr::Assign {
ExprInStmt: Expr<&'input str> = {
<sym:Ident> "=" <bnd:ExprLogicalOr<Struct>> => Expr::Assign {
sym,
bnd: Box::new(bnd),
},
"if" <cnd:ExprLogicalOr<Never>> "{" <thn:Expr<T>> "}" <els:("else" "{" <Expr<T>> "}")?> => Expr::If {
"if" <cnd:ExprLogicalOr<Never>> "{" <thn:Expr> "}" <els:("else" "{" <Expr> "}")?> => Expr::If {
cnd: Box::new(cnd),
thn: Box::new(thn),
els: Box::new(els.unwrap_or(Expr::Lit { val: Lit::Unit })),
},
"loop" "{" <bdy:Expr<T>> "}" => Expr::Loop {
"loop" "{" <bdy:Expr> "}" => Expr::Loop {
bdy: Box::new(bdy),
},
"while" <cnd:ExprLogicalOr<Never>> "{" <bdy:Expr<T>> "}" => Expr::Loop {
"while" <cnd:ExprLogicalOr<Never>> "{" <bdy:Expr> "}" => Expr::Loop {
bdy: Box::new(Expr::If {
cnd: Box::new(cnd),
thn: Box::new(bdy),
Expand All @@ -177,18 +177,18 @@ ExprInStmt<T>: Expr<&'input str> = {
}),
}),
},
"switch" <enm:ExprLogicalOr<Never>> "{" <arms:Comma<(<Ident> "(" <Ident> ")" "=>" <Expr<T>> )>> "}" => Expr::Switch {
"switch" <enm:ExprLogicalOr<Never>> "{" <arms:Comma<(<Ident> "(" <Ident> ")" "=>" <Expr> )>> "}" => Expr::Switch {
enm: Box::new(enm),
arms: arms.into_iter().map(|(s1, s2, e)| (s1, s2, Box::new(e))).collect(),
},
"break" <bdy:ExprLogicalOr<T>?> => Expr::Break {
"break" <bdy:ExprLogicalOr<Struct>?> => Expr::Break {
bdy: Box::new(bdy.unwrap_or(Expr::Lit { val: Lit::Unit })),
},
"return" <bdy:ExprLogicalOr<T>?> => Expr::Return {
"return" <bdy:ExprLogicalOr<Struct>?> => Expr::Return {
bdy: Box::new(bdy.unwrap_or(Expr::Lit { val: Lit::Unit })),
},
"continue" => Expr::Continue,
ExprLogicalOr<T>,
ExprLogicalOr<Struct>,
}

BinaryOps<Op,Next>: Expr<&'input str> = {
Expand Down Expand Up @@ -244,11 +244,11 @@ ExprCall<T>: Expr<&'input str> = {
op: Op::Read,
args: vec![],
},
"print" "(" <e: Expr<T>> ")" => Expr::Prim {
"print" "(" <e: Expr> ")" => Expr::Prim {
op: Op::Print,
args: vec![e],
},
<fun:ExprAtom<T>> "(" <args:Comma<Expr<T>>> ")" => Expr::Apply {
<fun:ExprAtom<T>> "(" <args:Comma<Expr>> ")" => Expr::Apply {
fun: Box::new(fun),
args,
},
Expand All @@ -260,17 +260,17 @@ ExprAtom<T>: Expr<&'input str> = {
<val:Bool> => Expr::Lit { val: Lit::Bool { val }},
"unit" => Expr::Lit { val: Lit::Unit },
<sym:Ident> => Expr::Var { sym },
<enum_sym:Ident> "::" <variant_sym:Ident> "(" <bdy:Expr<T>> ")" => Expr::Variant {
<enum_sym:Ident> "::" <variant_sym:Ident> "(" <bdy:Expr> ")" => Expr::Variant {
enum_sym,
variant_sym,
bdy: Box::new(bdy),
},
"(" <Expr<Struct>> ")",
"(" <Expr> ")",
<T>,
}

Struct: Expr<&'input str> = {
<sym:Ident> "{" <fields:Comma<(<Ident> ":" <Expr<Struct>>)>> "}" => Expr::Struct {
<sym:Ident> "{" <fields:Comma<(<Ident> ":" <Expr>)>> "}" => Expr::Struct {
sym,
fields,
},
Expand Down
Loading

0 comments on commit 0a79521

Please sign in to comment.