get regex searching working
This commit is contained in:
parent
de85141c84
commit
21a31755b6
8
grep.tal
8
grep.tal
|
@ -3,7 +3,7 @@
|
|||
( by d_m )
|
||||
|
||||
( print a character to STDOUT )
|
||||
%emit { #18 DEO }
|
||||
%emitt { #18 DEO }
|
||||
|
||||
( the first argument to grep is the regex )
|
||||
( arguments are passed on STDIN, so we just )
|
||||
|
@ -18,8 +18,8 @@
|
|||
|
||||
@println ( s* -> )
|
||||
&loop LDAk #00 EQU ,&eof JCN ( did we reach \0 ? )
|
||||
LDAk emit INC2 ,&loop JMP ( no so emit a char and repeat )
|
||||
&eof #0a emit POP2 JMP2r ( yes so emit \n and return )
|
||||
LDAk emitt INC2 ,&loop JMP ( no so emit a char and repeat )
|
||||
&eof #0a emitt POP2 JMP2r ( yes so emit \n and return )
|
||||
|
||||
@r-read-stdin ( -> )
|
||||
#12 DEI #0a EQU ,&execute JCN ( did we read \n ? )
|
||||
|
@ -34,7 +34,7 @@
|
|||
,&need-regex JCN ( jump if unset )
|
||||
|
||||
( regex is set )
|
||||
;buffer ;regex LDA2 ;match JSR2 ( match regex )
|
||||
;buffer ;regex LDA2 ;search JSR2 ( search line for a regex match )
|
||||
#00 EQU ,&no-match JCN ( did we match? )
|
||||
;buffer ;println JSR2 ( print any match )
|
||||
&no-match BRK ( return )
|
||||
|
|
38
regex.tal
38
regex.tal
|
@ -35,6 +35,14 @@
|
|||
%newline { #0a emit }
|
||||
%quit! { #01 #0f DEO BRK }
|
||||
|
||||
( now that uxnasm throws errors about writing into the zero page )
|
||||
( we have to do something like this to be able to compile library )
|
||||
( code. we have to guess what offset to use since it needs to )
|
||||
( avoid conficting with the program we're included in. )
|
||||
( )
|
||||
( remove this if needed when including it in other projects. )
|
||||
|2000
|
||||
|
||||
( ERROR HANDLING )
|
||||
|
||||
( using error! will print the given message before causing )
|
||||
|
@ -66,9 +74,24 @@
|
|||
( )
|
||||
( returns true if the string, and false otherwise. )
|
||||
@match ( str* regex* -> bool^ )
|
||||
#00 ;search-mode STA
|
||||
;reset-stack JSR2
|
||||
;loop JMP2
|
||||
|
||||
( )
|
||||
@search ( str* regex* -> bool^ )
|
||||
#01 ;search-mode STA STH2 ( s* [r*] )
|
||||
&loop LDAk #00 EQU ,&eof JCN ( s* [r*] )
|
||||
;reset-stack JSR2 ( s* [r*] )
|
||||
DUP2 ;search-start STA2 ( s* [r*] )
|
||||
DUP2 STH2kr ;loop JSR2 ( s* b^ [r*] )
|
||||
,&found JCN ( s* [r*] )
|
||||
INC2 ,&loop JMP ( s+1* [r*] )
|
||||
&found POP2 POP2r #01 JMP2r ( 01 )
|
||||
&eof ;reset-stack JSR2 ( s* [r*] )
|
||||
DUP2 ;search-start STA2 ( s* [r*] )
|
||||
STH2r ;loop JMP2 ( b^ )
|
||||
|
||||
( loop used during matching )
|
||||
( )
|
||||
( we don't use the return stack here since that )
|
||||
|
@ -95,9 +118,11 @@
|
|||
( follow the given address (next*) to continue matching )
|
||||
@goto-next ( str* next* -> bool^ )
|
||||
DUP2 #0000 GTH2 ,&has-next JCN
|
||||
POP2 LDA null? ,&end-of-string JCN
|
||||
;goto-backtrack JMP2
|
||||
&end-of-string #01 JMP2r
|
||||
POP2 LDAk null? ,&end-of-string JCN
|
||||
;search-mode LDA ,&end-of-search JCN
|
||||
POP2 ;goto-backtrack JMP2
|
||||
&end-of-search DUP2 ;search-end STA2
|
||||
&end-of-string POP2 #01 JMP2r
|
||||
&has-next ;loop JMP2
|
||||
|
||||
( handle the empty node -- just follow the next pointer )
|
||||
|
@ -133,6 +158,11 @@
|
|||
|
||||
( REGEX PARSING )
|
||||
|
||||
( are we in searching mode? )
|
||||
@search-mode $1
|
||||
@search-start $2
|
||||
@search-end $2
|
||||
|
||||
( track the position in the input string )
|
||||
@pos $2
|
||||
|
||||
|
@ -589,4 +619,4 @@
|
|||
&above POPr POP &below #00 JMP2r
|
||||
|
||||
@iv-find ( c^ iv* -> bool^ )
|
||||
)
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue