Housekeeping

This commit is contained in:
Devine Lu Linvega 2024-05-09 15:15:04 -07:00
parent 002da4bcc2
commit defe9ed963
1 changed files with 48 additions and 51 deletions

View File

@ -14,12 +14,6 @@ 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)
{
@ -49,12 +43,12 @@ sint(char *s)
static void
device_write(char *s)
{
char c = *s, *cap = walk(s), **reg = regs + '0';
char **reg = regs + '0';
/* phase: ALU */
if(*reg) {
int acc = sint(*reg++);
/* clang-format off */
switch(c) {
switch(*s) {
case '+': while(*reg) acc += sint(*reg++); break;
case '-': while(*reg) acc -= sint(*reg++); break;
case '*': while(*reg) acc *= sint(*reg++); break;
@ -71,19 +65,21 @@ device_write(char *s)
/* clang-format on */
dst_ += snprintf(dst_, 0x10, "%d", acc);
return;
}
/* phase: string */
if(*s == '(') s++, --cap;
while(s < cap) {
c = *s++;
if(c == '\\') {
switch(*s++) {
case 't': putc(0x09, stdout); break;
case 'n': putc(0x0a, stdout); break;
case 's': putc(0x20, stdout); break;
}
} else
putc(c, stdout);
} else {
/* phase: string */
char *cap = walk(s);
if(*s == '(') s++, --cap;
while(s < cap) {
char c = *s++;
if(c == '\\') {
switch(*s++) {
case 't': putc(0x09, stdout); break;
case 'n': putc(0x0a, stdout); break;
case 's': putc(0x20, stdout); break;
}
} else
putc(c, stdout);
}
}
}
@ -92,9 +88,9 @@ device_read(void)
{
char c, *origin = dst_;
while(fread(&c, 1, 1, stdin) && c >= ' ')
pushchr(c);
*dst_++ = c;
if(feof(stdin))
pushchr('E'), pushchr('O'), pushchr('F');
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
if(dst_ - origin == 0)
dst_--;
}
@ -102,38 +98,45 @@ device_read(void)
static void
write_reg(char r, char *reg)
{
char c, *cap = walk(reg);
switch(r) {
case ':': device_write(reg); return;
case '~': device_read(); return;
case '^': /* op: join */
case '^': { /* op: join */
char c, *cap = walk(reg);
if(*reg == '(') reg++, --cap;
while(reg < cap && (c = *reg++))
if(!spacer(c)) pushchr(c);
if(!spacer(c)) *dst_++ = c;
return;
case '.': /* op: unwrap */
}
case '.': { /* op: unwrap */
char *cap = walk(reg);
if(*reg == '(') reg++, --cap;
while(reg < cap) pushchr(*reg++);
while(reg < cap) *dst_++ = *reg++;
return;
}
case '*': { /* op: explode */
int i, depth = 0;
char c, *cap = walk(reg);
if(*reg == '(' && reg[1] != ')') { /* tuple */
reg++;
while(reg < cap) {
while((c = *reg) && !spacer(c))
pushchr(c), reg++;
pushchr(' '), pushchr('('), reg++, depth++;
*dst_++ = c, reg++;
*dst_++ = ' ';
*dst_++ = '(', reg++, depth++;
}
} else /* token */
while((c = *reg++) && !spacer(c))
pushchr(c), pushchr(' '), pushchr('('), depth++;
for(i = 0; i < depth; i++) pushchr(')');
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
for(i = 0; i < depth; i++) *dst_++ = ')';
return;
}
default:
while(reg < cap) pushchr(*reg++);
default: {
char *cap = walk(reg);
while(reg < cap) *dst_++ = *reg++;
return;
}
}
}
static int
@ -141,7 +144,7 @@ write_tail(char *s)
{
while((*dst_++ = *s++))
;
pushchr(0);
*dst_ = 0;
if((flip = !flip))
src_ = bank_b, dst_ = bank_a;
else
@ -175,14 +178,10 @@ apply_rule(Rule *r, char *s)
if(!spacer(c)) return 0;
/* phase: write rule */
while((c = *b++)) {
if(c == '?') {
rid = *b;
if((reg = regs[rid]))
b++, write_reg(rid, reg);
else
pushchr(c);
} else
pushchr(c);
if(c == '?' && (rid = *b) && (reg = regs[rid]))
write_reg(rid, reg), b++;
else
*dst_++ = c;
}
if(dst_ == origin) {
while(*s == ' ') s++;
@ -274,10 +273,9 @@ rewrite(void)
for(r = rules; r < rules_; r++)
if(r->a && apply_rule(r, s)) return 1;
}
pushchr(c), last = c;
s++;
*dst_++ = last = c, s++;
}
pushchr(0);
*dst_++ = 0;
return 0;
}
@ -285,7 +283,7 @@ int
main(int argc, char **argv)
{
FILE *f;
int i, pl = 0, pr = 0, rw = 0;
int i, pr = 0, rw = 0;
char c, last = 0, *w = bank_a;
if(argc < 2)
return !printf("usage: modal [-vqn] source.modal\n");
@ -304,15 +302,14 @@ main(int argc, char **argv)
if(c == ' ' && last == '(') continue;
if(c == ')' && last == ' ') w--;
if(c == ' ' && last == ' ') w--;
if(c == '(') pl++;
if(c == ')') pr++;
if(c == '(') pr++;
if(c == ')') pr--;
if(c == '(' && last != '?' && !spacer(last)) *w++ = ' ';
if(last == ')' && !spacer(c)) *w++ = ' ';
*w++ = last = c;
}
while(*(--w) <= ' ') *w = 0;
fclose(f);
if(pr != pl)
if(pr)
return !fprintf(stderr, "Modal program imbalanced.\n");
while(rewrite() && ++rw)
if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n");