retroforth/interface/retro-unix.retro
crc d86e97e162 slightly smaller/faster backspace handling; more use of d:set-source
FossilOrigin-Name: 0bf6a56da51afac0f4dde0c18df3d5f06a6c5c0aba846b382e7b6f2c8531f935
2023-04-12 00:40:25 +00:00

103 lines
3.1 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`.
The basic image has a space allocated for input at the end of
the kernel. A pointer to this is stored at address 7.
~~~
:TIB #7 fetch ;
~~~
~~~
: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.
~~~
{{
'EOT var
FALSE 'Ignoring var-n
(-f) :ignoring? @Ignoring ;
(-nn) :version @Version #100 /mod ;
(c-f) :done? dup !EOT
[ ASCII:CR eq? ]
[ ASCII:LF eq? ]
[ ASCII:SPACE eq? ] tri or or ;
(c-f) :eol? @EOT [ ASCII:CR eq? ] [ ASCII:LF eq? ] bi or ;
(s-sf) :valid? dup s:length n:strictly-positive? ;
(c-c) :check-eof dup [ #-1 eq? ] [ ASCII:EOT eq? ] bi or &bye if ;
:bs buffer:size #2 gteq?
[ buffer:get drop ] if buffer:get drop ;
(c-c) :check-bs dup [ ASCII:BS eq? ] [ ASCII:DEL eq? ] bi or &bs if ;
(c-c) :check check-eof check-bs ;
(-c) :character c:get dup buffer:add ;
(q-) :buffer [ TIB buffer:set call buffer:start ] buffer:preserve ;
(-s) :read-token [ [ character check done? ] until ] buffer s:chop ;
(-sf) :input read-token valid? ;
(sf-) :process ignoring? [ drop-pair eol? [ &Ignoring v:off ] if ] if;
&interpret &drop choose ;
---reveal---
:// script:ignore-to-eol &Ignoring v:on ; immediate
:banner version 'RETRO_12_(%n.%n)\n s:format s:put
FREE EOM FREE - EOM '%n_Max,_%n_Used,_%n_Free\n s:format s:put ;
:listen banner repeat input process again ;
}}
&listen #1 store
~~~
## d:source
~~~
'interface/retro-unix.retro s:keep
dup 'listen d:lookup d:source store
dup 'banner d:lookup d:source store
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
dup 'TIB d:lookup d:source store
drop
~~~