shithub: rgbds

Download patch

ref: c21c0f458fdc51335ae6b2773e60e5e6e3423ed1
parent: 51c01e3aaded3886f5ee8b8aa4b710f4639412fb
author: Anthony J. Bentley <[email protected]>
date: Sun May 19 13:07:34 EDT 2013

Improve error messages.

--- a/include/asm/main.h
+++ b/include/asm/main.h
@@ -19,8 +19,8 @@
 extern void opt_Pop(void);
 extern void opt_Parse(char *s);
 
-void fatalerror(char *s);
-void yyerror(char *s);
+void fatalerror(const char *fmt, ...);
+void yyerror(const char *fmt, ...);
 
 extern char temptext[1024];
 
--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -163,7 +163,7 @@
 /*
  * RGBAsm - FSTACK.C (FileStack routines)
  *
- * Dump the context stack to stdout
+ * Dump the context stack to stderr
  *
  */
 
@@ -175,11 +175,12 @@
 	pLastFile = pFileStack;
 
 	while (pLastFile) {
-		printf("%s(%ld) -> ", pLastFile->tzFileName, pLastFile->nLine);
+		fprintf(stderr, "%s(%ld) -> ", pLastFile->tzFileName,
+		    pLastFile->nLine);
 		pLastFile = pLastFile->pNext;
 	}
 
-	printf("%s(%ld)", tzCurrentFileName, nLineNo);
+	fprintf(stderr, "%s(%ld)", tzCurrentFileName, nLineNo);
 }
 /*
  * RGBAsm - FSTACK.C (FileStack routines)
@@ -326,7 +327,7 @@
 		    yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro));
 		yy_switch_to_buffer(CurrentFlexHandle);
 	} else
-		yyerror("No such string symbol");
+		yyerror("No such string symbol '%s'", s);
 }
 /*
  * RGBAsm - FSTACK.C (FileStack routines)
--- a/src/asm/gameboy/yaccprt4.y
+++ b/src/asm/gameboy/yaccprt4.y
@@ -8,7 +8,7 @@
 			if( $6>=0 && $6<0x10000 )
 				out_NewAbsSection($2,$4,$6,-1);
 			else
-				yyerror( "Address must be 16-bit" );
+				yyerror("Address $%x not 16-bit", $6);
 		}
 	|	T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' const ']'
 		{
@@ -16,9 +16,9 @@
 				if( $8>=1 && $8<=0x1ff )
 					out_NewAbsSection($2,$4,-1,$8);
 				else
-					yyerror( "BANK value out of range" );
+					yyerror("ROM bank value $%x out of range (1 to $1ff)", $8);
 			} else
-				yyerror( "BANK only allowed for CODE/DATA" );
+				yyerror("BANK only allowed for CODE/DATA");
 		}
 	|	T_POP_SECTION string ',' sectiontype '[' const ']' ',' T_OP_BANK '[' const ']'
 		{
@@ -27,11 +27,11 @@
 					if( $11>=1 && $11<=0x1ff )
 						out_NewAbsSection($2,$4,$6,$11);
 					else
-						yyerror( "BANK value out of range" );
+						yyerror("ROM bank value $%x out of range (1 to $1ff)", 8);
 				} else
-					yyerror( "Address must be 16-bit" );
+					yyerror("Address $%x not 16-bit", $6);
 			} else
-				yyerror( "BANK only allowed for CODE/DATA" );
+				yyerror("BANK only allowed for CODE/DATA");
 		}
 ;
 
@@ -196,7 +196,7 @@
 						if( (!rpn_isReloc(&$4))
 						&&	($4.nVal<0 || ($4.nVal>0xFF && $4.nVal<0xFF00) || $4.nVal>0xFFFF) )
 						{
-							yyerror( "Source must be in the IO/HRAM area" );
+							yyerror("Source address $%x not in HRAM ($FF00 to $FFFE)", $4.nVal);
 						}
 
 						out_AbsByte(0xF0);
@@ -210,7 +210,7 @@
 						if( (!rpn_isReloc(&$2))
 						&&	($2.nVal<0 || ($2.nVal>0xFF && $2.nVal<0xFF00) || $2.nVal>0xFFFF) )
 						{
-							yyerror( "Destination must be in the IO/HRAM area" );
+							yyerror("Destination address $%x not in HRAM ($FF00 to $FFFE)", $2.nVal);
 						}
 
 						out_AbsByte(0xE0);
@@ -271,7 +271,7 @@
 					{
 						if( ($2==REG_HL_IND) && ($4==REG_HL_IND) )
 						{
-							yyerror( "LD (HL),(HL) not allowed" );
+							yyerror("LD [HL],[HL] not a valid instruction");
 						}
 						else
 							out_AbsByte(0x40|($2<<3)|$4);
@@ -284,7 +284,7 @@
 							out_AbsByte(0xF2);
 						else
 						{
-							yyerror( "Destination operand must be A" );
+							yyerror("Destination operand must be A");
 						}
 					}
 				|	T_Z80_LD reg_r comma reg_rr
@@ -293,7 +293,7 @@
 							out_AbsByte(0x0A|($4<<4));
 						else
 						{
-							yyerror( "Destination operand must be A" );
+							yyerror("Destination operand must be A");
 						}
 					}
 				|	T_Z80_LD reg_r comma op_mem_ind
@@ -313,7 +313,7 @@
 						}
 						else
 						{
-							yyerror( "Destination operand must be A" );
+							yyerror("Destination operand must be A");
 						}
 					}
 ;
@@ -390,11 +390,11 @@
 					{
 						if( rpn_isReloc(&$2) )
 						{
-							yyerror( "Address for RST must be absolute" );
+							yyerror("Address for RST must be absolute");
 						}
 						else if( ($2.nVal&0x38)!=$2.nVal )
 						{
-							yyerror( "Invalid address for RST" );
+							yyerror("Invalid address $%x for RST", $2.nVal);
 						}
 						else
 							out_AbsByte(0xC7|$2.nVal);
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -7,6 +7,7 @@
 
 #include <math.h>
 #include <getopt.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -205,18 +206,26 @@
  */
 
 void 
