(uxnasm) Walk across comments in macros

This commit is contained in:
Devine Lu Linvega 2024-03-26 13:53:23 -07:00
parent 0ae8812680
commit c64fe94038
2 changed files with 25 additions and 23 deletions

View File

@ -1,6 +1,6 @@
( init ) ( init )
%emit { #18 DEO } %emit ( byte -- ) { #18 DEO }
|0100 @program |0100 @program

View File

@ -140,6 +140,21 @@ isopcode(char *s)
return findopcode(s) || scmp(s, "BRK", 4); return findopcode(s) || scmp(s, "BRK", 4);
} }
static int
walkcomment(char *w, FILE *f)
{
int depth = 1;
char c;
if(slen(w) == 1)
while(fread(&c, 1, 1, f)) {
if(c == '(')
depth++;
else if(c == ')' && --depth < 1)
return 1;
}
return 0;
}
static int static int
makemacro(char *name, FILE *f) makemacro(char *name, FILE *f)
{ {
@ -155,10 +170,12 @@ makemacro(char *name, FILE *f)
while(fscanf(f, "%63s", word) == 1) { while(fscanf(f, "%63s", word) == 1) {
if(word[0] == '{') continue; if(word[0] == '{') continue;
if(word[0] == '}') break; if(word[0] == '}') break;
if(word[0] == '%') if(word[0] == '%') return error_asm("Macro error");
return error_asm("Macro error"); if(m->len >= 0x40) return error_asm("Macro size exceeded");
if(m->len >= 0x40) if(word[0] == '(') {
return error_asm("Macro size exceeded"); walkcomment(word, f);
continue;
}
scpy(word, m->items[m->len++], 0x40); scpy(word, m->items[m->len++], 0x40);
} }
return 1; return 1;
@ -298,7 +315,7 @@ tokenize(FILE *f)
} }
static int static int
doinclude(char *filename) makeinclude(char *filename)
{ {
FILE *f; FILE *f;
int res = 0; int res = 0;
@ -311,21 +328,6 @@ doinclude(char *filename)
return res; return res;
} }
static int
walkcomment(char *w, FILE *f)
{
int depth = 1;
char c;
if(slen(w) == 1)
while(fread(&c, 1, 1, f)) {
if(c == '(')
depth++;
else if(c == ')' && --depth < 1)
return 1;
}
return 0;
}
static int static int
parse(char *w, FILE *f) parse(char *w, FILE *f)
{ {
@ -334,7 +336,7 @@ parse(char *w, FILE *f)
Macro *m; Macro *m;
switch(w[0]) { switch(w[0]) {
case '(': return !walkcomment(w, f) ? error_asm("Invalid comment") : 1; case '(': return !walkcomment(w, f) ? error_asm("Invalid comment") : 1;
case '~': return !doinclude(w + 1) ? error_asm("Invalid include") : 1; case '~': return !makeinclude(w + 1) ? error_asm("Invalid include") : 1;
case '%': return !makemacro(w + 1, f) ? error_asm("Invalid macro") : 1; case '%': return !makemacro(w + 1, f) ? error_asm("Invalid macro") : 1;
case '@': return !makelabel(w + 1, 1) ? error_asm("Invalid label") : 1; case '@': return !makelabel(w + 1, 1) ? error_asm("Invalid label") : 1;
case '&': return !makelabel(w, 0) ? error_asm("Invalid sublabel") : 1; case '&': return !makelabel(w, 0) ? error_asm("Invalid sublabel") : 1;
@ -466,7 +468,7 @@ main(int argc, char *argv[])
scpy("on-reset", scope, 0x40); scpy("on-reset", scope, 0x40);
if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom"); if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom");
if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 26 Mar 2024.\n"); if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 26 Mar 2024.\n");
if(!doinclude(argv[1]) || !resolve()) return !error_top("Assembly", "Failed to assemble rom."); if(!makeinclude(argv[1]) || !resolve()) return !error_top("Assembly", "Failed to assemble rom.");
if(!(dst = fopen(argv[2], "wb"))) return !error_top("Invalid Output", argv[2]); if(!(dst = fopen(argv[2], "wb"))) return !error_top("Invalid Output", argv[2]);
if(p.length <= TRIM) return !error_top("Assembly", "Output rom is empty."); if(p.length <= TRIM) return !error_top("Assembly", "Output rom is empty.");
review(argv[2]); review(argv[2]);