2021-01-07 00:56:31 +00:00
|
|
|
; Internal library routines - always included by the compiler
|
2017-12-30 12:34:52 +00:00
|
|
|
;
|
2018-01-08 02:31:23 +00:00
|
|
|
; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
|
2017-12-30 12:34:52 +00:00
|
|
|
|
2019-07-29 21:11:13 +00:00
|
|
|
prog8_lib {
|
2021-05-15 22:14:57 +00:00
|
|
|
%asminclude "library:prog8_lib.asm"
|
|
|
|
%asminclude "library:prog8_funcs.asm"
|
2020-12-30 22:34:00 +00:00
|
|
|
|
2021-11-08 18:06:26 +00:00
|
|
|
; to store intermediary expression results for return values:
|
2021-11-07 13:19:21 +00:00
|
|
|
; NOTE: these variables are used in the StatementReorderer and StatementOptimizer
|
|
|
|
uword @zp retval_interm_uw
|
|
|
|
word @zp retval_interm_w
|
|
|
|
ubyte @zp retval_interm_ub
|
|
|
|
byte @zp retval_interm_b
|
2021-11-08 18:06:26 +00:00
|
|
|
word retval_interm_w2
|
|
|
|
byte retval_interm_b2
|
2021-11-07 13:19:21 +00:00
|
|
|
|
2020-12-30 22:34:00 +00:00
|
|
|
|
2021-01-07 00:56:31 +00:00
|
|
|
asmsub pattern_match(str string @AY, str pattern @R0) clobbers(Y) -> ubyte @A {
|
|
|
|
%asm {{
|
2020-12-30 22:34:00 +00:00
|
|
|
; pattern matching of a string.
|
|
|
|
; Input: cx16.r0: A NUL-terminated, <255-length pattern
|
|
|
|
; AY: A NUL-terminated, <255-length string
|
|
|
|
;
|
|
|
|
; Output: A = 1 if the string matches the pattern, A = 0 if not.
|
|
|
|
;
|
|
|
|
; Notes: Clobbers A, X, Y. Each * in the pattern uses 4 bytes of stack.
|
|
|
|
;
|
|
|
|
; see http://6502.org/source/strings/patmatch.htm
|
|
|
|
|
|
|
|
str = P8ZP_SCRATCH_W1
|
|
|
|
|
|
|
|
stx P8ZP_SCRATCH_REG
|
|
|
|
sta str
|
|
|
|
sty str+1
|
|
|
|
lda cx16.r0
|
|
|
|
sta modify_pattern1+1
|
|
|
|
sta modify_pattern2+1
|
|
|
|
lda cx16.r0+1
|
|
|
|
sta modify_pattern1+2
|
|
|
|
sta modify_pattern2+2
|
|
|
|
jsr _match
|
|
|
|
lda #0
|
|
|
|
adc #0
|
|
|
|
ldx P8ZP_SCRATCH_REG
|
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
_match
|
2021-01-07 00:56:31 +00:00
|
|
|
ldx #$00 ; x is an index in the pattern
|
|
|
|
ldy #$ff ; y is an index in the string
|
2020-12-30 22:34:00 +00:00
|
|
|
modify_pattern1
|
|
|
|
next lda $ffff,x ; look at next pattern character MODIFIED
|
2021-01-07 00:56:31 +00:00
|
|
|
cmp #'*' ; is it a star?
|
|
|
|
beq star ; yes, do the complicated stuff
|
|
|
|
iny ; no, let's look at the string
|
|
|
|
cmp #'?' ; is the pattern caracter a ques?
|
|
|
|
bne reg ; no, it's a regular character
|
|
|
|
lda (str),y ; yes, so it will match anything
|
|
|
|
beq fail ; except the end of string
|
2020-12-30 22:34:00 +00:00
|
|
|
reg cmp (str),y ; are both characters the same?
|
2021-01-07 00:56:31 +00:00
|
|
|
bne fail ; no, so no match
|
|
|
|
inx ; yes, keep checking
|
|
|
|
cmp #0 ; are we at end of string?
|
|
|
|
bne next ; not yet, loop
|
2020-12-30 22:34:00 +00:00
|
|
|
found rts ; success, return with c=1
|
|
|
|
|
|
|
|
star inx ; skip star in pattern
|
|
|
|
modify_pattern2
|
2021-01-07 00:56:31 +00:00
|
|
|
cmp $ffff,x ; string of stars equals one star MODIFIED
|
|
|
|
beq star ; so skip them also
|
2020-12-30 22:34:00 +00:00
|
|
|
stloop txa ; we first try to match with * = ""
|
2021-01-07 00:56:31 +00:00
|
|
|
pha ; and grow it by 1 character every
|
|
|
|
tya ; time we loop
|
|
|
|
pha ; save x and y on stack
|
|
|
|
jsr next ; recursive call
|
|
|
|
pla ; restore x and y
|
|
|
|
tay
|
|
|
|
pla
|
|
|
|
tax
|
|
|
|
bcs found ; we found a match, return with c=1
|
|
|
|
iny ; no match yet, try to grow * string
|
|
|
|
lda (str),y ; are we at the end of string?
|
|
|
|
bne stloop ; not yet, add a character
|
2020-12-30 22:34:00 +00:00
|
|
|
fail clc ; yes, no match found, return with c=0
|
2021-01-07 00:56:31 +00:00
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
2018-12-24 23:33:04 +00:00
|
|
|
}
|