Only check for register value once

This commit is contained in:
Devine Lu Linvega 2024-04-21 09:13:30 -07:00
parent a2f60e12ff
commit 73e9ebcfdd
2 changed files with 34 additions and 36 deletions

View File

@ -85,4 +85,3 @@ implode (12 (34 (56 ()))) 123456 test
<> (reverse List (?x ?y) ?z) (reverse List ?y (?x ?z)) <> (reverse List (?x ?y) ?z) (reverse List ?y (?x ?z))
reverse (modal) = ladom test reverse (modal) = ladom test

View File

@ -46,51 +46,50 @@ set_reg(int r, char *b)
} }
static void static void
put_reg(char r) put_reg(char r, char *reg)
{ {
char c, *s = regs[(int)r], *ss = walk(s); char c, *cap = walk(reg);
if(r == '*') { if(r == '*') {
int i, depth = 0; int i, depth = 0;
if(*s == '(') { /* special explode tuple */ if(*reg == '(') { /* special explode tuple */
s++; reg++;
while(s < ss) { while(reg < cap) {
while((c = *s) && !spacer(c)) while((c = *reg) && !spacer(c))
*dst_++ = c, s++; *dst_++ = c, reg++;
*dst_++ = ' '; *dst_++ = ' ';
*dst_++ = '(', s++, depth++; *dst_++ = '(', reg++, depth++;
} }
} else { /* special explode token */ } else { /* special explode token */
while((c = *s++) && !spacer(c)) while((c = *reg++) && !spacer(c))
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++; *dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
} }
for(i = 0; i < depth; i++) for(i = 0; i < depth; i++)
*dst_++ = ')'; *dst_++ = ')';
} else if(r == '.') { /* special unpack */ } else if(r == '.') { /* special unpack */
if(*s == '(') s++, --ss; if(*reg == '(') reg++, --cap;
while(s < ss) *dst_++ = *s++; while(reg < cap) *dst_++ = *reg++;
} else if(r == '^') { /* special join */ } else if(r == '^') { /* special join */
if(*s == '(') s++, --ss; if(*reg == '(') reg++, --cap;
while(s < ss && (c = *s++)) while(reg < cap && (c = *reg++))
if(!spacer(c)) *dst_++ = c; if(!spacer(c)) *dst_++ = c;
} else if(r == '~') { /* special stdin */ } else if(r == '~') { /* special stdin */
while(fread(&c, 1, 1, stdin) && c >= ' ') while(fread(&c, 1, 1, stdin) && c >= ' ')
*dst_++ = c; *dst_++ = c;
} else if(r == ':') { /* special stdout */ } else if(r == ':') { /* special stdout */
if(*s == '(') s++, --ss; if(*reg == '(') reg++, --cap;
while(s < ss) { while(reg < cap) {
c = *s++; c = *reg++;
if(c == '\\') { if(c == '\\') {
switch(*s++) { switch(*reg++) {
case 't': putc(0x09, stdout); break; case 't': putc(0x09, stdout); break;
case 'n': putc(0x0a, stdout); break; case 'n': putc(0x0a, stdout); break;
case 's': putc(0x20, stdout); break; case 's': putc(0x20, stdout); break;
case '?': putc(0x3f, stdout); break;
} }
} else } else
putc(c, stdout); putc(c, stdout);
} }
} else } else
while(s < ss) *dst_++ = *s++; while(reg < cap) *dst_++ = *reg++;
} }
static char * static char *
@ -136,34 +135,34 @@ commit_rule(Rule *r, char *s, int create)
} }
static int static int
write_rule(Rule *r, char *res) write_rule(Rule *r, char *s)
{ {
char c, last = 0, *b = r->b, *origin = dst_; char c, *b = r->b, *reg, *origin = dst_;
while((c = *b++)) while((c = *b++))
if(c == '?' && last != '\\' && regs[(int)*b]) if(c == '?' && (reg = regs[(int)*b]))
put_reg(*b++); put_reg(*b++, reg);
else else
*dst_++ = c, last = c; *dst_++ = c;
if(dst_ == origin) { if(dst_ == origin) {
while(*res == ' ') res++; while(*s == ' ') s++;
if(*res == ')' && *(dst_ - 1) == ' ') dst_--; if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
} }
return commit_rule(r, res, 0); return commit_rule(r, s, 0);
} }
static char * static char *
parse_frag(char *s) parse_frag(char *s)
{ {
char c, *ss; char c, *cap;
while((c = *s) && c <= ' ') s++; while((c = *s) && c == ' ') s++;
if(c != ')' && !(c == '<' && s[1] == '>')) { if(c != ')' && !(c == '<' && s[1] == '>')) {
ss = walk(s); cap = walk(s);
if(c == '(') { if(c == '(') {
s++; s++;
while(s < ss - 1) *dict_++ = *s++; while(s < cap - 1) *dict_++ = *s++;
s++; s++;
} else } else
while(s < ss) *dict_++ = *s++; while(s < cap) *dict_++ = *s++;
} }
*dict_++ = 0; *dict_++ = 0;
return s; return s;
@ -182,20 +181,20 @@ static int
rewrite(void) rewrite(void)
{ {
char c, last = 0, *cap, *s = src_, *res; char c, last = 0, *cap, *s = src_, *res;
while((c = *s) && c <= ' ') s++; while(*s == ' ') s++;
while((c = *s)) { while((c = *s)) {
if(spacer(last)) { if(spacer(last)) {
Rule *r; Rule *r;
if(c == '<' && s[1] == '>') { if(c == '<' && s[1] == '>') {
r = rules_++; r = rules_++;
s = create_rule(r, rules_ - rules - 1, s); s = create_rule(r, rules_ - rules - 1, s);
while((c = *s) && c <= ' ') s++; while(*s == ' ') s++;
return commit_rule(r, s, 1); return commit_rule(r, s, 1);
} }
if(c == '?' && s[1] == '(') { if(c == '?' && s[1] == '(') {
r = &lambda, cap = walk(s + 1); r = &lambda, cap = walk(s + 1);
create_rule(&lambda, -1, s), s = cap; create_rule(&lambda, -1, s), s = cap;
while((c = *s) && c <= ' ') s++; while(*s == ' ') s++;
if((res = match_rule(&lambda, s)) != NULL) if((res = match_rule(&lambda, s)) != NULL)
return write_rule(&lambda, res); return write_rule(&lambda, res);
} }