From fd581ffc3765165622256d89ce7822fb45c22bbf Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 21 Mar 2022 21:19:32 +0100 Subject: [PATCH] moved pattern_match() from prog8_lib to string module --- .../prog8/codegen/virtual/AssemblyProgram.kt | 31 -------- compiler/res/prog8lib/diskio.p8 | 2 +- compiler/res/prog8lib/prog8_lib.p8 | 72 ------------------- compiler/res/prog8lib/string.p8 | 72 +++++++++++++++++++ compiler/res/prog8lib/virtual/prog8_lib.p8 | 6 +- .../prog8/compiler/IntermediateAstMaker.kt | 5 +- docs/source/todo.rst | 2 - examples/test.p8 | 4 ++ syntax-files/Vim/prog8_builtins.vim | 2 +- 9 files changed, 84 insertions(+), 112 deletions(-) diff --git a/codeGenVirtual/src/prog8/codegen/virtual/AssemblyProgram.kt b/codeGenVirtual/src/prog8/codegen/virtual/AssemblyProgram.kt index f4ac72e79..ae48ae24d 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/AssemblyProgram.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/AssemblyProgram.kt @@ -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""" - -} -*/ diff --git a/compiler/res/prog8lib/diskio.p8 b/compiler/res/prog8lib/diskio.p8 index aecee17db..56f4017ca 100644 --- a/compiler/res/prog8lib/diskio.p8 +++ b/compiler/res/prog8lib/diskio.p8 @@ -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 diff --git a/compiler/res/prog8lib/prog8_lib.p8 b/compiler/res/prog8lib/prog8_lib.p8 index 49c6a4687..0a2b4e5b3 100644 --- a/compiler/res/prog8lib/prog8_lib.p8 +++ b/compiler/res/prog8lib/prog8_lib.p8 @@ -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 - }} - } } diff --git a/compiler/res/prog8lib/string.p8 b/compiler/res/prog8lib/string.p8 index 48d6f5b22..1c66c38ac 100644 --- a/compiler/res/prog8lib/string.p8 +++ b/compiler/res/prog8lib/string.p8 @@ -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 + }} + } + } diff --git a/compiler/res/prog8lib/virtual/prog8_lib.p8 b/compiler/res/prog8lib/virtual/prog8_lib.p8 index a266379a0..cb7ca98d6 100644 --- a/compiler/res/prog8lib/virtual/prog8_lib.p8 +++ b/compiler/res/prog8lib/virtual/prog8_lib.p8 @@ -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 } diff --git a/compiler/src/prog8/compiler/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/IntermediateAstMaker.kt index 910675c93..9d95c7760 100644 --- a/compiler/src/prog8/compiler/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/IntermediateAstMaker.kt @@ -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) + } } } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 1c57dd9ab..4315231e2 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- move prog8_lib.pattern_match to string module - ... diff --git a/examples/test.p8 b/examples/test.p8 index d1da8444d..17212da30 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -15,6 +15,10 @@ main { %breakpoint %asmbinary "LICENSE", 10 ,1 + %asm {{ + nop + }} + ; calculate primes txt.print("prime numbers up to 255:\n\n") diff --git a/syntax-files/Vim/prog8_builtins.vim b/syntax-files/Vim/prog8_builtins.vim index 617806cac..e102624a4 100644 --- a/syntax-files/Vim/prog8_builtins.vim +++ b/syntax-files/Vim/prog8_builtins.vim @@ -798,7 +798,6 @@ syn match prog8BuiltInFunc "\" " prog8_lib.p8 -syn match prog8BuiltInFunc "\" " string.p8 @@ -811,6 +810,7 @@ syn match prog8BuiltInFunc "\" syn match prog8BuiltInFunc "\" syn match prog8BuiltInFunc "\" syn match prog8BuiltInFunc "\" +syn match prog8BuiltInFunc "\" " test_stack.p8