Road to merging frag parsing and rule compilation

This commit is contained in:
Devine Lu Linvega 2024-04-24 13:41:09 -07:00
parent 76fe5830be
commit 23ec4856e0
1 changed files with 10 additions and 9 deletions

View File

@ -176,11 +176,13 @@ find_register(Rule *r, char reg)
return -1;
}
static void
compile_rule(Rule *r)
static char *
compile_rule(Rule *r, char *src)
{
int i;
char c, *s;
r->a = dict_, src = parse_frag(src);
r->b = dict_, src = parse_frag(src);
r->ptr = 0;
s = r->a;
while((c = *s++)) { /* left */
@ -199,6 +201,7 @@ compile_rule(Rule *r)
*s = '0' + reg;
}
}
return src;
}
static int
@ -211,17 +214,15 @@ rewrite(void)
Rule *r = NULL;
if(c == '<' && s[1] == '>') { /* rule */
r = rules_++, r->id = rules_ - rules - 1;
r->a = dict_, s = parse_frag(s + 2);
r->b = dict_, s = parse_frag(s);
compile_rule(r);
s = compile_rule(r, s + 2);
while(*s == ' ') s++;
return write_rule(r, s, 1);
}
if(c == '?' && s[1] == '(') { /* lambda */
cap = walk(s + 1), lambda.id = -1;
lambda.a = dict_, s = parse_frag(s + 2);
lambda.b = dict_, parse_frag(s), s = cap;
compile_rule(&lambda);
cap = walk(s + 1);
r = &lambda, r->id = -1;
compile_rule(r, s + 2);
s = cap;
while(*s == ' ') s++;
return apply_rule(&lambda, s);
}