mirror of
https://github.com/irmen/prog8.git
synced 2024-12-24 16:29:21 +00:00
moved pattern_match() from prog8_lib to string module
This commit is contained in:
parent
ff57c5e9d3
commit
fd581ffc37
@ -21,34 +21,3 @@ internal class AssemblyProgram(override val name: String,
|
|||||||
return true
|
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"""
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
@ -178,7 +178,7 @@ io_error:
|
|||||||
if not list_skip_disk_name {
|
if not list_skip_disk_name {
|
||||||
if not list_pattern
|
if not list_pattern
|
||||||
return true
|
return true
|
||||||
if prog8_lib.pattern_match(list_filename, list_pattern)
|
if string.pattern_match(list_filename, list_pattern)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
list_skip_disk_name = false
|
list_skip_disk_name = false
|
||||||
|
@ -5,76 +5,4 @@
|
|||||||
prog8_lib {
|
prog8_lib {
|
||||||
%asminclude "library:prog8_lib.asm"
|
%asminclude "library:prog8_lib.asm"
|
||||||
%asminclude "library:prog8_funcs.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
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -225,4 +225,76 @@ _done rts
|
|||||||
_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
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,7 @@
|
|||||||
; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
|
; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
|
||||||
|
|
||||||
prog8_lib {
|
prog8_lib {
|
||||||
|
%option force_output
|
||||||
|
|
||||||
sub pattern_match(str string, str pattern) -> ubyte {
|
; nothing here for now
|
||||||
; TODO
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -187,7 +187,10 @@ class IntermediateAstMaker(val program: Program) {
|
|||||||
val assembly = result.getOrElse { throw it }
|
val assembly = result.getOrElse { throw it }
|
||||||
PtInlineAssembly(assembly, directive.position)
|
PtInlineAssembly(assembly, directive.position)
|
||||||
}
|
}
|
||||||
else -> TODO("directive to PtNode $directive")
|
else -> {
|
||||||
|
// other directives don't output any code
|
||||||
|
PtNop(directive.position)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@ TODO
|
|||||||
|
|
||||||
For next release
|
For next release
|
||||||
^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^
|
||||||
- move prog8_lib.pattern_match to string module
|
|
||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,6 +15,10 @@ main {
|
|||||||
%breakpoint
|
%breakpoint
|
||||||
%asmbinary "LICENSE", 10 ,1
|
%asmbinary "LICENSE", 10 ,1
|
||||||
|
|
||||||
|
%asm {{
|
||||||
|
nop
|
||||||
|
}}
|
||||||
|
|
||||||
|
|
||||||
; calculate primes
|
; calculate primes
|
||||||
txt.print("prime numbers up to 255:\n\n")
|
txt.print("prime numbers up to 255:\n\n")
|
||||||
|
@ -798,7 +798,6 @@ syn match prog8BuiltInFunc "\<diskio\.rename\>"
|
|||||||
|
|
||||||
|
|
||||||
" prog8_lib.p8
|
" prog8_lib.p8
|
||||||
syn match prog8BuiltInFunc "\<prog8_lib\.pattern_match\>"
|
|
||||||
|
|
||||||
|
|
||||||
" string.p8
|
" string.p8
|
||||||
@ -811,6 +810,7 @@ syn match prog8BuiltInFunc "\<string\.copy\>"
|
|||||||
syn match prog8BuiltInFunc "\<string\.compare\>"
|
syn match prog8BuiltInFunc "\<string\.compare\>"
|
||||||
syn match prog8BuiltInFunc "\<string\.lower\>"
|
syn match prog8BuiltInFunc "\<string\.lower\>"
|
||||||
syn match prog8BuiltInFunc "\<string\.upper\>"
|
syn match prog8BuiltInFunc "\<string\.upper\>"
|
||||||
|
syn match prog8BuiltInFunc "\<string\.pattern_match\>"
|
||||||
|
|
||||||
|
|
||||||
" test_stack.p8
|
" test_stack.p8
|
||||||
|
Loading…
Reference in New Issue
Block a user