improve "listener"

FossilOrigin-Name: cd78d6406dd3d05ad64ccdebbe8881f300a7ab2479e779ee5911d0f05b89f73d
This commit is contained in:
crc 2017-10-17 00:52:07 +00:00
parent 183c5bae38
commit 697674954a

View file

@ -1,29 +1,46 @@
#/bin/sh #/bin/sh
# RETRO12: Interactive Listener
# ---------------------------------------------------------------------
# This is a bit of trickery using shell scripting and RRE to implement
# an interactive environment for Retro.
# ---------------------------------------------------------------------
# To work as expected, input should process as it's entered and not be
# line buffered. I use `stty` to set this mode.
stty cbreak stty cbreak
cat >~/.listener.r12 << 'EOF'
# ---------------------------------------------------------------------
# Next, copy the actual Retro source into a temporary file for RRE to
# process.
cat >/tmp/_listener.forth << 'EOF'
~~~ ~~~
{{ {{
:banner (-) :version (-) @Version #100 /mod putn $. putc putn ;
'RETRO_12_(rx- puts @Version #100 /mod putn $. putc putn $) putc nl :banner (-) 'RETRO_12_(rx- puts version $) putc nl
EOM putn '_MAX,_TIB_@_1025,_Heap_@_ puts @Heap putn nl ; EOM putn '_MAX,_TIB_@_1025,_Heap_@_ puts here putn nl ;
:getc (-c) `1001 ; :eol? (c-f) [ ASCII:CR eq? ] [ ASCII:LF eq? ] [ ASCII:SPACE eq? ] tri or or ;
:eol? (c-f) :valid? (s-sf) dup s:length n:-zero? ;
[ ASCII:CR eq? ] [ ASCII:LF eq? ] [ ASCII:SPACE eq? ] tri or or ; :ok (-) compiling? [ nl 'Ok_ puts ] -if ;
:gets (-a) :gets (-s) [ #1025 buffer:set
#1025 buffer:set [ getc dup buffer:add eol? ] until
[ getc dup buffer:add eol? ] until buffer:start s:chop ; buffer:start s:chop ] buffer:preserve ;
:valid? (s-sf)
dup s:length n:-zero? ;
---reveal--- ---reveal---
:bye (-) `26 ; :listen (-) banner ok repeat gets valid? [ interpret ok ] [ drop ] choose again ;
:listen (-)
banner
repeat gets valid? [ interpret ] [ drop ] choose again ;
}} }}
listen listen
~~~ ~~~
EOF EOF
./bin/rre ~/.listener.r12
rm -f ~/.listener.r12 # ---------------------------------------------------------------------
# Now use RRE (in the current directory) to run the program.
./rre /tmp/_listener.forth
# ---------------------------------------------------------------------
# Finally, clean everything up and use `stty` to restore the normal
# input buffering mode.
rm -f /tmp/_listener.forth
stty -cbreak stty -cbreak