Skip to content

Commit

Permalink
parse constructor expression
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Feb 11, 2024
1 parent 8e4c0c6 commit e1093db
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/syntax/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ type_expr:

expr:
| c=constant { EConst c }
| p=path DOT v=IDENT { EField (p, v) }
| p=path DOT v=MIDENT { EFieldCons (p, v) }
| v=IDENT { EVar v }
| c=MIDENT { ECons c }
| 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 }
Expand Down
2 changes: 2 additions & 0 deletions lib/syntax/parsetree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and constant =
and expr =
| EConst of constant
| EVar of string
| ECons of string
| ELet of pattern * expr * expr
| ELetrec of (string * lambda) list * expr
| ELam of lambda
Expand All @@ -45,6 +46,7 @@ and expr =
| EAnn of expr * type_expr
| ETuple of expr list
| EField of path * string
| EFieldCons of path * string

and lambda = para * expr

Expand Down
9 changes: 8 additions & 1 deletion lib/test/parse_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ let%expect_test "Test: expression parsing" =
(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)))) |}]
(EApp (EVar odd) (EConst (CInt 1)))) |}];
print_parsed {|E.f y|};
[%expect {| (EApp (EField (PName E) f) (EVar y)) |}];
print_parsed {|Cons (x, y)|};
[%expect {| (EApp (ECons Cons) (ETuple ((EVar x) (EVar y)))) |}];
print_parsed {|L.Cons (x, y)|};
[%expect
{| (EApp (EFieldCons (PName L) Cons) (ETuple ((EVar x) (EVar y)))) |}]

let%expect_test "Test: full program parsing" =
print_parsed_program {|let x = 1|};
Expand Down
2 changes: 2 additions & 0 deletions lib/typing/check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ let rec type_check (e : T.expr) (env : Env.t) : expr * U.t =
| T.EAnn (e, te) -> tc_ann e te
| T.ETuple es -> tc_tuple es
| T.EField (p, x) -> tc_field p x
| T.ECons _ -> failwith ""
| T.EFieldCons _ -> failwith ""

and tc_const c =
match c with
Expand Down

0 comments on commit e1093db

Please sign in to comment.