Skip to content

Commit

Permalink
Fix for #67
Browse files Browse the repository at this point in the history
  • Loading branch information
envenomator committed Feb 29, 2024
1 parent a195427 commit 7032a16
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 25 deletions.
33 changes: 16 additions & 17 deletions src/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ void emit_quotedstring(char *str) {
str++;
}
// we missed an end-quote to this string, we shouldn't reach this
error(message[ERROR_STRINGFORMAT]);
error(message[ERROR_STRING_NOTTERMINATED]);
}

void parse_asm_single_immediate(void) {
Expand All @@ -1051,14 +1051,12 @@ void parse_asm_single_immediate(void) {

void handle_asm_db(void) {
streamtoken_t token;
uint24_t argcount = 0;
bool expectarg = true;

if(pass == 1) definelabel(address);

while(currentline.next) {
if(getDefineValueToken(&token, currentline.next)) {
argcount++;

if(currentExpandedMacro) {
if(macroExpandArg(_macro_ASM_buffer, token.start, currentExpandedMacro)) {
token.start = _macro_ASM_buffer;
Expand All @@ -1075,28 +1073,29 @@ void handle_asm_db(void) {
emit_8bit(operand1.immediate);
break;
}
expectarg = false;
}
if(token.terminator == ',') {
currentline.next = token.next;
expectarg = true;
}
if(token.terminator == ',') currentline.next = token.next;
else {
if((token.terminator != 0) &&(token.terminator != ';')) error(message[ERROR_LISTFORMAT]);
currentline.next = NULL;
}
}
if(argcount == 0) {
error(message[ERROR_MISSINGOPERAND]);
}
if(expectarg) error(message[ERROR_MISSINGOPERAND]);
}

void handle_asm_dw(uint8_t wordtype) {
label_t *lbl;
streamtoken_t token;
uint24_t argcount = 0;
bool expectarg = true;

if(pass == 1) definelabel(address);

while(currentline.next) {
if(getDefineValueToken(&token, currentline.next)) {
argcount++;

if(currentExpandedMacro) {
if(macroExpandArg(_macro_ASM_buffer, token.start, currentExpandedMacro)) {
Expand All @@ -1122,31 +1121,31 @@ void handle_asm_dw(uint8_t wordtype) {
error(message[ERROR_INTERNAL]);
break;
}
expectarg = false;
}
if(token.terminator == ',') {
currentline.next = token.next;
expectarg = true;
}
if(token.terminator == ',') currentline.next = token.next;
else {
if((token.terminator != 0) && (token.terminator != ';')) error(message[ERROR_LISTFORMAT]);
currentline.next = NULL;
currentline.next = NULL;
}
}
if(argcount == 0) error(message[ERROR_MISSINGOPERAND]);
if(expectarg) error(message[ERROR_MISSINGOPERAND]);
}

void handle_asm_equ(void) {
streamtoken_t token;
uint24_t argcount = 0;

if(getDefineValueToken(&token, currentline.next)) {
argcount++;
if((token.terminator != 0) && (token.terminator != ';')) error(message[ERROR_TOOMANYARGUMENTS]);
if(pass == 1) {
if(currentline.label) definelabel(getValue(token.start, true)); // needs to be defined in pass 1
else error(message[ERROR_MISSINGLABEL]);
}
}
else error(message[ERROR_MISSINGOPERAND]);

if(argcount == 0) error(message[ERROR_MISSINGOPERAND]);
}

void handle_asm_adl(void) {
Expand Down
2 changes: 1 addition & 1 deletion src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ char *message[] = {
"Value too large for 16-bits, truncation required",
"Suffix not matching mnemonic / ADL mode",
"Index register offset exceeded",
"String format error",
"String not terminated",
"Invalid ADL mode",
"Invalid assembler command",
"New address lower than PC",
Expand Down
17 changes: 10 additions & 7 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ void remove_ext (char* myStr, char extSep, char pathSep) {
}

void error(char* msg) {
vdp_set_text_colour(DARK_RED);
if(currentExpandedMacro) printf("MACRO [%s]",currentExpandedMacro->name);
else if(strlen(filename[FILE_CURRENT])) printf("\"%s\"", filename[FILE_CURRENT]);
if(linenumber) printf(" - line %d - ", linenumber);
printf("%s\r\n", msg);
vdp_set_text_colour(BRIGHT_WHITE);
global_errors++;
if(!global_errors) {
vdp_set_text_colour(DARK_RED);
if(currentExpandedMacro) printf("MACRO [%s]",currentExpandedMacro->name);
else if(strlen(filename[FILE_CURRENT])) printf("\"%s\"", filename[FILE_CURRENT]);
if(linenumber) printf(" - line %d - ", linenumber);
printf("%s\r\n", msg);
vdp_set_text_colour(BRIGHT_WHITE);
global_errors++;
}
}

void trimRight(char *str) {
Expand Down Expand Up @@ -264,6 +266,7 @@ uint8_t getDefineValueToken(streamtoken_t *token, char *src) {
if(length-- == 0) break;
}
}
if(state == TOKEN_STRING) error(message[ERROR_STRING_NOTTERMINATED]);
return length;
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Errors_defines/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# Negative test - assembler needs to fail tests in all subfolders
# return 0 on succesfull test (all failed)
# return 1 on issue during test (one or more tests didn't fail correctly)
# return 2 on error in test SETUP
#

test_number=0
negtest_failed_successfull=0

cd tests
rm -f *.bin
rm -f *.output
for FILE in *; do
if [ -f "$FILE" ]; then
test_number=$((test_number+1))
../$ASMBIN $FILE -c -b FF >> ${FILE%.*}.asm.output
if [ $? -eq 0 ]; then
echo "Failed to detect error in" \'$FILE\'
else
negtest_failed_successfull=$((negtest_failed_successfull+1))
fi
fi
done
rm -f *.bin
cd ..

if [ $test_number -eq $negtest_failed_successfull ]; then
echo "Detected all ($test_number) errors succesfully"
exit 0
else
exit 1
fi
exit 0
1 change: 1 addition & 0 deletions tests/Errors_defines/tests/listformat_db.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db 15=
1 change: 1 addition & 0 deletions tests/Errors_defines/tests/listformat_dw.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dw 15=
1 change: 1 addition & 0 deletions tests/Errors_defines/tests/missing_operand_db.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db 15,
1 change: 1 addition & 0 deletions tests/Errors_defines/tests/missing_operand_dw.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dw 15,

0 comments on commit 7032a16

Please sign in to comment.