Skip to content

Commit

Permalink
let rec expression
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Feb 7, 2024
1 parent 4785834 commit 56ab4a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/syntax/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
17 changes: 15 additions & 2 deletions lib/test/parse_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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|};
Expand Down

0 comments on commit 56ab4a9

Please sign in to comment.