Improved rule packing
This commit is contained in:
parent
69c2d1ec67
commit
e109696e7c
77
src/modal.c
77
src/modal.c
|
@ -4,28 +4,6 @@
|
|||
|
||||
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 */
|
||||
|
||||
typedef struct {
|
||||
|
@ -36,34 +14,28 @@ typedef struct {
|
|||
static int rules_len;
|
||||
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
|
||||
addrule(FILE *f)
|
||||
{
|
||||
int depth = 0;
|
||||
char c, *left, *right;
|
||||
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);
|
||||
Rule *r = &rules[rules_len++];
|
||||
r->a = addside(f), r->b = addside(f);
|
||||
}
|
||||
|
||||
/* program */
|
||||
|
@ -107,9 +79,14 @@ walk(FILE *f)
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -119,7 +96,7 @@ eval(char *path)
|
|||
if(!(f = fopen(path, "r")))
|
||||
return !printf("Invalid file: %s\n", path);
|
||||
walk(f);
|
||||
display(rules, program);
|
||||
display();
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue