Tokenizing rules

This commit is contained in:
Devine Lu Linvega 2024-04-04 09:50:17 -07:00
parent 4cfda08fe9
commit 69c2d1ec67
2 changed files with 37 additions and 14 deletions

View File

@ -39,30 +39,46 @@ static Rule rules[0x100];
static void static void
addrule(FILE *f) addrule(FILE *f)
{ {
char c; int depth = 0;
printf("Add rule: "); char c, *left, *right;
left = next;
while(f && fread(&c, 1, 1, f) && c && c != 0xa) { while(f && fread(&c, 1, 1, f) && c && c != 0xa) {
printf("%c", c); if(c == '(') depth++;
if(c == ')') --depth;
if(c == ' ' && !depth)
break;
else
*next++ = c;
} }
printf("\n"); *next++ = 0;
right = 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;
printf("rule: %s -> %s\n", left, right);
} }
/* program */ /* program */
static char program[0x1000]; static char program[0x1000], *prog_ = program;
static void static void
addprogram(FILE *f){ addprogram(FILE *f)
{
char c; char c;
printf("Add program: "); while(f && fread(&c, 1, 1, f) && c && c != 0xa)
while(f && fread(&c, 1, 1, f) && c && c != 0xa) { *prog_++ = c;
printf("%c", c); *prog_++ = 0x20;
}
printf("\n");
} }
static int phase;
static void static void
tokenize(char *t, FILE *f) tokenize(char *t, FILE *f)
{ {
@ -90,6 +106,12 @@ walk(FILE *f)
return 1; return 1;
} }
static void
display(Rule *r, char *p)
{
printf("Program: %s\n", p);
}
static int static int
eval(char *path) eval(char *path)
{ {
@ -97,7 +119,7 @@ eval(char *path)
if(!(f = fopen(path, "r"))) if(!(f = fopen(path, "r")))
return !printf("Invalid file: %s\n", path); return !printf("Invalid file: %s\n", path);
walk(f); walk(f);
display(rules, program);
fclose(f); fclose(f);
return 1; return 1;
} }

View File

@ -1,4 +1,5 @@
<> (dup ?x) (?x ?x done) <> (dup ?x) (?x ?x done)
<> hello bye <> hello bye
<> (a (b (c))) (end)
(dup hello) (dup hello)