diff --git a/apps/README.md b/apps/README.md index 304aa9e..6265bd6 100644 --- a/apps/README.md +++ b/apps/README.md @@ -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. diff --git a/apps/lib/expr.asm b/apps/lib/expr.asm index e144d46..1816e3f 100644 --- a/apps/lib/expr.asm +++ b/apps/lib/expr.asm @@ -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 diff --git a/tools/tests/unit/test_expr.asm b/tools/tests/unit/test_expr.asm index a906ad5..1c11d75 100644 --- a/tools/tests/unit/test_expr.asm +++ b/tools/tests/unit/test_expr.asm @@ -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