Merged all bind helpers

This commit is contained in:
Devine Lu Linvega 2024-04-08 08:59:41 -07:00
parent 36f9bfdaee
commit 04bf0f67d4
1 changed files with 12 additions and 20 deletions

View File

@ -31,22 +31,21 @@ walk(char *s)
} }
static int static int
compare(char *a, char *b) bind(int id, char *b)
{ {
char *aa = walk(a), *bb = walk(b); if(regs[id]) {
char *a = regs[id], *aa = walk(a), *bb = walk(b);
while(a < aa && b < bb) while(a < aa && b < bb)
if(*a++ != *b++) return 0; if(*a++ != *b++) return 0;
} else if(id == ':') {
char *bb = walk(b);
if(*b == '(') b++, --bb;
while(b < bb) putc(*(b++), stdout);
} else
regs[id] = b;
return 1; return 1;
} }
static void
call(char *s)
{
char *ss = walk(s);
if(*s == '(') s++, --ss;
while(s < ss) putc(*(s++), stdout);
}
static char * static char *
match(char *p, Rule *r) match(char *p, Rule *r)
{ {
@ -57,14 +56,7 @@ match(char *p, Rule *r)
regs[i] = 0; regs[i] = 0;
while((c = *a)) { while((c = *a)) {
if(c == '?') { if(c == '?') {
int id = (int)*(++a); if(!bind(*(++a), b)) return NULL;
if(regs[id]) {
if(!compare(regs[id], b))
return NULL;
} else if(id == ':')
call(b);
else
regs[id] = b;
a++, b = walk(b); a++, b = walk(b);
} }
if(!*a && spacer(*b)) return b; if(!*a && spacer(*b)) return b;