diff --git a/src/modal.c b/src/modal.c index 7db2b7b..e799db9 100644 --- a/src/modal.c +++ b/src/modal.c @@ -25,17 +25,16 @@ walk(char *s) return s - 1; } -static int +static char * match(char *p, Rule *r) { char c, *a = r->a, *b = p; while((c = *a)) { if(c == '?') regs[(int)*(++a)] = b, a++, b = walk(b), c = *b; - if(c != *b) return 0; + if(c != *b) return NULL; a++, b++; } - printf("> found: %s = %s, rule: %s\n", p, r->a, r->b); - return 1; + return b; } static int @@ -43,13 +42,23 @@ rewrite(void) { char c, *p = prog; while((c = *p)) { - int i; + int i, found = 0; for(i = 0; i < rules_len; i++) { Rule *r = &rules[i]; - match(p, r); + char *res = match(p, r); + if(res != NULL) { + char cc, *b = r->b; + while((cc = *b++)) { + *outp_++ = cc; + } + found = 1; + p = res; + } + } + if(!found) { + *outp_++ = c; + p++; } - *outp_++ = c; - p++; } return 1; } @@ -62,7 +71,7 @@ display(void) Rule *r = &rules[i]; printf("Rule #%d: %s -> %s\n", i, r->a, r->b); } - printf("\nEval: %s -- %s\n", prog, outp); + printf("\nStep %s -- %s\n", prog, outp); } static char *