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;
|
|
|
|
|
|
|
|
static int rules_len;
|
|
|
|
static Rule rules[0x100];
|
2024-04-04 14:54:31 -04:00
|
|
|
static char dict[0x8000], *dict_ = dict;
|
2024-04-04 13:30:31 -04:00
|
|
|
static char prog[0x1000], *prog_ = prog;
|
2024-04-04 14:54:31 -04:00
|
|
|
static char outp[0x1000], *outp_ = outp;
|
2024-04-04 13:59:00 -04:00
|
|
|
static char *regs[0x100];
|
|
|
|
|
2024-04-04 19:52:09 -04:00
|
|
|
#define spacer(c) (c == ' ' || c == '(' || c == ')')
|
|
|
|
|
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-04 15:09:47 -04:00
|
|
|
static char *
|
2024-04-04 13:30:31 -04:00
|
|
|
match(char *p, Rule *r)
|
|
|
|
{
|
|
|
|
char c, *a = r->a, *b = p;
|
|
|
|
while((c = *a)) {
|
2024-04-04 22:48:10 -04:00
|
|
|
if(c == '?') regs[(int)*(++a)] = b, a++, b = walk(b), c = *b;
|
2024-04-04 23:35:31 -04:00
|
|
|
if(!a[0]) 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-04 15:09:47 -04:00
|
|
|
return b;
|
2024-04-04 13:30:31 -04:00
|
|
|
}
|
2024-04-04 12:29:36 -04:00
|
|
|
|
2024-04-04 22:48:10 -04:00
|
|
|
static void
|
2024-04-04 15:20:06 -04:00
|
|
|
writereg(char r)
|
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
char c, *s = regs[(int)r];
|
2024-04-04 22:48:10 -04:00
|
|
|
if(s[0] == '(') {
|
|
|
|
while((c = *s++)) {
|
|
|
|
if(c == '(') depth++;
|
|
|
|
*outp_++ = c;
|
|
|
|
if(c == ')') --depth;
|
|
|
|
if(!depth) return;
|
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++))
|
|
|
|
;
|
|
|
|
return;
|
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
|
|
|
{
|
|
|
|
int i, end = outp_ - outp;
|
2024-04-04 18:33:22 -04:00
|
|
|
/* todo: change pointer instead of copying memory */
|
2024-04-04 18:10:25 -04:00
|
|
|
for(i = 0; i <= end; i++) prog[i] = outp[i];
|
|
|
|
prog_ = prog, outp_ = outp;
|
2024-04-05 12:20:18 -04:00
|
|
|
printf("..%02d %s\n", rule, prog);
|
2024-04-04 17:55:51 -04:00
|
|
|
}
|
|
|
|
|
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-04 13:30:31 -04:00
|
|
|
char c, *p = prog;
|
|
|
|
while((c = *p)) {
|
2024-04-04 22:55:05 -04:00
|
|
|
int i;
|
2024-04-04 13:30:31 -04:00
|
|
|
for(i = 0; i < rules_len; i++) {
|
|
|
|
Rule *r = &rules[i];
|
2024-04-04 15:09:47 -04:00
|
|
|
char *res = match(p, r);
|
|
|
|
if(res != NULL) {
|
|
|
|
char cc, *b = r->b;
|
2024-04-04 20:15:16 -04:00
|
|
|
while((cc = *b++)) {
|
2024-04-04 15:20:06 -04:00
|
|
|
if(cc == '?')
|
|
|
|
writereg(*b++);
|
|
|
|
else
|
|
|
|
*outp_++ = cc;
|
2024-04-04 20:15:16 -04:00
|
|
|
}
|
2024-04-04 22:55:05 -04:00
|
|
|
while((*outp_++ = *res++))
|
|
|
|
;
|
|
|
|
*outp_++ = 0;
|
2024-04-05 12:20:18 -04:00
|
|
|
save(i);
|
2024-04-04 22:55:05 -04:00
|
|
|
return 1;
|
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-05 11:54:39 -04:00
|
|
|
for(i = 0; i < rules_len; i++)
|
2024-04-05 12:12:49 -04:00
|
|
|
printf("Rule #%d: <%s> -> <%s>\n", i, 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:56:55 -04:00
|
|
|
static char *
|
2024-04-05 11:54:39 -04:00
|
|
|
parse_rulefrag(char *line)
|
2024-04-04 12:29:36 -04:00
|
|
|
{
|
2024-04-05 11:54:39 -04:00
|
|
|
int depth = 0;
|
|
|
|
char c, *s = line, *res = dict_;
|
|
|
|
if(s[0] == '(') {
|
|
|
|
while((c = *s++)) {
|
2024-04-05 12:12:49 -04:00
|
|
|
if(c == '(') {
|
|
|
|
depth++;
|
|
|
|
if(depth == 1) continue;
|
2024-04-05 11:54:39 -04:00
|
|
|
}
|
2024-04-05 12:12:49 -04:00
|
|
|
if(c == ')') {
|
|
|
|
--depth;
|
|
|
|
if(!depth) {
|
|
|
|
*dict_++ = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dict_++ = c;
|
2024-04-04 23:13:14 -04:00
|
|
|
}
|
2024-04-04 12:29:36 -04:00
|
|
|
}
|
2024-04-05 11:54:39 -04:00
|
|
|
while(!spacer(s[0]) && (*dict_++ = *s++))
|
|
|
|
;
|
2024-04-04 14:54:31 -04:00
|
|
|
*dict_++ = 0;
|
2024-04-05 11:54:39 -04:00
|
|
|
return res;
|
2024-04-04 12:56:55 -04:00
|
|
|
}
|
2024-04-04 12:50:17 -04:00
|
|
|
|
2024-04-05 11:54:39 -04:00
|
|
|
static int
|
|
|
|
parse_line(char *line)
|
2024-04-04 12:29:36 -04:00
|
|
|
{
|
2024-04-05 11:54:39 -04:00
|
|
|
if(line[0] == 0) return 1;
|
|
|
|
if(line[0] == '<' && line[1] == '>') {
|
2024-04-04 13:07:49 -04:00
|
|
|
Rule *r = &rules[rules_len++];
|
2024-04-05 11:54:39 -04:00
|
|
|
line += 3;
|
|
|
|
r->a = parse_rulefrag(line);
|
|
|
|
line = walk(line) + 1;
|
|
|
|
r->b = parse_rulefrag(line);
|
|
|
|
return 1;
|
2024-04-04 12:29:36 -04:00
|
|
|
}
|
2024-04-05 11:54:39 -04:00
|
|
|
while((*prog_++ = *line++))
|
|
|
|
;
|
|
|
|
*prog_++ = 0xa;
|
|
|
|
return 1;
|
2024-04-04 12:50:17 -04:00
|
|
|
}
|
|
|
|
|
2024-04-04 12:04:27 -04:00
|
|
|
static int
|
2024-04-04 13:07:49 -04:00
|
|
|
parse(char *path)
|
2024-04-04 12:04:27 -04:00
|
|
|
{
|
|
|
|
FILE *f;
|
2024-04-05 11:54:39 -04:00
|
|
|
char c, line[0x400], *line_ = line;
|
2024-04-04 12:04:27 -04:00
|
|
|
if(!(f = fopen(path, "r")))
|
|
|
|
return !printf("Invalid file: %s\n", path);
|
2024-04-04 13:01:09 -04:00
|
|
|
while(f && fread(&c, 1, 1, f)) {
|
2024-04-05 11:54:39 -04:00
|
|
|
if(c == 0xa)
|
|
|
|
*line_++ = 0x00, parse_line(line), line_ = line;
|
|
|
|
else if(line_ - line < 0x400)
|
|
|
|
*line_++ = c;
|
2024-04-04 13:01:09 -04:00
|
|
|
}
|
2024-04-05 11:54:39 -04:00
|
|
|
*line_++ = 0x00, parse_line(line), line_ = line;
|
2024-04-04 12:04:27 -04:00
|
|
|
fclose(f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
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, 3 Apr 2024.\n");
|
2024-04-04 13:07:49 -04:00
|
|
|
parse(argv[1]);
|
2024-04-04 17:55:51 -04:00
|
|
|
print_rules();
|
2024-04-05 12:20:18 -04:00
|
|
|
printf(".... %s\n", prog);
|
2024-04-04 18:10:25 -04:00
|
|
|
while(rewrite())
|
|
|
|
;
|
2024-04-04 14:54:31 -04:00
|
|
|
return 0;
|
2024-04-04 12:04:27 -04:00
|
|
|
}
|