Use stored pointer for src_

This commit is contained in:
Devine Lu Linvega 2024-04-14 19:36:16 -07:00
parent 8705ebebc6
commit 4205cc8792
1 changed files with 21 additions and 21 deletions

View File

@ -5,11 +5,11 @@ typedef struct {
char *a, *b; char *a, *b;
} Rule; } Rule;
static int dst; static int flip;
static Rule rules[0x1000], lambda, *rules_ = rules; static Rule rules[0x1000], lambda, *rules_ = rules;
static char dict[0x8000], *dict_ = dict; static char dict[0x8000], *dict_ = dict;
static char bank_a[0x4000], *prog_ = bank_a; static char bank_a[0x4000], *src_ = bank_a;
static char bank_b[0x4000], *outp_ = bank_b; static char bank_b[0x4000], *dst_ = bank_b;
static char *regs[0x100]; static char *regs[0x100];
#define spacer(c) (c <= ' ' || c == '(' || c == ')') #define spacer(c) (c <= ' ' || c == '(' || c == ')')
@ -47,20 +47,20 @@ put_reg(char r)
{ {
char c, *s = regs[(int)r], *ss; char c, *s = regs[(int)r], *ss;
if(!s) { if(!s) {
*outp_++ = '?', *outp_++ = r; *dst_++ = '?', *dst_++ = r;
return; return;
} }
ss = walk(s); ss = walk(s);
if(r == '*') { if(r == '*') {
if(*s == '(') { /* special implode */ if(*s == '(') { /* special implode */
while(s < ss && (c = *s++)) while(s < ss && (c = *s++))
if(!spacer(c)) *outp_++ = c; if(!spacer(c)) *dst_++ = c;
} else { /* special explode */ } else { /* special explode */
int i, depth = 0; int i, depth = 0;
while((c = *s++) && !spacer(c)) while((c = *s++) && !spacer(c))
*outp_++ = c, *outp_++ = ' ', *outp_++ = '(', depth++; *dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
for(i = 0; i < depth; i++) for(i = 0; i < depth; i++)
*outp_++ = ')'; *dst_++ = ')';
} }
} else if(r == ':') { /* special stdout */ } else if(r == ':') { /* special stdout */
if(*s == '(') s++, --ss; if(*s == '(') s++, --ss;
@ -77,9 +77,9 @@ put_reg(char r)
} }
} else if(r == '~') { /* special stdin */ } else if(r == '~') { /* special stdin */
while(fread(&c, 1, 1, stdin) && c >= ' ') while(fread(&c, 1, 1, stdin) && c >= ' ')
*outp_++ = c; *dst_++ = c;
} else } else
while(s < ss) *outp_++ = *s++; while(s < ss) *dst_++ = *s++;
} }
static char * static char *
@ -105,30 +105,30 @@ match_rule(Rule *r, char *p)
static int static int
commit_rule(Rule *r, char *s, int create) commit_rule(Rule *r, char *s, int create)
{ {
while((*outp_++ = *s++)) while((*dst_++ = *s++))
; ;
*outp_++ = 0; *dst_++ = 0;
if((dst = !dst)) if((flip = !flip))
prog_ = bank_b, outp_ = bank_a; src_ = bank_b, dst_ = bank_a;
else else
prog_ = bank_a, outp_ = bank_b; src_ = bank_a, dst_ = bank_b;
if(create) if(create)
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b); fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
else else
fprintf(stderr, "%02d %s\n", r->id, prog_); fprintf(stderr, "%02d %s\n", r->id, src_);
return 1; return 1;
} }
static int static int
write_rule(Rule *r, char last, char *res) write_rule(Rule *r, char last, char *res)
{ {
char c, *b = r->b, *origin = outp_; char c, *b = r->b, *origin = dst_;
while((c = *b++)) while((c = *b++))
if(spacer(last) && c == '?') if(spacer(last) && c == '?')
put_reg(*b++); put_reg(*b++);
else else
*outp_++ = c, last = c; *dst_++ = c, last = c;
if(outp_ - origin == 0) if(dst_ - origin == 0)
while(*res == ' ') res++; while(*res == ' ') res++;
return commit_rule(r, res, 0); return commit_rule(r, res, 0);
} }
@ -160,7 +160,7 @@ create_rule(Rule *r, int id, char *s)
static int static int
rewrite(void) rewrite(void)
{ {
char c, last = 0, *cap, *s = dst ? bank_b : bank_a, *res; char c, last = 0, *cap, *s = src_, *res;
while((c = *s) && c <= ' ') s++; while((c = *s) && c <= ' ') s++;
while((c = *s)) { while((c = *s)) {
if(spacer(last)) { if(spacer(last)) {
@ -182,10 +182,10 @@ rewrite(void)
if((res = match_rule(r, s)) != NULL) if((res = match_rule(r, s)) != NULL)
return write_rule(r, last, res); return write_rule(r, last, res);
} }
*outp_++ = last = c; *dst_++ = last = c;
s++; s++;
} }
*outp_++ = 0; *dst_++ = 0;
return 0; return 0;
} }