(uxnasm) Allow returning errors from writebyte().

This commit is contained in:
Andrew Alderwick 2022-02-19 00:26:55 +00:00
parent 0ae6992089
commit da4f0e70a5
1 changed files with 34 additions and 27 deletions

View File

@ -186,37 +186,39 @@ makereference(char *scope, char *label, Uint16 addr)
return 1; return 1;
} }
static void static int
writebyte(Uint8 b) writebyte(Uint8 b)
{ {
if(p.ptr < TRIM) if(p.ptr < TRIM) {
fprintf(stderr, "-- Writing in zero-page: %02x\n", b); fprintf(stderr, "-- Writing in zero-page: %02x\n", b);
return 0;
}
p.data[p.ptr++] = b; p.data[p.ptr++] = b;
p.length = p.ptr; p.length = p.ptr;
litlast = 0; litlast = 0;
return 1;
} }
static void static int
writeshort(Uint16 s, int lit) writeshort(Uint16 s, int lit)
{ {
if(lit) if(lit)
writebyte(findopcode("LIT2")); if(!writebyte(findopcode("LIT2"))) return 0;
writebyte(s >> 8); return writebyte(s >> 8) && writebyte(s & 0xff);
writebyte(s & 0xff);
} }
static void static int
writelitbyte(Uint8 b) writelitbyte(Uint8 b)
{ {
if(litlast) { /* combine literals */ if(litlast) { /* combine literals */
Uint8 hb = p.data[p.ptr - 1]; Uint8 hb = p.data[p.ptr - 1];
p.ptr -= 2; p.ptr -= 2;
writeshort((hb << 8) + b, 1); return writeshort((hb << 8) + b, 1);
return;
} }
writebyte(findopcode("LIT")); if(!writebyte(findopcode("LIT"))) return 0;
writebyte(b); if(!writebyte(b)) return 0;
litlast = 1; litlast = 1;
return 1;
} }
static int static int
@ -288,47 +290,52 @@ parse(char *w, FILE *f)
case '#': /* literals hex */ case '#': /* literals hex */
if(!sihx(w + 1) || (slen(w) != 3 && slen(w) != 5)) if(!sihx(w + 1) || (slen(w) != 3 && slen(w) != 5))
return error("Invalid hex literal", w); return error("Invalid hex literal", w);
if(slen(w) == 3) if(slen(w) == 3) {
writelitbyte(shex(w + 1)); if(!writelitbyte(shex(w + 1))) return 0;
else if(slen(w) == 5) }
writeshort(shex(w + 1), 1); else if(slen(w) == 5) {
if(!writeshort(shex(w + 1), 1)) return 0;
}
break; break;
case '.': /* literal byte zero-page */ case '.': /* literal byte zero-page */
makereference(p.scope, w, p.ptr - litlast); makereference(p.scope, w, p.ptr - litlast);
writelitbyte(0xff); if(!writelitbyte(0xff)) return 0;
break; break;
case ',': /* literal byte relative */ case ',': /* literal byte relative */
makereference(p.scope, w, p.ptr - litlast); makereference(p.scope, w, p.ptr - litlast);
writelitbyte(0xff); if(!writelitbyte(0xff)) return 0;
break; break;
case ';': /* literal short absolute */ case ';': /* literal short absolute */
makereference(p.scope, w, p.ptr); makereference(p.scope, w, p.ptr);
writeshort(0xffff, 1); if(!writeshort(0xffff, 1)) return 0;
break; break;
case ':': /* raw short absolute */ case ':': /* raw short absolute */
makereference(p.scope, w, p.ptr); makereference(p.scope, w, p.ptr);
writeshort(0xffff, 0); if(!writeshort(0xffff, 0)) return 0;
break; break;
case '\'': /* raw char */ case '\'': /* raw char */
writebyte((Uint8)w[1]); if(!writebyte((Uint8)w[1])) return 0;
break; break;
case '"': /* raw string */ case '"': /* raw string */
i = 0; i = 0;
while((c = w[++i])) while((c = w[++i]))
writebyte(c); if(!writebyte(c)) return 0;
break; break;
case '[': break; /* ignored */ case '[': break; /* ignored */
case ']': break; /* ignored */ case ']': break; /* ignored */
default: default:
/* opcode */ /* opcode */
if(findopcode(w) || scmp(w, "BRK", 4)) if(findopcode(w) || scmp(w, "BRK", 4)) {
writebyte(findopcode(w)); if(!writebyte(findopcode(w))) return 0;
}
/* raw byte */ /* raw byte */
else if(sihx(w) && slen(w) == 2) else if(sihx(w) && slen(w) == 2) {
writebyte(shex(w)); if(!writebyte(shex(w))) return 0;
}
/* raw short */ /* raw short */
else if(sihx(w) && slen(w) == 4) else if(sihx(w) && slen(w) == 4) {
writeshort(shex(w), 0); if(!writeshort(shex(w), 0)) return 0;
}
/* macro */ /* macro */
else if((m = findmacro(w))) { else if((m = findmacro(w))) {
for(i = 0; i < m->len; i++) for(i = 0; i < m->len; i++)