Converted regs array to stack

This commit is contained in:
Devine Lu Linvega 2024-04-23 19:36:49 -07:00
parent f18adb503d
commit 8bb661ced0
1 changed files with 22 additions and 20 deletions

View File

@ -5,14 +5,18 @@ typedef struct {
char *a, *b;
} Rule;
static int flip, quiet, rmin = 0xff, rmax = 0x00, cycles = 0x10000;
static int flip, quiet, cycles = 0x10000;
static Rule rules[0x1000], *rules_ = rules, lambda;
static char dict[0x8000], *dict_ = dict;
static char bank_a[0x4000], *src_ = bank_a;
static char bank_b[0x4000], *dst_ = bank_b;
static char *regs[0x100];
/* clang-format off */
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
#define get_reg(x) { regid = x; for(i = 0; i < regs_ptr; i++) if(regs_key[i] == regid) { reg = regs_val[i]; break; } }
/* clang-format on */
static char *
walk(char *s)
@ -107,26 +111,18 @@ write_rule(Rule *r, char *s, int create)
static int
apply_rule(Rule *r, char *s)
{
int i;
char c, *a = r->a;
if(rmax) {
for(i = rmin; i <= rmax; i++)
regs[i] = 0;
rmin = 0xff, rmax = 0x00;
}
int i, regid, regs_ptr = 0;
char c, *a = r->a, regs_key[0x10], *regs_val[0x10];
while((c = *a)) { /* phase: match rule */
if(c == '?') {
int regid = (int)*(++a);
char *pcap = walk(s), *reg = regs[regid];
char *pcap = walk(s), *reg = NULL;
get_reg(*(++a));
if(reg) { /* reg cmp */
char *rcap = walk(reg), *pp = s;
while(reg < rcap || pp < pcap)
if(*reg++ != *pp++) return 0;
} else { /* reg set */
regs[regid] = s;
if(regid < rmin) rmin = regid;
if(regid > rmax) rmax = regid;
}
} else /* reg set */
regs_key[regs_ptr] = regid, regs_val[regs_ptr++] = s;
a++, s = pcap;
if(!spacer(*a))
while((c = *a) && !spacer(c)) a++;
@ -136,12 +132,18 @@ apply_rule(Rule *r, char *s)
a++, s++;
}
if(!(c = *s) || spacer(c)) { /* phase: write rule */
char *b = r->b, *reg, *origin = dst_;
while((c = *b++))
if(c == '?' && (reg = regs[(int)*b])) {
if(!write_reg(*b++, reg) && dst_ != origin) dst_--;
char *b = r->b, *origin = dst_;
while((c = *b++)) {
if(c == '?') {
char *reg = NULL;
get_reg(*b);
if(reg) {
if(!write_reg(*b++, reg) && dst_ != origin) dst_--;
} else
*dst_++ = c;
} else
*dst_++ = c;
}
if(dst_ == origin) {
while(*s == ' ') s++;
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;