Skip to content

Commit

Permalink
fix: Fix match arms parsing
Browse files Browse the repository at this point in the history
Indentation of match arms were not handled correctly.
  • Loading branch information
vain0x committed Aug 16, 2019
1 parent 330fb36 commit 933d87a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 26 deletions.
4 changes: 3 additions & 1 deletion boot/MiloneLang/Parsing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,11 @@ let parseMatch boxX matchLoc tokens =
| _, tokens ->
parseError "Expected 'with'" tokens

let pipeX = nextX tokens

let rec go acc tokens =
match parseMatchArm boxX tokens with
| arm, (Token.Pipe, _) :: tokens ->
| arm, (Token.Pipe, (_, x)) :: tokens when x = pipeX ->
go (arm :: acc) tokens
| arm, tokens ->
List.rev (arm :: acc), tokens
Expand Down
25 changes: 12 additions & 13 deletions boot/tests/examples/calc/calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,33 +681,32 @@ next_68:;
struct IntToken_ListTuple2 match_22;
struct IntToken_ListTuple2 call_38 = evalExpr_(tokens_6);
int value_ = call_38.t0;
if (!((!((!(call_38.t1)))))) goto next_71;
if (!((call_38.t1->head.tag == TkOp_))) goto next_71;
if (!(((*(call_38.t1->head.TkOp_)) == ')'))) goto next_71;
if (!((!((!(call_38.t1)))))) goto next_72;
if (!((call_38.t1->head.tag == TkOp_))) goto next_72;
if (!(((*(call_38.t1->head.TkOp_)) == ')'))) goto next_72;
struct Token_List* tokens_7 = call_38.t1->tail;
struct IntToken_ListTuple2 tuple_25;
tuple_25.t0 = value_;
tuple_25.t1 = tokens_7;
match_22 = tuple_25;
goto end_match_70;
next_71:;
goto end_match_71;
next_72:;
printf("expected ')'\n");
int call_39 = 0;
exit(1);
match_22 = ((struct IntToken_ListTuple2){});
goto end_match_70;
next_72:;
printf("expected an int\n");
int call_40 = 0;
exit(1);
match_22 = ((struct IntToken_ListTuple2){});
goto end_match_70;
goto end_match_71;
next_73:;
end_match_70:;
end_match_71:;
match_21 = match_22;
goto end_match_67;
next_69:;
printf("expected an int\n");
int call_40 = 0;
exit(1);
match_21 = ((struct IntToken_ListTuple2){});
goto end_match_67;
next_70:;
end_match_67:;
return match_21;
}
Expand Down
63 changes: 53 additions & 10 deletions boot/tests/features/match_many/match_many.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
int testTrivialCase_(int arg_);

struct IntList;

int testListMatching_(int arg_1);

struct IntIntTuple2;

int main();
int testMatchArmsMakeScope_(int arg_2);

struct IntList {
int head;
struct IntList* tail;
};
int testNestedMatchesParseCorrectly_(int arg_3);

struct IntIntTuple2 {
int t0;
int t1;
};
int main();

int main() {
int testTrivialCase_(int arg_) {
int match_;
match_ = 0;
goto end_match_1;
next_2:;
end_match_1:;
return 0;
}

struct IntList {
int head;
struct IntList* tail;
};

int testListMatching_(int arg_1) {
int match_1;
struct IntList* list_ = (struct IntList*)malloc(sizeof(struct IntList));
list_->head = 0;
Expand All @@ -43,6 +50,15 @@ next_5:;
next_6:;
exit(1);
end_match_3:;
return 0;
}

struct IntIntTuple2 {
int t0;
int t1;
};

int testMatchArmsMakeScope_(int arg_2) {
int x_ = 2;
int match_2;
struct IntIntTuple2 tuple_;
Expand Down Expand Up @@ -77,3 +93,30 @@ next_10:;
end_match_7:;
return 0;
}

int testNestedMatchesParseCorrectly_(int arg_3) {
int match_4;
if (!(0)) goto next_15;
int match_5;
exit(1);
match_5 = 0;
goto end_match_17;
next_18:;
end_match_17:;
match_4 = 0;
goto end_match_14;
next_15:;
match_4 = 0;
goto end_match_14;
next_16:;
end_match_14:;
return 0;
}

int main() {
int call_ = testTrivialCase_(0);
int call_1 = testListMatching_(0);
int call_2 = testMatchArmsMakeScope_(0);
int call_3 = testNestedMatchesParseCorrectly_(0);
return 0;
}
19 changes: 17 additions & 2 deletions boot/tests/features/match_many/match_many.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
let main _ =
let testTrivialCase () =
match () with
| () -> ()

let testListMatching () =
match 0 :: [] with
| _ :: _ :: [] -> exit 1
| _ :: [] -> ()
| [] -> exit 1

// Check if match arms make a scope.
let testMatchArmsMakeScope () =
let x = 2
match x, 1 with
| 1, x ->
Expand All @@ -17,4 +18,18 @@ let main _ =
| _ ->
exit 1

let testNestedMatchesParseCorrectly () =
match 1 with
| _ when false ->
match 1 with
| _ ->
exit 1
| _ ->
()

let main _ =
testTrivialCase ()
testListMatching ()
testMatchArmsMakeScope ()
testNestedMatchesParseCorrectly ()
0

0 comments on commit 933d87a

Please sign in to comment.