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-08 14:46:24 -04:00
|
|
|
static int direction;
|
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-06 18:11:38 -04:00
|
|
|
static char bank_a[0x4000], *prog_ = bank_a;
|
|
|
|
static char bank_b[0x4000], *outp_ = bank_b;
|
2024-04-04 13:59:00 -04:00
|
|
|
static char *regs[0x100];
|
|
|
|
|
2024-04-08 13:50:43 -04:00
|
|
|
#define spacer(c) (c < 0x21 || 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-04 22:37:03 -04:00
|
|
|
if(s[0] == '(') {
|
|
|
|
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-07 12:08:58 -04:00
|
|
|
static char *
|
|
|
|
plode(char *s)
|
|
|
|
{
|
2024-04-07 12:13:35 -04:00
|
|
|
int i, depth = 0;
|
2024-04-07 12:08:58 -04:00
|
|
|
char c;
|
|
|
|
if(s[0] == '(') { /* implode */
|
2024-04-07 12:13:35 -04:00
|
|
|
while((c = *s++)) {
|
|
|
|
if(c == '(') depth++;
|
|
|
|
if(!spacer(c)) *outp_++ = c;
|
|
|
|
if(c == ')') --depth;
|
|
|
|
if(!depth) return s;
|
|
|
|
}
|
2024-04-07 12:08:58 -04:00
|
|
|
} else { /* explode */
|
2024-04-10 12:04:01 -04:00
|
|
|
*outp_++ = *s++;
|
|
|
|
if(!spacer(*s)) *outp_++ = ' ';
|
2024-04-07 14:22:18 -04:00
|
|
|
while((c = *s++) && !spacer(c)) {
|
2024-04-08 13:10:56 -04:00
|
|
|
*outp_++ = '(', *outp_++ = c, depth++, c = *s;
|
2024-04-10 12:04:01 -04:00
|
|
|
if(!spacer(c)) *outp_++ = ' ';
|
2024-04-07 14:22:18 -04:00
|
|
|
}
|
2024-04-07 12:13:35 -04:00
|
|
|
for(i = 0; i < depth; i++)
|
2024-04-07 12:08:58 -04:00
|
|
|
*outp_++ = ')';
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
while(a < aa && b < bb)
|
|
|
|
if(*a++ != *b++) return 0;
|
|
|
|
} else
|
|
|
|
regs[r] = b;
|
|
|
|
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-08 12:28:36 -04:00
|
|
|
char *s = regs[(int)r];
|
2024-04-07 12:08:58 -04:00
|
|
|
if(r == '*')
|
|
|
|
s = plode(s);
|
2024-04-09 13:29:33 -04:00
|
|
|
else if(r == '~') {
|
|
|
|
char buf;
|
|
|
|
while(fread(&buf, 1, 1, stdin) && buf >= ' ')
|
|
|
|
*outp_++ = buf;
|
|
|
|
} else if(s) {
|
2024-04-08 12:28:36 -04:00
|
|
|
char *ss = walk(s);
|
2024-04-08 23:23:58 -04:00
|
|
|
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++;
|
2024-04-08 12:28:36 -04:00
|
|
|
} else
|
|
|
|
*outp_++ = r;
|
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;
|
|
|
|
char c, *a = r->a, *b = p;
|
|
|
|
for(i = 0x21; i < 0x7f; i++)
|
2024-04-08 22:57:33 -04:00
|
|
|
regs[i] = 0;
|
2024-04-08 13:23:28 -04:00
|
|
|
while((c = *a)) {
|
|
|
|
if(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;
|
|
|
|
}
|
|
|
|
if(*a != *b) return NULL;
|
|
|
|
a++, b++;
|
|
|
|
}
|
|
|
|
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-07 18:50:27 -04:00
|
|
|
while((*outp_++ = *s++))
|
2024-04-07 17:55:55 -04:00
|
|
|
;
|
|
|
|
*outp_++ = 0;
|
2024-04-06 11:36:10 -04:00
|
|
|
if((direction = !direction))
|
|
|
|
prog_ = bank_b, outp_ = bank_a;
|
|
|
|
else
|
|
|
|
prog_ = bank_a, outp_ = bank_b;
|
2024-04-09 11:56:25 -04:00
|
|
|
if(create)
|
|
|
|
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
|
|
|
|
else
|
2024-04-08 14:46:24 -04:00
|
|
|
fprintf(stderr, "%02d %s\n", r->id, prog_);
|
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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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++;
|
|
|
|
ss = walk(s);
|
2024-04-08 12:42:37 -04:00
|
|
|
if(*s == '(') s++, ss--;
|
2024-04-08 18:14:05 -04:00
|
|
|
while((s < ss)) *dict_++ = *s++;
|
2024-04-06 11:17:58 -04:00
|
|
|
*dict_++ = 0;
|
2024-04-10 14:39:15 -04:00
|
|
|
s++;
|
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)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
r->id = id, s += 2;
|
|
|
|
r->a = dict_, s = parse_frag(s), r->b = dict_, s = parse_frag(s);
|
|
|
|
while((c = *s) && c <= ' ') s++;
|
|
|
|
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-10 15:25:31 -04:00
|
|
|
char c, last = 0, *p = direction ? bank_b : bank_a, *res;
|
2024-04-07 18:50:27 -04:00
|
|
|
while((c = *p) && c <= ' ') p++;
|
2024-04-04 13:30:31 -04:00
|
|
|
while((c = *p)) {
|
2024-04-08 13:32:06 -04:00
|
|
|
if(spacer(last)) {
|
2024-04-10 16:08:50 -04:00
|
|
|
Rule *r;
|
2024-04-10 15:19:44 -04:00
|
|
|
if(p[0] == '<' && p[1] == '>') {
|
|
|
|
r = rules_++;
|
2024-04-11 11:53:49 -04:00
|
|
|
p = create_rule(r, rules_ - rules - 1, p);
|
2024-04-10 15:19:44 -04:00
|
|
|
return commit_rule(r, p, 1);
|
|
|
|
}
|
|
|
|
if(p[0] == '?' && p[1] == '(') {
|
2024-04-11 11:53:49 -04:00
|
|
|
r = λ
|
|
|
|
p = create_rule(&lambda, -1, p) + 1;
|
2024-04-10 16:08:50 -04:00
|
|
|
if((res = match_rule(&lambda, p)) != 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++)
|
|
|
|
if((res = match_rule(r, p)) != 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-08 13:03:15 -04:00
|
|
|
*outp_++ = last = c;
|
2024-04-04 22:55:05 -04:00
|
|
|
p++;
|
2024-04-04 13:30:31 -04:00
|
|
|
}
|
2024-04-04 17:48:04 -04:00
|
|
|
*outp_++ = 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-07 13:56:43 -04:00
|
|
|
char c, *w = bank_a;
|
2024-04-07 13:58:39 -04:00
|
|
|
if(argc < 2)
|
|
|
|
return !printf("usage: modal [-v] source.modal\n");
|
|
|
|
if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v')
|
2024-04-11 12:08:07 -04:00
|
|
|
return !printf("Modal Interpreter, 11 Apr 2024.\n");
|
2024-04-07 13:58:39 -04:00
|
|
|
if(!(f = fopen(argv[1], "r")))
|
|
|
|
return !printf("Invalid Modal file: %s.\n", argv[1]);
|
2024-04-07 13:56:43 -04:00
|
|
|
while(fread(&c, 1, 1, f)) {
|
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--;
|
|
|
|
}
|
|
|
|
*w++ = c;
|
|
|
|
}
|
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-04 18:10:25 -04:00
|
|
|
;
|
2024-04-04 14:54:31 -04:00
|
|
|
return 0;
|
2024-04-04 12:04:27 -04:00
|
|
|
}
|