-yyerror(char *s)
+yyerror(const char *fmt, ...)
 {
-	printf("*ERROR*\t");
+	fprintf(stderr, "ERROR:\t");
 	fstk_Dump();
-	printf(" :\n\t%s\n", s);
+	fprintf(stderr, " :\n\t");
+	va_list args;
+	va_start(args, fmt);
+	vfprintf(stderr, fmt, args);
+	va_end(args);
+	fprintf(stderr, "\n");
 	nErrors += 1;
 }
 
 void 
-fatalerror(char *s)
+fatalerror(const char *fmt, ...)
 {
-	yyerror(s);
+	va_list args;
+	va_start(args, fmt);
+	yyerror(fmt, args);
+	va_end(args);
 	exit(5);
 }
 /*
--- a/src/asm/rpn.c
+++ b/src/asm/rpn.c
@@ -155,8 +155,7 @@
 
 		psym = sym_FindSymbol(tzSym);
 		if (nPass == 2 && psym == NULL) {
-			sprintf(temptext, "'%s' not defined", tzSym);
-			yyerror(temptext);
+			yyerror("'%s' not defined", tzSym);
 		}
 		expr->isReloc = 1;
 		pushbyte(expr, RPN_BANK);
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -217,8 +217,7 @@
 
 		free(pSym);
 	} else {
-		sprintf(temptext, "'%s' not defined", tzName);
-		yyerror(temptext);
+		yyerror("'%s' not defined", tzName);
 	}
 }
 /*
@@ -245,10 +244,8 @@
 		    nType & (SYMF_EQU | SYMF_SET | SYMF_MACRO | SYMF_STRING)) {
 			return (1);
 		} else {
-			sprintf(temptext,
-			    "'%s' is not allowed as argument to the DEF function",
-			    tzName);
-			fatalerror(temptext);
+			fatalerror("'%s' is not allowed as argument to the "
+			    "DEF function", tzName);
 		}
 	}
 	return (0);
@@ -309,8 +306,7 @@
 	if ((pSym = sym_FindSymbol(tzSym)) != NULL)
 		return (pSym->pMacro);
 	else {
-		sprintf(temptext, "Stringsymbol '%s' not defined", tzSym);
-		yyerror(temptext);
+		yyerror("Stringsymbol '%s' not defined", tzSym);
 	}
 
 	return (NULL);
@@ -339,8 +335,7 @@
 			fatalerror("Expression must have a constant value");
 		}
 	} else {
-		sprintf(temptext, "'%s' not defined", s);
-		yyerror(temptext);
+		yyerror("'%s' not defined", s);
 	}
 
 	return (0);
@@ -365,9 +360,7 @@
 	if ((psym = findsymbol(s, pscope)) != NULL) {
 		if (psym->nType & SYMF_DEFINED) {
 			if (psym->nType & (SYMF_MACRO | SYMF_STRING)) {
-				sprintf(temptext,
-				    "'%s' is a macro or string symbol", s);
-				yyerror(temptext);
+				yyerror("'%s' is a macro or string symbol", s);
 			}
 			return (getvaluefield(psym));
 		} else {
@@ -375,8 +368,7 @@
 				/* 0x80 seems like a good default value... */
 				return (0x80);
 			} else {
-				sprintf(temptext, "'%s' not defined", s);
-				yyerror(temptext);
+				yyerror("'%s' not defined", s);
 			}
 		}
 	} else {
@@ -384,8 +376,7 @@
 			createsymbol(s);
 			return (0x80);
 		} else {
-			sprintf(temptext, "'%s' not defined", s);
-			yyerror(temptext);
+			yyerror("'%s' not defined", s);
 		}
 	}
 
@@ -411,18 +402,14 @@
 	if ((psym = findsymbol(s, pscope)) != NULL) {
 		if ((psym->nType & SYMF_DEFINED)) {
 			if (psym->nType & (SYMF_MACRO | SYMF_STRING)) {
-				sprintf(temptext,
-				    "'%s' is a macro or string symbol", s);
-				yyerror(temptext);
+				yyerror("'%s' is a macro or string symbol", s);
 			}
 			return (getvaluefield(psym));
 		} else {
-			sprintf(temptext, "'%s' not defined", s);
-			yyerror(temptext);
+			yyerror("'%s' not defined", s);
 		}
 	} else {
-		sprintf(temptext, "'%s' not defined", s);
-		yyerror(temptext);
+		yyerror("'%s' not defined", s);
 	}
 
 	return (0);
