modal/src/modal.c

130 lines
2.3 KiB
C
Raw Normal View History

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];
static char *
walk(char *s)
{
2024-04-04 14:35:58 -04:00
int depth = 0;
2024-04-04 13:59:00 -04:00
char c;
2024-04-04 14:35:58 -04:00
while((c = *s++)) {
if(c == '(') depth++;
if(c == ')') --depth;
if(c == ' ' && !depth)
break;
}
return s - 1;
2024-04-04 13:59:00 -04:00
}
2024-04-04 13:30:31 -04:00
static int
match(char *p, Rule *r)
{
char c, *a = r->a, *b = p;
while((c = *a)) {
2024-04-04 14:35:58 -04:00
if(c == '?') regs[(int)*(++a)] = b, a++, b = walk(b), c = *b;
2024-04-04 13:30:31 -04:00
if(c != *b) return 0;
a++, b++;
}
printf("> found: %s = %s, rule: %s\n", p, r->a, r->b);
return 1;
}
2024-04-04 12:29:36 -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)) {
int i;
for(i = 0; i < rules_len; i++) {
Rule *r = &rules[i];
match(p, r);
}
2024-04-04 14:54:31 -04:00
*outp_++ = c;
2024-04-04 13:30:31 -04:00
p++;
}
2024-04-04 13:07:49 -04:00
return 1;
}
static void
display(void)
{
int i;
for(i = 0; i < rules_len; i++) {
Rule *r = &rules[i];
printf("Rule #%d: %s -> %s\n", i, r->a, r->b);
}
2024-04-04 14:54:31 -04:00
printf("\nEval: %s -- %s\n", prog, outp);
2024-04-04 13:07:49 -04:00
}
2024-04-04 12:56:55 -04:00
static char *
2024-04-04 13:07:49 -04:00
parse_rulefrag(FILE *f)
2024-04-04 12:29:36 -04:00
{
2024-04-04 12:50:17 -04:00
int depth = 0;
2024-04-04 14:54:31 -04:00
char c, *origin = dict_;
2024-04-04 12:29:36 -04:00
while(f && fread(&c, 1, 1, f) && c && c != 0xa) {
2024-04-04 12:50:17 -04:00
if(c == '(') depth++;
if(c == ')') --depth;
if(c == ' ' && !depth)
break;
else
2024-04-04 14:54:31 -04:00
*dict_++ = c;
2024-04-04 12:29:36 -04:00
}
2024-04-04 14:54:31 -04:00
*dict_++ = 0;
2024-04-04 12:56:55 -04:00
return origin;
}
2024-04-04 12:50:17 -04:00
2024-04-04 12:29:36 -04:00
static void
tokenize(char *t, FILE *f)
{
2024-04-04 13:07:49 -04:00
char c;
2024-04-04 12:29:36 -04:00
if(t[0] == '<' && t[1] == '>') {
2024-04-04 13:07:49 -04:00
Rule *r = &rules[rules_len++];
r->a = parse_rulefrag(f), r->b = parse_rulefrag(f);
2024-04-04 12:29:36 -04:00
return;
}
2024-04-04 14:38:39 -04:00
while(f && fread(&c, 1, 1, f) && c)
*prog_++ = c == 0xa ? 0x20 : c;
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-04 13:01:09 -04:00
char c, token[0x40], *tokptr;
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
tokptr = token;
while(f && fread(&c, 1, 1, f)) {
if(c < 0x21)
*tokptr++ = 0x00, tokenize(token, f), tokptr = token;
else if(tokptr - token < 0x3f)
*tokptr++ = c;
else
return printf("Token too long: %s\n", token);
}
*tokptr++ = 0x00, tokenize(token, f), tokptr = token;
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 14:54:31 -04:00
rewrite();
2024-04-04 13:07:49 -04:00
display();
2024-04-04 14:54:31 -04:00
return 0;
2024-04-04 12:04:27 -04:00
}