diff --git a/src/modal.c b/src/modal.c index f54c9ac..0737597 100644 --- a/src/modal.c +++ b/src/modal.c @@ -31,22 +31,21 @@ walk(char *s) } static int -compare(char *a, char *b) +bind(int id, char *b) { - char *aa = walk(a), *bb = walk(b); - while(a < aa && b < bb) - if(*a++ != *b++) return 0; + if(regs[id]) { + char *a = regs[id], *aa = walk(a), *bb = walk(b); + while(a < aa && b < bb) + 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; } -static void -call(char *s) -{ - char *ss = walk(s); - if(*s == '(') s++, --ss; - while(s < ss) putc(*(s++), stdout); -} - static char * match(char *p, Rule *r) { @@ -57,14 +56,7 @@ match(char *p, Rule *r) regs[i] = 0; while((c = *a)) { if(c == '?') { - int id = (int)*(++a); - if(regs[id]) { - if(!compare(regs[id], b)) - return NULL; - } else if(id == ':') - call(b); - else - regs[id] = b; + if(!bind(*(++a), b)) return NULL; a++, b = walk(b); } if(!*a && spacer(*b)) return b;