commit 4f3ced9

Devine Lu Linvega  ·  2024-03-31 02:53:04 +0000 UTC
parent 618dc4e
(uxnasm) Improved errors
1 files changed,  +32, -33
+32, -33
  1@@ -97,7 +97,7 @@ findopcode(char *s)
  2 			else if(s[m] == 'k')
  3 				i |= (1 << 7);
  4 			else
  5-				return error_top("Unknown opcode mode", s);
  6+				return 0;
  7 			m++;
  8 		}
  9 		return i;
 10@@ -115,7 +115,7 @@ walkcomment(FILE *f, Context *ctx)
 11 		if(c == '(') depth++;
 12 		if(c == ')' && --depth < 1) return 1;
 13 	}
 14-	return 0;
 15+	return error_asm("Comment incomplete");
 16 }
 17 
 18 static int
 19@@ -177,9 +177,9 @@ makemacro(char *name, FILE *f, Context *ctx)
 20 		if(c == 0xa) ctx->line += 1;
 21 	while(f && fread(&c, 1, 1, f) && c != '}') {
 22 		if(c == 0xa) ctx->line += 1;
 23-		if(c == '%') return error_top("Nested macro", name);
 24-		if(c == '(')
 25-			walkcomment(f, ctx);
 26+		if(c == '%') return error_top("Macro nested", name);
 27+		if(c == '(' && !walkcomment(f, ctx))
 28+			return 0;
 29 		else
 30 			*dictnext++ = c;
 31 	}
 32@@ -194,8 +194,8 @@ makelabel(char *name, int setscope, Context *ctx)
 33 	if(name[0] == '&')
 34 		name = join(scope, '/', name + 1);
 35 	if(labels_len >= 0x400) return error_asm("Labels limit exceeded");
 36-	if(isinvalid(name)) return error_asm("Label is invalid");
 37-	if(findlabel(name)) return error_asm("Label is duplicate");
 38+	if(isinvalid(name)) return error_asm("Label invalid");
 39+	if(findlabel(name)) return error_asm("Label duplicate");
 40 	l = &labels[labels_len++];
 41 	l->name = push(name, 0);
 42 	l->addr = ptr;
 43@@ -223,7 +223,7 @@ makeref(char *label, char rune, Uint16 addr)
 44 }
 45 
 46 static int
 47-writepad(char *w)
 48+writepad(char *w, Context *ctx)
 49 {
 50 	Item *l;
 51 	int rel = w[0] == '$' ? ptr : 0;
 52@@ -235,14 +235,14 @@ writepad(char *w)
 53 		ptr = l->addr + rel;
 54 		return 1;
 55 	}
 56-	return 0;
 57+	return error_asm("Padding invalid");
 58 }
 59 
 60 static int
 61 writebyte(Uint8 b, Context *ctx)
 62 {
 63 	if(ptr < PAGE)
 64-		return error_asm("Writing in zero-page");
 65+		return error_asm("Writing zero-page");
 66 	else if(ptr >= 0x10000)
 67 		return error_asm("Writing outside memory");
 68 	else if(ptr < length)
 69@@ -263,7 +263,7 @@ writehex(char *w, Context *ctx)
 70 	else if(w[3] && !w[4])
 71 		return writeshort(shex(w));
 72 	else
 73-		return 0;
 74+		return error_asm("Hexadecimal invalid");
 75 }
 76 
 77 static int
 78@@ -271,7 +271,7 @@ writestring(char *w, Context *ctx)
 79 {
 80 	char c;
 81 	while((c = *(w++)))
 82-		if(!writebyte(c, ctx)) return 0;
 83+		if(!writebyte(c, ctx)) return error_asm("String invalid");
 84 	return 1;
 85 }
 86 
 87@@ -284,7 +284,7 @@ assemble(char *filename)
 88 	ctx.line = 0;
 89 	ctx.path = push(filename, 0);
 90 	if(!(f = fopen(filename, "r")))
 91-		return error_top("Invalid source", filename);
 92+		return error_top("Source missing", filename);
 93 	res = walkfile(f, &ctx);
 94 	fclose(f);
 95 	return res;
 96@@ -295,12 +295,12 @@ parse(char *w, FILE *f, Context *ctx)
 97 {
 98 	Item *m;
 99 	switch(w[0]) {
100-	case '(': return !walkcomment(f, ctx) ? error_asm("Invalid comment") : 1;
101-	case '%': return !makemacro(w + 1, f, ctx) ? error_asm("Invalid macro") : 1;
102-	case '@': return !makelabel(w + 1, 1, ctx) ? error_asm("Invalid label") : 1;
103-	case '&': return !makelabel(w, 0, ctx) ? error_asm("Invalid sublabel") : 1;
104-	case '}': return !makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx) ? error_asm("Invalid label") : 1;
105-	case '#': return !ishex(w + 1) || !writehex(w, ctx) ? error_asm("Invalid hexadecimal") : 1;
106+	case '(': return walkcomment(f, ctx);
107+	case '%': return makemacro(w + 1, f, ctx);
108+	case '@': return makelabel(w + 1, 1, ctx);
109+	case '&': return makelabel(w, 0, ctx);
110+	case '}': return makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ctx);
111+	case '#': return ishex(w + 1) && writehex(w, ctx);
112 	case '_': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
113 	case ',': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ctx) && writebyte(0xff, ctx);
114 	case '-': return makeref(w + 1, w[0], ptr) && writebyte(0xff, ctx);
115@@ -310,10 +310,10 @@ parse(char *w, FILE *f, Context *ctx)
116 	case ';': return makeref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ctx) && writeshort(0xffff);
117 	case '?': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x20, ctx) && writeshort(0xffff);
118 	case '!': return makeref(w + 1, w[0], ptr + 1) && writebyte(0x40, ctx) && writeshort(0xffff);
119-	case '"': return !writestring(w + 1, ctx) ? error_asm("Invalid string") : 1;
120-	case '~': return !assemble(w + 1) ? error_asm("Invalid include") : 1;
121+	case '"': return writestring(w + 1, ctx);
122+	case '~': return !assemble(w + 1) ? error_asm("Include missing") : 1;
123 	case '$':
124-	case '|': return !writepad(w) ? error_asm("Invalid padding") : 1;
125+	case '|': return writepad(w, ctx);
126 	case '[':
127 	case ']': return 1;
128 	}
129@@ -324,20 +324,20 @@ parse(char *w, FILE *f, Context *ctx)
130 }
131 
132 static int
133-resolve(void)
134+resolve(char *filename)
135 {
136 	int i, rel;
137-	if(!length) return error_top("Assembly", "Output rom is empty.");
138+	if(!length) return error_top("Output empty", filename);
139 	for(i = 0; i < refs_len; i++) {
140 		Item *r = &refs[i], *l = findlabel(r->name);
141 		Uint8 *rom = data + r->addr;
142-		if(!l) return error_top("Unknown label", r->name);
143+		if(!l) return error_top("Label unknown", r->name);
144 		switch(r->rune) {
145 		case '_':
146 		case ',':
147 			*rom = rel = l->addr - r->addr - 2;
148 			if((Sint8)data[r->addr] != rel)
149-				return error_top("Relative reference is too far", r->name);
150+				return error_top("Relative reference too far", r->name);
151 			break;
152 		case '-':
153 		case '.':
154@@ -368,7 +368,7 @@ build(char *rompath)
155 	char *sympath = join(rompath, '.', "sym");
156 	/* rom */
157 	if(!(dst = fopen(rompath, "wb")))
158-		return !error_top("Invalid output file", rompath);
159+		return !error_top("Output file invalid", rompath);
160 	for(i = 0; i < labels_len; i++)
161 		if(labels[i].name[0] - 'A' > 25 && !labels[i].refs)
162 			fprintf(stdout, "-- Unused label: %s\n", labels[i].name);
163@@ -382,7 +382,7 @@ build(char *rompath)
164 		macro_len);
165 	/* sym */
166 	if(!(dstsym = fopen(sympath, "w")))
167-		return !error_top("Invalid symbols file", sympath);
168+		return !error_top("Symbols file invalid", sympath);
169 	for(i = 0; i < labels_len; i++) {
170 		Uint8 hb = labels[i].addr >> 8, lb = labels[i].addr;
171 		char c, d = 0, *name = labels[i].name;
172@@ -400,11 +400,10 @@ main(int argc, char *argv[])
173 {
174 	ptr = PAGE;
175 	copy("on-reset", scope, 0);
176-	if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 29 Mar 2024.\n");
177+	if(argc == 2 && scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 30 Mar 2024.\n");
178 	if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom");
179-	if(!assemble(argv[1])) return !error_top("Assembly", "Failed to assemble rom.");
180-	if(!length) return !error_top("Assembly", "Output rom is empty.");
181-	if(!resolve()) return !error_top("Assembly", "Failed to resolve symbols.");
182-	if(!build(argv[2])) return !error_top("Assembly", "Failed to build rom.");
183+	if(!assemble(argv[1])) return 1;
184+	if(!resolve(argv[2])) return 1;
185+	if(!build(argv[2])) return 1;
186 	return 0;
187 }