2024-04-04 12:04:27 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-04-04 12:29:36 -04:00
|
|
|
typedef struct {
|
2024-04-04 13:01:09 -04:00
|
|
|
char *a, *b;
|
2024-04-04 12:29:36 -04:00
|
|
|
} Rule;
|
|
|
|
|
2024-04-06 11:36:10 -04:00
|
|
|
static int rules_len, direction = 0;
|
2024-04-04 12:29:36 -04:00
|
|
|
static Rule rules[0x100];
|
2024-04-04 14:54:31 -04:00
|
|
|
static char dict[0x8000], *dict_ = dict;
|
2024-04-06 11:21:14 -04:00
|
|
|
static char bank_a[0x1000], *prog_ = bank_a;
|
|
|
|
static char bank_b[0x1000], *outp_ = bank_b;
|
2024-04-04 13:59:00 -04:00
|
|
|
static char *regs[0x100];
|
|
|
|
|
2024-04-06 12:42:55 -04:00
|
|
|
#define spacer(c) (c < 0x21 || c == '(' || c == ')')
|
2024-04-04 19:52:09 -04:00
|
|
|
|
2024-04-05 16:19:34 -04:00
|
|
|
static char *parse_rulefrag(char *line);
|
|
|
|
|
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-04 22:40:16 -04:00
|
|
|
while(!spacer(s[0]) && (c = *s++))
|
|
|
|
;
|
2024-04-04 20:15:16 -04:00
|
|
|
return s;
|
2024-04-04 13:59:00 -04:00
|
|
|
}
|
|
|
|
|
2024-04-05 13:34:46 -04:00
|
|
|
static int
|
|
|
|
compare(char *a, char *b)
|
|
|
|
{
|
|
|
|
int i = 0, al = walk(a) - a, bl = walk(b) - b;
|
2024-04-06 12:42:55 -04:00
|
|
|
if(al == bl)
|
|
|
|
while(a[i] == b[i])
|
|
|
|
if(!a[i] || ++i >= al) return 1;
|
2024-04-05 13:34:46 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-04 15:09:47 -04:00
|
|
|
static char *
|
2024-04-04 13:30:31 -04:00
|
|
|
match(char *p, Rule *r)
|
|
|
|
{
|
2024-04-05 13:34:46 -04:00
|
|
|
int i;
|
2024-04-04 13:30:31 -04:00
|
|
|
char c, *a = r->a, *b = p;
|
2024-04-05 13:34:46 -04:00
|
|
|
for(i = 0x21; i < 0x7f; i++)
|
|
|
|
regs[i] = 0;
|
2024-04-04 13:30:31 -04:00
|
|
|
while((c = *a)) {
|
2024-04-05 13:34:46 -04:00
|
|
|
if(c == '?') {
|
|
|
|
int id = (int)*(++a);
|
|
|
|
if(regs[id]) {
|
|
|
|
if(!compare(regs[id], b))
|
|
|
|
return NULL;
|
|
|
|
} else
|
|
|
|
regs[id] = b;
|
|
|
|
a++, b = walk(b), c = *b;
|
|
|
|
}
|
2024-04-06 12:14:01 -04:00
|
|
|
if(!*a && spacer(*b)) return b;
|
2024-04-04 15:09:47 -04:00
|
|
|
if(c != *b) return NULL;
|
2024-04-04 13:30:31 -04:00
|
|
|
a++, b++;
|
|
|
|
}
|
2024-04-06 12:19:58 -04:00
|
|
|
return spacer(*b) ? b : NULL;
|
2024-04-04 13:30:31 -04:00
|
|
|
}
|
2024-04-04 12:29:36 -04:00
|
|
|
|
2024-04-06 11:17:58 -04:00
|
|
|
static int
|
2024-04-06 11:02:54 -04:00
|
|
|
bind(char r)
|
2024-04-04 15:20:06 -04:00
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
char c, *s = regs[(int)r];
|
2024-04-06 13:28:00 -04:00
|
|
|
if(!s) return !printf("?%c Empty\n", r);
|
2024-04-04 22:48:10 -04:00
|
|
|
if(s[0] == '(') {
|
|
|
|
while((c = *s++)) {
|
|
|
|
if(c == '(') depth++;
|
|
|
|
*outp_++ = c;
|
|
|
|
if(c == ')') --depth;
|
2024-04-06 11:21:14 -04:00
|
|
|
if(!depth) return 1;
|
2024-04-04 20:26:16 -04:00
|
|
|
}
|
2024-04-04 15:20:06 -04:00
|
|
|
}
|
2024-04-04 22:48:10 -04:00
|
|
|
while(!spacer(s[0]) && (*outp_++ = *s++))
|
|
|
|
;
|
2024-04-06 11:17:58 -04:00
|
|
|
return 1;
|
2024-04-04 15:20:06 -04:00
|
|
|
}
|
|
|
|
|
2024-04-04 17:55:51 -04:00
|
|
|
static void
|
2024-04-05 12:20:18 -04:00
|
|
|
save(int rule)
|
2024-04-04 17:55:51 -04:00
|
|
|
{
|
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-06 13:59:02 -04:00
|
|
|
if(rule >= 0) {
|
|
|
|
char *program = direction ? bank_b : bank_a;
|
|
|
|
while(program[1] && program[0] < 0x21) program++;
|
|
|
|
printf("%02d %s\n", rule, program);
|
|
|
|
}
|
2024-04-04 17:55:51 -04:00
|
|
|
}
|
|
|
|
|
2024-04-06 11:17:58 -04:00
|
|
|
static char *
|
|
|
|
parse_rulefrag(char *line)
|
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
char c, *s = line, *res = dict_;
|
|
|
|
if(s[0] == '(') {
|
|
|
|
while((c = *s++)) {
|
|
|
|
if(c == '(') {
|
|
|
|
depth++;
|
|
|
|
if(depth == 1) continue;
|
|
|
|
}
|
|
|
|
if(c == ')') {
|
|
|
|
--depth;
|
|
|
|
if(!depth) {
|
|
|
|
*dict_++ = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dict_++ = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(!spacer(s[0]) && (*dict_++ = *s++))
|
|
|
|
;
|
|
|
|
*dict_++ = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-04-05 16:19:34 -04:00
|
|
|
static char *
|
|
|
|
addrule(char *s)
|
|
|
|
{
|
|
|
|
Rule *r = &rules[rules_len++];
|
|
|
|
s += 3;
|
2024-04-06 12:42:55 -04:00
|
|
|
r->a = parse_rulefrag(s), s = walk(s) + 1;
|
|
|
|
r->b = parse_rulefrag(s), s = walk(s);
|
2024-04-05 16:19:34 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2024-04-04 13:07:49 -04:00
|
|
|
static int
|
2024-04-04 14:54:31 -04:00
|
|
|
rewrite(void)
|
2024-04-04 13:07:49 -04:00
|
|
|
{
|
2024-04-06 11:36:10 -04:00
|
|
|
char c, *p = direction ? bank_b : bank_a;
|
2024-04-04 13:30:31 -04:00
|
|
|
while((c = *p)) {
|
2024-04-04 22:55:05 -04:00
|
|
|
int i;
|
2024-04-06 13:59:02 -04:00
|
|
|
if(p[0] == '<' && p[1] == '>') {
|
|
|
|
p = addrule(p) + 1;
|
|
|
|
while((*outp_++ = *p++))
|
|
|
|
;
|
|
|
|
save(-1);
|
|
|
|
return 1;
|
|
|
|
}
|
2024-04-06 12:10:19 -04:00
|
|
|
if(p == bank_a || p == bank_b || 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;
|
|
|
|
while((cc = *b++)) {
|
|
|
|
if(cc == '?')
|
|
|
|
bind(*b++);
|
|
|
|
else
|
|
|
|
*outp_++ = cc;
|
|
|
|
}
|
|
|
|
while((*outp_++ = *res++))
|
|
|
|
;
|
|
|
|
*outp_++ = 0;
|
|
|
|
save(i);
|
|
|
|
return 1;
|
2024-04-04 20:15:16 -04:00
|
|
|
}
|
2024-04-04 15:09:47 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 22:55:05 -04:00
|
|
|
*outp_++ = c;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-04-04 17:55:51 -04:00
|
|
|
print_rules(void)
|
2024-04-04 13:07:49 -04:00
|
|
|
{
|
|
|
|
int i;
|
2024-04-06 13:28:00 -04:00
|
|
|
printf("\n");
|
2024-04-05 11:54:39 -04:00
|
|
|
for(i = 0; i < rules_len; i++)
|
2024-04-05 13:34:46 -04:00
|
|
|
printf("<> (%s) (%s)\n", rules[i].a, rules[i].b);
|
2024-04-04 17:55:51 -04:00
|
|
|
printf("\n");
|
2024-04-04 13:07:49 -04:00
|
|
|
}
|
|
|
|
|
2024-04-04 12:04:27 -04:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2024-04-06 13:59:02 -04:00
|
|
|
FILE *f;
|
2024-04-04 12:04:27 -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-05 13:34:46 -04:00
|
|
|
return !printf("Modal - Modal Interpreter, 4 Apr 2024.\n");
|
2024-04-06 13:59:02 -04:00
|
|
|
if(!(f = fopen(argv[1], "r")))
|
2024-04-06 11:17:58 -04:00
|
|
|
return !printf("Invalid Modal file: %s.\n", argv[1]);
|
2024-04-06 13:59:02 -04:00
|
|
|
fread(bank_a, 1, 0x1000, f), fclose(f);
|
2024-04-04 18:10:25 -04:00
|
|
|
while(rewrite())
|
|
|
|
;
|
2024-04-06 13:28:00 -04:00
|
|
|
print_rules();
|
2024-04-04 14:54:31 -04:00
|
|
|
return 0;
|
2024-04-04 12:04:27 -04:00
|
|
|
}
|