From 3dbb23319ed01b82c6b805112f5647c4523d7d80 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 24 Apr 2024 14:54:40 -0700 Subject: [PATCH] Break out of compilation early --- src/modal.c | 65 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/modal.c b/src/modal.c index 735e27a..6d962e7 100644 --- a/src/modal.c +++ b/src/modal.c @@ -162,30 +162,36 @@ static char * compile_rule(Rule *r, int id, char *src) { int wrapped, reg; - char c, *cap, *s, *s2; + char c, *cap, *s2; r->id = id, r->ptr = 0; /* left ==================================== */ r->a = dict_; s2 = src; + while((c = *s2) && c == ' ') s2++; - if(c != ')' && !(c == '<' && s2[1] == '>')) { - cap = walk(s2); - wrapped = c == '('; - if(wrapped) s2++, cap--; - while(s2 < cap) { - if(*s2 == '?') { - *dict_++ = *s2++; - reg = find_register(r, *s2); - if(reg < 0 && *s2 != '(') - r->key[r->ptr] = *s2, reg = r->ptr++; - *dict_++ = '0' + reg; - } else - *dict_++ = *s2++; - } - s2 += wrapped; + if(c == ')' || (c == '<' && s2[1] == '>')) { + r->b = dict_; + *dict_++ = 0; + return s2; } + + cap = walk(s2); + wrapped = c == '('; + if(wrapped) s2++, cap--; + while(s2 < cap) { + if(*s2 == '?') { + *dict_++ = *s2++; + reg = find_register(r, *s2); + if(reg < 0 && *s2 != '(') + r->key[r->ptr] = *s2, reg = r->ptr++; + *dict_++ = '0' + reg; + } else + *dict_++ = *s2++; + } + s2 += wrapped; + *dict_++ = 0; src = s2; @@ -193,23 +199,30 @@ compile_rule(Rule *r, int id, char *src) r->b = dict_; s2 = src; + while((c = *s2) && c == ' ') s2++; - if(c != ')' && !(c == '<' && s2[1] == '>')) { - cap = walk(s2); - wrapped = c == '('; - if(wrapped) s2++, cap--; - while(s2 < cap) *dict_++ = *s2++; - s2 += wrapped; + if(c == ')' || (c == '<' && s2[1] == '>')) { + *dict_++ = 0; + return s2; } + + cap = walk(s2); + wrapped = c == '('; + if(wrapped) s2++, cap--; + while(s2 < cap) { + *dict_++ = *s2++; + } + s2 += wrapped; + *dict_++ = 0; src = s2; - s = r->b; - while((c = *s++)) { /* right */ + s2 = r->b; + while((c = *s2++)) { /* right */ if(c == '?') { - reg = find_register(r, *s); + reg = find_register(r, *s2); if(reg >= 0) - *s = '0' + reg; + *s2 = '0' + reg; } } return src;