(uxnasm) Abstracted writehex

This commit is contained in:
Devine Lu Linvega 2024-03-26 12:40:43 -07:00
parent f1fa4f6ea1
commit a6fb9a22c5
1 changed files with 30 additions and 31 deletions

View File

@ -257,6 +257,25 @@ writeshort(Uint16 s, int lit)
return (lit ? writebyte(findopcode("LIT2")) : 1) && writebyte(s >> 8) && writebyte(s & 0xff); return (lit ? writebyte(findopcode("LIT2")) : 1) && writebyte(s >> 8) && writebyte(s & 0xff);
} }
static int
writehex(char *w)
{
if(w[0] == '#') {
if(sihx(w + 1) && slen(w) == 3)
return writebyte(findopcode("LIT")) && writebyte(shex(w + 1));
else if(sihx(w + 1) && slen(w) == 5)
return writeshort(shex(w + 1), 1);
else
return error_asm("Invalid hex literal");
} else {
if(sihx(w) && slen(w) == 2)
return writebyte(shex(w));
/* raw short */
else if(sihx(w) && slen(w) == 4)
return writeshort(shex(w), 0);
}
}
static int static int
tokenize(FILE *f) tokenize(FILE *f)
{ {
@ -314,18 +333,12 @@ parse(char *w, FILE *f)
} }
break; break;
case '~': /* include */ case '~': /* include */
if(!doinclude(w + 1)) return !doinclude(w + 1) ? error_asm("Invalid include") : 1;
return error_asm("Invalid include");
break;
case '%': /* macro */ case '%': /* macro */
if(!makemacro(w + 1, f)) return !makemacro(w + 1, f) ? error_asm("Invalid macro") : 1;
return error_asm("Invalid macro"); case '$': /* pad relative */
break; case '|': /* pad absolute */
case '$': /* pad-relative */ return !makepad(w) ? error_asm("Invalid padding") : 1;
case '|': /* pad-absolute */
if(!makepad(w))
return error_asm("Invalid padding");
break;
case '@': /* label */ case '@': /* label */
if(!makelabel(w + 1)) if(!makelabel(w + 1))
return error_asm("Invalid label"); return error_asm("Invalid label");
@ -335,17 +348,9 @@ parse(char *w, FILE *f)
scope[i] = '\0'; scope[i] = '\0';
break; break;
case '&': /* sublabel */ case '&': /* sublabel */
if(!makelabel(w)) return !makelabel(w) ? error_asm("Invalid sublabel") : 1;
return error_asm("Invalid sublabel");
break;
case '#': /* literals hex */ case '#': /* literals hex */
if(sihx(w + 1) && slen(w) == 3) return !writehex(w) ? error_asm("Invalid hexadecimal") : 1;
return writebyte(findopcode("LIT")) && writebyte(shex(w + 1));
else if(sihx(w + 1) && slen(w) == 5)
return writeshort(shex(w + 1), 1);
else
return error_asm("Invalid hex literal");
break;
case '_': /* raw byte relative */ case '_': /* raw byte relative */
return addref(w + 1, w[0], p.ptr) && writebyte(0xff); return addref(w + 1, w[0], p.ptr) && writebyte(0xff);
case ',': /* literal byte relative */ case ',': /* literal byte relative */
@ -369,22 +374,16 @@ parse(char *w, FILE *f)
if(!writebyte(c)) return 0; if(!writebyte(c)) return 0;
break; break;
case '}': /* lambda end */ case '}': /* lambda end */
if(!makelabel(makelambda(p.lambda_stack[--p.lambda_ptr]))) return !makelabel(makelambda(p.lambda_stack[--p.lambda_ptr])) ? error_asm("Invalid label") : 1;
return error_asm("Invalid label");
break;
case '[': case '[':
case ']': case ']':
if(slen(w) == 1) break; /* else fallthrough */ if(slen(w) == 1) break; /* else fallthrough */
default: default:
/* opcode */ /* opcode */
if(isopcode(w)) if(isopcode(w)) return writebyte(findopcode(w));
return writebyte(findopcode(w));
/* raw byte */ /* raw byte */
else if(sihx(w) && slen(w) == 2) else if(sihx(w))
return writebyte(shex(w)); return writehex(w);
/* raw short */
else if(sihx(w) && slen(w) == 4)
return writeshort(shex(w), 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++)