Handle empty register writes at the register level

This commit is contained in:
Devine Lu Linvega 2024-05-24 18:56:06 -07:00
parent 13ec84d7ef
commit 89f393e349
1 changed files with 8 additions and 14 deletions

View File

@ -123,16 +123,17 @@ file_import(char *path, char *ptr)
return copy("NAF", ptr, 3);
}
static int
static void
write_reg(char r, char *reg)
{
char *origin = dst_;
switch(r) {
case ':': device_write(reg); break;
case '~': {
unsigned char c;
char *origin = dst_;
while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c;
if(feof(stdin)) dst_ = copy("EOF", dst_, 3);
if(origin == dst_) dst_--;
break;
}
case '_': {
@ -172,7 +173,6 @@ write_reg(char r, char *reg)
}
default: dst_ = copy(reg, dst_, walk(reg) - reg);
}
return dst_ - origin;
}
static int
@ -193,7 +193,7 @@ static int
apply_rule(Rule *r, char *s)
{
unsigned char rid;
char c, *a = r->a, *b = r->b, *origin = dst_, *reg, last = 0;
char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
/* phase: clean regs */
while(stack_ != stack) regs[(int)*(--stack_)] = 0;
/* phase: match rule */
@ -215,16 +215,10 @@ apply_rule(Rule *r, char *s)
if(!spacer(c)) return 0;
/* phase: write rule */
while((c = *b++))
if(c == '?' && (rid = *b) && (reg = regs[rid])) {
b++;
if(!write_reg(rid, reg)) {
if(*b == ' ')
b++;
else if(last == ' ')
dst_--;
}
} else
*dst_++ = last = c;
if(c == '?' && (rid = *b) && (reg = regs[rid]))
write_reg(rid, reg), b++;
else
*dst_++ = c;
if(dst_ == origin) {
while(*s == ' ') s++;
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;