Merged rule compilation for left side

This commit is contained in:
Devine Lu Linvega 2024-04-24 14:31:17 -07:00
parent 97d406c3a8
commit dfe9b076dc
1 changed files with 12 additions and 13 deletions

View File

@ -161,7 +161,7 @@ find_register(Rule *r, char reg)
static char * static char *
compile_rule(Rule *r, int id, char *src) compile_rule(Rule *r, int id, char *src)
{ {
int wrapped; int wrapped, reg;
char c, *cap, *s, *s2; char c, *cap, *s, *s2;
r->id = id, r->ptr = 0; r->id = id, r->ptr = 0;
@ -174,22 +174,21 @@ compile_rule(Rule *r, int id, char *src)
cap = walk(s2); cap = walk(s2);
wrapped = c == '('; wrapped = c == '(';
if(wrapped) s2++, cap--; if(wrapped) s2++, cap--;
while(s2 < cap) *dict_++ = *s2++; while(s2 < cap) {
if(*s2 == '?') {
*dict_++ = *s2++;
reg = find_register(r, *s2);
if(reg < 0 && *s2 != '(')
r->key[r->ptr] = *s2, reg = r->ptr++;
*dict_++ = '0' + reg;
} else
*dict_++ = *s2++;
}
s2 += wrapped; s2 += wrapped;
} }
*dict_++ = 0; *dict_++ = 0;
src = s2; src = s2;
s = r->a;
while((c = *s++)) { /* left */
if(c == '?') {
int reg = find_register(r, *s);
if(reg < 0 && *s != '(')
r->key[r->ptr] = *s, reg = r->ptr++;
*s = '0' + reg;
}
}
/* right ==================================== */ /* right ==================================== */
r->b = dict_; r->b = dict_;
@ -208,7 +207,7 @@ compile_rule(Rule *r, int id, char *src)
s = r->b; s = r->b;
while((c = *s++)) { /* right */ while((c = *s++)) { /* right */
if(c == '?') { if(c == '?') {
int reg = find_register(r, *s); reg = find_register(r, *s);
if(reg >= 0) if(reg >= 0)
*s = '0' + reg; *s = '0' + reg;
} }