nxu/grep.tal

59 lines
2.1 KiB
Tal

( grep.tal )
( )
( by d_m )
( NOTE: currently uxncli doesn't write 00 when its stdin )
( is closed. that means that grep.tal only "sees" lines )
( with a trailing newline, and also that it never exits. )
( print a character to STDOUT )
%emitt { #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 )
@done 00
@println ( s* -> )
&loop LDAk #00 EQU ,&eof JCN ( did we reach \0 ? )
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 #00 EQU ,&finishing JCN ( did we read 00 ? )
#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 )
&finishing
#01 ;done STA ( this will be our last iteration )
&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 ;search JSR2 ( search line for a regex match )
#00 EQU ,&no-match JCN ( did we match? )
;buffer ;println JSR2 ( print any match )
&no-match ;done LDA ,&exit JCN ( ready to exit? )
&return BRK ( no, return )
&exit #01 #0f DEO BRK ( yes, exit )
( regex is unset )
&need-regex ;buffer ;compile JSR2 ( compile regex )
;regex STA2 BRK ( save regex and return )
( include the actual regex machinery )
~regex.tal