2024-04-04 12:04:27 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-04-04 12:29:36 -04:00
|
|
|
typedef struct {
|
2024-04-08 14:46:24 -04:00
|
|
|
int id;
|
2024-04-04 13:01:09 -04:00
|
|
|
char *a, *b;
|
2024-04-04 12:29:36 -04:00
|
|
|
} Rule;
|
|
|
|
|
2024-04-16 16:51:34 -04:00
|
|
|
static int flip, rmin = 0xff, rmax = 0x00, cycles = 0x10000;
|
2024-04-11 11:53:49 -04:00
|
|
|
static Rule rules[0x1000], lambda, *rules_ = rules;
|
2024-04-04 14:54:31 -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-04-04 13:59:00 -04:00
|
|
|
static char *regs[0x100];
|
|
|
|
|
2024-04-11 12:26:55 -04:00
|
|
|
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
|
2024-04-04 19:52:09 -04:00
|
|
|
|
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-06 11:17:58 -04:00
|
|
|
static int
|
2024-04-08 18:14:05 -04:00
|
|
|
set_reg(int r, char *b)
|
2024-04-08 13:23:28 -04:00
|
|
|
{
|
|
|
|
if(regs[r]) {
|
|
|
|
char *a = regs[r], *aa = walk(a), *bb = walk(b);
|
2024-04-19 17:08:18 -04:00
|
|
|
while(a < aa || b < bb)
|
2024-04-08 13:23:28 -04:00
|
|
|
if(*a++ != *b++) return 0;
|
2024-04-16 16:51:34 -04:00
|
|
|
} else {
|
2024-04-08 13:23:28 -04:00
|
|
|
regs[r] = b;
|
2024-04-16 16:51:34 -04:00
|
|
|
if(r < rmin) rmin = r;
|
|
|
|
if(r > rmax) rmax = r;
|
|
|
|
}
|
2024-04-08 13:23:28 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-04-11 11:21:57 -04:00
|
|
|
static void
|
2024-04-08 18:14:05 -04:00
|
|
|
put_reg(char r)
|
2024-04-04 15:20:06 -04:00
|
|
|
{
|
2024-04-20 14:09:43 -04:00
|
|
|
char c, *s = regs[(int)r], *ss = walk(s);
|
2024-04-14 20:33:42 -04:00
|
|
|
if(r == '*') {
|
2024-04-17 13:35:40 -04:00
|
|
|
int i, depth = 0;
|
|
|
|
if(*s == '(') { /* special explode tuple */
|
|
|
|
s++;
|
|
|
|
while(s < ss) {
|
|
|
|
while((c = *s) && !spacer(c))
|
|
|
|
*dst_++ = c, s++;
|
|
|
|
*dst_++ = ' ';
|
|
|
|
*dst_++ = '(', s++, depth++;
|
|
|
|
}
|
|
|
|
} else { /* special explode token */
|
2024-04-14 20:33:42 -04:00
|
|
|
while((c = *s++) && !spacer(c))
|
2024-04-14 22:36:16 -04:00
|
|
|
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
|
2024-04-17 12:17:31 -04:00
|
|
|
}
|
|
|
|
for(i = 0; i < depth; i++)
|
|
|
|
*dst_++ = ')';
|
2024-04-17 13:35:40 -04:00
|
|
|
} else if(r == '.') { /* special unpack */
|
|
|
|
if(*s == '(') s++, --ss;
|
|
|
|
while(s < ss) *dst_++ = *s++;
|
|
|
|
} else if(r == '^') { /* special join */
|
|
|
|
if(*s == '(') s++, --ss;
|
|
|
|
while(s < ss && (c = *s++))
|
|
|
|
if(!spacer(c)) *dst_++ = c;
|
|
|
|
} else if(r == '~') { /* special stdin */
|
|
|
|
while(fread(&c, 1, 1, stdin) && c >= ' ')
|
|
|
|
*dst_++ = c;
|
2024-04-14 20:33:42 -04:00
|
|
|
} else if(r == ':') { /* special stdout */
|
|
|
|
if(*s == '(') s++, --ss;
|
|
|
|
while(s < ss) {
|
|
|
|
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-08 12:28:36 -04:00
|
|
|
} else
|
2024-04-14 22:36:16 -04:00
|
|
|
while(s < ss) *dst_++ = *s++;
|
2024-04-04 15:20:06 -04:00
|
|
|
}
|
|
|
|
|
2024-04-08 13:23:28 -04:00
|
|
|
static char *
|
2024-04-10 16:08:50 -04:00
|
|
|
match_rule(Rule *r, char *p)
|
2024-04-08 13:23:28 -04:00
|
|
|
{
|
|
|
|
int i;
|
2024-04-12 12:45:53 -04:00
|
|
|
char c, last = 0, *a = r->a, *b = p;
|
2024-04-16 16:51:34 -04:00
|
|
|
if(rmax) {
|
2024-04-16 17:45:56 -04:00
|
|
|
for(i = rmin; i <= rmax; i++)
|
2024-04-16 16:51:34 -04:00
|
|
|
regs[i] = 0;
|
|
|
|
rmin = 0xff, rmax = 0x00;
|
|
|
|
}
|
2024-04-08 13:23:28 -04:00
|
|
|
while((c = *a)) {
|
2024-04-12 12:45:53 -04:00
|
|
|
if(spacer(last) && c == '?') {
|
2024-04-08 18:14:05 -04:00
|
|
|
if(!set_reg(*(++a), b)) return NULL;
|
2024-04-08 13:23:28 -04:00
|
|
|
a++, b = walk(b);
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-16 15:24:57 -04:00
|
|
|
if(c != *b) return NULL;
|
2024-04-12 12:45:53 -04:00
|
|
|
a++, b++, last = c;
|
2024-04-08 13:23:28 -04:00
|
|
|
}
|
|
|
|
c = *b;
|
|
|
|
return spacer(c) ? b : NULL;
|
|
|
|
}
|
|
|
|
|
2024-04-07 17:55:55 -04:00
|
|
|
static int
|
2024-04-09 11:56:25 -04:00
|
|
|
commit_rule(Rule *r, char *s, int create)
|
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-04-14 22:36:16 -04:00
|
|
|
*dst_++ = 0;
|
|
|
|
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-09 11:56:25 -04:00
|
|
|
if(create)
|
|
|
|
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
|
|
|
|
else
|
2024-04-14 22:36:16 -04:00
|
|
|
fprintf(stderr, "%02d %s\n", r->id, src_);
|
2024-04-07 17:55:55 -04:00
|
|
|
return 1;
|
2024-04-04 17:55:51 -04:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:08:07 -04:00
|
|
|
static int
|
|
|
|
write_rule(Rule *r, char last, char *res)
|
|
|
|
{
|
2024-04-14 22:36:16 -04:00
|
|
|
char c, *b = r->b, *origin = dst_;
|
2024-04-12 12:56:07 -04:00
|
|
|
while((c = *b++))
|
2024-04-20 14:09:43 -04:00
|
|
|
if(spacer(last) && c == '?' && regs[(int)*b])
|
2024-04-11 12:08:07 -04:00
|
|
|
put_reg(*b++);
|
|
|
|
else
|
2024-04-14 22:36:16 -04:00
|
|
|
*dst_++ = c, last = c;
|
2024-04-17 14:18:37 -04:00
|
|
|
if(dst_ == origin) {
|
2024-04-14 19:58:26 -04:00
|
|
|
while(*res == ' ') res++;
|
2024-04-17 14:40:47 -04:00
|
|
|
if(*res == ')' && *(dst_ - 1) == ' ') dst_--;
|
2024-04-17 14:18:37 -04:00
|
|
|
}
|
2024-04-11 12:08:07 -04:00
|
|
|
return commit_rule(r, res, 0);
|
|
|
|
}
|
|
|
|
|
2024-04-06 11:17:58 -04:00
|
|
|
static char *
|
2024-04-10 14:39:15 -04:00
|
|
|
parse_frag(char *s)
|
2024-04-06 11:17:58 -04:00
|
|
|
{
|
2024-04-10 14:39:15 -04:00
|
|
|
char c, *ss;
|
|
|
|
while((c = *s) && c <= ' ') s++;
|
2024-04-20 14:01:20 -04:00
|
|
|
if(c != ')' && !(c == '<' && s[1] == '>')) {
|
2024-04-12 13:30:59 -04:00
|
|
|
ss = walk(s);
|
2024-04-20 14:01:20 -04:00
|
|
|
if(c == '(') {
|
2024-04-15 18:58:38 -04:00
|
|
|
s++;
|
2024-04-15 19:26:25 -04:00
|
|
|
while(s < ss - 1) *dict_++ = *s++;
|
2024-04-15 18:58:38 -04:00
|
|
|
s++;
|
2024-04-15 19:26:25 -04:00
|
|
|
} else
|
2024-04-15 18:58:38 -04:00
|
|
|
while(s < ss) *dict_++ = *s++;
|
2024-04-12 13:30:59 -04:00
|
|
|
}
|
2024-04-06 11:17:58 -04:00
|
|
|
*dict_++ = 0;
|
2024-04-10 14:13:18 -04:00
|
|
|
return s;
|
2024-04-08 13:23:28 -04:00
|
|
|
}
|
|
|
|
|
2024-04-11 11:53:49 -04:00
|
|
|
static char *
|
|
|
|
create_rule(Rule *r, int id, char *s)
|
|
|
|
{
|
|
|
|
r->id = id, s += 2;
|
2024-04-12 13:30:59 -04:00
|
|
|
r->a = dict_, s = parse_frag(s);
|
|
|
|
r->b = dict_, s = parse_frag(s);
|
2024-04-11 11:53:49 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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-14 22:36:16 -04:00
|
|
|
char c, last = 0, *cap, *s = src_, *res;
|
2024-04-11 12:40:04 -04:00
|
|
|
while((c = *s) && c <= ' ') s++;
|
|
|
|
while((c = *s)) {
|
2024-04-08 13:32:06 -04:00
|
|
|
if(spacer(last)) {
|
2024-04-10 16:08:50 -04:00
|
|
|
Rule *r;
|
2024-04-20 14:01:20 -04:00
|
|
|
if(c == '<' && s[1] == '>') {
|
2024-04-10 15:19:44 -04:00
|
|
|
r = rules_++;
|
2024-04-11 12:40:04 -04:00
|
|
|
s = create_rule(r, rules_ - rules - 1, s);
|
2024-04-12 13:07:42 -04:00
|
|
|
while((c = *s) && c <= ' ') s++;
|
2024-04-11 12:40:04 -04:00
|
|
|
return commit_rule(r, s, 1);
|
2024-04-10 15:19:44 -04:00
|
|
|
}
|
2024-04-20 14:01:20 -04:00
|
|
|
if(c == '?' && s[1] == '(') {
|
2024-04-11 16:37:23 -04:00
|
|
|
r = &lambda, cap = walk(s + 1);
|
|
|
|
create_rule(&lambda, -1, s), s = cap;
|
2024-04-11 15:41:05 -04:00
|
|
|
while((c = *s) && c <= ' ') s++;
|
2024-04-11 12:40:04 -04:00
|
|
|
if((res = match_rule(&lambda, s)) != NULL)
|
2024-04-11 12:08:07 -04:00
|
|
|
return write_rule(&lambda, last, res);
|
2024-04-04 15:09:47 -04:00
|
|
|
}
|
2024-04-10 16:08:50 -04:00
|
|
|
for(r = rules; r < rules_; r++)
|
2024-04-11 12:40:04 -04:00
|
|
|
if((res = match_rule(r, s)) != NULL)
|
2024-04-11 12:08:07 -04:00
|
|
|
return write_rule(r, last, res);
|
2024-04-04 15:09:47 -04:00
|
|
|
}
|
2024-04-14 22:36:16 -04:00
|
|
|
*dst_++ = last = c;
|
2024-04-11 12:40:04 -04:00
|
|
|
s++;
|
2024-04-04 13:30:31 -04:00
|
|
|
}
|
2024-04-14 22:36:16 -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-04-07 13:58:39 -04:00
|
|
|
FILE *f;
|
2024-04-18 14:33:23 -04:00
|
|
|
int i;
|
2024-04-07 13:56:43 -04:00
|
|
|
char c, *w = bank_a;
|
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-04-20 14:09:43 -04:00
|
|
|
case 'v': /* version */ return !printf("Modal Interpreter, 20 Apr 2024.\n");
|
2024-04-18 14:33:23 -04:00
|
|
|
case 'q': /* quiet */ fclose(stderr); break;
|
|
|
|
case 'n': /* infinite */ cycles = 0xffffffff; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!(f = fopen(argv[i], "r")))
|
|
|
|
return !fprintf(stdout, "Invalid Modal file: %s.\n", argv[i]);
|
2024-04-07 13:56:43 -04:00
|
|
|
while(fread(&c, 1, 1, f)) {
|
2024-04-12 11:54:07 -04:00
|
|
|
c = c <= 0x20 ? 0x20 : c;
|
2024-04-07 16:09:58 -04:00
|
|
|
if(w > bank_a) {
|
2024-04-07 18:32:53 -04:00
|
|
|
if(c == ' ' && *(w - 1) == '(') continue;
|
|
|
|
if(c == ')' && *(w - 1) == ' ') w--;
|
2024-04-07 13:56:43 -04:00
|
|
|
if(c == ' ' && *(w - 1) == ' ') w--;
|
|
|
|
}
|
2024-04-13 22:28:39 -04:00
|
|
|
*w++ = c;
|
2024-04-07 13:56:43 -04:00
|
|
|
}
|
2024-04-08 18:26:53 -04:00
|
|
|
while(*(--w) <= ' ') *w = 0;
|
2024-04-07 13:56:43 -04:00
|
|
|
fclose(f);
|
2024-04-07 19:16:33 -04:00
|
|
|
while(rewrite())
|
2024-04-18 14:33:23 -04:00
|
|
|
if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n");
|
2024-04-04 14:54:31 -04:00
|
|
|
return 0;
|
2024-04-04 12:04:27 -04:00
|
|
|
}
|