@@ -558,9 +545,7 @@
 
 		if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
 			if (nsym->nType & SYMF_DEFINED) {
-				sprintf(temptext, "'%s' already defined",
-				    tzSym);
-				yyerror(temptext);
+				yyerror("'%s' already defined", tzSym);
 			}
 		} else
 			nsym = createsymbol(tzSym);
@@ -586,8 +571,7 @@
 
 	if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
 		if (nsym->nType & SYMF_DEFINED) {
-			sprintf(temptext, "'%s' already defined", tzSym);
-			yyerror(temptext);
+			yyerror("'%s' already defined", tzSym);
 		}
 	} else
 		nsym = createsymbol(tzSym);
@@ -661,9 +645,7 @@
 		if (pScope) {
 			if ((nsym = findsymbol(tzSym, pScope)) != NULL) {
 				if (nsym->nType & SYMF_DEFINED) {
-					sprintf(temptext,
-					    "'%s' already defined", tzSym);
-					yyerror(temptext);
+					yyerror("'%s' already defined", tzSym);
 				}
 			} else
 				nsym = createsymbol(tzSym);
@@ -696,9 +678,7 @@
 
 		if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
 			if (nsym->nType & SYMF_DEFINED) {
-				sprintf(temptext, "'%s' already defined",
-				    tzSym);
-				yyerror(temptext);
+				yyerror("'%s' already defined", tzSym);
 			}
 		} else
 			nsym = createsymbol(tzSym);
@@ -739,8 +719,7 @@
 			if (nsym->nType & SYMF_DEFINED)
 				return;
 		}
-		sprintf(temptext, "'%s' not defined", tzSym);
-		yyerror(temptext);
+		yyerror("'%s' not defined", tzSym);
 	}
 
 }
@@ -759,8 +738,7 @@
 		struct sSymbol *nsym;
 
 		if (findsymbol(tzSym, NULL)) {
-			sprintf(temptext, "'%s' already defined", tzSym);
-			yyerror(temptext);
+			yyerror("'%s' already defined", tzSym);
 		}
 		if ((nsym = createsymbol(tzSym)) != NULL)
 			nsym->nType |= SYMF_IMPORT;
@@ -811,9 +789,7 @@
 
 		if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
 			if (nsym->nType & SYMF_DEFINED) {
-				sprintf(temptext, "'%s' already defined",
-				    tzSym);
-				yyerror(temptext);
+				yyerror("'%s' already defined", tzSym);
 			}
 		} else
 			nsym = createsymbol(tzSym);
--- a/src/asm/yaccprt3.y
+++ b/src/asm/yaccprt3.y
@@ -47,8 +47,7 @@
 
 						if( !fstk_RunMacro($1) )
 						{
-							fprintf(stderr, "Macro '%s' not defined", $1);
-							yyerror( "No such macro" );
+							yyerror("Macro '%s' not defined", $1);
 						}
 					}
 ;
@@ -138,11 +137,11 @@
 ;
 
 fail			:	T_POP_FAIL string
-					{ fatalerror($2); }
+					{ fatalerror("%s", $2); }
 ;
 
 warn			:	T_POP_WARN string
-					{ yyerror($2); }
+					{ yyerror("%s", $2); }
 ;
 
 shift			:	T_POP_SHIFT
@@ -268,8 +267,7 @@
 					{
 						if( !fstk_RunInclude($2) )
 						{
-							fprintf(stderr, "Could not open file '%s' : %s\n", $2, strerror(errno));
-							yyerror( "File not found" );
+							yyerror("Could not open file '%s' : %s\n", $2, strerror(errno));
 						}
 					}
 ;
@@ -328,7 +326,7 @@
 					{
 						if( ($1<0) || ($1>7) )
 						{
-							yyerror( "Immediate value must be 3-bit" );
+							yyerror("Immediate value must be 3-bit");
 						}
 						else
 							$$=$1&0x7;
@@ -366,7 +364,7 @@
 					{
 						$$ = $1;
 						if( !rpn_isPCRelative(&$1) )
-							yyerror( "Expression must be PC-relative" );
+							yyerror("Expression must be PC-relative");
 					}
 ;
 
@@ -374,7 +372,7 @@
 					{
 						if( (!rpn_isReloc(&$1)) && (($1.nVal<-128) || ($1.nVal>255)) )
 						{
-							yyerror( "Expression must be 8-bit" );
+							yyerror("Expression must be 8-bit");
 						}
 						$$=$1;
 					}
@@ -384,7 +382,7 @@
 					{
 						if( (!rpn_isReloc(&$1)) && (($1.nVal<-32768) || ($1.nVal>65535)) )
 						{
-							yyerror( "Expression must be 16-bit" );
+							yyerror("Expression must be 16-bit");
 						}
 						$$=$1;
 					}