Improved rule packing

This commit is contained in:
Devine Lu Linvega 2024-04-04 09:56:55 -07:00
parent 69c2d1ec67
commit e109696e7c
1 changed files with 27 additions and 50 deletions

View File

@ -4,28 +4,6 @@
static char dict[0x8000], *next = 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 */ /* rule */
typedef struct { typedef struct {
@ -36,34 +14,28 @@ typedef struct {
static int rules_len; static int rules_len;
static Rule rules[0x100]; static Rule rules[0x100];
static char *
addside(FILE *f)
{
int depth = 0;
char c, *origin = 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;
return origin;
}
static void static void
addrule(FILE *f) addrule(FILE *f)
{ {
int depth = 0; Rule *r = &rules[rules_len++];
char c, *left, *right; r->a = addside(f), r->b = addside(f);
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;
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 */
@ -107,9 +79,14 @@ walk(FILE *f)
} }
static void static void
display(Rule *r, char *p) display()
{ {
printf("Program: %s\n", p); 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 static int
@ -119,7 +96,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); display();
fclose(f); fclose(f);
return 1; return 1;
} }