From 56ab4a9fec6d928a2824a5bd5222bd59f1272598 Mon Sep 17 00:00:00 2001 From: butterunderflow Date: Wed, 7 Feb 2024 18:58:50 +0800 Subject: [PATCH] let rec expression --- lib/syntax/parser.mly | 1 + lib/test/parse_test.ml | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/syntax/parser.mly b/lib/syntax/parser.mly index 51de560..275ef3d 100644 --- a/lib/syntax/parser.mly +++ b/lib/syntax/parser.mly @@ -120,6 +120,7 @@ expr: | c=constant { EConst c } | v=IDENT { EVar v } | LET p=pattern EQ e1=expr IN e2=expr { ELet (p, e1, e2) } + | LET REC binds=separated_nonempty_list(AND, function_bind) IN body=expr { ELetrec (binds, body) } | tu=tuple_expr { tu } | func=expr arg=expr { EApp (func, arg) } | LPAREN e=expr RPAREN { e } diff --git a/lib/test/parse_test.ml b/lib/test/parse_test.ml index 57cd0df..bfcb1b2 100644 --- a/lib/test/parse_test.ml +++ b/lib/test/parse_test.ml @@ -22,10 +22,23 @@ let%expect_test "Test: expression parsing" = print_parsed "let x = 1 in y"; [%expect {| (ELet (PVar x) (EConst (CInt 1)) (EVar y)) |}]; print_parsed "1,3,4,(5,6),7"; - [%expect {| + [%expect + {| (ETuple ((EConst (CInt 1)) (EConst (CInt 3)) (EConst (CInt 4)) - (ETuple ((EConst (CInt 5)) (EConst (CInt 6)))) (EConst (CInt 7)))) |}] + (ETuple ((EConst (CInt 5)) (EConst (CInt 6)))) (EConst (CInt 7)))) |}]; + print_parsed + {| + let rec odd = fun x -> even x + and even = fun (x:int) -> odd x + in + odd 1 + |}; + [%expect {| + (ELetrec + ((odd ((PBare x) (EApp (EVar even) (EVar x)))) + (even ((PAnn x (TCons int ())) (EApp (EVar odd) (EVar x))))) + (EApp (EVar odd) (EConst (CInt 1)))) |}] let%expect_test "Test: full program parsing" = print_parsed_program {|let x = 1|};