Housekeeping

This commit is contained in:
Devine Lu Linvega 2024-04-24 15:34:11 -07:00
parent 399ac8dd17
commit 5ab8e10a6b
1 changed files with 14 additions and 23 deletions

View File

@ -153,54 +153,45 @@ find_register(Rule *r, char reg)
{
int i;
for(i = 0; i < (int)r->ptr; i++)
if(r->key[i] == reg)
return i;
if(r->key[i] == reg) return i;
return -1;
}
static char *
compile_rule(Rule *r, int id, char *src)
{
int wrapped, reg;
char c, *cap;
int wrapped, reg;
r->id = id, r->ptr = 0, r->a = &empty, r->b = &empty;
/* phase: left */
while((c = *src) && c == ' ') src++;
if(c == ')' || (c == '<' && src[1] == '>')) return src;
r->a = dict_;
cap = walk(src);
wrapped = c == '(';
r->a = dict_, cap = walk(src), wrapped = c == '(';
if(wrapped) src++, cap--;
while(src < cap) {
if(*src == '?') {
*dict_++ = *src++;
c = *src, *dict_++ = *src++;
if(c == '?') {
reg = find_register(r, *src);
if(reg < 0 && *src != '(')
if(reg == -1 && *src != '(')
r->key[r->ptr] = *src, reg = r->ptr++;
*dict_++ = '0' + reg;
} else
*dict_++ = *src++;
}
}
src += wrapped;
*dict_++ = 0;
src += wrapped, *dict_++ = 0;
/* phase: right */
while((c = *src) && c == ' ') src++;
if(c == ')' || (c == '<' && src[1] == '>')) return src;
r->b = dict_;
cap = walk(src);
wrapped = c == '(';
r->b = dict_, cap = walk(src), wrapped = c == '(';
if(wrapped) src++, cap--;
while(src < cap) {
if(*src == '?') {
*dict_++ = *src++;
c = *src, *dict_++ = *src++;
if(c == '?') {
reg = find_register(r, *src);
*dict_++ = reg >= 0 ? ('0' + reg) : *src;
*dict_++ = reg != -1 ? '0' + reg : *src;
src++;
} else
*dict_++ = *src++;
}
}
src += wrapped;
*dict_++ = 0;
src += wrapped, *dict_++ = 0;
return src;
}