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
An expression is a bunch of literals or symbols assembled by operators. For
now, only `+`, `-` and `*` operators are supported. No parenthesis yet.
An expression is a bunch of literals or symbols assembled by operators.
Supported operators are `+`, `-`, `*`, `/` and `%` (modulo). No parenthesis yet.
Symbols have a different meaning depending on the application. In zasm, it's
labels and constants. In basic, it's variables.

View File

@ -113,6 +113,10 @@ exprTbl:
.dw .minus
.db '*'
.dw .mult
.db '/'
.dw .div
.db '%'
.dw .mod
.db 0 ; end of table
.plus:
@ -137,3 +141,19 @@ exprTbl:
push hl \ pop ix
cp a ; ensure Z
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
test:
ld hl, 0xffff
ld sp, hl
ld sp, 0xffff
; Old-style tests, not touching them now.
ld hl, s1
call parseExpr
jp nz, fail
@ -128,10 +128,40 @@ test:
jp nz, fail
call nexttest
; New-style tests
call testParseExpr
; success
xor a
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:
ld a, (testNum)
inc a