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