-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
40 lines (37 loc) · 1.04 KB
/
parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdio.h"
#include "common.h"
#include "parse.h"
int matchBrackets(bf_command* prgm) {
int i = 0, j = 0;
int count = 0;
while (prgm[i].type != bf_end) {
if (prgm[i].type == bf_open) {
j = i + 1;
while (prgm[j].type != bf_end) {
if (prgm[j].type == bf_open) {
count++;
}
else if (prgm[j].type == bf_close) {
count--;
}
if (count == -1) {
prgm[i].val = j;
prgm[j].val = i;
count = 0;
break;
}
j++;
}
if (prgm[j].type == bf_end) {
printf("Error: Mismatched brackets.\n");
return PARSE_BRACKETS;
}
}
if (prgm[i].type == bf_close && prgm[i].val == -1) {
printf("Error: Mismatched brackets.\n");
return PARSE_BRACKETS;
}
i++;
}
return 0;
}