Skip to content

Commit

Permalink
Bugfix for envenomator#60, additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
envenomator committed Feb 25, 2024
1 parent c56cabb commit c987a5c
Show file tree
Hide file tree
Showing 70 changed files with 145 additions and 419 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ bin/
src/.vs/
*.vsidx
*Browse.VC.db
*.suo
*.suo

# Test output
test.output
tests/*.bin
82 changes: 39 additions & 43 deletions src/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,31 @@ uint8_t getAsciiValue(char *string) {
// labelb-1
// labela+labelb+offset1-1
// The string should not contain any spaces, needs to be a single token
int24_t getValue(char *string, bool req_firstpass) {
uint8_t length,substlength;
int24_t total, tmp;
char operator, *ptr, unary_operator;
label_t *lbl;
int24_t getValue(char *str, bool req_firstpass) {
streamtoken_t token;

ptr = string;
total = 0;
unary_operator = 0;
label_t *lbl;
uint24_t total, tmp;
uint8_t length, substlength;
char prev_op = '+', unary = 0;
bool expect = true;

if((pass == 1) && !req_firstpass) return 0;

operator = '+'; // previous operand in case of single value/label
while(ptr) {
tmp = 0;
length = getOperatorToken(&token, ptr);
if(length) {
if(currentExpandedMacro) {
substlength = macroExpandArg(_macro_VAL_buffer, token.start, currentExpandedMacro);
if(substlength) {
token.start = _macro_VAL_buffer;
length = substlength;
}
total = 0;
while(str) {
length = getOperatorToken(&token, str);
if(currentExpandedMacro) {
substlength = macroExpandArg(_macro_VAL_buffer, token.start, currentExpandedMacro);
if(substlength) {
token.start = _macro_VAL_buffer;
length = substlength;
}

}
if(length == 0) { // at begin, or middle, OK. Expect catch at end
expect = true;
unary = token.terminator;
}
else { // normal processing
lbl = findLabel(token.start);
if(lbl) {
tmp = lbl->address;
Expand All @@ -116,19 +115,19 @@ int24_t getValue(char *string, bool req_firstpass) {
}
}
}
}

if(unary_operator) {
switch(unary_operator) {
case '-': tmp = -tmp; break;
case '~': tmp = ~tmp; break;
case '+': break;
default: break;
if(unary) {
switch(unary) {
case '-': tmp = -tmp; break;
case '~': tmp = ~tmp; break;
case '+': break;
default:
error(message[ERROR_UNARYOPERATOR]);
return 0;
}
unary = 0; // reset
expect = false;
}
}

if(length) { // when an actual value is present between operators
switch(operator) {
switch(prev_op) {
case '+': total += tmp; break;
case '-': total -= tmp; break;
case '*': total *= tmp; break;
Expand All @@ -144,17 +143,14 @@ int24_t getValue(char *string, bool req_firstpass) {
error(message[ERROR_OPERATOR]);
return total;
}
prev_op = token.terminator;
expect = false;
}

if(token.terminator && (length == 0)) {
unary_operator = token.terminator;
}
else {
unary_operator = 0;
operator = token.terminator;
}
if(operator) ptr = token.next;
else ptr = NULL;
str = token.next;
}
if(expect) {
error(message[ERROR_MISSINGOPERAND]);
return 0;
}
return total;
}
Expand Down
1 change: 1 addition & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ char *message[] = {
"Invalid literal format",
"Parse error",
"Invalid operator",
"Illegal unary opeator",
"Argument is not a power of 2",
"Maximum nested level of include files reached",
"Maximum number of macros reached",
Expand Down
1 change: 1 addition & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ enum {
ERROR_ASCIIFORMAT,
ERROR_PARSE,
ERROR_OPERATOR,
ERROR_UNARYOPERATOR,
ERROR_POWER2,
ERROR_MAXINCLUDEFILES,
ERROR_MAXMACROS,
Expand Down
10 changes: 6 additions & 4 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,12 @@ uint8_t getOperatorToken(streamtoken_t *token, char *src) {
if(*src) token->next = shift?src+2:src+1;
else token->next = NULL;

*src-- = 0; // terminate early and revert one character
while(isspace(*src)) { // remove trailing space(s)
*src-- = 0; // terminate on trailing spaces
if(length-- == 0) break;
if(length) {
*src-- = 0; // terminate early and revert one character
while(isspace(*src)) { // remove trailing space(s)
*src-- = 0; // terminate on trailing spaces
if(length-- == 0) break;
}
}
return length;
}
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export ASMBIN=../../bin/ez80asm
NOCOLOR='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
FORMAT='%-20.20s'
FORMAT='%-25.25s'

testresult=2
failed=0
Expand Down
1 change: 0 additions & 1 deletion tests/Conditional_asm/test.output

This file was deleted.

1 change: 0 additions & 1 deletion tests/Errors_labels/test.output

This file was deleted.

1 change: 0 additions & 1 deletion tests/Errors_literals/test.output

This file was deleted.

1 change: 0 additions & 1 deletion tests/Errors_macros/test.output

This file was deleted.

1 change: 0 additions & 1 deletion tests/Errors_opcodes/test.output

This file was deleted.

1 change: 0 additions & 1 deletion tests/Errors_registers/test.output

This file was deleted.

1 change: 0 additions & 1 deletion tests/Errors_strings/test.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/Errors_value_operators/test.output

This file was deleted.

1 change: 1 addition & 0 deletions tests/Errors_value_operators/tests/illegal_unary.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ld a, *1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ld a, 5-*2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ld a, 5/
1 change: 1 addition & 0 deletions tests/Errors_value_operators/tests/missing_operand_unary.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ld a, 5+
1 change: 0 additions & 1 deletion tests/Labels/test.output

This file was deleted.

1 change: 0 additions & 1 deletion tests/Value_operators/test.output

This file was deleted.

73 changes: 73 additions & 0 deletions tests/Value_operators/tests/compound_basics_spaced.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
; Test spacing
ld a, + 1
ld a, +1
ld a, + 1
ld a, + 1
ld a, +1
ld a, 1+1
ld a, 1+ 1
ld a, 1 +1
ld a, 1 + 1
ld a, 1 + 1
ld a, - 1
ld a, -1
ld a, - 1
ld a, - 1
ld a, -1
ld a, 1-1
ld a, 1- 1
ld a, 1 -1
ld a, 1 - 1
ld a, 1 - 1
ld a, ~ 1
ld a, ~1
ld a, ~ 1
ld a, ~ 1
ld a, ~1
ld a, 1+~1
ld a, 1+~ 1
ld a, 1+ ~1
ld a, 1+ ~ 1
ld a, 1+ ~ 1

ld a, 1*1
ld a, 1* 1
ld a, 1 *1
ld a, 1 * 1
ld a, 1 * 1

ld a, 1/1
ld a, 1/ 1
ld a, 1 /1
ld a, 1 / 1
ld a, 1 / 1

ld a, 1<<1
ld a, 1<< 1
ld a, 1 <<1
ld a, 1 << 1
ld a, 1 << 1

ld a, 1>>1
ld a, 1>> 1
ld a, 1 >>1
ld a, 1 >> 1
ld a, 1 >> 1

ld a, 1^1
ld a, 1^ 1
ld a, 1 ^1
ld a, 1 ^ 1
ld a, 1 ^ 1

ld a, 1&1
ld a, 1& 1
ld a, 1 &1
ld a, 1 & 1
ld a, 1 & 1

ld a, 1|1
ld a, 1| 1
ld a, 1 |1
ld a, 1 | 1
ld a, 1 | 1
4 changes: 4 additions & 0 deletions tests/Value_operators/tests/equ_basic.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rows: equ 5
cols: equ 10

ld a, rows
4 changes: 4 additions & 0 deletions tests/Value_operators/tests/equ_two_operands.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rows: equ 5
cols: equ 10

ld a, 5*rows+cols
4 changes: 4 additions & 0 deletions tests/Value_operators/tests/equ_unary.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rows: equ 5
cols: equ 10

ld a, -rows
1 change: 1 addition & 0 deletions tests/Value_operators/tests/unary_2nd_operand_min.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ld a, 1--1
1 change: 1 addition & 0 deletions tests/Value_operators/tests/unary_2nd_operand_not.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ld a, 1+~1
1 change: 1 addition & 0 deletions tests/Value_operators/tests/unary_2nd_operand_plus.s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ld a, 10-+1
5 changes: 0 additions & 5 deletions tests/aligning/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/aligning/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/capital_regs/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/capital_regs/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/data_directives/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data_directives/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/incbin/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/incbin/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/include/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/include/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/line_parser/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/line_parser/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/literals/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/literals/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/macros/asm.output

This file was deleted.

3 changes: 0 additions & 3 deletions tests/macros/test.output

This file was deleted.

5 changes: 0 additions & 5 deletions tests/number_operations/asm.output

This file was deleted.

12 changes: 0 additions & 12 deletions tests/number_operations/reference.bat

This file was deleted.

Binary file removed tests/number_operations/reference.bin
Binary file not shown.
Binary file removed tests/number_operations/test.bin
Binary file not shown.
Loading

0 comments on commit c987a5c

Please sign in to comment.