Skip to content

Commit

Permalink
Add parsing rules for fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaz1502 committed Dec 6, 2024
1 parent ccbd66a commit 86e6e9d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Lam/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ eraseNames = go []
go (id2 : lctx) gctx e2 >>= \e2' ->
go (id3 : lctx) gctx e3 >>= \e3' ->
Right (Case e1' id2 e2' id3 e3')
go lctx gctx (RawFix e) =
go lctx gctx e >>= \e' ->
Right (Fix e')

parseUntypedExpr :: String -> Either String Expr
parseUntypedExpr str =
Expand Down
2 changes: 2 additions & 0 deletions src/Lam/Parser/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokens :-
<0> "." { tok Dot }
<0> "," { tok Comma }
<0> "lam" { tok Lam }
<0> "fix" { tok Fix }
<0> "EVAL" { tok Eval }
<0> "EXIT" { tok Exit }
<0> "TYPEDEF" { tok Typedef }
Expand Down Expand Up @@ -72,6 +73,7 @@ data Token =
| Dot
| Comma
| Lam
| Fix
| Eval
| Exit
| Typedef
Expand Down
3 changes: 3 additions & 0 deletions src/Lam/Parser/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Lam.Parser.Lexer qualified as L

%token
"lam" { L.Lam }
"fix" { L.Fix }
":" { L.Colon }
"::" { L.TypeColon }
";" { L.Semicolon }
Expand Down Expand Up @@ -155,6 +156,7 @@ UntypedRawExpr :: { RawExpr }
| "proj1" UntypedRawExpr { RawUnOp Proj1 $2 }
| "proj2" UntypedRawExpr { RawUnOp Proj2 $2 }
| "<" UntypedRawExpr "," UntypedRawExpr ">" { RawBinOp MkPair $2 $4 }
| "fix" UntypedRawExpr { RawFix $2 }
| UntypedParExpr { $1 }

UntypedParExpr : "(" UntypedRawExpr ")" { $2 }
Expand All @@ -180,6 +182,7 @@ RawExpr :: { RawExpr }
| "inl" RawExpr "as" RawTypeL { RawInl $2 $4 }
| "inr" RawExpr "as" RawTypeL { RawInr $2 $4 }
| "case" RawExpr "of" "inl" var "=>" RawExpr "|" "inr" var "=>" RawExpr { RawCase $2 $5 $7 $10 $12 }
| "fix" RawExpr { RawFix $2 }
| ParExpr { $1 }

ParExpr : "(" RawExpr ")" { $2 }
Expand Down
2 changes: 2 additions & 0 deletions src/Lam/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ prettyPrint = go []
go ctx isUntyped (App e1 e2) =
let f = go ctx isUntyped
in unwords ["(", f e1, ".", f e2, ")"]
go ctx isUntyped (Fix e) =
unwords [ "( fix", go ctx isUntyped e, ")" ]

untypedPrettyPrint, typedPrettyPrint :: Expr -> String
untypedPrettyPrint = prettyPrint True
Expand Down

0 comments on commit 86e6e9d

Please sign in to comment.