First replacement

This commit is contained in:
Devine Lu Linvega 2024-04-04 12:09:47 -07:00
parent 921cb53e2a
commit 36cf4b295f
1 changed files with 18 additions and 9 deletions

View File

@ -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 *