2024-04-04 12:04:27 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-04-04 12:29:36 -04:00
|
|
|
typedef struct {
|
2024-04-24 13:31:46 -04:00
|
|
|
unsigned int id, refs, ptr;
|
2024-04-24 18:46:27 -04:00
|
|
|
char *a, *b, reg[0x10];
|
2024-04-04 12:29:36 -04:00
|
|
|
} Rule;
|
|
|
|
|
2024-04-23 22:36:49 -04:00
|
|
|
static int flip, quiet, cycles = 0x10000;
|
2024-04-25 15:17:31 -04:00
|
|
|
static Rule rules[0x1000], *rules_ = rules, lambda;
|
2024-04-24 18:17:57 -04:00
|
|
|
static char dict[0x8000], *dict_ = dict, empty;
|
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-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-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-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-21 12:13:30 -04:00
|
|
|
char c, *cap = walk(reg);
|
2024-04-22 23:22:54 -04:00
|
|
|
switch(r) {
|
|
|
|
case ':': /* op: output */
|
2024-04-21 12:13:30 -04:00
|
|
|
if(*reg == '(') reg++, --cap;
|
|
|
|
while(reg < cap) {
|
|
|
|
c = *reg++;
|
2024-04-14 20:33:42 -04:00
|
|
|
if(c == '\\') {
|
2024-04-21 12:13:30 -04:00
|
|
|
switch(*reg++) {
|
2024-04-14 20:33:42 -04:00
|
|
|
case 't': putc(0x09, stdout); break;
|
|
|
|
case 'n': putc(0x0a, stdout); break;
|
|
|
|
case 's': putc(0x20, stdout); break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
putc(c, stdout);
|
|
|
|
}
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
|
|
|
case '~': { /* op: input */
|
2024-04-22 23:22:54 -04:00
|
|
|
while(fread(&c, 1, 1, stdin) && c >= ' ')
|
|
|
|
*dst_++ = c;
|
2024-04-25 21:36:45 -04:00
|
|
|
if(feof(stdin))
|
|
|
|
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
|
|
|
|
return;
|
|
|
|
}
|
2024-04-22 23:22:54 -04:00
|
|
|
case '^': /* op: join */
|
|
|
|
if(*reg == '(') reg++, --cap;
|
|
|
|
while(reg < cap && (c = *reg++))
|
|
|
|
if(!spacer(c)) *dst_++ = c;
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
2024-04-22 23:22:54 -04:00
|
|
|
case '.': /* op: unwrap */
|
|
|
|
if(*reg == '(') reg++, --cap;
|
2024-04-21 12:13:30 -04:00
|
|
|
while(reg < cap) *dst_++ = *reg++;
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
2024-04-22 23:22:54 -04:00
|
|
|
case '*': { /* op: explode */
|
|
|
|
int i, depth = 0;
|
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))
|
|
|
|
*dst_++ = c, reg++;
|
|
|
|
*dst_++ = ' ';
|
|
|
|
*dst_++ = '(', reg++, depth++;
|
|
|
|
}
|
2024-04-23 23:20:31 -04:00
|
|
|
} else /* token */
|
2024-04-22 23:22:54 -04:00
|
|
|
while((c = *reg++) && !spacer(c))
|
|
|
|
*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
|
|
|
}
|
|
|
|
default:
|
|
|
|
while(reg < cap) *dst_++ = *reg++;
|
2024-04-25 21:36:45 -04:00
|
|
|
return;
|
2024-04-22 23:22:54 -04:00
|
|
|
}
|
2024-04-04 15:20:06 -04:00
|
|
|
}
|
|
|
|
|
2024-04-07 17:55:55 -04:00
|
|
|
static int
|
2024-04-22 20:54:17 -04:00
|
|
|
write_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-24 18:46:27 -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-22 18:29:54 -04:00
|
|
|
if(!quiet) {
|
|
|
|
if(create)
|
|
|
|
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
|
|
|
|
}
|
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-04-24 18:55:48 -04:00
|
|
|
unsigned int i, id;
|
2024-04-25 21:39:17 -04:00
|
|
|
char c, *a = r->a, *b = r->b, *origin = dst_, *reg, *regs[0x08];
|
2024-04-24 18:46:27 -04:00
|
|
|
/* phase: clean registers */
|
2024-04-24 18:55:48 -04:00
|
|
|
for(i = 0; i < r->ptr; i++)
|
|
|
|
regs[i] = NULL;
|
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-24 23:26:56 -04:00
|
|
|
id = *a - '0';
|
2024-04-24 14:12:57 -04:00
|
|
|
if((reg = regs[id])) { /* 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-04-24 13:39:14 -04:00
|
|
|
} else /* reg set */
|
|
|
|
regs[id] = s;
|
2024-04-24 23:26:56 -04:00
|
|
|
c = *a++, s = pcap;
|
|
|
|
if(!spacer(c))
|
2024-04-22 20:43:35 -04:00
|
|
|
while((c = *a) && !spacer(c)) a++;
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-25 15:17:31 -04:00
|
|
|
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-04-25 12:48:24 -04:00
|
|
|
while((c = *b++)) {
|
|
|
|
if(c == '?') {
|
|
|
|
id = *b - '0';
|
|
|
|
if(id < 9 && (reg = regs[id]))
|
|
|
|
b++, write_reg(r->reg[id], reg);
|
|
|
|
else
|
2024-04-22 20:54:17 -04:00
|
|
|
*dst_++ = c;
|
2024-04-25 12:48:24 -04:00
|
|
|
} 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_--;
|
|
|
|
}
|
|
|
|
return write_rule(r, s, 0);
|
2024-04-22 20:54:17 -04:00
|
|
|
}
|
|
|
|
|
2024-04-24 16:35:12 -04:00
|
|
|
static int
|
|
|
|
find_register(Rule *r, char reg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < (int)r->ptr; i++)
|
2024-04-24 18:46:27 -04:00
|
|
|
if(r->reg[i] == reg) return i;
|
2024-04-24 16:35:12 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-04-24 16:41:09 -04:00
|
|
|
static char *
|
2024-04-24 16:45:08 -04:00
|
|
|
compile_rule(Rule *r, int id, char *src)
|
2024-04-24 13:31:46 -04:00
|
|
|
{
|
2024-04-24 18:08:09 -04:00
|
|
|
char c, *cap;
|
2024-04-24 18:34:11 -04:00
|
|
|
int wrapped, reg;
|
2024-04-24 18:17:57 -04:00
|
|
|
r->id = id, r->ptr = 0, r->a = &empty, r->b = ∅
|
2024-04-24 18:46:27 -04:00
|
|
|
/* phase: compile left */
|
2024-04-24 18:08:09 -04:00
|
|
|
while((c = *src) && c == ' ') src++;
|
2024-04-24 18:17:57 -04:00
|
|
|
if(c == ')' || (c == '<' && src[1] == '>')) return src;
|
2024-04-24 18:34:11 -04:00
|
|
|
r->a = dict_, cap = walk(src), wrapped = c == '(';
|
2024-04-24 18:08:09 -04:00
|
|
|
if(wrapped) src++, cap--;
|
|
|
|
while(src < cap) {
|
2024-04-24 18:34:11 -04:00
|
|
|
c = *src, *dict_++ = *src++;
|
|
|
|
if(c == '?') {
|
2024-04-26 11:54:05 -04:00
|
|
|
c = *src++, reg = find_register(r, c);
|
2024-04-24 23:26:56 -04:00
|
|
|
if(reg == -1 && c != '(')
|
|
|
|
r->reg[r->ptr] = c, reg = r->ptr++;
|
2024-04-24 17:54:40 -04:00
|
|
|
*dict_++ = '0' + reg;
|
2024-04-24 18:34:11 -04:00
|
|
|
}
|
2024-04-24 17:03:14 -04:00
|
|
|
}
|
2024-04-24 18:34:11 -04:00
|
|
|
src += wrapped, *dict_++ = 0;
|
2024-04-24 18:46:27 -04:00
|
|
|
/* phase: compile right */
|
2024-04-24 18:08:09 -04:00
|
|
|
while((c = *src) && c == ' ') src++;
|
2024-04-24 18:17:57 -04:00
|
|
|
if(c == ')' || (c == '<' && src[1] == '>')) return src;
|
2024-04-24 18:34:11 -04:00
|
|
|
r->b = dict_, cap = walk(src), wrapped = c == '(';
|
2024-04-24 18:08:09 -04:00
|
|
|
if(wrapped) src++, cap--;
|
|
|
|
while(src < cap) {
|
2024-04-24 18:34:11 -04:00
|
|
|
c = *src, *dict_++ = *src++;
|
|
|
|
if(c == '?') {
|
2024-04-24 23:26:56 -04:00
|
|
|
c = *src++, reg = find_register(r, c);
|
|
|
|
*dict_++ = reg != -1 ? '0' + reg : c;
|
2024-04-24 18:34:11 -04:00
|
|
|
}
|
2024-04-24 17:54:40 -04:00
|
|
|
}
|
2024-04-24 18:34:11 -04:00
|
|
|
src += wrapped, *dict_++ = 0;
|
2024-04-24 16:41:09 -04:00
|
|
|
return src;
|
2024-04-24 13:31:46 -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-24 18:55:48 -04:00
|
|
|
/* phase: rule */
|
|
|
|
if(c == '<' && s[1] == '>') {
|
2024-04-24 16:45:08 -04:00
|
|
|
r = rules_++;
|
2024-04-24 16:49:21 -04:00
|
|
|
s = compile_rule(r, rules_ - rules - 1, s + 2);
|
2024-04-21 12:13:30 -04:00
|
|
|
while(*s == ' ') s++;
|
2024-04-22 20:54:17 -04:00
|
|
|
return write_rule(r, s, 1);
|
2024-04-10 15:19:44 -04:00
|
|
|
}
|
2024-04-24 18:55:48 -04:00
|
|
|
/* phase: lambda */
|
|
|
|
if(c == '?' && s[1] == '(') {
|
2024-04-25 13:32:39 -04:00
|
|
|
cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap;
|
2024-04-21 12:13:30 -04:00
|
|
|
while(*s == ' ') s++;
|
2024-04-26 11:05:21 -04:00
|
|
|
if(!apply_rule(&lambda, s)) write_rule(&lambda, s, 0);
|
2024-04-26 01:39:37 -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-04-25 21:39:17 -04:00
|
|
|
if(apply_rule(r, s)) return 1;
|
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-21 12:46:53 -04:00
|
|
|
int i, pl = 0, pr = 0;
|
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-26 11:05:21 -04:00
|
|
|
case 'v': /* version */ return !printf("Modal Interpreter, 26 Apr 2024.\n");
|
2024-04-22 18:29:54 -04:00
|
|
|
case 'q': /* quiet */ quiet = 1; break;
|
2024-04-18 14:33:23 -04:00
|
|
|
case 'n': /* infinite */ cycles = 0xffffffff; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!(f = fopen(argv[i], "r")))
|
2024-04-21 12:46:53 -04:00
|
|
|
return !fprintf(stdout, "Modal file invalid: %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-23 12:35:58 -04:00
|
|
|
if(c == '(') pl++;
|
|
|
|
if(c == ')') pr++;
|
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-21 12:46:53 -04:00
|
|
|
if(pr != pl)
|
|
|
|
return !fprintf(stdout, "Modal program imbalanced.\n");
|
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-22 18:29:54 -04:00
|
|
|
while(rules_-- > rules && !quiet)
|
|
|
|
if(!rules_->refs) printf("-- Unused rule: %d <> (%s) (%s)\n", rules_->refs, rules_->a, rules_->b);
|
2024-04-04 14:54:31 -04:00
|
|
|
return 0;
|
2024-04-04 12:04:27 -04:00
|
|
|
}
|