#include typedef struct { char *a, *b; } Rule; static int rules_len, direction; static Rule rules[0x100]; 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(!spacer(s[0]) && *s++) ; return s; } static int bind(int id, char *b) { if(regs[id]) { char *a = regs[id], *aa = walk(a), *bb = walk(b); while(a < aa && b < bb) if(*a++ != *b++) return 0; } else if(id == ':') { char *bb = walk(b); if(*b == '(') b++, --bb; while(b < bb) putc(*(b++), stdout); } else regs[id] = b; return 1; } static char * match(char *p, Rule *r) { int i; char c, *a = r->a, *b = p; for(i = 0x21; i < 0x7f; i++) if((char)i != ':') regs[i] = 0; while((c = *a)) { if(c == '?') { if(!bind(*(++a), b)) return NULL; a++, b = walk(b); continue; } if(*a != *b) return NULL; a++, b++; } return spacer(*b) ? b : NULL; } 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++, *outp_++ = ' '; while((c = *s++) && !spacer(c)) { *outp_++ = '(', *outp_++ = c, depth++; if(!spacer(*s)) *outp_++ = ' '; } for(i = 0; i < depth; i++) *outp_++ = ')'; } return s; } static int commit(char r) { char *s = regs[(int)r]; if(r == '*') s = plode(s); else if(s) { char *ss = walk(s); while((s < ss) && (*outp_++ = *s++)) ; } else *outp_++ = r; return 1; } static int save(int rule, char *s) { while((*outp_++ = *s++)) ; *outp_++ = 0; if((direction = !direction)) prog_ = bank_b, outp_ = bank_a; else prog_ = bank_a, outp_ = bank_b; if(rule >= 0) fprintf(stderr, "%02d %s\n", rule, prog_); return 1; } static char * parse_rule(char *s) { char *ss = walk(s), *d = dict_; if(*s == '(') s++, ss--; while((s < ss) && (*dict_++ = *s++)) ; *dict_++ = 0; return d; } static int rewrite(void) { char c, *p = direction ? bank_b : bank_a, *o = p; while((c = *p) && c <= ' ') p++; while((c = *p)) { int i; if(p[0] == '<' && p[1] == '>') { Rule *r = &rules[rules_len++]; p += 3; r->a = parse_rule(p), p = walk(p) + 1; r->b = parse_rule(p), p = walk(p); return save(-1, p); } if(p == o || spacer(*(p - 1))) { for(i = 0; i < rules_len; i++) { Rule *r = &rules[i]; char *res = match(p, r); if(res != NULL) { char cc, *b = r->b; if(!*b && outp_ != bank_a && outp_ != bank_b) outp_--; while((cc = *b++)) { if(cc == '?') commit(*b++); else *outp_++ = cc; } return save(i, res); } } } *outp_++ = c; p++; } *outp_++ = 0; return 0; } static void print_rules(void) { int i; fprintf(stderr, "\n"); for(i = 0; i < rules_len; i++) fprintf(stderr, "<> (%s) (%s)\n", rules[i].a, rules[i].b); fprintf(stderr, "\n"); } 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 - Modal Interpreter, 8 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; } *w++ = 0; fclose(f); regs[':'] = argv[2]; while(rewrite()) ; print_rules(); return 0; }