2019-05-15 05:26:29 +10:00
|
|
|
; Parse expression in string at (HL) and returns the result in IX.
|
|
|
|
; We expect (HL) to be disposable: we mutate it to avoid having to make a copy.
|
|
|
|
; Sets Z on success, unset on error.
|
|
|
|
parseExpr:
|
2019-05-15 06:38:19 +10:00
|
|
|
push bc
|
|
|
|
push de
|
2019-05-15 05:26:29 +10:00
|
|
|
push hl
|
|
|
|
ld a, '+'
|
|
|
|
call JUMP_FINDCHAR
|
2019-05-15 06:38:19 +10:00
|
|
|
jr z, .hasExpr
|
|
|
|
pop hl
|
|
|
|
push hl
|
|
|
|
ld a, '-'
|
|
|
|
call JUMP_FINDCHAR
|
2019-05-15 05:26:29 +10:00
|
|
|
jr nz, .noExpr
|
2019-05-15 06:38:19 +10:00
|
|
|
ld c, '-'
|
|
|
|
jr .hasExpr
|
|
|
|
.hasPlus:
|
|
|
|
ld c, '+'
|
|
|
|
jr .hasExpr
|
|
|
|
.hasExpr:
|
|
|
|
; Alright, we have a +/ and we're pointing at it. Let's advance HL and
|
2019-05-15 05:26:29 +10:00
|
|
|
; recurse. But first, let's change this + into a null char. It will be
|
|
|
|
; handy later.
|
|
|
|
xor a
|
|
|
|
ld (hl), a ; + changed to \0
|
|
|
|
|
|
|
|
inc hl
|
2019-05-15 06:38:19 +10:00
|
|
|
pop de ; we pop out the HL we pushed earlier into DE
|
|
|
|
; That's our original beginning of string.
|
|
|
|
call _applyExprToHL
|
|
|
|
pop de
|
|
|
|
pop bc
|
|
|
|
ret
|
|
|
|
|
|
|
|
.noExpr:
|
2019-05-15 05:26:29 +10:00
|
|
|
pop hl
|
2019-05-15 06:38:19 +10:00
|
|
|
pop de
|
|
|
|
pop bc
|
|
|
|
jp parseNumberOrSymbol
|
|
|
|
|
|
|
|
; Parse number or symbol in (DE) and expression in (HL) and apply operator
|
|
|
|
; specified in C to them.
|
|
|
|
_applyExprToHL:
|
|
|
|
call parseExpr
|
2019-05-15 05:26:29 +10:00
|
|
|
ret nz ; return immediately if error
|
|
|
|
; Now we have parsed everything to the right and we have its result in
|
2019-05-15 06:38:19 +10:00
|
|
|
; IX. What we need to do now is parseNumberOrSymbol on (DE) and apply
|
|
|
|
; operator. Let's save IX somewhere and parse this.
|
|
|
|
ex hl, de
|
|
|
|
push ix
|
|
|
|
pop de
|
2019-05-15 05:26:29 +10:00
|
|
|
call parseNumberOrSymbol
|
2019-05-15 06:38:19 +10:00
|
|
|
ret nz ; error
|
|
|
|
; Good! let's do the math! IX has our left part, DE has our right one.
|
|
|
|
ld a, c ; restore operator
|
|
|
|
cp '-'
|
|
|
|
jr z, .sub
|
|
|
|
; addition
|
2019-05-15 05:26:29 +10:00
|
|
|
add ix, de
|
2019-05-15 06:38:19 +10:00
|
|
|
jr .end
|
|
|
|
.sub:
|
|
|
|
push ix
|
|
|
|
pop hl
|
|
|
|
sbc hl, de
|
|
|
|
push hl
|
|
|
|
pop ix
|
2019-05-15 05:26:29 +10:00
|
|
|
.end:
|
2019-05-15 06:38:19 +10:00
|
|
|
cp a ; ensure Z
|
2019-05-15 05:26:29 +10:00
|
|
|
ret
|