add grep and femto

This commit is contained in:
~d6 2022-02-07 23:00:15 -05:00
parent 49df8ea93e
commit 4cb04d03e9
2 changed files with 54 additions and 0 deletions

7
femto Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
TTY=`stty -g`
uxnasm femto.tal femto.rom
stty raw -echo
uxncli femto.rom
stty "$TTY"

47
grep.tal Normal file
View File

@ -0,0 +1,47 @@
( 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