2024-04-04 12:04:27 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-04-04 12:29:36 -04:00
|
|
|
typedef struct {
|
2024-04-27 17:47:06 -04:00
|
|
|
unsigned int id, refs;
|
|
|
|
char *a, *b;
|
2024-04-04 12:29:36 -04:00
|
|
|
} Rule;
|
|
|
|
|
2024-05-09 19:17:12 -04:00
|
|
|
static int flip, quiet, debug, access, cycles = 0x200000;
|
2024-05-07 11:20:39 -04:00
|
|
|
static Rule rules[0x1000], *rules_ = rules;
|
2024-05-10 17:05:15 -04:00
|
|
|
static char dict[0x8000], *dict_ = dict;
|
2024-04-14 22:36:16 -04:00
|
|
|
static char bank_a[0x4000], *src_ = bank_a;
|
|
|
|
static char bank_b[0x4000], *dst_ = bank_b;
|
2024-05-04 15:34:38 -04:00
|
|
|
static char *regs[0x100], stack[0x10], *stack_ = stack;
|
2024-04-23 22:36:49 -04:00
|
|
|
|
2024-04-11 12:26:55 -04:00
|
|
|
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
|
2024-04-04 19:52:09 -04:00
|
|
|
|
2024-05-09 19:45:34 -04:00
|
|
|
static char *
|
|
|
|
copy(char *src, char *dst, int length)
|
|
|
|
{
|
|
|
|
while(length--) *dst++ = *src++;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sint(char *s)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int r = 0, n = 1;
|
|
|
|
if(*s == '-') { n = -1, s++; }
|
|
|
|
while((c = *s++) && !spacer(c)) r = r * 10 + c - '0';
|
|
|
|
return r * n;
|
|
|
|
}
|
|
|
|
|
2024-04-04 13:59:00 -04:00
|
|
|
static char *
|
|
|
|
walk(char *s)
|
|
|
|
{
|
|
|
|
char c;
|
2024-04-04 22:40:16 -04:00
|
|
|
int depth = 0;
|
2024-04-12 13:30:59 -04:00
|
|
|
if(*s == '(') {
|
2024-04-04 22:37:03 -04:00
|
|
|
while((c = *s++)) {
|
|
|
|
if(c == '(') depth++;
|
|
|
|
if(c == ')') --depth;
|
|
|
|
if(!depth) return s;
|
2024-04-04 20:26:16 -04:00
|
|
|
}
|
2024-04-04 14:35:58 -04:00
|
|
|
}
|
2024-04-08 18:08:22 -04:00
|
|
|
while((c = *s) && !spacer(c)) s++;
|
2024-04-04 20:15:16 -04:00
|
|
|
return s;
|
2024-04-04 13:59:00 -04:00
|
|
|
}
|
|
|
|
|
2024-04-28 21:55:03 -04:00
|
|
|
static void
|
|
|
|
device_write(char *s)
|
|
|
|
{
|
2024-05-09 18:15:04 -04:00
|
|
|
char **reg = regs + '0';
|
2024-04-28 23:07:41 -04:00
|
|
|
/* phase: ALU */
|
|
|
|
if(*reg) {
|
2024-04-29 00:23:27 -04:00
|
|
|
int acc = sint(*reg++);
|
2024-04-28 23:07:41 -04:00
|
|
|
/* clang-format off */
|
2024-05-09 18:15:04 -04:00
|
|
|
switch(*s) {
|
2024-04-29 12:50:43 -04:00
|
|
|
case '+': while(*reg) acc += sint(*reg++); break;
|
|
|
|
case '-': while(*reg) acc -= sint(*reg++); break;
|
|
|
|
case '*': while(*reg) acc *= sint(*reg++); break;
|
|
|
|
case '/': while(*reg) acc /= sint(*reg++); break;
|
|
|
|
case '%': while(*reg) acc %= sint(*reg++); break;
|
|
|
|
case '&': while(*reg) acc &= sint(*reg++); break;
|
|
|
|
case '^': while(*reg) acc ^= sint(*reg++); break;
|
|
|
|
case '|': while(*reg) acc |= sint(*reg++); break;
|
|
|
|
case '=': while(*reg) acc = acc == sint(*reg++); break;
|
|
|
|
case '!': while(*reg) acc = acc != sint(*reg++); break;
|
|
|
|
case '>': while(*reg) acc = acc > sint(*reg++); break;
|
|
|
|
case '<': while(*reg) acc = acc < sint(*reg++); break;
|
2024-04-28 22:45:46 -04:00
|
|
|
}
|
2024-04-28 23:13:51 -04:00
|
|
|
/* clang-format on */
|
2024-04-28 23:07:41 -04:00
|
|
|
dst_ += snprintf(dst_, 0x10, "%d", acc);
|
2024-04-28 22:45:46 -04:00
|
|
|
return;
|
2024-05-09 18:15:04 -04:00
|
|
|
} 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);
|
|
|
|
}
|
2024-04-28 21:55:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 11:42:37 -04:00
|
|
|
static char *
|
|
|
|
file_import(char *path, char *ptr)
|
2024-05-09 19:17:12 -04:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int pr = 0;
|
|
|
|
if((f = fopen(path, "r"))) {
|
2024-05-10 17:36:58 -04:00
|
|
|
unsigned char c;
|
2024-05-09 19:17:12 -04:00
|
|
|
while(fread(&c, 1, 1, f)) {
|
|
|
|
c = c <= 0x20 ? 0x20 : c;
|
|
|
|
if(c == '(') pr++;
|
|
|
|
if(c == ')') pr--;
|
2024-05-10 11:42:37 -04:00
|
|
|
*ptr++ = c;
|
2024-05-09 19:17:12 -04:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
if(pr) fprintf(stderr, "Modal program imbalanced.\n");
|
2024-05-10 11:42:37 -04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
return copy("NAF", ptr, 3);
|
2024-05-09 19:17:12 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 21:36:45 -04:00
|
|
|
static void
|
2024-04-22 20:54:17 -04:00
|
|
|
write_reg(char r, char *reg)
|
2024-04-04 15:20:06 -04:00
|
|
|
{
|
2024-04-22 23:22:54 -04:00
|
|
|
switch(r) {
|
2024-04-28 23:37:53 -04:00
|
|
|
case ':': device_write(reg); return;
|
2024-05-10 23:24:47 -04:00
|
|
|
case '~': {
|
|
|
|
unsigned char c;
|
|
|
|
while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c;
|
|
|
|
if(feof(stdin)) dst_ = copy("EOF", dst_, 3);
|
|
|
|
return;
|
|
|
|
}
|
2024-05-09 19:17:12 -04:00
|
|
|
case '_': {
|
2024-05-09 19:45:34 -04:00
|
|
|
char filepath[0x80];
|
|
|
|
copy(reg, filepath, walk(reg) - reg);
|
2024-05-10 11:42:37 -04:00
|
|
|
dst_ = file_import(filepath, dst_);
|
2024-05-09 19:17:12 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-05-09 18:15:04 -04:00
|
|
|
case '^': { /* op: join */
|
|
|
|
char c, *cap = walk(reg);
|
2024-04-22 23:22:54 -04:00
|
|
|
if(*reg == '(') reg++, --cap;
|
|
|
|
while(reg < cap && (c = *reg++))
|
2024-05-09 18:15:04 -04:00
|
|
|
if(!spacer(c)) *dst_++ = c;
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
2024-05-09 18:15:04 -04:00
|
|
|
}
|
|
|
|
case '.': { /* op: unwrap */
|
|
|
|
char *cap = walk(reg);
|
2024-04-22 23:22:54 -04:00
|
|
|
if(*reg == '(') reg++, --cap;
|
2024-05-09 19:45:34 -04:00
|
|
|
dst_ = copy(reg, dst_, cap - reg);
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
2024-05-09 18:15:04 -04:00
|
|
|
}
|
2024-04-22 23:22:54 -04:00
|
|
|
case '*': { /* op: explode */
|
|
|
|
int i, depth = 0;
|
2024-05-09 18:15:04 -04:00
|
|
|
char c, *cap = walk(reg);
|
2024-04-23 16:51:25 -04:00
|
|
|
if(*reg == '(' && reg[1] != ')') { /* tuple */
|
2024-04-22 23:22:54 -04:00
|
|
|
reg++;
|
|
|
|
while(reg < cap) {
|
|
|
|
while((c = *reg) && !spacer(c))
|
2024-05-09 18:15:04 -04:00
|
|
|
*dst_++ = c, reg++;
|
|
|
|
*dst_++ = ' ';
|
|
|
|
*dst_++ = '(', reg++, depth++;
|
2024-04-22 23:22:54 -04:00
|
|
|
}
|
2024-04-23 23:20:31 -04:00
|
|
|
} else /* token */
|
2024-04-22 23:22:54 -04:00
|
|
|
while((c = *reg++) && !spacer(c))
|
2024-05-09 18:15:04 -04:00
|
|
|
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
|
|
|
|
for(i = 0; i < depth; i++) *dst_++ = ')';
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
2024-04-22 23:22:54 -04:00
|
|
|
}
|
2024-05-09 18:15:04 -04:00
|
|
|
default: {
|
2024-05-09 19:45:34 -04:00
|
|
|
dst_ = copy(reg, dst_, walk(reg) - reg);
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
2024-04-22 23:22:54 -04:00
|
|
|
}
|
2024-05-09 18:15:04 -04:00
|
|
|
}
|
2024-04-04 15:20:06 -04:00
|
|
|
}
|
|
|
|
|
2024-04-07 17:55:55 -04:00
|
|
|
static int
|
2024-05-02 19:02:33 -04:00
|
|
|
write_tail(char *s)
|
2024-04-04 17:55:51 -04:00
|
|
|
{
|
2024-04-14 22:36:16 -04:00
|
|
|
while((*dst_++ = *s++))
|
2024-04-07 17:55:55 -04:00
|
|
|
;
|
2024-05-09 18:15:04 -04:00
|
|
|
*dst_ = 0;
|
2024-04-14 22:36:16 -04:00
|
|
|
if((flip = !flip))
|
|
|
|
src_ = bank_b, dst_ = bank_a;
|
2024-04-06 11:36:10 -04:00
|
|
|
else
|
2024-04-14 22:36:16 -04:00
|
|
|
src_ = bank_a, dst_ = bank_b;
|
2024-04-07 17:55:55 -04:00
|
|
|
return 1;
|
2024-04-04 17:55:51 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 15:17:31 -04:00
|
|
|
static int
|
2024-04-25 21:39:17 -04:00
|
|
|
apply_rule(Rule *r, char *s)
|
2024-04-22 20:39:39 -04:00
|
|
|
{
|
2024-05-04 15:34:38 -04:00
|
|
|
unsigned char rid;
|
2024-04-27 17:47:06 -04:00
|
|
|
char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
|
2024-04-27 11:46:31 -04:00
|
|
|
/* phase: clean regs */
|
2024-05-04 15:34:38 -04:00
|
|
|
while(stack_ != stack) regs[(int)*(--stack_)] = 0;
|
2024-04-24 14:12:57 -04:00
|
|
|
/* phase: match rule */
|
2024-04-24 23:26:56 -04:00
|
|
|
while((c = *a++)) {
|
2024-04-22 20:43:35 -04:00
|
|
|
if(c == '?') {
|
2024-04-24 14:18:06 -04:00
|
|
|
char *pcap = walk(s);
|
2024-04-27 17:47:06 -04:00
|
|
|
rid = *a++;
|
2024-04-27 12:24:48 -04:00
|
|
|
if((reg = regs[rid])) { /* reg cmp */
|
2024-04-22 23:32:42 -04:00
|
|
|
char *rcap = walk(reg), *pp = s;
|
2024-04-22 20:43:35 -04:00
|
|
|
while(reg < rcap || pp < pcap)
|
2024-04-25 15:17:31 -04:00
|
|
|
if(*reg++ != *pp++) return 0;
|
2024-05-04 15:34:38 -04:00
|
|
|
} else /* reg set */
|
|
|
|
regs[rid] = s, *stack_++ = rid;
|
2024-04-26 14:06:30 -04:00
|
|
|
s = pcap;
|
|
|
|
} else if(c != *s++)
|
|
|
|
return 0;
|
2024-04-22 20:43:35 -04:00
|
|
|
}
|
2024-04-26 01:35:15 -04:00
|
|
|
c = *s;
|
|
|
|
if(!spacer(c)) return 0;
|
2024-04-24 14:12:57 -04:00
|
|
|
/* phase: write rule */
|
2024-05-10 17:05:15 -04:00
|
|
|
if(b != NULL) {
|
|
|
|
while((c = *b++))
|
|
|
|
if(c == '?' && (rid = *b) && (reg = regs[rid]))
|
|
|
|
write_reg(rid, reg), b++;
|
|
|
|
else
|
|
|
|
*dst_++ = c;
|
2024-04-22 20:54:17 -04:00
|
|
|
}
|
2024-04-25 12:48:24 -04:00
|
|
|
if(dst_ == origin) {
|
|
|
|
while(*s == ' ') s++;
|
|
|
|
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
|
|
|
|
}
|
2024-05-02 19:02:33 -04:00
|
|
|
if(!quiet) fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
|
|
|
|
return write_tail(s);
|
2024-04-22 20:54:17 -04:00
|
|
|
}
|
|
|
|
|
2024-04-24 16:41:09 -04:00
|
|
|
static char *
|
2024-05-10 12:31:09 -04:00
|
|
|
parse_frag(char **side, char *s)
|
2024-04-24 13:31:46 -04:00
|
|
|
{
|
2024-04-27 18:06:00 -04:00
|
|
|
char c, *cap;
|
2024-05-10 12:31:09 -04:00
|
|
|
while((c = *s) && c == ' ') s++;
|
2024-05-10 23:17:05 -04:00
|
|
|
if(c == ')') {
|
2024-05-10 17:05:15 -04:00
|
|
|
*side = NULL;
|
2024-05-10 23:17:05 -04:00
|
|
|
return s;
|
|
|
|
} else if((c == '<' && s[1] == '>') || (c == '>' && s[1] == '<') || (c == '(' && s[1] == ')')) {
|
|
|
|
*side = NULL;
|
|
|
|
return s + 2;
|
|
|
|
} else {
|
2024-05-10 12:45:58 -04:00
|
|
|
cap = walk(s), *side = dict_;
|
|
|
|
if(c == '(')
|
|
|
|
dict_ = copy(s + 1, dict_, cap - s - 2);
|
|
|
|
else
|
|
|
|
dict_ = copy(s, dict_, cap - s);
|
|
|
|
s = cap, *dict_++ = 0;
|
2024-04-28 00:12:14 -04:00
|
|
|
}
|
2024-05-10 12:31:09 -04:00
|
|
|
return s;
|
2024-04-24 13:31:46 -04:00
|
|
|
}
|
|
|
|
|
2024-04-26 23:50:22 -04:00
|
|
|
static Rule *
|
2024-04-27 00:18:02 -04:00
|
|
|
find_rule(char *s, char *cap)
|
2024-04-26 23:50:22 -04:00
|
|
|
{
|
|
|
|
Rule *r = rules;
|
2024-04-27 00:18:02 -04:00
|
|
|
if(*s == '(') s++, cap--;
|
2024-04-26 23:50:22 -04:00
|
|
|
while(r < rules_) {
|
|
|
|
char *ss = s, *a = r->a;
|
2024-04-27 14:24:00 -04:00
|
|
|
if(a)
|
|
|
|
while(*ss++ == *a++)
|
|
|
|
if(!*a && ss == cap) return r;
|
2024-04-26 23:50:22 -04:00
|
|
|
r++;
|
|
|
|
}
|
2024-04-28 00:12:14 -04:00
|
|
|
return NULL;
|
2024-04-26 23:50:22 -04:00
|
|
|
}
|
|
|
|
|
2024-04-04 13:07:49 -04:00
|
|
|
static int
|
2024-04-07 19:16:33 -04:00
|
|
|
rewrite(void)
|
2024-04-04 13:07:49 -04:00
|
|
|
{
|
2024-04-25 21:39:17 -04:00
|
|
|
char c, last = 0, *cap, *s = src_;
|
2024-04-21 12:13:30 -04:00
|
|
|
while(*s == ' ') s++;
|
2024-04-11 12:40:04 -04:00
|
|
|
while((c = *s)) {
|
2024-04-26 12:02:03 -04:00
|
|
|
if(c == '(' || spacer(last)) {
|
2024-04-24 13:31:46 -04:00
|
|
|
Rule *r = NULL;
|
2024-04-26 23:50:22 -04:00
|
|
|
/* phase: undefine */
|
|
|
|
if(c == '>' && s[1] == '<') {
|
2024-04-27 00:35:31 -04:00
|
|
|
s += 2;
|
2024-04-21 12:13:30 -04:00
|
|
|
while(*s == ' ') s++;
|
2024-04-27 13:04:01 -04:00
|
|
|
cap = walk(s), r = find_rule(s, cap);
|
2024-04-28 21:55:03 -04:00
|
|
|
if(r != NULL) {
|
2024-05-10 23:17:05 -04:00
|
|
|
if(!quiet) fprintf(stderr, ">< (%s) (%s)\n", r->a ? r->a : "", r->b ? r->b : "");
|
2024-04-28 00:12:14 -04:00
|
|
|
r->a = 0;
|
|
|
|
}
|
2024-04-27 00:18:02 -04:00
|
|
|
while(*cap == ' ') cap++;
|
2024-05-02 19:02:33 -04:00
|
|
|
return write_tail(cap);
|
2024-04-27 11:46:31 -04:00
|
|
|
}
|
2024-04-28 00:12:14 -04:00
|
|
|
/* phase: define */
|
|
|
|
if(c == '<' && s[1] == '>') {
|
2024-04-29 12:54:37 -04:00
|
|
|
r = rules_, r->id = rules_ - rules;
|
2024-04-28 00:12:14 -04:00
|
|
|
s = parse_frag(&r->b, parse_frag(&r->a, s + 2));
|
2024-05-10 17:05:15 -04:00
|
|
|
if(r->a != NULL) {
|
2024-05-10 23:17:05 -04:00
|
|
|
if(!quiet) fprintf(stderr, "<> (%s) (%s)\n", r->a ? r->a : "", r->b ? r->b : "");
|
2024-04-28 00:12:14 -04:00
|
|
|
rules_++;
|
|
|
|
}
|
|
|
|
while(*s == ' ') s++;
|
2024-05-02 19:02:33 -04:00
|
|
|
return write_tail(s);
|
2024-04-28 00:12:14 -04:00
|
|
|
}
|
2024-04-27 11:46:31 -04:00
|
|
|
/* phase: lambda */
|
|
|
|
if(c == '?' && s[1] == '(') {
|
2024-05-07 11:20:39 -04:00
|
|
|
char *d = dict_;
|
2024-04-27 18:06:00 -04:00
|
|
|
cap = walk(s + 1);
|
2024-05-07 11:20:39 -04:00
|
|
|
r = rules_, r->id = -1;
|
2024-04-28 00:12:14 -04:00
|
|
|
parse_frag(&r->b, parse_frag(&r->a, s + 2));
|
2024-04-27 18:06:00 -04:00
|
|
|
s = cap;
|
2024-04-27 11:46:31 -04:00
|
|
|
while(*s == ' ') s++;
|
2024-05-10 17:05:15 -04:00
|
|
|
if(r->a == NULL || !apply_rule(r, s)) write_tail(s);
|
2024-05-07 11:20:39 -04:00
|
|
|
dict_ = d;
|
2024-04-27 11:46:31 -04:00
|
|
|
return 1;
|
2024-04-22 23:13:19 -04:00
|
|
|
}
|
2024-04-24 18:55:48 -04:00
|
|
|
/* phase: match */
|
2024-04-22 23:15:33 -04:00
|
|
|
for(r = rules; r < rules_; r++)
|
2024-05-10 17:05:15 -04:00
|
|
|
if(r->a != NULL && apply_rule(r, s)) return 1;
|
2024-04-04 15:09:47 -04:00
|
|
|
}
|
2024-05-09 18:15:04 -04:00
|
|
|
*dst_++ = last = c, s++;
|
2024-04-04 13:30:31 -04:00
|
|
|
}
|
2024-05-09 18:15:04 -04:00
|
|
|
*dst_++ = 0;
|
2024-04-04 22:55:05 -04:00
|
|
|
return 0;
|
2024-04-04 13:07:49 -04:00
|
|
|
}
|
|
|
|
|
2024-04-07 13:58:39 -04:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2024-04-07 13:56:43 -04:00
|
|
|
{
|
2024-05-09 19:17:12 -04:00
|
|
|
int i, rw = 0;
|
2024-04-07 13:58:39 -04:00
|
|
|
if(argc < 2)
|
2024-04-18 14:33:23 -04:00
|
|
|
return !printf("usage: modal [-vqn] source.modal\n");
|
|
|
|
for(i = 1; i < argc && *argv[i] == '-'; i++) {
|
|
|
|
switch(argv[i][1]) {
|
2024-05-10 17:25:54 -04:00
|
|
|
case 'v': /* version */ return !printf("Modal Interpreter, 10 May 2024.\n");
|
2024-04-22 18:29:54 -04:00
|
|
|
case 'q': /* quiet */ quiet = 1; break;
|
2024-04-28 15:32:46 -04:00
|
|
|
case 'p': /* debug */ debug = 1; break;
|
2024-05-09 19:17:12 -04:00
|
|
|
case 'a': /* access */ access = 1; break;
|
2024-04-18 14:33:23 -04:00
|
|
|
case 'n': /* infinite */ cycles = 0xffffffff; break;
|
|
|
|
}
|
|
|
|
}
|
2024-05-10 11:42:37 -04:00
|
|
|
file_import(argv[i], src_);
|
2024-04-27 11:46:31 -04:00
|
|
|
while(rewrite() && ++rw)
|
2024-04-28 03:15:25 -04:00
|
|
|
if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n");
|
2024-04-27 12:02:56 -04:00
|
|
|
if(!quiet) {
|
2024-05-09 19:45:34 -04:00
|
|
|
while(rules_-- > rules)
|
|
|
|
if(rules_->a && !rules_->refs)
|
|
|
|
fprintf(stderr, "-- Unused rule: %d <> (%s) (%s)\n", rules_->id, rules_->a, rules_->b);
|
2024-04-29 12:46:27 -04:00
|
|
|
if(rw)
|
2024-05-04 17:47:31 -04:00
|
|
|
fprintf(stderr, ".. %s\nCompleted in %d rewrites.\n", src_, rw);
|
2024-04-27 12:02:56 -04:00
|
|
|
}
|
2024-04-04 14:54:31 -04:00
|
|
|
return 0;
|
2024-04-28 03:15:25 -04:00
|
|
|
}
|