Smarter clean up of dirty registers

This commit is contained in:
Devine Lu Linvega 2024-05-04 12:34:38 -07:00
parent 19d03dc116
commit 7c247d7181
1 changed files with 6 additions and 14 deletions

View File

@ -5,13 +5,12 @@ typedef struct {
char *a, *b;
} Rule;
static unsigned char rmin = 0xff, rmax = 0x00;
static int flip, quiet, debug, cycles = 0x10000;
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;
static char *regs[0x100];
static char *regs[0x100], stack[0x10], *stack_ = stack;
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
@ -148,14 +147,10 @@ write_tail(char *s)
static int
apply_rule(Rule *r, char *s)
{
unsigned char i, rid;
unsigned char rid;
char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
/* phase: clean regs */
if(rmax) {
for(i = 0; i <= rmax; i++)
regs[i] = 0;
rmin = 0xff, rmax = 0x00;
}
while(stack_ != stack) regs[(int)*(--stack_)] = 0;
/* phase: match rule */
while((c = *a++)) {
if(c == '?') {
@ -165,11 +160,8 @@ apply_rule(Rule *r, char *s)
char *rcap = walk(reg), *pp = s;
while(reg < rcap || pp < pcap)
if(*reg++ != *pp++) return 0;
} else { /* reg set */
regs[rid] = s;
if(rid < rmin) rmin = rid;
if(rid > rmax) rmax = rid;
}
} else /* reg set */
regs[rid] = s, *stack_++ = rid;
s = pcap;
} else if(c != *s++)
return 0;
@ -294,7 +286,7 @@ main(int argc, char **argv)
return !printf("usage: modal [-vqn] source.modal\n");
for(i = 1; i < argc && *argv[i] == '-'; i++) {
switch(argv[i][1]) {
case 'v': /* version */ return !printf("Modal Interpreter, 2 May 2024.\n");
case 'v': /* version */ return !printf("Modal Interpreter, 4 May 2024.\n");
case 'q': /* quiet */ quiet = 1; break;
case 'p': /* debug */ debug = 1; break;
case 'n': /* infinite */ cycles = 0xffffffff; break;