This commit is contained in:
Devine Lu Linvega 2024-04-04 10:01:09 -07:00
parent e109696e7c
commit 22312cf425
1 changed files with 14 additions and 30 deletions

View File

@ -1,18 +1,13 @@
#include <stdio.h>
/* dict */
static char dict[0x8000], *next = dict;
/* rule */
typedef struct {
char *a;
char *b;
char *a, *b;
} Rule;
static int rules_len;
static Rule rules[0x100];
static char dict[0x8000], *next = dict;
static char program[0x1000], *prog_ = program;
static char *
addside(FILE *f)
@ -38,10 +33,6 @@ addrule(FILE *f)
r->a = addside(f), r->b = addside(f);
}
/* program */
static char program[0x1000], *prog_ = program;
static void
addprogram(FILE *f)
{
@ -61,23 +52,6 @@ tokenize(char *t, FILE *f)
addprogram(f);
}
static int
walk(FILE *f)
{
char c, token[0x40], *tokptr;
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;
return 1;
}
static void
display()
{
@ -93,9 +67,19 @@ static int
eval(char *path)
{
FILE *f;
char c, token[0x40], *tokptr;
if(!(f = fopen(path, "r")))
return !printf("Invalid file: %s\n", path);
walk(f);
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;
display();
fclose(f);
return 1;