moved pattern_match() from prog8_lib to string module

This commit is contained in:
Irmen de Jong 2022-03-21 21:19:32 +01:00
parent ff57c5e9d3
commit fd581ffc37
9 changed files with 84 additions and 112 deletions

View File

@ -21,34 +21,3 @@ internal class AssemblyProgram(override val name: String,
return true
}
}
/*
; enable lores gfx screen
load r0, 0
syscall 8
load.w r10, 320
load.w r11, 240
load.b r12, 0
_forever:
load.w r1, 0
_yloop:
load.w r0, 0
_xloop:
mul.b r2,r0,r1
add.b r2,r2,r12
syscall 10
addi.w r0,r0,1
blt.w r0, r10, _xloop
addi.w r1,r1,1
blt.w r1, r11,_yloop
addi.b r12,r12,1
jump _forever
load.w r0, 2000
syscall 7
load.w r0,0
return"""
}
*/

View File

@ -178,7 +178,7 @@ io_error:
if not list_skip_disk_name {
if not list_pattern
return true
if prog8_lib.pattern_match(list_filename, list_pattern)
if string.pattern_match(list_filename, list_pattern)
return true
}
list_skip_disk_name = false

View File

@ -5,76 +5,4 @@
prog8_lib {
%asminclude "library:prog8_lib.asm"
%asminclude "library:prog8_funcs.asm"
asmsub pattern_match(str string @AY, str pattern @R0) clobbers(Y) -> ubyte @A {
%asm {{
; 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
ldx #$00 ; x is an index in the pattern
ldy #$ff ; y is an index in the string
modify_pattern1
next lda $ffff,x ; look at next pattern character MODIFIED
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
reg cmp (str),y ; are both characters the same?
bne fail ; no, so no match
inx ; yes, keep checking
cmp #0 ; are we at end of string?
bne next ; not yet, loop
found rts ; success, return with c=1
star inx ; skip star in pattern
modify_pattern2
cmp $ffff,x ; string of stars equals one star MODIFIED
beq star ; so skip them also
stloop txa ; we first try to match with * = ""
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
fail clc ; yes, no match found, return with c=0
rts
}}
}
}

View File

@ -225,4 +225,76 @@ _done rts
_done rts
}}
}
asmsub pattern_match(str string @AY, str pattern @R0) clobbers(Y) -> ubyte @A {
%asm {{
; 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
ldx #$00 ; x is an index in the pattern
ldy #$ff ; y is an index in the string
modify_pattern1
next lda $ffff,x ; look at next pattern character MODIFIED
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
reg cmp (str),y ; are both characters the same?
bne fail ; no, so no match
inx ; yes, keep checking
cmp #0 ; are we at end of string?
bne next ; not yet, loop
found rts ; success, return with c=1
star inx ; skip star in pattern
modify_pattern2
cmp $ffff,x ; string of stars equals one star MODIFIED
beq star ; so skip them also
stloop txa ; we first try to match with * = ""
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
fail clc ; yes, no match found, return with c=0
rts
}}
}
}

View File

@ -3,9 +3,7 @@
; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
prog8_lib {
%option force_output
sub pattern_match(str string, str pattern) -> ubyte {
; TODO
return 0
}
; nothing here for now
}

View File

@ -187,7 +187,10 @@ class IntermediateAstMaker(val program: Program) {
val assembly = result.getOrElse { throw it }
PtInlineAssembly(assembly, directive.position)
}
else -> TODO("directive to PtNode $directive")
else -> {
// other directives don't output any code
PtNop(directive.position)
}
}
}

View File

@ -3,8 +3,6 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- move prog8_lib.pattern_match to string module
...

View File

@ -15,6 +15,10 @@ main {
%breakpoint
%asmbinary "LICENSE", 10 ,1
%asm {{
nop
}}
; calculate primes
txt.print("prime numbers up to 255:\n\n")

View File

@ -798,7 +798,6 @@ syn match prog8BuiltInFunc "\<diskio\.rename\>"
" prog8_lib.p8
syn match prog8BuiltInFunc "\<prog8_lib\.pattern_match\>"
" string.p8
@ -811,6 +810,7 @@ syn match prog8BuiltInFunc "\<string\.copy\>"
syn match prog8BuiltInFunc "\<string\.compare\>"
syn match prog8BuiltInFunc "\<string\.lower\>"
syn match prog8BuiltInFunc "\<string\.upper\>"
syn match prog8BuiltInFunc "\<string\.pattern_match\>"
" test_stack.p8