Improved catching of empty registers

This commit is contained in:
Devine Lu Linvega 2024-05-24 18:34:43 -07:00
parent 9780e2aad7
commit 6f5b5fc36c
1 changed files with 17 additions and 17 deletions

View File

@ -123,35 +123,36 @@ file_import(char *path, char *ptr)
return copy("NAF", ptr, 3); return copy("NAF", ptr, 3);
} }
static void static int
write_reg(char r, char *reg) write_reg(char r, char *reg)
{ {
char *origin = dst_;
switch(r) { switch(r) {
case ':': device_write(reg); return; case ':': device_write(reg); break;
case '~': { case '~': {
unsigned char c; unsigned char c;
while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c; while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c;
if(feof(stdin)) dst_ = copy("EOF", dst_, 3); if(feof(stdin)) dst_ = copy("EOF", dst_, 3);
return; break;
} }
case '_': { case '_': {
char filepath[0x80]; char filepath[0x80];
copy(reg, filepath, walk(reg) - reg); copy(reg, filepath, walk(reg) - reg);
dst_ = file_import(filepath, dst_); dst_ = file_import(filepath, dst_);
return; break;
} }
case '^': { /* op: join */ case '^': { /* op: join */
char c, *cap = walk(reg); char c, *cap = walk(reg);
if(*reg == '(') reg++, --cap; if(*reg == '(') reg++, --cap;
while(reg < cap && (c = *reg++)) while(reg < cap && (c = *reg++))
if(!spacer(c)) *dst_++ = c; if(!spacer(c)) *dst_++ = c;
return; break;
} }
case '.': { /* op: unwrap */ case '.': { /* op: unwrap */
char *cap = walk(reg); char *cap = walk(reg);
if(*reg == '(') reg++, --cap; if(*reg == '(') reg++, --cap;
dst_ = copy(reg, dst_, cap - reg); dst_ = copy(reg, dst_, cap - reg);
return; break;
} }
case '*': { /* op: explode */ case '*': { /* op: explode */
int i, depth = 0; int i, depth = 0;
@ -167,13 +168,11 @@ write_reg(char r, char *reg)
while((c = *reg++) && !spacer(c)) while((c = *reg++) && !spacer(c))
*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++; *dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
for(i = 0; i < depth; i++) *dst_++ = ')'; for(i = 0; i < depth; i++) *dst_++ = ')';
return; break;
}
default: {
dst_ = copy(reg, dst_, walk(reg) - reg);
return;
} }
default: dst_ = copy(reg, dst_, walk(reg) - reg);
} }
return dst_ - origin;
} }
static int static int
@ -216,12 +215,13 @@ apply_rule(Rule *r, char *s)
if(!spacer(c)) return 0; if(!spacer(c)) return 0;
/* phase: write rule */ /* phase: write rule */
while((c = *b++)) while((c = *b++))
if(c == '?' && (rid = *b) && (reg = regs[rid])){ if(c == '?' && (rid = *b) && (reg = regs[rid])) {
char *ori = dst_; b++;
write_reg(rid, reg), b++; if(!write_reg(rid, reg)) {
if(dst_ == ori){ if(*b == ' ')
if(b[0] == ' ') b++; b++;
else if(last == ' ') dst_--; else if(last == ' ')
dst_--;
} }
} else } else
*dst_++ = last = c; *dst_++ = last = c;