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