From 7c247d718149334554fb02a1e444e48d2c7fcecf Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 4 May 2024 12:34:38 -0700 Subject: [PATCH] Smarter clean up of dirty registers --- src/modal.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/modal.c b/src/modal.c index 66c0dfb..f14f906 100644 --- a/src/modal.c +++ b/src/modal.c @@ -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;