This commit is contained in:
Devine Lu Linvega 2024-04-25 12:17:31 -07:00
parent f28444f60e
commit 88b5c527f4
1 changed files with 9 additions and 22 deletions

View File

@ -6,7 +6,7 @@ typedef struct {
} Rule;
static int flip, quiet, cycles = 0x10000;
static Rule rules[0x1000], *rules_ = rules;
static Rule rules[0x1000], *rules_ = rules, lambda;
static char dict[0x8000], *dict_ = dict, empty;
static char bank_a[0x4000], *src_ = bank_a;
static char bank_b[0x4000], *dst_ = bank_b;
@ -102,11 +102,11 @@ write_rule(Rule *r, char *s, int create)
return 1;
}
static char *
match_rule(Rule *r, char **regs, char *s)
static int
apply_rule(Rule *r, char **regs, char *s)
{
unsigned int i, id;
char c, *a = r->a, *reg;
char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
/* phase: clean registers */
for(i = 0; i < r->ptr; i++)
regs[i] = NULL;
@ -118,7 +118,7 @@ match_rule(Rule *r, char **regs, char *s)
if((reg = regs[id])) { /* reg cmp */
char *rcap = walk(reg), *pp = s;
while(reg < rcap || pp < pcap)
if(*reg++ != *pp++) return NULL;
if(*reg++ != *pp++) return 0;
} else /* reg set */
regs[id] = s;
c = *a++, s = pcap;
@ -126,16 +126,8 @@ match_rule(Rule *r, char **regs, char *s)
while((c = *a) && !spacer(c)) a++;
continue;
}
if(c != *s++) return NULL;
if(c != *s++) return 0;
}
return s;
}
static int
apply_rule(Rule *r, char **regs, char *s)
{
unsigned int id;
char c, *b = r->b, *origin = dst_, *reg;
/* phase: write rule */
while((c = *b++)) {
if(c == '?') {
@ -203,7 +195,7 @@ compile_rule(Rule *r, int id, char *src)
static int
rewrite(void)
{
char c, last = 0, *cap, *s = src_, *regs[0x08], *res = NULL;
char c, last = 0, *cap, *s = src_, *regs[0x08];
while(*s == ' ') s++;
while((c = *s)) {
if(spacer(last)) {
@ -217,18 +209,13 @@ rewrite(void)
}
/* phase: lambda */
if(c == '?' && s[1] == '(') {
Rule lambda;
cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap;
while(*s == ' ') s++;
if((res = match_rule(&lambda, regs, s)))
return apply_rule(&lambda, regs, res);
return apply_rule(&lambda, regs, s);
}
/* phase: match */
for(r = rules; r < rules_; r++)
if((res = match_rule(r, regs, s))) break;
/* phase: apply */
if(res)
return apply_rule(r, regs, res);
if(apply_rule(r, regs, s)) return 1;
}
*dst_++ = last = c;
s++;