This commit is contained in:
Devine Lu Linvega 2024-04-24 20:26:56 -07:00
parent 56bb5969ab
commit 97f8cf03ac
1 changed files with 10 additions and 12 deletions

View File

@ -111,23 +111,22 @@ apply_rule(Rule *r, char *s)
for(i = 0; i < r->ptr; i++) for(i = 0; i < r->ptr; i++)
regs[i] = NULL; regs[i] = NULL;
/* phase: match rule */ /* phase: match rule */
while((c = *a)) { while((c = *a++)) {
if(c == '?') { if(c == '?') {
char *pcap = walk(s); char *pcap = walk(s);
id = *(++a) - '0'; id = *a - '0';
if((reg = regs[id])) { /* reg cmp */ if((reg = regs[id])) { /* reg cmp */
char *rcap = walk(reg), *pp = s; char *rcap = walk(reg), *pp = s;
while(reg < rcap || pp < pcap) while(reg < rcap || pp < pcap)
if(*reg++ != *pp++) return 0; if(*reg++ != *pp++) return 0;
} else /* reg set */ } else /* reg set */
regs[id] = s; regs[id] = s;
a++, s = pcap; c = *a++, s = pcap;
if(!spacer(*a)) if(!spacer(c))
while((c = *a) && !spacer(c)) a++; while((c = *a) && !spacer(c)) a++;
continue; continue;
} }
if(c != *s) return 0; if(c != *s++) return 0;
a++, s++;
} }
/* phase: write rule */ /* phase: write rule */
if(!(c = *s) || spacer(c)) { if(!(c = *s) || spacer(c)) {
@ -174,9 +173,9 @@ compile_rule(Rule *r, int id, char *src)
while(src < cap) { while(src < cap) {
c = *src, *dict_++ = *src++; c = *src, *dict_++ = *src++;
if(c == '?') { if(c == '?') {
reg = find_register(r, *src); c = *src, reg = find_register(r, c);
if(reg == -1 && *src != '(') if(reg == -1 && c != '(')
r->reg[r->ptr] = *src, reg = r->ptr++; r->reg[r->ptr] = c, reg = r->ptr++;
*dict_++ = '0' + reg; *dict_++ = '0' + reg;
} }
} }
@ -189,9 +188,8 @@ compile_rule(Rule *r, int id, char *src)
while(src < cap) { while(src < cap) {
c = *src, *dict_++ = *src++; c = *src, *dict_++ = *src++;
if(c == '?') { if(c == '?') {
reg = find_register(r, *src); c = *src++, reg = find_register(r, c);
*dict_++ = reg != -1 ? '0' + reg : *src; *dict_++ = reg != -1 ? '0' + reg : c;
src++;
} }
} }
src += wrapped, *dict_++ = 0; src += wrapped, *dict_++ = 0;