lib/expr: add division and modulo operators

This commit is contained in:
Virgil Dupras 2019-11-22 15:03:16 -05:00
parent 1b01f13105
commit 972e8221f1
3 changed files with 54 additions and 4 deletions

View File

@ -89,8 +89,8 @@ this way, it's going to mess with the parser.
### Expressions ### Expressions
An expression is a bunch of literals or symbols assembled by operators. For An expression is a bunch of literals or symbols assembled by operators.
now, only `+`, `-` and `*` operators are supported. No parenthesis yet. Supported operators are `+`, `-`, `*`, `/` and `%` (modulo). No parenthesis yet.
Symbols have a different meaning depending on the application. In zasm, it's Symbols have a different meaning depending on the application. In zasm, it's
labels and constants. In basic, it's variables. labels and constants. In basic, it's variables.

View File

@ -113,6 +113,10 @@ exprTbl:
.dw .minus .dw .minus
.db '*' .db '*'
.dw .mult .dw .mult
.db '/'
.dw .div
.db '%'
.dw .mod
.db 0 ; end of table .db 0 ; end of table
.plus: .plus:
@ -137,3 +141,19 @@ exprTbl:
push hl \ pop ix push hl \ pop ix
cp a ; ensure Z cp a ; ensure Z
ret ret
.div:
; divide takes HL/DE
push bc
ex de, hl
push ix \ pop de
call divide
push bc \ pop ix
pop bc
cp a ; ensure Z
ret
.mod:
call .div
push hl \ pop ix
ret

View File

@ -42,9 +42,9 @@ sFOO: .db "FOO", 0
sBAR: .db "BAR", 0 sBAR: .db "BAR", 0
test: test:
ld hl, 0xffff ld sp, 0xffff
ld sp, hl
; Old-style tests, not touching them now.
ld hl, s1 ld hl, s1
call parseExpr call parseExpr
jp nz, fail jp nz, fail
@ -128,10 +128,40 @@ test:
jp nz, fail jp nz, fail
call nexttest call nexttest
; New-style tests
call testParseExpr
; success ; success
xor a xor a
halt halt
testParseExpr:
ld iy, .t1
call .testEQ
ld iy, .t2
call .testEQ
ret
.testEQ:
push iy \ pop hl
inc hl \ inc hl
call parseExpr
jp nz, fail
push ix \ pop de
ld a, e
cp (iy)
jp nz, fail
ld a, d
cp (iy+1)
jp nz, fail
jp nexttest
.t1:
.dw 7
.db "42/6", 0
.t2:
.dw 1
.db "7%3", 0
nexttest: nexttest:
ld a, (testNum) ld a, (testNum)
inc a inc a