This commit is contained in:
parent
22312cf425
commit
22229bdbb9
61
src/modal.c
61
src/modal.c
|
@ -9,8 +9,25 @@ static Rule rules[0x100];
|
|||
static char dict[0x8000], *next = dict;
|
||||
static char program[0x1000], *prog_ = program;
|
||||
|
||||
static int
|
||||
reduce(void)
|
||||
{
|
||||
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);
|
||||
}
|
||||
printf("Program: %s\n", program);
|
||||
}
|
||||
|
||||
static char *
|
||||
addside(FILE *f)
|
||||
parse_rulefrag(FILE *f)
|
||||
{
|
||||
int depth = 0;
|
||||
char c, *origin = next;
|
||||
|
@ -27,44 +44,21 @@ addside(FILE *f)
|
|||
}
|
||||
|
||||
static void
|
||||
addrule(FILE *f)
|
||||
{
|
||||
Rule *r = &rules[rules_len++];
|
||||
r->a = addside(f), r->b = addside(f);
|
||||
}
|
||||
|
||||
static void
|
||||
addprogram(FILE *f)
|
||||
tokenize(char *t, FILE *f)
|
||||
{
|
||||
char c;
|
||||
if(t[0] == '<' && t[1] == '>') {
|
||||
Rule *r = &rules[rules_len++];
|
||||
r->a = parse_rulefrag(f), r->b = parse_rulefrag(f);
|
||||
return;
|
||||
}
|
||||
while(f && fread(&c, 1, 1, f) && c && c != 0xa)
|
||||
*prog_++ = c;
|
||||
*prog_++ = 0x20;
|
||||
}
|
||||
|
||||
static void
|
||||
tokenize(char *t, FILE *f)
|
||||
{
|
||||
if(t[0] == '<' && t[1] == '>') {
|
||||
addrule(f);
|
||||
return;
|
||||
}
|
||||
addprogram(f);
|
||||
}
|
||||
|
||||
static void
|
||||
display()
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < rules_len; i++) {
|
||||
Rule *r = &rules[i];
|
||||
printf("Rule #%d: %s -> %s\n", i, r->a, r->b);
|
||||
}
|
||||
printf("Program: %s\n", program);
|
||||
}
|
||||
|
||||
static int
|
||||
eval(char *path)
|
||||
parse(char *path)
|
||||
{
|
||||
FILE *f;
|
||||
char c, token[0x40], *tokptr;
|
||||
|
@ -80,7 +74,6 @@ eval(char *path)
|
|||
return printf("Token too long: %s\n", token);
|
||||
}
|
||||
*tokptr++ = 0x00, tokenize(token, f), tokptr = token;
|
||||
display();
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
|
@ -92,5 +85,7 @@ main(int argc, char **argv)
|
|||
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]);
|
||||
parse(argv[1]);
|
||||
display();
|
||||
return !reduce();
|
||||
}
|
|
@ -2,4 +2,4 @@
|
|||
<> hello bye
|
||||
<> (a (b (c))) (end)
|
||||
|
||||
(dup hello)
|
||||
hello world
|
Loading…
Reference in New Issue