zasm: begin erroring out reliably

Up until now, invalid source input had undefined behavior. We're now
beginning to define that behavior so that zasm can be a bit more usable.
This commit is contained in:
Virgil Dupras 2019-05-27 11:04:31 -04:00
parent 976a93971c
commit af2c561c6b
5 changed files with 36 additions and 9 deletions

View File

@ -15,6 +15,10 @@
.equ ZASM_ORG ZASM_CTX_PC+2
.equ ZASM_RAMEND ZASM_ORG+2
; *** Errors ***
; Unknown instruction or directive
.equ ERR_UNKWN 0x01
; Read file through blockdev ID in H and outputs its upcodes through blockdev
; ID in L.
zasmMain:
@ -98,7 +102,7 @@ zasmParseFile:
; resulting opcode(s) through ioPutC and increases (IO_PC) by the number of
; bytes written. BC is set to the result of the call to tokenize.
; Sets Z if parse was successful, unset if there was an error. EOF is not an
; error.
; error. If there is an error, A is set to the corresponding error code (ERR_*).
parseLine:
call tokenize
ld a, b ; TOK_*
@ -109,9 +113,10 @@ parseLine:
cp TOK_LABEL
jr z, _parseLabel
cp TOK_EOF
ret ; Z is correct. If EOF, Z is set and not an
; error, otherwise, it means bad token and
; errors out.
ret z ; We're finished, no error.
; Bad token
ld a, ERR_UNKWN
jp unsetZ ; return with Z unset
_parseInstr:
ld a, c ; I_*

Binary file not shown.

View File

@ -188,9 +188,10 @@ int main()
}
#endif
fflush(stdout);
#ifdef DEBUG
fprintf(stderr, "Ended with A=%d DE=%d\n", cpu.R1.br.A, cpu.R1.wr.DE);
#endif
return 0;
int res = cpu.R1.br.A;
if (res != 0) {
fprintf(stderr, "Error %d\n", res);
}
return res;
}

20
tools/tests/zasm/errtests.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/sh
# no "set -e" because we test errors
ZASM=../../emul/zasm/zasm
chkerr() {
echo "Check that '$1' results in error $2"
${ZASM} <<< $1 > /dev/null
local res=$?
if [[ $res == $2 ]]; then
echo "Good!"
else
echo "$res != $2"
exit 1
fi
}
chkerr "foo" 1

View File

@ -23,7 +23,7 @@ cmpas() {
fi
}
for fn in *.asm; do
for fn in test*.asm; do
echo "Comparing ${fn}"
cmpas $fn
done
@ -34,3 +34,4 @@ while read line; do
cmpas ${TMPFILE}
done
./errtests.sh