Warn on imbalance

This commit is contained in:
Devine Lu Linvega 2024-04-21 09:46:53 -07:00
parent 111e143d2f
commit ed5c0763e2
1 changed files with 6 additions and 2 deletions

View File

@ -207,7 +207,7 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
FILE *f; FILE *f;
int i; int i, pl = 0, pr = 0;
char c, *w = bank_a; char c, *w = bank_a;
if(argc < 2) if(argc < 2)
return !printf("usage: modal [-vqn] source.modal\n"); return !printf("usage: modal [-vqn] source.modal\n");
@ -219,18 +219,22 @@ main(int argc, char **argv)
} }
} }
if(!(f = fopen(argv[i], "r"))) if(!(f = fopen(argv[i], "r")))
return !fprintf(stdout, "Invalid Modal file: %s.\n", argv[i]); return !fprintf(stdout, "Modal file invalid: %s.\n", argv[i]);
while(fread(&c, 1, 1, f)) { while(fread(&c, 1, 1, f)) {
c = c <= 0x20 ? 0x20 : c; c = c <= 0x20 ? 0x20 : c;
if(w > bank_a) { if(w > bank_a) {
if(c == ' ' && *(w - 1) == '(') continue; if(c == ' ' && *(w - 1) == '(') continue;
if(c == ')' && *(w - 1) == ' ') w--; if(c == ')' && *(w - 1) == ' ') w--;
if(c == ' ' && *(w - 1) == ' ') w--; if(c == ' ' && *(w - 1) == ' ') w--;
if(c == '(') pl++;
if(c == ')') pr++;
} }
*w++ = c; *w++ = c;
} }
while(*(--w) <= ' ') *w = 0; while(*(--w) <= ' ') *w = 0;
fclose(f); fclose(f);
if(pr != pl)
return !fprintf(stdout, "Modal program imbalanced.\n");
while(rewrite()) while(rewrite())
if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n"); if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n");
return 0; return 0;