1
0
mirror of https://github.com/hsoft/collapseos.git synced 2025-04-02 09:18:40 +11:00

zasm: support char literals

This commit is contained in:
Virgil Dupras 2019-05-14 13:53:12 -04:00
parent 2de69ee7cd
commit a486a2a81e
4 changed files with 59 additions and 13 deletions

View File

@ -27,7 +27,7 @@ handleDB:
call toWord call toWord
call readWord call readWord
ld hl, scratchpad ld hl, scratchpad
call parseNumber call parseLiteral
ld a, ixl ld a, ixl
ld (direcData), a ld (direcData), a
ld a, 1 ld a, 1

View File

@ -13,7 +13,7 @@ parseDecimalDigit:
ret ret
; Parse string at (HL) as a decimal value and return value in IX under the ; Parse string at (HL) as a decimal value and return value in IX under the
; same conditions as parseNumber. ; same conditions as parseLiteral.
parseDecimal: parseDecimal:
push hl push hl
push de push de
@ -60,8 +60,10 @@ parseDecimal:
; Parse string at (HL) as a hexadecimal value and return value in IX under the ; Parse string at (HL) as a hexadecimal value and return value in IX under the
; same conditions as parseNumber. ; same conditions as parseLiteral.
parseHexadecimal: parseHexadecimal:
call hasHexPrefix
ret nz
push hl push hl
xor a xor a
ld ixh, a ld ixh, a
@ -106,19 +108,55 @@ hasHexPrefix:
pop hl pop hl
ret ret
; Parses the string at (HL) and returns the 16-bit value in IX. ; Parse string at (HL) and, if it is a char literal, sets Z and return
; corresponding value in IXL. Clears IXH.
;
; A valid char literal starts with ', ends with ' and has one character in the
; middle. No escape sequence are accepted, but ''' will return the apostrophe
; character.
parseCharLiteral:
ld a, 0x27 ; apostrophe (') char
cp (hl)
ret nz
push hl
inc hl
inc hl
cp (hl)
jr nz, .end ; not ending with an apostrophe
inc hl
ld a, (hl)
or a ; cp 0
jr nz, .end ; string has to end there
; Valid char, good
ld ixh, a ; A is zero, take advantage of that
dec hl
dec hl
ld a, (hl)
ld ixl, a
cp a ; ensure Z
.end:
pop hl
ret
; Parses the string at (HL) and returns the 16-bit value in IX. The string
; can be a decimal literal (1234), a hexadecimal literal (0x1234) or a char
; literal ('X').
;
; As soon as the number doesn't fit 16-bit any more, parsing stops and the ; As soon as the number doesn't fit 16-bit any more, parsing stops and the
; number is invalid. If the number is valid, Z is set, otherwise, unset. ; number is invalid. If the number is valid, Z is set, otherwise, unset.
parseNumber: parseLiteral:
call hasHexPrefix call parseCharLiteral
jr z, parseHexadecimal ret z
jr parseDecimal call parseHexadecimal
ret z
jp parseDecimal
; Parse string in (HL) and return its numerical value whether its a number ; Parse string in (HL) and return its numerical value whether its a number
; literal or a symbol. Returns value in IX. ; literal or a symbol. Returns value in IX.
; Sets Z if number or symbol is valid, unset otherwise. ; Sets Z if number or symbol is valid, unset otherwise.
parseNumberOrSymbol: parseNumberOrSymbol:
call parseNumber call parseLiteral
ret z ret z
call zasmIsFirstPass call zasmIsFirstPass
ret z ; first pass? we don't care about the value, ret z ; first pass? we don't care about the value,

View File

@ -21,13 +21,14 @@ cmpas() {
fi fi
} }
for fn in *.asm; do
echo "Comparing ${fn}"
cmpas $fn
done
./geninstrs.py $ASMFILE | \ ./geninstrs.py $ASMFILE | \
while read line; do while read line; do
echo $line | tee "${TMPFILE}" echo $line | tee "${TMPFILE}"
cmpas ${TMPFILE} cmpas ${TMPFILE}
done done
for fn in *.asm; do
echo "Comparing ${fn}"
cmpas $fn
done

View File

@ -0,0 +1,7 @@
; test literals parsing
ld a, 42
ld a, 0x42
ld hl, 0x4234
ld hl, (0x4234)
ld a, 'X'