Use switch case for special registers

This commit is contained in:
Devine Lu Linvega 2024-04-22 20:22:54 -07:00
parent f7e0e74888
commit b67cf12f60
1 changed files with 35 additions and 29 deletions

View File

@ -34,33 +34,8 @@ static void
write_reg(char r, char *reg)
{
char c, *cap = walk(reg);
if(r == '*') {
int i, depth = 0;
if(*reg == '(') { /* special explode tuple */
reg++;
while(reg < cap) {
while((c = *reg) && !spacer(c))
*dst_++ = c, reg++;
*dst_++ = ' ';
*dst_++ = '(', reg++, depth++;
}
} else { /* special explode token */
while((c = *reg++) && !spacer(c))
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
}
for(i = 0; i < depth; i++)
*dst_++ = ')';
} else if(r == '.') { /* special unpack */
if(*reg == '(') reg++, --cap;
while(reg < cap) *dst_++ = *reg++;
} else if(r == '^') { /* special join */
if(*reg == '(') reg++, --cap;
while(reg < cap && (c = *reg++))
if(!spacer(c)) *dst_++ = c;
} else if(r == '~') { /* special stdin */
while(fread(&c, 1, 1, stdin) && c >= ' ')
*dst_++ = c;
} else if(r == ':') { /* special stdout */
switch(r) {
case ':': /* op: output */
if(*reg == '(') reg++, --cap;
while(reg < cap) {
c = *reg++;
@ -73,8 +48,40 @@ write_reg(char r, char *reg)
} else
putc(c, stdout);
}
} else
return;
case '~': /* op: input */
while(fread(&c, 1, 1, stdin) && c >= ' ')
*dst_++ = c;
return;
case '^': /* op: join */
if(*reg == '(') reg++, --cap;
while(reg < cap && (c = *reg++))
if(!spacer(c)) *dst_++ = c;
return;
case '.': /* op: unwrap */
if(*reg == '(') reg++, --cap;
while(reg < cap) *dst_++ = *reg++;
return;
case '*': { /* op: explode */
int i, depth = 0;
if(*reg == '(') { /* tuple */
reg++;
while(reg < cap) {
while((c = *reg) && !spacer(c))
*dst_++ = c, reg++;
*dst_++ = ' ';
*dst_++ = '(', reg++, depth++;
}
} else { /* token */
while((c = *reg++) && !spacer(c))
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
}
for(i = 0; i < depth; i++) *dst_++ = ')';
return;
}
default:
while(reg < cap) *dst_++ = *reg++;
}
}
static int
@ -185,7 +192,6 @@ rewrite(void)
while(*s == ' ') s++;
return run_rule(&lambda, s);
}
for(r = rules; r < rules_; r++)
if(run_rule(r, s)) return 1;
}