From c174832c8ef6af6f738481c2ed479d338f8fe46d Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 4 Apr 2024 14:55:51 -0700 Subject: [PATCH] Skip empty rules --- src/modal.c | 34 ++++++++++++++++++---------------- test.modal | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/modal.c b/src/modal.c index ee4ab26..47b6088 100644 --- a/src/modal.c +++ b/src/modal.c @@ -53,11 +53,22 @@ writereg(char r) return 1; } +static void +save(void) +{ + int i, end = outp_ - outp; + for(i = 0; i < end; i++) + prog[i] = outp[i]; + prog[i] = 0; + prog_ = prog; + outp_ = outp; + printf(".. %s\n", prog); +} + static int rewrite(void) { char c, *p = prog; - printf("STEP ---------\n"); while((c = *p)) { int i, found = 0; for(i = 0; i < rules_len; i++) { @@ -65,7 +76,6 @@ rewrite(void) char *res = match(p, r); if(res != NULL) { char cc, *b = r->b; - printf("MATCH: %s -> %s\n", r->a, r->b); while((cc = *b++)) { if(cc == '?') writereg(*b++); @@ -82,18 +92,19 @@ rewrite(void) } } *outp_++ = 0; + save(); return 1; } static void -display(void) +print_rules(void) { int i; for(i = 0; i < rules_len; i++) { Rule *r = &rules[i]; printf("Rule #%d: %s -> %s\n", i, r->a, r->b); } - printf("\nStep %s -- %s\n", prog, outp); + printf("\n"); } static char * @@ -117,6 +128,7 @@ static void tokenize(char *t, FILE *f) { char c; + if(!t[0]) return; if(t[0] == '<' && t[1] == '>') { Rule *r = &rules[rules_len++]; r->a = parse_rulefrag(f), r->b = parse_rulefrag(f); @@ -147,15 +159,6 @@ parse(char *path) return 1; } -static void -save(void) -{ - int i, end = outp_ - outp; - for(i = 0; i < end; i++) - prog[i] = outp[i]; - prog[i] = 0; -} - int main(int argc, char **argv) { @@ -164,10 +167,9 @@ main(int argc, char **argv) if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v') return !printf("Modal - Modal Interpreter, 3 Apr 2024.\n"); parse(argv[1]); + print_rules(); + printf(".. %s\n", prog); rewrite(); - display(); - save(); rewrite(); - display(); return 0; } \ No newline at end of file diff --git a/test.modal b/test.modal index 0c2e849..dd63d61 100644 --- a/test.modal +++ b/test.modal @@ -1,4 +1,4 @@ -<> (?x ?y swap) (?y ?x) <> (?x dup) (?x ?x) +<> (?x ?y swap) (?y ?x) foo ((A B swap) dup) \ No newline at end of file