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