nxu/grep.tal

48 lines
1.7 KiB
Tal
Raw Normal View History

2022-02-07 23:00:15 -05:00
( grep.tal )
( )
( by d_m )
( print a character to STDOUT )
2022-02-19 01:23:39 -05:00
%emitt { #18 DEO }
2022-02-07 23:00:15 -05:00
( 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 ? )
2022-02-19 01:23:39 -05:00
LDAk emitt INC2 ,&loop JMP ( no so emit a char and repeat )
&eof #0a emitt POP2 JMP2r ( yes so emit \n and return )
2022-02-07 23:00:15 -05:00
@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 )
2022-02-19 01:23:39 -05:00
;buffer ;regex LDA2 ;search JSR2 ( search line for a regex match )
2022-02-07 23:00:15 -05:00
#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