Skip to content

Commit

Permalink
give if-then-else expression a precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Aug 23, 2024
1 parent 3f1c543 commit aea7647
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/syntax/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ expr:
{ make_node (ELet (x, e1, e2)) $startpos $endpos }
| LET REC binds=separated_nonempty_list(AND, function_bind) IN body=expr %prec over_TOP
{ make_node (ELetrec (binds, body)) $startpos $endpos }
| IF e0=expr THEN e1=expr ELSE e2=expr
| IF e0=expr THEN e1=expr ELSE e2=expr %prec over_TOP
{ make_node (EIf (e0, e1, e2)) $startpos $endpos }
| tu=tuple_expr
{ tu }
Expand Down
32 changes: 32 additions & 0 deletions tests/regular/parse_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,38 @@ let result = print_int (sum 4)
(start_loc ((pos_fname "") (pos_lnum 3) (pos_bol 46) (pos_cnum 78)))
(end_loc ((pos_fname "") (pos_lnum 3) (pos_bol 46) (pos_cnum 79)))
(attrs ()))))
|}];

print_parsed_program {|
let x = if true then 2 else 1

let x = 1
|};
[%expect
{|
((TopLet x
((desc
(EIf
((desc (EConst (CBool true)))
(start_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 12)))
(end_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 16)))
(attrs ()))
((desc (EConst (CInt 2)))
(start_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 22)))
(end_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 23)))
(attrs ()))
((desc (EConst (CInt 1)))
(start_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 29)))
(end_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 30)))
(attrs ()))))
(start_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 9)))
(end_loc ((pos_fname "") (pos_lnum 2) (pos_bol 1) (pos_cnum 30)))
(attrs ())))
(TopLet x
((desc (EConst (CInt 1)))
(start_loc ((pos_fname "") (pos_lnum 4) (pos_bol 33) (pos_cnum 41)))
(end_loc ((pos_fname "") (pos_lnum 4) (pos_bol 33) (pos_cnum 42)))
(attrs ()))))
|}]

let%expect_test "Test: path parsing" =
Expand Down

0 comments on commit aea7647

Please sign in to comment.