Abstracted writing
This commit is contained in:
parent
680a039cf9
commit
002da4bcc2
37
src/modal.c
37
src/modal.c
|
@ -14,6 +14,12 @@ static char *regs[0x100], stack[0x10], *stack_ = stack;
|
|||
|
||||
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
|
||||
|
||||
static void
|
||||
pushchr(char c)
|
||||
{
|
||||
*dst_++ = c;
|
||||
}
|
||||
|
||||
static char *
|
||||
walk(char *s)
|
||||
{
|
||||
|
@ -86,9 +92,9 @@ device_read(void)
|
|||
{
|
||||
char c, *origin = dst_;
|
||||
while(fread(&c, 1, 1, stdin) && c >= ' ')
|
||||
*dst_++ = c;
|
||||
pushchr(c);
|
||||
if(feof(stdin))
|
||||
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
|
||||
pushchr('E'), pushchr('O'), pushchr('F');
|
||||
if(dst_ - origin == 0)
|
||||
dst_--;
|
||||
}
|
||||
|
@ -103,11 +109,11 @@ write_reg(char r, char *reg)
|
|||
case '^': /* op: join */
|
||||
if(*reg == '(') reg++, --cap;
|
||||
while(reg < cap && (c = *reg++))
|
||||
if(!spacer(c)) *dst_++ = c;
|
||||
if(!spacer(c)) pushchr(c);
|
||||
return;
|
||||
case '.': /* op: unwrap */
|
||||
if(*reg == '(') reg++, --cap;
|
||||
while(reg < cap) *dst_++ = *reg++;
|
||||
while(reg < cap) pushchr(*reg++);
|
||||
return;
|
||||
case '*': { /* op: explode */
|
||||
int i, depth = 0;
|
||||
|
@ -115,18 +121,17 @@ write_reg(char r, char *reg)
|
|||
reg++;
|
||||
while(reg < cap) {
|
||||
while((c = *reg) && !spacer(c))
|
||||
*dst_++ = c, reg++;
|
||||
*dst_++ = ' ';
|
||||
*dst_++ = '(', reg++, depth++;
|
||||
pushchr(c), reg++;
|
||||
pushchr(' '), pushchr('('), reg++, depth++;
|
||||
}
|
||||
} else /* token */
|
||||
while((c = *reg++) && !spacer(c))
|
||||
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
|
||||
for(i = 0; i < depth; i++) *dst_++ = ')';
|
||||
pushchr(c), pushchr(' '), pushchr('('), depth++;
|
||||
for(i = 0; i < depth; i++) pushchr(')');
|
||||
return;
|
||||
}
|
||||
default:
|
||||
while(reg < cap) *dst_++ = *reg++;
|
||||
while(reg < cap) pushchr(*reg++);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -136,7 +141,7 @@ write_tail(char *s)
|
|||
{
|
||||
while((*dst_++ = *s++))
|
||||
;
|
||||
*dst_ = 0;
|
||||
pushchr(0);
|
||||
if((flip = !flip))
|
||||
src_ = bank_b, dst_ = bank_a;
|
||||
else
|
||||
|
@ -175,9 +180,9 @@ apply_rule(Rule *r, char *s)
|
|||
if((reg = regs[rid]))
|
||||
b++, write_reg(rid, reg);
|
||||
else
|
||||
*dst_++ = c;
|
||||
pushchr(c);
|
||||
} else
|
||||
*dst_++ = c;
|
||||
pushchr(c);
|
||||
}
|
||||
if(dst_ == origin) {
|
||||
while(*s == ' ') s++;
|
||||
|
@ -269,10 +274,10 @@ rewrite(void)
|
|||
for(r = rules; r < rules_; r++)
|
||||
if(r->a && apply_rule(r, s)) return 1;
|
||||
}
|
||||
*dst_++ = last = c;
|
||||
pushchr(c), last = c;
|
||||
s++;
|
||||
}
|
||||
*dst_++ = 0;
|
||||
pushchr(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -286,7 +291,7 @@ main(int argc, char **argv)
|
|||
return !printf("usage: modal [-vqn] source.modal\n");
|
||||
for(i = 1; i < argc && *argv[i] == '-'; i++) {
|
||||
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 'p': /* debug */ debug = 1; break;
|
||||
case 'n': /* infinite */ cycles = 0xffffffff; break;
|
||||
|
|
Loading…
Reference in New Issue