From b67cf12f6019524d165da88f4f007238c273111d Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Mon, 22 Apr 2024 20:22:54 -0700 Subject: [PATCH] Use switch case for special registers --- src/modal.c | 64 +++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/modal.c b/src/modal.c index 83499a2..ef791ce 100644 --- a/src/modal.c +++ b/src/modal.c @@ -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; }