integrated operator recognition routine

This commit is contained in:
FeralChild64 committed 2020-06-07 15:01:09 +02:00
1 parent 1b26151a3f
commit 831bc8f453
3 files changed
+131 -44

No files matched your search

+19 -19
View File
@@ -129,26 +129,22 @@ FRMEVL_got_value:
FRMEVL_fetch_operator: FRMEVL_fetch_operator:
// We have a value, but this is not the end of expression. We expect // We have a value, but this is not the end of expression. Check if
// a two argument operator here. First make sure we have enough space // there is an operator waiting to be served
// on a stack - we will surely need it
jsr fetch_operator
bcs FRMEVL_calculate // branch if operator not recognized
// We have a two-argument operator - check if there is enough stack space
// XXX this can probably be checked later
tsx tsx
cpx #$40 // XXX is this a safe threshold? cpx #$40 // XXX is this a safe threshold?
bcc_16 do_FORMULA_TOO_COMPLEX_error bcc_16 do_FORMULA_TOO_COMPLEX_error
// Now look for an operator // XXX check the priorities - either push to stack, or execute it immediately
// XXX for now only addition operator is implemented
jsr fetch_character bcs FRMEVL_push_value_operator // branch always
cmp #$AA // check for a plus operator
beq FRMEVL_push_value_operator
// Something unknown - consider this the end of expression
jsr unconsume_character
// FALTROUGH
FRMEVL_calculate: // this is the exit point for the operators FRMEVL_calculate: // this is the exit point for the operators
@@ -168,7 +164,11 @@ FRMEVL_done:
FRMEVL_push_value_operator: FRMEVL_push_value_operator:
// First push the return address // Move the operator ID in .Y
tay
// Push the return address
lda #>(FRMEVL_calculate - 1) lda #>(FRMEVL_calculate - 1)
pha pha
@@ -204,16 +204,16 @@ FRMEVL_push_operator_address:
// Push the operator address to stack, in a form // Push the operator address to stack, in a form
// suitable for RTS // suitable for RTS
// XXX add support for other operators // XXX add support for other operators, use .Y
lda #>(oper_add - 1) lda operator_jumptable_hi - 1, y
pha pha
lda #<(oper_add - 1) lda operator_jumptable_lo - 1, y
pha pha
// Push operator priority // Push operator priority
lda #$01 // XXX add support for other operators lda operator_priorities - 1, y
pha pha
// Continue // Continue
+93
View File
@@ -0,0 +1,93 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Fetches an operator - for the expression parser
//
// Note: does not recognize unary operators
//
fetch_operator: // XXX change the operator code to start from 0
// Retrieve the next character
jsr fetch_character
// Check if within range
sec
sbc #$A9
bcc fetch_operator_failed // branch if below 0
beq fetch_operator_failed // branch 0
cmp #$0B
bcs fetch_operator_failed // branch if $0B or greater
// Now we have to recognize '>=', '<=' and '<>' operators
cmp #$08
beq fetch_operator_try_gteq
cmp #$0A
beq fetch_operator_try_lteq_neq
// FALLTROUGH
fetch_operator_success:
clc
rts
fetch_operator_failed:
sec
rts
fetch_operator_try_gteq:
// Here we distinguish between '>' and '>='
jsr fetch_character
cmp #$B2 // '='
beq !+
// This is a '>'
jsr unconsume_character
lda #$08
bne fetch_operator_success // branch always
!:
// This is a '>='
lda #$0B
bne fetch_operator_success // branch always
fetch_operator_try_lteq_neq:
// Here we distinguish between '<', '<=', and '<>'
jsr fetch_character
cmp #$B2 // '='
beq !+
cmp #$B1 // '>'
beq !++
// This is a '<'
jsr unconsume_character
lda #$0A
bne fetch_operator_success // branch always
!:
// This is a '<='
lda #$0C
bne fetch_operator_success // branch always
!:
// This is a '<>'
lda #$0D
bne fetch_operator_success // branch always
+19 -25
View File
@@ -9,38 +9,32 @@
.const operator_list = List().add( .const operator_list = List().add(
// Tokens XXX // Tokens $AA-$B0
oper_add, oper_add, // ID: $01
oper_sub, oper_sub, // ID: $02
oper_mul, oper_mul, // ID: $03
oper_div, oper_div, // ID: $04
oper_pow, // ID: $05
oper_and, // ID: $06
oper_or, // ID: $07
// Tokens XXX // Tokens $B1-$B3
oper_and, oper_cmp_gt, // ID: $08
oper_or, oper_cmp_eq, // ID: $09
oper_cmp_lt, // ID: $0A
// Tokens XXX
oper_cmp_gt,
oper_cmp_eq,
oper_cmp_lt,
// Combined two tokens // Combined two tokens
oper_cmp_gteq, oper_cmp_gteq, // ID: $0B
oper_cmp_lteq, oper_cmp_lteq, // ID: $0C
oper_cmp_neq, oper_cmp_neq, // ID: $0D
// Not a token // Unary operators
oper_pow, oper_unary_minus, // ID: $0E
oper_unary_not // ID: $0F
// Unary
oper_unary_minus,
oper_unary_not
) )
@@ -50,6 +44,7 @@ operator_priorities: // higher number = higher priority
.byte $05 // oper_sub .byte $05 // oper_sub
.byte $06 // oper_mul .byte $06 // oper_mul
.byte $06 // oper_div .byte $06 // oper_div
.byte $08 // oper_pow
.byte $02 // oper_and .byte $02 // oper_and
.byte $01 // oper_or .byte $01 // oper_or
.byte $04 // oper_cmp_gt .byte $04 // oper_cmp_gt
@@ -58,7 +53,6 @@ operator_priorities: // higher number = higher priority
.byte $04 // oper_cmp_gteq .byte $04 // oper_cmp_gteq
.byte $04 // oper_cmp_lteq .byte $04 // oper_cmp_lteq
.byte $04 // oper_cmp_neq .byte $04 // oper_cmp_neq
.byte $08 // oper_pow
.byte $07 // oper_unary_minus .byte $07 // oper_unary_minus
.byte $03 // oper_unary_not .byte $03 // oper_unary_not