1
0
mirror of https://github.com/hsoft/collapseos.git synced 2025-02-17 19:06:02 +11:00

lib/expr: refactor for easier operator addition

This commit is contained in:
Virgil Dupras 2019-11-22 14:45:12 -05:00
parent fd5b2ab856
commit 1b01f13105
9 changed files with 95 additions and 47 deletions

View File

@ -25,3 +25,20 @@ divide:
sbc a, d sbc a, d
ld h, a ld h, a
ret ret
; DE * BC -> DE (high) and HL (low)
multDEBC:
ld hl, 0
ld a, 0x10
.loop:
add hl, hl
rl e
rl d
jr nc, .noinc
add hl, bc
jr nc, .noinc
inc de
.noinc:
dec a
jr nz, .loop
ret

View File

@ -23,16 +23,30 @@ parseExpr:
ret ret
_parseExpr: _parseExpr:
ld a, '+' ld de, exprTbl
.loop:
ld a, (de)
or a
jp z, EXPR_PARSE ; no operator, just parse the literal
push de ; --> lvl 1. save operator row
call _findAndSplit call _findAndSplit
jp z, _applyPlus jr z, .found
ld a, '-' pop de ; <-- lvl 1
call _findAndSplit inc de \ inc de \ inc de
jp z, _applyMinus jr .loop
ld a, '*' .found:
call _findAndSplit ; Operator found, string splitted. Left in (HL), right in (DE)
jp z, _applyMult call _resolveLeftAndRight
jp EXPR_PARSE ; Whether _resolveLeftAndRight was a success, we pop our lvl 1 stack
; out, which contains our operator row. We pop it in HL because we
; don't need our string anymore. L-R numbers are parsed, and in DE and
; IX.
pop hl ; <-- lvl 1
ret nz
; Resolving left and right succeeded, proceed!
inc hl ; point to routine pointer
call intoHL
jp (hl)
; Given a string in (HL) and a separator char in A, return a splitted string, ; Given a string in (HL) and a separator char in A, return a splitted string,
; that is, the same (HL) string but with the found A char replaced by a null ; that is, the same (HL) string but with the found A char replaced by a null
@ -88,20 +102,25 @@ _resolveLeftAndRight:
pop de ; numeric left expr result in DE pop de ; numeric left expr result in DE
jp parseExpr jp parseExpr
; Parse expr in (HL) and expr in (DE) and apply + operator to both sides. ; Routines in here all have the same signature: they take two numbers, DE (left)
; Put result in IX. ; and IX (right), apply the operator and put the resulting number in IX.
_applyPlus: ; The table has 3 bytes per row: 1 byte for operator and 2 bytes for routine
call _resolveLeftAndRight ; pointer.
ret nz exprTbl:
; Good! let's do the math! IX has our right part, DE has our left one. .db '+'
.dw .plus
.db '-'
.dw .minus
.db '*'
.dw .mult
.db 0 ; end of table
.plus:
add ix, de add ix, de
cp a ; ensure Z cp a ; ensure Z
ret ret
; Same as _applyPlus but with - .minus:
_applyMinus:
call _resolveLeftAndRight
ret nz
push ix push ix
pop hl pop hl
ex de, hl ex de, hl
@ -112,9 +131,7 @@ _applyMinus:
cp a ; ensure Z cp a ; ensure Z
ret ret
_applyMult: .mult:
call _resolveLeftAndRight
ret nz
push ix \ pop bc push ix \ pop bc
call multDEBC call multDEBC
push hl \ pop ix push hl \ pop ix

View File

@ -73,20 +73,3 @@ strlen:
pop hl pop hl
pop bc pop bc
ret ret
; DE * BC -> DE (high) and HL (low)
multDEBC:
ld hl, 0
ld a, 0x10
.loop:
add hl, hl
rl e
rl d
jr nc, .noinc
add hl, bc
jr nc, .noinc
inc de
.noinc:
dec a
jr nz, .loop
ret

View File

@ -66,6 +66,7 @@ jp zasmMain
.inc "core.asm" .inc "core.asm"
.inc "zasm/const.asm" .inc "zasm/const.asm"
.inc "lib/util.asm" .inc "lib/util.asm"
.inc "lib/ari.asm"
.inc "lib/parse.asm" .inc "lib/parse.asm"
.inc "lib/args.asm" .inc "lib/args.asm"
.inc "zasm/util.asm" .inc "zasm/util.asm"

View File

@ -3,6 +3,7 @@ jp test
.inc "core.asm" .inc "core.asm"
.inc "str.asm" .inc "str.asm"
.inc "lib/util.asm" .inc "lib/util.asm"
.inc "lib/ari.asm"
.inc "lib/parse.asm" .inc "lib/parse.asm"
.equ EXPR_PARSE parseLiteral .equ EXPR_PARSE parseLiteral
.inc "lib/expr.asm" .inc "lib/expr.asm"

View File

@ -12,6 +12,7 @@ jp test
.inc "core.asm" .inc "core.asm"
.inc "str.asm" .inc "str.asm"
.inc "lib/util.asm" .inc "lib/util.asm"
.inc "lib/ari.asm"
.inc "zasm/util.asm" .inc "zasm/util.asm"
.inc "zasm/const.asm" .inc "zasm/const.asm"
.inc "lib/parse.asm" .inc "lib/parse.asm"

View File

@ -0,0 +1,35 @@
jp test
.inc "core.asm"
.inc "lib/ari.asm"
testNum: .db 1
test:
ld sp, 0xffff
ld de, 12
ld bc, 4
call multDEBC
ld a, l
cp 48
jp nz, fail
call nexttest
; success
xor a
halt
nexttest:
ld a, (testNum)
inc a
ld (testNum), a
ret
fail:
ld a, (testNum)
halt

View File

@ -12,14 +12,6 @@ test:
ld hl, 0xffff ld hl, 0xffff
ld sp, hl ld sp, hl
ld de, 12
ld bc, 4
call multDEBC
ld a, l
cp 48
jp nz, fail
call nexttest
ld hl, sFoo ld hl, sFoo
call strlen call strlen
cp 3 cp 3

View File

@ -4,6 +4,7 @@ jp runTests
.inc "core.asm" .inc "core.asm"
.inc "str.asm" .inc "str.asm"
.inc "zasm/const.asm" .inc "zasm/const.asm"
.inc "lib/ari.asm"
.inc "lib/util.asm" .inc "lib/util.asm"
.inc "zasm/util.asm" .inc "zasm/util.asm"
.inc "lib/parse.asm" .inc "lib/parse.asm"