Skip to content

Commit

Permalink
Implement annotated let binding syntactic sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-healy committed Nov 20, 2023
1 parent 21148e8 commit 2b64e61
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 9 additions & 0 deletions examples/pass/annotated_let_binding.uplp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- category = "value"
--
-- [metadata]
-- type = "Num"
-- value = 10
let a: Num = 1 in
let b: Num = 10 in
let c: Bool = false in
if c then a else b
5 changes: 5 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ impl RawExpr {
pub fn make_let(
rec: bool,
ident: RawIdent,
ann: Option<Type>,
binding: Box<RawExpr>,
body: Box<RawExpr>,
) -> Box<Self> {
let binding = match ann {
Some(ann) => Box::new(RawExpr::Ascribed(binding, ann)),
None => binding,
};
Box::new(Self::Let(rec, interner::Id::new(ident.0), binding, body))
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub Uplp = Expr;
Expr: Box<RawExpr> = {
InfixExpr,
<InfixExpr> ":" <Type> => Box::new(RawExpr::Ascribed(<>)),
"let" <rec: "rec"?> <ident: Ident> "=" <bnd: Expr> "in" <body: Expr> =>
RawExpr::make_let(rec.is_some(), ident, bnd, body),
"let" <rec: "rec"?> <ident: Ident> <ann: Annotation?> "=" <bnd: Expr> "in" <body: Expr> =>
RawExpr::make_let(rec.is_some(), ident, ann, bnd, body),
"|" <CommaSeparated<FnArg>> "|" <Expr> => RawExpr::make_lambda(<>),
"if" <Expr> "then" <Expr> "else" <Expr> => Box::new(RawExpr::IfThenElse(<>)),
}
Expand Down Expand Up @@ -125,4 +125,4 @@ match {
// skip single-line comments beginning with "--"
r"--[^\n\r]*[\n\r]*" => { },
_
}
}

0 comments on commit 2b64e61

Please sign in to comment.