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 {
|
2020-10-04 16:18:58 +00:00
|
|
|
%asminclude "library:prog8_lib.asm", ""
|
2020-11-05 00:26:45 +00:00
|
|
|
%asminclude "library:prog8_funcs.asm", ""
|
2020-12-30 22:34:00 +00:00
|
|
|
|
2021-01-07 21:49:54 +00:00
|
|
|
uword @zp retval_interm_uw ; to store intermediary expression results for return values (hopefully allocated on ZP to reduce code size)
|
|
|
|
word @zp retval_interm_w ; to store intermediary expression results for return values (hopefully allocated on ZP to reduce code size)
|
|
|
|
ubyte @zp retval_interm_ub ; to store intermediary expression results for return values (hopefully allocated on ZP to reduce code size)
|
|
|
|
byte @zp retval_interm_b ; to store intermediary expression results for return values (hopefully allocated on ZP to reduce code size)
|
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
|
|
|
}
|