Abstracted reg lookup

This commit is contained in:
Devine Lu Linvega 2024-04-24 13:35:12 -07:00
parent 986a1b47dd
commit 76fe5830be
1 changed files with 14 additions and 11 deletions

View File

@ -166,6 +166,16 @@ parse_frag(char *s)
return s; return s;
} }
static int
find_register(Rule *r, char reg)
{
int i;
for(i = 0; i < (int)r->ptr; i++)
if(r->key[i] == reg)
return i;
return -1;
}
static void static void
compile_rule(Rule *r) compile_rule(Rule *r)
{ {
@ -175,12 +185,7 @@ compile_rule(Rule *r)
s = r->a; s = r->a;
while((c = *s++)) { /* left */ while((c = *s++)) { /* left */
if(c == '?') { if(c == '?') {
int reg = -1; int reg = find_register(r, *s);
for(i = 0; i < (int)r->ptr; i++)
if(r->key[i] == *s) {
reg = i;
break;
}
if(reg < 0 && *s != '(') if(reg < 0 && *s != '(')
r->key[r->ptr] = *s, reg = r->ptr++; r->key[r->ptr] = *s, reg = r->ptr++;
*s = '0' + reg; *s = '0' + reg;
@ -189,11 +194,9 @@ compile_rule(Rule *r)
s = r->b; s = r->b;
while((c = *s++)) { /* right */ while((c = *s++)) { /* right */
if(c == '?') { if(c == '?') {
for(i = 0; i < (int)r->ptr; i++) int reg = find_register(r, *s);
if(r->key[i] == *s) { if(reg >= 0)
*s = '0' + i; *s = '0' + reg;
break;
}
} }
} }
} }