From e109696e7c1aed465e1ed8c79f89abcf1e6eb842 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 4 Apr 2024 09:56:55 -0700 Subject: [PATCH] Improved rule packing --- src/modal.c | 77 +++++++++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/src/modal.c b/src/modal.c index 9ff9ab5..40e72e3 100644 --- a/src/modal.c +++ b/src/modal.c @@ -4,28 +4,6 @@ static char dict[0x8000], *next = dict; -static char * -save(char *s, char c) -{ - char *o = next; - while((*next++ = *s++) && *s) - ; - *next++ = c; - return o; -} - -static char * -push(char *s, char c) -{ - char *d = dict; - for(d = dict; d < next; d++) { - char *ss = s, *dd = d, a, b; - while((a = *dd++) == (b = *ss++)) - if(!a && !b) return d; - } - return save(s, c); -} - /* rule */ typedef struct { @@ -36,34 +14,28 @@ typedef struct { static int rules_len; static Rule rules[0x100]; +static char * +addside(FILE *f) +{ + int depth = 0; + char c, *origin = next; + while(f && fread(&c, 1, 1, f) && c && c != 0xa) { + if(c == '(') depth++; + if(c == ')') --depth; + if(c == ' ' && !depth) + break; + else + *next++ = c; + } + *next++ = 0; + return origin; +} + static void addrule(FILE *f) { - int depth = 0; - char c, *left, *right; - left = next; - while(f && fread(&c, 1, 1, f) && c && c != 0xa) { - if(c == '(') depth++; - if(c == ')') --depth; - if(c == ' ' && !depth) - break; - else - *next++ = c; - } - *next++ = 0; - - right = next; - while(f && fread(&c, 1, 1, f) && c && c != 0xa) { - if(c == '(') depth++; - if(c == ')') --depth; - if(c == ' ' && !depth) - break; - else - *next++ = c; - } - *next++ = 0; - - printf("rule: %s -> %s\n", left, right); + Rule *r = &rules[rules_len++]; + r->a = addside(f), r->b = addside(f); } /* program */ @@ -107,9 +79,14 @@ walk(FILE *f) } static void -display(Rule *r, char *p) +display() { - printf("Program: %s\n", p); + 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("Program: %s\n", program); } static int @@ -119,7 +96,7 @@ eval(char *path) if(!(f = fopen(path, "r"))) return !printf("Invalid file: %s\n", path); walk(f); - display(rules, program); + display(); fclose(f); return 1; }