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