2024-04-04 12:04:27 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-04-04 12:29:36 -04:00
|
|
|
/* dict */
|
|
|
|
|
|
|
|
static char dict[0x8000], *next = dict;
|
|
|
|
|
|
|
|
static char *
|
|
|
|
save(char *s, char c)
|
|
|
|
{
|
|
|
|
char *o = next;
|
|
|
|
while((*next++ = *s++) && *s)
|
|
|
|
;
|
|
|
|
*next++ = c;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
push(char *s, char c)
|
|
|
|
{
|
|
|
|
char *d = dict;
|
|
|
|
for(d = dict; d < next; d++) {
|
|
|
|
char *ss = s, *dd = d, a, b;
|
|
|
|
while((a = *dd++) == (b = *ss++))
|
|
|
|
if(!a && !b) return d;
|
|
|
|
}
|
|
|
|
return save(s, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rule */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *a;
|
|
|
|
char *b;
|
|
|
|
} Rule;
|
|
|
|
|
|
|
|
static int rules_len;
|
|
|
|
static Rule rules[0x100];
|
|
|
|
|
|
|
|
static void
|
|
|
|
addrule(FILE *f)
|
|
|
|
{
|
2024-04-04 12:50:17 -04:00
|
|
|
int depth = 0;
|
|
|
|
char c, *left, *right;
|
|
|
|
left = next;
|
|
|
|
while(f && fread(&c, 1, 1, f) && c && c != 0xa) {
|
|
|
|
if(c == '(') depth++;
|
|
|
|
if(c == ')') --depth;
|
|
|
|
if(c == ' ' && !depth)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
*next++ = c;
|
|
|
|
}
|
|
|
|
*next++ = 0;
|
|
|
|
|
|
|
|
right = next;
|
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
|
|
|
|
*next++ = c;
|
2024-04-04 12:29:36 -04:00
|
|
|
}
|
2024-04-04 12:50:17 -04:00
|
|
|
*next++ = 0;
|
|
|
|
|
|
|
|
printf("rule: %s -> %s\n", left, right);
|
2024-04-04 12:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* program */
|
|
|
|
|
2024-04-04 12:50:17 -04:00
|
|
|
static char program[0x1000], *prog_ = program;
|
2024-04-04 12:29:36 -04:00
|
|
|
|
|
|
|
static void
|
2024-04-04 12:50:17 -04:00
|
|
|
addprogram(FILE *f)
|
|
|
|
{
|
2024-04-04 12:29:36 -04:00
|
|
|
char c;
|
2024-04-04 12:50:17 -04:00
|
|
|
while(f && fread(&c, 1, 1, f) && c && c != 0xa)
|
|
|
|
*prog_++ = c;
|
|
|
|
*prog_++ = 0x20;
|
2024-04-04 12:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tokenize(char *t, FILE *f)
|
|
|
|
{
|
|
|
|
if(t[0] == '<' && t[1] == '>') {
|
|
|
|
addrule(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addprogram(f);
|
|
|
|
}
|
|
|
|
|
2024-04-04 12:04:27 -04:00
|
|
|
static int
|
|
|
|
walk(FILE *f)
|
|
|
|
{
|
2024-04-04 12:29:36 -04:00
|
|
|
char c, token[0x40], *tokptr;
|
|
|
|
tokptr = token;
|
2024-04-04 12:04:27 -04:00
|
|
|
while(f && fread(&c, 1, 1, f)) {
|
2024-04-04 12:29:36 -04:00
|
|
|
if(c < 0x21)
|
|
|
|
*tokptr++ = 0x00, tokenize(token, f), tokptr = token;
|
|
|
|
else if(tokptr - token < 0x3f)
|
|
|
|
*tokptr++ = c;
|
2024-04-04 12:04:27 -04:00
|
|
|
else
|
|
|
|
return printf("Token too long: %s\n", token);
|
|
|
|
}
|
2024-04-04 12:29:36 -04:00
|
|
|
*tokptr++ = 0x00, tokenize(token, f), tokptr = token;
|
2024-04-04 12:04:27 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-04-04 12:50:17 -04:00
|
|
|
static void
|
|
|
|
display(Rule *r, char *p)
|
|
|
|
{
|
|
|
|
printf("Program: %s\n", p);
|
|
|
|
}
|
|
|
|
|
2024-04-04 12:04:27 -04:00
|
|
|
static int
|
|
|
|
eval(char *path)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
if(!(f = fopen(path, "r")))
|
|
|
|
return !printf("Invalid file: %s\n", path);
|
|
|
|
walk(f);
|
2024-04-04 12:50:17 -04:00
|
|
|
display(rules, program);
|
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");
|
|
|
|
return !eval(argv[1]);
|
|
|
|
}
|