329 lines
7.6 KiB
C
329 lines
7.6 KiB
C
#include <stdio.h>
|
|
|
|
typedef struct {
|
|
unsigned int id, refs;
|
|
char *a, *b;
|
|
} Rule;
|
|
|
|
static unsigned char rmin = 0xff, rmax = 0x00;
|
|
static int flip, quiet, debug, cycles = 0x10000;
|
|
static Rule rules[0x1000], *rules_ = rules, lambda;
|
|
static char dict[0x8000], *dict_ = dict, empty;
|
|
static char bank_a[0x4000], *src_ = bank_a;
|
|
static char bank_b[0x4000], *dst_ = bank_b;
|
|
static char *regs[0x100];
|
|
|
|
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
|
|
|
|
static char *
|
|
walk(char *s)
|
|
{
|
|
char c;
|
|
int depth = 0;
|
|
if(*s == '(') {
|
|
while((c = *s++)) {
|
|
if(c == '(') depth++;
|
|
if(c == ')') --depth;
|
|
if(!depth) return s;
|
|
}
|
|
}
|
|
while((c = *s) && !spacer(c)) s++;
|
|
return s;
|
|
}
|
|
|
|
static int
|
|
sint(char *s)
|
|
{
|
|
int r = 0;
|
|
char c;
|
|
while((c = *s++) && c > 0x20) r = r * 10 + c - '0';
|
|
return r;
|
|
}
|
|
|
|
static void
|
|
device_write(char *s)
|
|
{
|
|
char c = *s, *cap = walk(s), **reg = regs + '0';
|
|
/* phase: ALU */
|
|
if(*reg) {
|
|
int acc = sint(*reg++);
|
|
/* clang-format off */
|
|
switch(c) {
|
|
case '+': for(;*reg != 0; reg++) acc += sint(*reg); break;
|
|
case '-': for(;*reg != 0; reg++) acc -= sint(*reg); break;
|
|
case '*': for(;*reg != 0; reg++) acc *= sint(*reg); break;
|
|
case '/': for(;*reg != 0; reg++) acc /= sint(*reg); break;
|
|
case '%': for(;*reg != 0; reg++) acc %= sint(*reg); break;
|
|
case '&': for(;*reg != 0; reg++) acc &= sint(*reg); break;
|
|
case '^': for(;*reg != 0; reg++) acc ^= sint(*reg); break;
|
|
case '|': for(;*reg != 0; reg++) acc |= sint(*reg); break;
|
|
case '=': for(;*reg != 0; reg++) acc = acc == sint(*reg); break;
|
|
case '!': for(;*reg != 0; reg++) acc = acc != sint(*reg); break;
|
|
case '>': for(;*reg != 0; reg++) acc = acc > sint(*reg); break;
|
|
case '<': for(;*reg != 0; reg++) acc = acc < sint(*reg); break;
|
|
}
|
|
/* 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);
|
|
}
|
|
}
|
|
|
|
static void
|
|
device_read(void)
|
|
{
|
|
char c, *origin = dst_;
|
|
while(fread(&c, 1, 1, stdin) && c >= ' ')
|
|
*dst_++ = c;
|
|
if(feof(stdin))
|
|
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
|
|
if(dst_ - origin == 0)
|
|
dst_--;
|
|
}
|
|
|
|
static void
|
|
write_reg(char r, char *reg)
|
|
{
|
|
char c, *cap = walk(reg);
|
|
switch(r) {
|
|
case '>': /* op: output */
|
|
case ':': device_write(reg); return;
|
|
case '<': /* op: input */
|
|
case '~': device_read(); 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 == '(' && reg[1] != ')') { /* 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++;
|
|
return;
|
|
}
|
|
}
|
|
|
|
static int
|
|
write_tail(char *s, Rule *r)
|
|
{
|
|
while((*dst_++ = *s++))
|
|
;
|
|
*dst_ = 0;
|
|
if((flip = !flip))
|
|
src_ = bank_b, dst_ = bank_a;
|
|
else
|
|
src_ = bank_a, dst_ = bank_b;
|
|
if(!quiet && r) fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
apply_rule(Rule *r, char *s)
|
|
{
|
|
unsigned char i, rid;
|
|
char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
|
|
/* phase: clean regs */
|
|
if(rmax) {
|
|
for(i = 0; i <= rmax; i++)
|
|
regs[i] = 0;
|
|
rmin = 0xff, rmax = 0x00;
|
|
}
|
|
/* phase: match rule */
|
|
while((c = *a++)) {
|
|
if(c == '?') {
|
|
char *pcap = walk(s);
|
|
rid = *a++;
|
|
if((reg = regs[rid])) { /* reg cmp */
|
|
char *rcap = walk(reg), *pp = s;
|
|
while(reg < rcap || pp < pcap)
|
|
if(*reg++ != *pp++) return 0;
|
|
} else { /* reg set */
|
|
regs[rid] = s;
|
|
if(rid < rmin) rmin = rid;
|
|
if(rid > rmax) rmax = rid;
|
|
}
|
|
s = pcap;
|
|
} else if(c != *s++)
|
|
return 0;
|
|
}
|
|
c = *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
|
|
*dst_++ = c;
|
|
} else
|
|
*dst_++ = c;
|
|
}
|
|
if(dst_ == origin) {
|
|
while(*s == ' ') s++;
|
|
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
|
|
}
|
|
return write_tail(s, r);
|
|
}
|
|
|
|
static char *
|
|
parse_frag(char **side, char *src)
|
|
{
|
|
int wrapped;
|
|
char c, *cap;
|
|
while((c = *src) && c == ' ') src++;
|
|
if(c == ')' || (c == '<' && src[1] == '>')) {
|
|
*side = ∅
|
|
return src;
|
|
}
|
|
*side = dict_, cap = walk(src), wrapped = c == '(';
|
|
if(wrapped) src++, cap--;
|
|
while(src < cap) c = *src, *dict_++ = *src++;
|
|
src += wrapped, *dict_++ = 0;
|
|
return src;
|
|
}
|
|
|
|
static Rule *
|
|
find_rule(char *s, char *cap)
|
|
{
|
|
Rule *r = rules;
|
|
if(*s == '(') s++, cap--;
|
|
while(r < rules_) {
|
|
char *ss = s, *a = r->a;
|
|
if(a)
|
|
while(*ss++ == *a++)
|
|
if(!*a && ss == cap) return r;
|
|
r++;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static int
|
|
rewrite(void)
|
|
{
|
|
char c, last = 0, *cap, *s = src_;
|
|
while(*s == ' ') s++;
|
|
while((c = *s)) {
|
|
if(c == '(' || spacer(last)) {
|
|
Rule *r = NULL;
|
|
/* phase: undefine */
|
|
if(c == '>' && s[1] == '<') {
|
|
s += 2;
|
|
while(*s == ' ') s++;
|
|
cap = walk(s), r = find_rule(s, cap);
|
|
if(r != NULL) {
|
|
if(!quiet) fprintf(stderr, ">< (%s) (%s)\n", r->a, r->b);
|
|
r->a = 0;
|
|
}
|
|
while(*cap == ' ') cap++;
|
|
return write_tail(cap, NULL);
|
|
}
|
|
/* phase: define */
|
|
if(c == '<' && s[1] == '>') {
|
|
r = rules_, r->id = rules_ - rules - 1;
|
|
s = parse_frag(&r->b, parse_frag(&r->a, s + 2));
|
|
if(*r->a) {
|
|
if(!quiet) fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
|
|
rules_++;
|
|
}
|
|
while(*s == ' ') s++;
|
|
return write_tail(s, NULL);
|
|
}
|
|
/* phase: lambda */
|
|
if(c == '?' && s[1] == '(') {
|
|
cap = walk(s + 1);
|
|
r = &lambda, r->id = -1;
|
|
parse_frag(&r->b, parse_frag(&r->a, s + 2));
|
|
s = cap;
|
|
while(*s == ' ') s++;
|
|
if(!apply_rule(&lambda, s)) write_tail(s, r);
|
|
return 1;
|
|
}
|
|
/* phase: match */
|
|
for(r = rules; r < rules_; r++)
|
|
if(r->a && apply_rule(r, s)) return 1;
|
|
}
|
|
*dst_++ = last = c;
|
|
s++;
|
|
}
|
|
*dst_++ = 0;
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
FILE *f;
|
|
int i, pl = 0, pr = 0, rw = 0;
|
|
char c, last = 0, *w = bank_a;
|
|
if(argc < 2)
|
|
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, 28 Apr 2024.\n");
|
|
case 'q': /* quiet */ quiet = 1; break;
|
|
case 'p': /* debug */ debug = 1; break;
|
|
case 'n': /* infinite */ cycles = 0xffffffff; break;
|
|
}
|
|
}
|
|
if(!(f = fopen(argv[i], "r")))
|
|
return !printf("Modal file invalid: %s.\n", argv[i]);
|
|
while(fread(&c, 1, 1, f)) {
|
|
c = c <= 0x20 ? 0x20 : c;
|
|
if(c == ' ' && last == '(') continue;
|
|
if(c == ')' && last == ' ') w--;
|
|
if(c == ' ' && last == ' ') w--;
|
|
if(c == '(') pl++;
|
|
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)
|
|
return !fprintf(stderr, "Modal program imbalanced.\n");
|
|
while(rewrite() && ++rw)
|
|
if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n");
|
|
if(!quiet) {
|
|
while(rules_-- > rules) {
|
|
if(!rules_->refs && rules_->a)
|
|
fprintf(stderr, "-- Unused rule: %d <> (%s) (%s)\n", rules_->id, rules_->a, rules_->b);
|
|
if(debug && rules_->a)
|
|
fprintf(stderr, " (%s) (%s), %d times.\n", rules_->a, rules_->b, rules_->refs);
|
|
}
|
|
fprintf(stderr, ".. %s\nCompleted in %d rewrites.\n", src_, rw);
|
|
}
|
|
return 0;
|
|
}
|