(uxnasm) Walk macro

This commit is contained in:
Devine Lu Linvega 2024-03-27 10:18:06 -07:00
parent fe4103cadb
commit 7760593a88
1 changed files with 25 additions and 16 deletions

View File

@ -19,7 +19,7 @@ typedef signed char Sint8;
typedef unsigned short Uint16; typedef unsigned short Uint16;
typedef struct { typedef struct {
char *name, items[0x40][0x40]; char *name, content[0x80];
Uint8 len; Uint8 len;
} Macro; } Macro;
@ -156,7 +156,7 @@ makemacro(char *name, FILE *f)
if(!walkcomment(word, f)) return error_asm("Comment error"); if(!walkcomment(word, f)) return error_asm("Comment error");
continue; continue;
} }
scpy(word, m->items[m->len++], 0x40); scat(scat(m->content, word), " ");
} }
return 1; return 1;
} }
@ -256,17 +256,30 @@ writehex(char *w)
} }
static int static int
tokenize(FILE *f) walkmacro(Macro *m)
{ {
char c; char c, *contentptr = m->content, *cptr = token;
char *cptr = token; while((c = *contentptr++)) {
if(c < 0x21) {
*cptr++ = 0x00;
if(token[0] && !parse(token, NULL)) return 0;
cptr = token;
} else
*cptr++ = c;
}
return 1;
}
static int
walkfile(FILE *f)
{
char c, *cptr = token;
while(fread(&c, 1, 1, f)) { while(fread(&c, 1, 1, f)) {
if(c < 0x21) { if(c < 0x21) {
*cptr++ = 0x00; *cptr++ = 0x00;
if(c == 0x0a) if(c == 0x0a)
p.line++; p.line++;
if(token[0]) if(token[0] && !parse(token, f))
if(!parse(token, f))
return 0; return 0;
cptr = token; cptr = token;
} else if(cptr - token < 0x3f) } else if(cptr - token < 0x3f)
@ -286,7 +299,7 @@ makeinclude(char *filename)
return error_top("Invalid source", filename); return error_top("Invalid source", filename);
scpy(filename, source, 0x40); scpy(filename, source, 0x40);
p.line = 0; p.line = 0;
res = tokenize(f); res = walkfile(f);
fclose(f); fclose(f);
return res; return res;
} }
@ -294,7 +307,6 @@ makeinclude(char *filename)
static int static int
parse(char *w, FILE *f) parse(char *w, FILE *f)
{ {
int i;
char c; char c;
Macro *m; Macro *m;
switch(w[0]) { switch(w[0]) {
@ -328,12 +340,9 @@ parse(char *w, FILE *f)
return writehex(w); return writehex(w);
else if(isopcode(w)) else if(isopcode(w))
return writebyte(findopcode(w)); return writebyte(findopcode(w));
else if((m = findmacro(w))) { else if((m = findmacro(w)))
for(i = 0; i < m->len; i++) return walkmacro(m);
if(!parse(m->items[i], f)) else
return 0;
return 1;
} else
return addref(w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff); return addref(w, ' ', p.ptr + 1) && writebyte(0x60) && writeshort(0xffff);
} }
return 1; return 1;