Abstracted create_rule

This commit is contained in:
Devine Lu Linvega 2024-04-11 08:53:49 -07:00
parent 528331b35a
commit 5f70f09ce3
1 changed files with 14 additions and 8 deletions

View File

@ -6,7 +6,7 @@ typedef struct {
} Rule;
static int direction;
static Rule rules[0x1000], *rules_ = rules;
static Rule rules[0x1000], lambda, *rules_ = rules;
static char dict[0x8000], *dict_ = dict;
static char bank_a[0x4000], *prog_ = bank_a;
static char bank_b[0x4000], *outp_ = bank_b;
@ -155,6 +155,16 @@ write(Rule *r, char last, char *res)
return commit_rule(r, res, 0);
}
static char *
create_rule(Rule *r, int id, char *s)
{
char c;
r->id = id, s += 2;
r->a = dict_, s = parse_frag(s), r->b = dict_, s = parse_frag(s);
while((c = *s) && c <= ' ') s++;
return s;
}
static int
rewrite(void)
{
@ -165,16 +175,12 @@ rewrite(void)
Rule *r;
if(p[0] == '<' && p[1] == '>') {
r = rules_++;
r->id = rules_ - rules - 1;
p += 2, r->a = dict_, p = parse_frag(p), r->b = dict_, p = parse_frag(p);
p = create_rule(r, rules_ - rules - 1, p);
return commit_rule(r, p, 1);
}
if(p[0] == '?' && p[1] == '(') {
Rule lambda;
lambda.id = -1;
p += 2, lambda.a = dict_, p = parse_frag(p), lambda.b = dict_, p = parse_frag(p);
p++;
while((c = *p) && c <= ' ') p++;
r = &lambda;
p = create_rule(&lambda, -1, p) + 1;
if((res = match_rule(&lambda, p)) != NULL)
return write(&lambda, last, res);
}