retroforth/interface/retro-unix.retro
crc c84b9a7e5d move the full listener from interface/retro-unix to the basic image
FossilOrigin-Name: e6fd949a0911730c0db46faf3c20fdb7b265b38bc2b3b3456980d26a4bb1bdbc
2023-12-15 14:10:08 +00:00

64 lines
1.6 KiB
Forth

# RETRO on Unix - Listener and Extensions
In this file I am implementing the interactive listener that
RETRO will run when started with `-i`, `-i,c`, or `-i,fs`.
~~~
:image:save (s-) #1000 io:scan-for io:invoke ;
~~~
Now that I can read characters, it's time to support reading
strings. I do this via two words. The first is `parse-until`.
This will setup a temporary string as an input buffer, then
read input, passing each character ot a provided quote. When
the quote returns `TRUE`, it ends and returns the string. When
not `TRUE` it will add the character to the buffer.
~~~
{{
(c-cf) :edit? dup [ ASCII:BS eq? ] [ ASCII:DEL eq? ] bi or ;
(c-) :gather edit? &drop &buffer:add choose ;
(q-qc) :cycle repeat c:get dup-pair swap call not 0; drop gather again ;
---reveal---
:parse-until (q-s)
[ s:empty buffer:set cycle drop-pair buffer:start ] buffer:preserve ;
}}
~~~
Using this, a simple `s:get` can be implemented very easily as
a quote which looks for an end of line character.
~~~
:s:get (-s) [ [ ASCII:CR eq? ]
[ ASCII:LF eq? ] bi or ] parse-until ;
~~~
~~~
:clear '\^[2J\^[0;0H s:format s:put ;
~~~
Hide the support words.
# Standard Interactive Listener
The main part of this file is the *listener*, an interactive
read-eval-print loop.
RRE's C part will access a couple parts of this, based on the
startup flags passed.
~~~
:// script:ignore-to-eol &Ignoring v:on ; immediate
~~~
## d:source
~~~
'interface/retro-unix.retro s:keep
dup '// d:lookup d:source store
dup 'clear d:lookup d:source store
dup 's:get d:lookup d:source store
dup 'parse-until d:lookup d:source store
dup 'image:save d:lookup d:source store
drop
~~~