retroforth/bin/listener
crc 697674954a improve "listener"
FossilOrigin-Name: cd78d6406dd3d05ad64ccdebbe8881f300a7ab2479e779ee5911d0f05b89f73d
2017-10-17 00:52:07 +00:00

46 lines
1.6 KiB
Text
Executable file

#/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
# ---------------------------------------------------------------------
# Next, copy the actual Retro source into a temporary file for RRE to
# process.
cat >/tmp/_listener.forth << 'EOF'
~~~
{{
:version (-) @Version #100 /mod putn $. putc putn ;
:banner (-) 'RETRO_12_(rx- puts version $) putc nl
EOM putn '_MAX,_TIB_@_1025,_Heap_@_ puts here putn nl ;
:eol? (c-f) [ ASCII:CR eq? ] [ ASCII:LF eq? ] [ ASCII:SPACE eq? ] tri or or ;
:valid? (s-sf) dup s:length n:-zero? ;
:ok (-) compiling? [ nl 'Ok_ puts ] -if ;
:gets (-s) [ #1025 buffer:set
[ getc dup buffer:add eol? ] until
buffer:start s:chop ] buffer:preserve ;
---reveal---
:listen (-) banner ok repeat gets valid? [ interpret ok ] [ drop ] choose again ;
}}
listen
~~~
EOF
# ---------------------------------------------------------------------
# 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