#include typedef struct { int id; char *a, *b; } Rule; static int direction; static Rule rules[0x1000], *rules_ = rules; static char dict[0x8000], *dict_ = dict; static char bank_a[0x4000], *prog_ = bank_a; static char bank_b[0x4000], *outp_ = bank_b; static char *regs[0x100]; #define spacer(c) (c < 0x21 || c == '(' || c == ')') static char * walk(char *s) { char c; int depth = 0; if(s[0] == '(') { while((c = *s++)) { if(c == '(') depth++; if(c == ')') --depth; if(!depth) return s; } } while((c = *s) && !spacer(c)) s++; return s; } static char * plode(char *s) { int i, depth = 0; char c; if(s[0] == '(') { /* implode */ while((c = *s++)) { if(c == '(') depth++; if(!spacer(c)) *outp_++ = c; if(c == ')') --depth; if(!depth) return s; } } else { /* explode */ *outp_++ = *s++; if(!spacer(*s)) *outp_++ = ' '; while((c = *s++) && !spacer(c)) { *outp_++ = '(', *outp_++ = c, depth++, c = *s; if(!spacer(c)) *outp_++ = ' '; } for(i = 0; i < depth; i++) *outp_++ = ')'; } 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; return 1; } static int put_reg(char r) { char *s = regs[(int)r]; if(r == '*') s = plode(s); else if(r == '~') { char buf; while(fread(&buf, 1, 1, stdin) && buf >= ' ') *outp_++ = buf; } else if(s) { char *ss = walk(s); if(r == ':') { if(*s == '(') s++, --ss; while(s < ss) { char c = *(s++); if(c == '\\' && *(s++) == 'n') c = 0xa; putc(c, stdout); } } else while((s < ss)) *outp_++ = *s++; } else *outp_++ = r; return 1; } static char * match_rule(char *p, Rule *r) { int i; char c, *a = r->a, *b = p; for(i = 0x21; i < 0x7f; i++) regs[i] = 0; while((c = *a)) { if(c == '?') { if(!set_reg(*(++a), b)) return NULL; a++, b = walk(b); continue; } if(*a != *b) return NULL; a++, b++; } c = *b; return spacer(c) ? b : NULL; } static int commit_rule(Rule *r, char *s, int create) { while((*outp_++ = *s++)) ; *outp_++ = 0; if((direction = !direction)) prog_ = bank_b, outp_ = bank_a; else prog_ = bank_a, outp_ = bank_b; if(create) fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b); else fprintf(stderr, "%02d %s\n", r->id, prog_); return 1; } static char * parse_frag(char *s) { char c, *ss; while((c = *s) && c <= ' ') s++; ss = walk(s); if(*s == '(') s++, ss--; while((s < ss)) *dict_++ = *s++; *dict_++ = 0; s++; return s; } static int write(Rule *r, char last, char *res){ char cc, *b = r->b; if(!*b && last == ' ') outp_--; while((cc = *b++)) if(cc == '?') put_reg(*b++); else *outp_++ = cc; return commit_rule(r, res, 0); } static int rewrite(void) { char c, last = 0, *p = direction ? bank_b : bank_a, *res; while((c = *p) && c <= ' ') p++; while((c = *p)) { if(spacer(last)) { Rule *r = NULL, lambda; if(p[0] == '<' && p[1] == '>') { r = rules_++; r->id = rules_ - rules - 1; p += 2, r->a = dict_, p = parse_frag(p), r->b = dict_, p = parse_frag(p); return commit_rule(r, p, 1); } if(p[0] == '?' && p[1] == '(') { r = λ p += 2, r->a = dict_, p = parse_frag(p), r->b = dict_, p = parse_frag(p); p++; while((c = *p) && c <= ' ') p++; if((res = match_rule(p, r)) != NULL) return write(r, last, res); } for(r = rules; r < rules_; r++) if((res = match_rule(p, r)) != NULL) return write(r, last, res); } *outp_++ = last = c; p++; } *outp_++ = 0; return 0; } int main(int argc, char **argv) { FILE *f; char c, *w = bank_a; if(argc < 2) return !printf("usage: modal [-v] source.modal\n"); if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v') return !printf("Modal Interpreter, 10 Apr 2024.\n"); if(!(f = fopen(argv[1], "r"))) return !printf("Invalid Modal file: %s.\n", argv[1]); while(fread(&c, 1, 1, f)) { if(w > bank_a) { if(c == ' ' && *(w - 1) == '(') continue; if(c == ')' && *(w - 1) == ' ') w--; if(c == ' ' && *(w - 1) == ' ') w--; } *w++ = c; } while(*(--w) <= ' ') *w = 0; fclose(f); while(rewrite()) ; return 0; }