From ed5c0763e220ca4bef1a0f3dcf99a5708d57d350 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sun, 21 Apr 2024 09:46:53 -0700 Subject: [PATCH] Warn on imbalance --- src/modal.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/modal.c b/src/modal.c index adaa97d..38d9cae 100644 --- a/src/modal.c +++ b/src/modal.c @@ -207,7 +207,7 @@ int main(int argc, char **argv) { FILE *f; - int i; + int i, pl = 0, pr = 0; char c, *w = bank_a; if(argc < 2) return !printf("usage: modal [-vqn] source.modal\n"); @@ -219,18 +219,22 @@ main(int argc, char **argv) } } 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)) { c = c <= 0x20 ? 0x20 : c; if(w > bank_a) { if(c == ' ' && *(w - 1) == '(') continue; if(c == ')' && *(w - 1) == ' ') w--; if(c == ' ' && *(w - 1) == ' ') w--; + if(c == '(') pl++; + if(c == ')') pr++; } *w++ = c; } while(*(--w) <= ' ') *w = 0; fclose(f); + if(pr != pl) + return !fprintf(stdout, "Modal program imbalanced.\n"); while(rewrite()) if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n"); return 0;