From 4cb04d03e9d6a8048d465dbfc9adb6c74bfda495 Mon Sep 17 00:00:00 2001 From: d6 Date: Mon, 7 Feb 2022 23:00:15 -0500 Subject: [PATCH] add grep and femto --- femto | 7 +++++++ grep.tal | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100755 femto create mode 100644 grep.tal diff --git a/femto b/femto new file mode 100755 index 0000000..fb63adb --- /dev/null +++ b/femto @@ -0,0 +1,7 @@ +#!/bin/sh + +TTY=`stty -g` +uxnasm femto.tal femto.rom +stty raw -echo +uxncli femto.rom +stty "$TTY" diff --git a/grep.tal b/grep.tal new file mode 100644 index 0000000..b98cc4c --- /dev/null +++ b/grep.tal @@ -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