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

View File

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