modal/src/modal.c

241 lines
5.1 KiB
C

#include <stdio.h>
typedef struct {
int id;
char *a, *b;
} Rule;
static int flip, rmin = 0xff, rmax = 0x00, cycles = 0x10000;
static Rule rules[0x1000], lambda, *rules_ = rules;
static char dict[0x8000], *dict_ = dict;
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
set_reg(int r, char *b)
{
if(regs[r]) {
char *a = regs[r], *aa = walk(a), *bb = walk(b);
while(a < aa || b < bb)
if(*a++ != *b++) return 0;
} else {
regs[r] = b;
if(r < rmin) rmin = r;
if(r > rmax) rmax = r;
}
return 1;
}
static void
put_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 */
if(*reg == '(') reg++, --cap;
while(reg < cap) {
c = *reg++;
if(c == '\\') {
switch(*reg++) {
case 't': putc(0x09, stdout); break;
case 'n': putc(0x0a, stdout); break;
case 's': putc(0x20, stdout); break;
}
} else
putc(c, stdout);
}
} else
while(reg < cap) *dst_++ = *reg++;
}
static char *
match_rule(Rule *r, char *p)
{
int i;
char c, *a = r->a, *b = p;
if(rmax) {
for(i = rmin; i <= rmax; i++)
regs[i] = 0;
rmin = 0xff, rmax = 0x00;
}
while((c = *a)) {
if(c == '?' && !spacer(c)) {
if(!set_reg(*(++a), b)) return NULL;
a++, b = walk(b);
if(!spacer(*a))
while((c = *a) && !spacer(c)) a++;
continue;
}
if(c != *b) return NULL;
a++, b++;
}
c = *b;
return spacer(c) ? b : NULL;
}
static int
commit_rule(Rule *r, char *s, int create)
{
while((*dst_++ = *s++))
;
*dst_++ = 0;
if((flip = !flip))
src_ = bank_b, dst_ = bank_a;
else
src_ = bank_a, dst_ = bank_b;
if(create)
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
else
fprintf(stderr, "%02d %s\n", r->id, src_);
return 1;
}
static int
write_rule(Rule *r, char *s)
{
char c, *b = r->b, *reg, *origin = dst_;
while((c = *b++))
if(c == '?' && (reg = regs[(int)*b]))
put_reg(*b++, reg);
else
*dst_++ = c;
if(dst_ == origin) {
while(*s == ' ') s++;
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
}
return commit_rule(r, s, 0);
}
static char *
parse_frag(char *s)
{
char c, *cap;
while((c = *s) && c == ' ') s++;
if(c != ')' && !(c == '<' && s[1] == '>')) {
cap = walk(s);
if(c == '(') {
s++;
while(s < cap - 1) *dict_++ = *s++;
s++;
} else
while(s < cap) *dict_++ = *s++;
}
*dict_++ = 0;
return s;
}
static int
rewrite(void)
{
char c, last = 0, *cap, *s = src_, *res;
while(*s == ' ') s++;
while((c = *s)) {
if(spacer(last)) {
Rule *r;
if(c == '<' && s[1] == '>') {
r = rules_++, r->id = rules_ - rules - 1;
r->a = dict_, s = parse_frag(s + 2);
r->b = dict_, s = parse_frag(s);
while(*s == ' ') s++;
return commit_rule(r, s, 1);
}
if(c == '?' && s[1] == '(') {
cap = walk(s + 1);
r = &lambda, r->id = -1;
r->a = dict_, s = parse_frag(s + 2);
r->b = dict_, parse_frag(s), s = cap;
while(*s == ' ') s++;
if((res = match_rule(&lambda, s)) != NULL)
return write_rule(&lambda, res);
}
for(r = rules; r < rules_; r++)
if((res = match_rule(r, s)) != NULL)
return write_rule(r, res);
}
*dst_++ = last = c;
s++;
}
*dst_++ = 0;
return 0;
}
int
main(int argc, char **argv)
{
FILE *f;
int i, pl = 0, pr = 0;
char c, *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, 21 Apr 2024.\n");
case 'q': /* quiet */ fclose(stderr); break;
case 'n': /* infinite */ cycles = 0xffffffff; break;
}
}
if(!(f = fopen(argv[i], "r")))
return !fprintf(stdout, "Modal file invalid: %s.\n", argv[i]);
while(fread(&c, 1, 1, f)) {
c = c <= 0x20 ? 0x20 : c;
if(w > bank_a) {
if(c == ' ' && *(w - 1) == '(') continue;
if(c == ')' && *(w - 1) == ' ') w--;
if(c == ' ' && *(w - 1) == ' ') w--;
if(c == '(') pl++;
if(c == ')') pr++;
}
*w++ = c;
}
while(*(--w) <= ' ') *w = 0;
fclose(f);
if(pr != pl)
return !fprintf(stdout, "Modal program imbalanced.\n");
while(rewrite())
if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n");
return 0;
}