48 lines
1.7 KiB
Tal
48 lines
1.7 KiB
Tal
|
( grep.tal )
|
||
|
( )
|
||
|
( by d_m )
|
||
|
|
||
|
( print a character to STDOUT )
|
||
|
%emit { #18 DEO }
|
||
|
|
||
|
( the first argument to grep is the regex )
|
||
|
( arguments are passed on STDIN, so we just )
|
||
|
( assume the first "line" is the argument )
|
||
|
( and the rest are lines to be grepped )
|
||
|
|0100
|
||
|
;r-read-stdin #10 DEO2 BRK
|
||
|
|
||
|
@regex 0000 ( compiled regex address (if any) )
|
||
|
@buffer $1000 ( buffer to read user input )
|
||
|
@ptr :buffer ( next byte to write in buffer )
|
||
|
|
||
|
@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 )
|
||
|
|
||
|
@r-read-stdin ( -> )
|
||
|
#12 DEI #0a EQU ,&execute JCN ( did we read \n ? )
|
||
|
#12 DEI ;ptr LDA2 STA ( no, so save in buffer )
|
||
|
;ptr LDA2k INC2 SWP2 STA2 ( ptr++ )
|
||
|
BRK ( return )
|
||
|
|
||
|
&execute ( we saw a newline, so do something )
|
||
|
#00 ;ptr LDA2 STA ( null terminate str )
|
||
|
;buffer ;ptr STA2 ( reset ptr )
|
||
|
;regex LDA2 #0000 EQU2 ( is regex initialized? )
|
||
|
,&need-regex JCN ( jump if unset )
|
||
|
|
||
|
( regex is set )
|
||
|
;buffer ;regex LDA2 ;match JSR2 ( match regex )
|
||
|
#00 EQU ,&no-match JCN ( did we match? )
|
||
|
;buffer ;println JSR2 ( print any match )
|
||
|
&no-match BRK ( return )
|
||
|
|
||
|
( regex is unset )
|
||
|
&need-regex ;buffer ;compile JSR2 ( compile regex )
|
||
|
;regex STA2 BRK ( save regex and return )
|
||
|
|
||
|
( include the actual regex machinery )
|
||
|
~regex.tal
|