Improve error messages.

This commit is contained in:
Anthony J. Bentley
2013-05-19 17:07:34 -06:00
parent 51c01e3aad
commit c21c0f458f
7 changed files with 62 additions and 79 deletions

View File

@@ -19,8 +19,8 @@ extern void opt_Push(void);
extern void opt_Pop(void); extern void opt_Pop(void);
extern void opt_Parse(char *s); extern void opt_Parse(char *s);
void fatalerror(char *s); void fatalerror(const char *fmt, ...);
void yyerror(char *s); void yyerror(const char *fmt, ...);
extern char temptext[1024]; extern char temptext[1024];

View File

@@ -163,7 +163,7 @@ yywrap(void)
/* /*
* RGBAsm - FSTACK.C (FileStack routines) * RGBAsm - FSTACK.C (FileStack routines)
* *
* Dump the context stack to stdout * Dump the context stack to stderr
* *
*/ */
@@ -175,11 +175,12 @@ fstk_Dump(void)
pLastFile = pFileStack; pLastFile = pFileStack;
while (pLastFile) { while (pLastFile) {
printf("%s(%ld) -> ", pLastFile->tzFileName, pLastFile->nLine); fprintf(stderr, "%s(%ld) -> ", pLastFile->tzFileName,
pLastFile->nLine);
pLastFile = pLastFile->pNext; pLastFile = pLastFile->pNext;
} }
printf("%s(%ld)", tzCurrentFileName, nLineNo); fprintf(stderr, "%s(%ld)", tzCurrentFileName, nLineNo);
} }
/* /*
* RGBAsm - FSTACK.C (FileStack routines) * RGBAsm - FSTACK.C (FileStack routines)
@@ -326,7 +327,7 @@ fstk_RunString(char *s)
yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro)); yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro));
yy_switch_to_buffer(CurrentFlexHandle); yy_switch_to_buffer(CurrentFlexHandle);
} else } else
yyerror("No such string symbol"); yyerror("No such string symbol '%s'", s);
} }
/* /*
* RGBAsm - FSTACK.C (FileStack routines) * RGBAsm - FSTACK.C (FileStack routines)

View File

@@ -8,7 +8,7 @@ section:
if( $6>=0 && $6<0x10000 ) if( $6>=0 && $6<0x10000 )
out_NewAbsSection($2,$4,$6,-1); out_NewAbsSection($2,$4,$6,-1);
else else
yyerror( "Address must be 16-bit" ); yyerror("Address $%x not 16-bit", $6);
} }
| T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' const ']' | T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' const ']'
{ {
@@ -16,9 +16,9 @@ section:
if( $8>=1 && $8<=0x1ff ) if( $8>=1 && $8<=0x1ff )
out_NewAbsSection($2,$4,-1,$8); out_NewAbsSection($2,$4,-1,$8);
else else
yyerror( "BANK value out of range" ); yyerror("ROM bank value $%x out of range (1 to $1ff)", $8);
} else } 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 ']' | T_POP_SECTION string ',' sectiontype '[' const ']' ',' T_OP_BANK '[' const ']'
{ {
@@ -27,11 +27,11 @@ section:
if( $11>=1 && $11<=0x1ff ) if( $11>=1 && $11<=0x1ff )
out_NewAbsSection($2,$4,$6,$11); out_NewAbsSection($2,$4,$6,$11);
else else
yyerror( "BANK value out of range" ); yyerror("ROM bank value $%x out of range (1 to $1ff)", 8);
} else } else
yyerror( "Address must be 16-bit" ); yyerror("Address $%x not 16-bit", $6);
} else } else
yyerror( "BANK only allowed for CODE/DATA" ); yyerror("BANK only allowed for CODE/DATA");
} }
; ;
@@ -196,7 +196,7 @@ z80_ldio : T_Z80_LDIO T_MODE_A comma op_mem_ind
if( (!rpn_isReloc(&$4)) if( (!rpn_isReloc(&$4))
&& ($4.nVal<0 || ($4.nVal>0xFF && $4.nVal<0xFF00) || $4.nVal>0xFFFF) ) && ($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); out_AbsByte(0xF0);
@@ -210,7 +210,7 @@ z80_ldio : T_Z80_LDIO T_MODE_A comma op_mem_ind
if( (!rpn_isReloc(&$2)) if( (!rpn_isReloc(&$2))
&& ($2.nVal<0 || ($2.nVal>0xFF && $2.nVal<0xFF00) || $2.nVal>0xFFFF) ) && ($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); out_AbsByte(0xE0);
@@ -271,7 +271,7 @@ z80_ld_r : T_Z80_LD reg_r comma const_8bit
{ {
if( ($2==REG_HL_IND) && ($4==REG_HL_IND) ) if( ($2==REG_HL_IND) && ($4==REG_HL_IND) )
{ {
yyerror( "LD (HL),(HL) not allowed" ); yyerror("LD [HL],[HL] not a valid instruction");
} }
else else
out_AbsByte(0x40|($2<<3)|$4); out_AbsByte(0x40|($2<<3)|$4);
@@ -284,7 +284,7 @@ z80_ld_a : T_Z80_LD reg_r comma T_MODE_C_IND
out_AbsByte(0xF2); out_AbsByte(0xF2);
else else
{ {
yyerror( "Destination operand must be A" ); yyerror("Destination operand must be A");
} }
} }
| T_Z80_LD reg_r comma reg_rr | T_Z80_LD reg_r comma reg_rr
@@ -293,7 +293,7 @@ z80_ld_a : T_Z80_LD reg_r comma T_MODE_C_IND
out_AbsByte(0x0A|($4<<4)); out_AbsByte(0x0A|($4<<4));
else else
{ {
yyerror( "Destination operand must be A" ); yyerror("Destination operand must be A");
} }
} }
| T_Z80_LD reg_r comma op_mem_ind | T_Z80_LD reg_r comma op_mem_ind
@@ -313,7 +313,7 @@ z80_ld_a : T_Z80_LD reg_r comma T_MODE_C_IND
} }
else else
{ {
yyerror( "Destination operand must be A" ); yyerror("Destination operand must be A");
} }
} }
; ;
@@ -390,11 +390,11 @@ z80_rst : T_Z80_RST const_8bit
{ {
if( rpn_isReloc(&$2) ) 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 ) else if( ($2.nVal&0x38)!=$2.nVal )
{ {
yyerror( "Invalid address for RST" ); yyerror("Invalid address $%x for RST", $2.nVal);
} }
else else
out_AbsByte(0xC7|$2.nVal); out_AbsByte(0xC7|$2.nVal);

View File

@@ -7,6 +7,7 @@
#include <math.h> #include <math.h>
#include <getopt.h> #include <getopt.h>
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -205,18 +206,26 @@ opt_Pop(void)
*/ */
void void
yyerror(char *s) yyerror(const char *fmt, ...)
{ {
printf("*ERROR*\t"); fprintf(stderr, "ERROR:\t");
fstk_Dump(); 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; nErrors += 1;
} }
void 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); exit(5);
} }
/* /*

View File

@@ -155,8 +155,7 @@ rpn_Bank(struct Expression * expr, char *tzSym)
psym = sym_FindSymbol(tzSym); psym = sym_FindSymbol(tzSym);
if (nPass == 2 && psym == NULL) { if (nPass == 2 && psym == NULL) {
sprintf(temptext, "'%s' not defined", tzSym); yyerror("'%s' not defined", tzSym);
yyerror(temptext);
} }
expr->isReloc = 1; expr->isReloc = 1;
pushbyte(expr, RPN_BANK); pushbyte(expr, RPN_BANK);

View File

@@ -217,8 +217,7 @@ sym_Purge(char *tzName)
free(pSym); free(pSym);
} else { } else {
sprintf(temptext, "'%s' not defined", tzName); yyerror("'%s' not defined", tzName);
yyerror(temptext);
} }
} }
/* /*
@@ -245,10 +244,8 @@ sym_isConstDefined(char *tzName)
nType & (SYMF_EQU | SYMF_SET | SYMF_MACRO | SYMF_STRING)) { nType & (SYMF_EQU | SYMF_SET | SYMF_MACRO | SYMF_STRING)) {
return (1); return (1);
} else { } else {
sprintf(temptext, fatalerror("'%s' is not allowed as argument to the "
"'%s' is not allowed as argument to the DEF function", "DEF function", tzName);
tzName);
fatalerror(temptext);
} }
} }
return (0); return (0);
@@ -309,8 +306,7 @@ sym_GetStringValue(char *tzSym)
if ((pSym = sym_FindSymbol(tzSym)) != NULL) if ((pSym = sym_FindSymbol(tzSym)) != NULL)
return (pSym->pMacro); return (pSym->pMacro);
else { else {
sprintf(temptext, "Stringsymbol '%s' not defined", tzSym); yyerror("Stringsymbol '%s' not defined", tzSym);
yyerror(temptext);
} }
return (NULL); return (NULL);
@@ -339,8 +335,7 @@ sym_GetConstantValue(char *s)
fatalerror("Expression must have a constant value"); fatalerror("Expression must have a constant value");
} }
} else { } else {
sprintf(temptext, "'%s' not defined", s); yyerror("'%s' not defined", s);
yyerror(temptext);
} }
return (0); return (0);
@@ -365,9 +360,7 @@ sym_GetValue(char *s)
if ((psym = findsymbol(s, pscope)) != NULL) { if ((psym = findsymbol(s, pscope)) != NULL) {
if (psym->nType & SYMF_DEFINED) { if (psym->nType & SYMF_DEFINED) {
if (psym->nType & (SYMF_MACRO | SYMF_STRING)) { if (psym->nType & (SYMF_MACRO | SYMF_STRING)) {
sprintf(temptext, yyerror("'%s' is a macro or string symbol", s);
"'%s' is a macro or string symbol", s);
yyerror(temptext);
} }
return (getvaluefield(psym)); return (getvaluefield(psym));
} else { } else {
@@ -375,8 +368,7 @@ sym_GetValue(char *s)
/* 0x80 seems like a good default value... */ /* 0x80 seems like a good default value... */
return (0x80); return (0x80);
} else { } else {
sprintf(temptext, "'%s' not defined", s); yyerror("'%s' not defined", s);
yyerror(temptext);
} }
} }
} else { } else {
@@ -384,8 +376,7 @@ sym_GetValue(char *s)
createsymbol(s); createsymbol(s);
return (0x80); return (0x80);
} else { } else {
sprintf(temptext, "'%s' not defined", s); yyerror("'%s' not defined", s);
yyerror(temptext);
} }
} }
@@ -411,18 +402,14 @@ sym_GetDefinedValue(char *s)
if ((psym = findsymbol(s, pscope)) != NULL) { if ((psym = findsymbol(s, pscope)) != NULL) {
if ((psym->nType & SYMF_DEFINED)) { if ((psym->nType & SYMF_DEFINED)) {
if (psym->nType & (SYMF_MACRO | SYMF_STRING)) { if (psym->nType & (SYMF_MACRO | SYMF_STRING)) {
sprintf(temptext, yyerror("'%s' is a macro or string symbol", s);
"'%s' is a macro or string symbol", s);
yyerror(temptext);
} }
return (getvaluefield(psym)); return (getvaluefield(psym));
} else { } else {
sprintf(temptext, "'%s' not defined", s); yyerror("'%s' not defined", s);
yyerror(temptext);
} }
} else { } else {
sprintf(temptext, "'%s' not defined", s); yyerror("'%s' not defined", s);
yyerror(temptext);
} }
return (0); return (0);
@@ -558,9 +545,7 @@ sym_AddEqu(char *tzSym, SLONG value)
if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
sprintf(temptext, "'%s' already defined", yyerror("'%s' already defined", tzSym);
tzSym);
yyerror(temptext);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
@@ -586,8 +571,7 @@ sym_AddString(char *tzSym, char *tzValue)
if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
sprintf(temptext, "'%s' already defined", tzSym); yyerror("'%s' already defined", tzSym);
yyerror(temptext);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
@@ -661,9 +645,7 @@ sym_AddLocalReloc(char *tzSym)
if (pScope) { if (pScope) {
if ((nsym = findsymbol(tzSym, pScope)) != NULL) { if ((nsym = findsymbol(tzSym, pScope)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
sprintf(temptext, yyerror("'%s' already defined", tzSym);
"'%s' already defined", tzSym);
yyerror(temptext);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
@@ -696,9 +678,7 @@ sym_AddReloc(char *tzSym)
if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
sprintf(temptext, "'%s' already defined", yyerror("'%s' already defined", tzSym);
tzSym);
yyerror(temptext);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);
@@ -739,8 +719,7 @@ sym_Export(char *tzSym)
if (nsym->nType & SYMF_DEFINED) if (nsym->nType & SYMF_DEFINED)
return; return;
} }
sprintf(temptext, "'%s' not defined", tzSym); yyerror("'%s' not defined", tzSym);
yyerror(temptext);
} }
} }
@@ -759,8 +738,7 @@ sym_Import(char *tzSym)
struct sSymbol *nsym; struct sSymbol *nsym;
if (findsymbol(tzSym, NULL)) { if (findsymbol(tzSym, NULL)) {
sprintf(temptext, "'%s' already defined", tzSym); yyerror("'%s' already defined", tzSym);
yyerror(temptext);
} }
if ((nsym = createsymbol(tzSym)) != NULL) if ((nsym = createsymbol(tzSym)) != NULL)
nsym->nType |= SYMF_IMPORT; nsym->nType |= SYMF_IMPORT;
@@ -811,9 +789,7 @@ sym_AddMacro(char *tzSym)
if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if ((nsym = findsymbol(tzSym, NULL)) != NULL) {
if (nsym->nType & SYMF_DEFINED) { if (nsym->nType & SYMF_DEFINED) {
sprintf(temptext, "'%s' already defined", yyerror("'%s' already defined", tzSym);
tzSym);
yyerror(temptext);
} }
} else } else
nsym = createsymbol(tzSym); nsym = createsymbol(tzSym);

View File

@@ -47,8 +47,7 @@ macro : T_ID
if( !fstk_RunMacro($1) ) if( !fstk_RunMacro($1) )
{ {
fprintf(stderr, "Macro '%s' not defined", $1); yyerror("Macro '%s' not defined", $1);
yyerror( "No such macro" );
} }
} }
; ;
@@ -138,11 +137,11 @@ pushs : T_POP_PUSHS
; ;
fail : T_POP_FAIL string fail : T_POP_FAIL string
{ fatalerror($2); } { fatalerror("%s", $2); }
; ;
warn : T_POP_WARN string warn : T_POP_WARN string
{ yyerror($2); } { yyerror("%s", $2); }
; ;
shift : T_POP_SHIFT shift : T_POP_SHIFT
@@ -268,8 +267,7 @@ include : T_POP_INCLUDE string
{ {
if( !fstk_RunInclude($2) ) if( !fstk_RunInclude($2) )
{ {
fprintf(stderr, "Could not open file '%s' : %s\n", $2, strerror(errno)); yyerror("Could not open file '%s' : %s\n", $2, strerror(errno));
yyerror( "File not found" );
} }
} }
; ;
@@ -328,7 +326,7 @@ const_3bit : const
{ {
if( ($1<0) || ($1>7) ) if( ($1<0) || ($1>7) )
{ {
yyerror( "Immediate value must be 3-bit" ); yyerror("Immediate value must be 3-bit");
} }
else else
$$=$1&0x7; $$=$1&0x7;
@@ -366,7 +364,7 @@ const_PCrel : relocconst
{ {
$$ = $1; $$ = $1;
if( !rpn_isPCRelative(&$1) ) if( !rpn_isPCRelative(&$1) )
yyerror( "Expression must be PC-relative" ); yyerror("Expression must be PC-relative");
} }
; ;
@@ -374,7 +372,7 @@ const_8bit : relocconst
{ {
if( (!rpn_isReloc(&$1)) && (($1.nVal<-128) || ($1.nVal>255)) ) if( (!rpn_isReloc(&$1)) && (($1.nVal<-128) || ($1.nVal>255)) )
{ {
yyerror( "Expression must be 8-bit" ); yyerror("Expression must be 8-bit");
} }
$$=$1; $$=$1;
} }
@@ -384,7 +382,7 @@ const_16bit : relocconst
{ {
if( (!rpn_isReloc(&$1)) && (($1.nVal<-32768) || ($1.nVal>65535)) ) if( (!rpn_isReloc(&$1)) && (($1.nVal<-32768) || ($1.nVal>65535)) )
{ {
yyerror( "Expression must be 16-bit" ); yyerror("Expression must be 16-bit");
} }
$$=$1; $$=$1;
} }