#include typedef struct { unsigned int id, refs, ptr; char *a, *b, reg[0x10]; } Rule; static int flip, quiet, cycles = 0x10000; static Rule rules[0x1000], *rules_ = rules; static char dict[0x8000], *dict_ = dict, empty; static char bank_a[0x4000], *src_ = bank_a; static char bank_b[0x4000], *dst_ = bank_b; #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 write_reg(char r, char *reg) { char c, *cap = walk(reg); switch(r) { case ':': /* op: output */ 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); } return 0; case '~': /* op: input */ while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c; return 1; case '^': /* op: join */ if(*reg == '(') reg++, --cap; while(reg < cap && (c = *reg++)) if(!spacer(c)) *dst_++ = c; return 1; case '.': /* op: unwrap */ if(*reg == '(') reg++, --cap; while(reg < cap) *dst_++ = *reg++; return 1; 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 1; } default: while(reg < cap) *dst_++ = *reg++; return 1; } } static int write_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(!quiet) { if(create) fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b); else fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs; } return 1; } static char * match_rule(Rule *r, char **regs, char *s) { unsigned int i, id; char c, *a = r->a, *reg; /* phase: clean registers */ for(i = 0; i < r->ptr; i++) regs[i] = NULL; /* phase: match rule */ while((c = *a++)) { if(c == '?') { char *pcap = walk(s); id = *a - '0'; if((reg = regs[id])) { /* reg cmp */ char *rcap = walk(reg), *pp = s; while(reg < rcap || pp < pcap) if(*reg++ != *pp++) return NULL; } else /* reg set */ regs[id] = s; c = *a++, s = pcap; if(!spacer(c)) while((c = *a) && !spacer(c)) a++; continue; } if(c != *s++) return NULL; } return s; } static int apply_rule(Rule *r, char **regs, char *s) { unsigned int id; char c, *b = r->b, *origin = dst_, *reg; /* phase: write rule */ while((c = *b++)) { if(c == '?') { id = *b - '0'; if(id < 9 && (reg = regs[id])) b++, write_reg(r->reg[id], reg); else *dst_++ = c; } else *dst_++ = c; } if(dst_ == origin) { while(*s == ' ') s++; if(*s == ')' && *(dst_ - 1) == ' ') dst_--; } return write_rule(r, s, 0); } static int find_register(Rule *r, char reg) { int i; for(i = 0; i < (int)r->ptr; i++) if(r->reg[i] == reg) return i; return -1; } static char * compile_rule(Rule *r, int id, char *src) { char c, *cap; int wrapped, reg; r->id = id, r->ptr = 0, r->a = &empty, r->b = ∅ /* phase: compile left */ while((c = *src) && c == ' ') src++; if(c == ')' || (c == '<' && src[1] == '>')) return src; r->a = dict_, cap = walk(src), wrapped = c == '('; if(wrapped) src++, cap--; while(src < cap) { c = *src, *dict_++ = *src++; if(c == '?') { c = *src, reg = find_register(r, c); if(reg == -1 && c != '(') r->reg[r->ptr] = c, reg = r->ptr++; *dict_++ = '0' + reg; } } src += wrapped, *dict_++ = 0; /* phase: compile right */ while((c = *src) && c == ' ') src++; if(c == ')' || (c == '<' && src[1] == '>')) return src; r->b = dict_, cap = walk(src), wrapped = c == '('; if(wrapped) src++, cap--; while(src < cap) { c = *src, *dict_++ = *src++; if(c == '?') { c = *src++, reg = find_register(r, c); *dict_++ = reg != -1 ? '0' + reg : c; } } src += wrapped, *dict_++ = 0; return src; } static int rewrite(void) { char c, last = 0, *cap, *s = src_, *regs[0x08], *res = NULL; while(*s == ' ') s++; while((c = *s)) { if(spacer(last)) { Rule *r = NULL; /* phase: rule */ if(c == '<' && s[1] == '>') { r = rules_++; s = compile_rule(r, rules_ - rules - 1, s + 2); while(*s == ' ') s++; return write_rule(r, s, 1); } /* phase: lambda */ if(c == '?' && s[1] == '(') { Rule lambda; cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap; while(*s == ' ') s++; if((res = match_rule(&lambda, regs, s))) return apply_rule(&lambda, regs, res); } /* phase: match */ for(r = rules; r < rules_; r++) if((res = match_rule(r, regs, s))) break; /* phase: apply */ if(res) return apply_rule(r, regs, 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, 25 Apr 2024.\n"); case 'q': /* quiet */ quiet = 1; 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"); while(rules_-- > rules && !quiet) if(!rules_->refs) printf("-- Unused rule: %d <> (%s) (%s)\n", rules_->refs, rules_->a, rules_->b); return 0; }