Abstracted writing

This commit is contained in:
Devine Lu Linvega 2024-05-09 14:24:21 -07:00
parent 680a039cf9
commit 002da4bcc2
1 changed files with 21 additions and 16 deletions

View File

@ -14,6 +14,12 @@ static char *regs[0x100], stack[0x10], *stack_ = stack;
#define spacer(c) (c <= ' ' || c == '(' || c == ')') #define spacer(c) (c <= ' ' || c == '(' || c == ')')
static void
pushchr(char c)
{
*dst_++ = c;
}
static char * static char *
walk(char *s) walk(char *s)
{ {
@ -86,9 +92,9 @@ device_read(void)
{ {
char c, *origin = dst_; char c, *origin = dst_;
while(fread(&c, 1, 1, stdin) && c >= ' ') while(fread(&c, 1, 1, stdin) && c >= ' ')
*dst_++ = c; pushchr(c);
if(feof(stdin)) if(feof(stdin))
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F'; pushchr('E'), pushchr('O'), pushchr('F');
if(dst_ - origin == 0) if(dst_ - origin == 0)
dst_--; dst_--;
} }
@ -103,11 +109,11 @@ write_reg(char r, char *reg)
case '^': /* op: join */ case '^': /* op: join */
if(*reg == '(') reg++, --cap; if(*reg == '(') reg++, --cap;
while(reg < cap && (c = *reg++)) while(reg < cap && (c = *reg++))
if(!spacer(c)) *dst_++ = c; if(!spacer(c)) pushchr(c);
return; return;
case '.': /* op: unwrap */ case '.': /* op: unwrap */
if(*reg == '(') reg++, --cap; if(*reg == '(') reg++, --cap;
while(reg < cap) *dst_++ = *reg++; while(reg < cap) pushchr(*reg++);
return; return;
case '*': { /* op: explode */ case '*': { /* op: explode */
int i, depth = 0; int i, depth = 0;
@ -115,18 +121,17 @@ write_reg(char r, char *reg)
reg++; reg++;
while(reg < cap) { while(reg < cap) {
while((c = *reg) && !spacer(c)) while((c = *reg) && !spacer(c))
*dst_++ = c, reg++; pushchr(c), reg++;
*dst_++ = ' '; pushchr(' '), pushchr('('), reg++, depth++;
*dst_++ = '(', reg++, depth++;
} }
} else /* token */ } else /* token */
while((c = *reg++) && !spacer(c)) while((c = *reg++) && !spacer(c))
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++; pushchr(c), pushchr(' '), pushchr('('), depth++;
for(i = 0; i < depth; i++) *dst_++ = ')'; for(i = 0; i < depth; i++) pushchr(')');
return; return;
} }
default: default:
while(reg < cap) *dst_++ = *reg++; while(reg < cap) pushchr(*reg++);
return; return;
} }
} }
@ -136,7 +141,7 @@ write_tail(char *s)
{ {
while((*dst_++ = *s++)) while((*dst_++ = *s++))
; ;
*dst_ = 0; pushchr(0);
if((flip = !flip)) if((flip = !flip))
src_ = bank_b, dst_ = bank_a; src_ = bank_b, dst_ = bank_a;
else else
@ -175,9 +180,9 @@ apply_rule(Rule *r, char *s)
if((reg = regs[rid])) if((reg = regs[rid]))
b++, write_reg(rid, reg); b++, write_reg(rid, reg);
else else
*dst_++ = c; pushchr(c);
} else } else
*dst_++ = c; pushchr(c);
} }
if(dst_ == origin) { if(dst_ == origin) {
while(*s == ' ') s++; while(*s == ' ') s++;
@ -269,10 +274,10 @@ rewrite(void)
for(r = rules; r < rules_; r++) for(r = rules; r < rules_; r++)
if(r->a && apply_rule(r, s)) return 1; if(r->a && apply_rule(r, s)) return 1;
} }
*dst_++ = last = c; pushchr(c), last = c;
s++; s++;
} }
*dst_++ = 0; pushchr(0);
return 0; return 0;
} }
@ -286,7 +291,7 @@ main(int argc, char **argv)
return !printf("usage: modal [-vqn] source.modal\n"); return !printf("usage: modal [-vqn] source.modal\n");
for(i = 1; i < argc && *argv[i] == '-'; i++) { for(i = 1; i < argc && *argv[i] == '-'; i++) {
switch(argv[i][1]) { switch(argv[i][1]) {
case 'v': /* version */ return !printf("Modal Interpreter, 7 May 2024.\n"); case 'v': /* version */ return !printf("Modal Interpreter, 9 May 2024.\n");
case 'q': /* quiet */ quiet = 1; break; case 'q': /* quiet */ quiet = 1; break;
case 'p': /* debug */ debug = 1; break; case 'p': /* debug */ debug = 1; break;
case 'n': /* infinite */ cycles = 0xffffffff; break; case 'n': /* infinite */ cycles = 0xffffffff; break;