2019-02-05 21:22:17 +01:00
|
|
|
# RRE Listener and Extensions
|
2017-10-20 05:05:43 +02:00
|
|
|
|
2019-02-05 21:22:17 +01:00
|
|
|
In this file I am implementing the interactive listener that
|
|
|
|
RRE will run when started with `-i` or `-c`.
|
2017-10-20 05:05:43 +02:00
|
|
|
|
2019-02-05 21:22:17 +01:00
|
|
|
## Console Input
|
2018-11-23 21:38:46 +01:00
|
|
|
|
2019-02-05 21:22:17 +01:00
|
|
|
The RRE interface provides a keyboard device. This exposes it
|
|
|
|
via `c:get`.
|
2017-10-20 05:05:43 +02:00
|
|
|
|
|
|
|
~~~
|
2018-11-23 21:38:46 +01:00
|
|
|
{{
|
|
|
|
'io:Keyboard var
|
|
|
|
:identify
|
|
|
|
@io:Keyboard n:zero? [
|
|
|
|
#1 io:scan-for dup n:negative?
|
|
|
|
[ drop 'IO_DEVICE_TYPE_0001_NOT_FOUND s:put nl ]
|
|
|
|
[ !io:Keyboard ] choose ] if ;
|
|
|
|
---reveal---
|
|
|
|
:c:get (-c) identify @io:Keyboard io:invoke ;
|
|
|
|
}}
|
2017-10-20 05:05:43 +02:00
|
|
|
~~~
|
|
|
|
|
2019-02-05 21:22:17 +01:00
|
|
|
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.
|
2017-10-20 05:05:43 +02:00
|
|
|
|
2019-02-05 21:22:17 +01:00
|
|
|
~~~
|
|
|
|
{{
|
|
|
|
:gather (c-)
|
|
|
|
dup [ #8 eq? ] [ #127 eq? ] bi or [ drop ] [ buffer:add ] choose ;
|
|
|
|
:cycle (q-qc) 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:LF eq? ] [ ASCII:CR eq? ] bi or ] parse-until ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
|
|
## Scripting: Command Line Arguments
|
|
|
|
|
|
|
|
RRE also provides access to the command line arguments passed
|
|
|
|
to a script. The next few words map the scripting device to
|
|
|
|
words we can use.
|
2017-10-20 05:05:43 +02:00
|
|
|
|
2017-10-20 03:16:46 +02:00
|
|
|
~~~
|
2018-11-23 21:38:46 +01:00
|
|
|
{{
|
|
|
|
'io:Scripting var
|
|
|
|
:identify
|
|
|
|
@io:Scripting n:zero? [
|
|
|
|
#9 io:scan-for dup n:negative?
|
|
|
|
[ drop 'IO_DEVICE_TYPE_0009_NOT_FOUND s:put nl ]
|
|
|
|
[ !io:Scripting ] choose ] if ;
|
|
|
|
---reveal---
|
|
|
|
:sys:argc (-n) identify #0 @io:Scripting io:invoke ;
|
|
|
|
:sys:argv (n-s) s:empty swap identify #1 @io:Scripting io:invoke ;
|
|
|
|
:include (s-) identify #2 @io:Scripting io:invoke ;
|
|
|
|
}}
|
2017-10-20 03:16:46 +02:00
|
|
|
~~~
|
|
|
|
|
2019-02-05 21:22:17 +01:00
|
|
|
|
|
|
|
## 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.
|
2017-11-15 20:57:17 +01:00
|
|
|
|
|
|
|
~~~
|
2018-11-14 14:52:30 +01:00
|
|
|
'NoEcho var
|
2017-11-15 20:57:17 +01:00
|
|
|
|
|
|
|
{{
|
2018-05-07 18:24:36 +02:00
|
|
|
:version (-) @Version #100 /mod n:put $. c:put n:put ;
|
2017-11-15 20:57:17 +01:00
|
|
|
:eol? (c-f) [ ASCII:CR eq? ] [ ASCII:LF eq? ] [ ASCII:SPACE eq? ] tri or or ;
|
|
|
|
:valid? (s-sf) dup s:length n:-zero? ;
|
2018-11-14 14:52:30 +01:00
|
|
|
:ok (-) @NoEcho not 0; drop compiling? [ nl 'Ok_ s:put ] -if ;
|
2017-11-26 16:20:56 +01:00
|
|
|
:check-eof (c-c) dup [ #-1 eq? ] [ #4 eq? ] bi or [ 'bye d:lookup d:xt fetch call ] if ;
|
2018-04-03 22:45:55 +02:00
|
|
|
:check-bs (c-c) dup [ #8 eq? ] [ #127 eq? ] bi or [ buffer:get buffer:get drop-pair ] if ;
|
2018-05-07 18:24:36 +02:00
|
|
|
:s:get (-s) [ #1025 buffer:set
|
|
|
|
[ c:get dup buffer:add check-eof check-bs eol? ] until
|
|
|
|
buffer:start s:chop ] buffer:preserve ;
|
2017-11-15 20:57:17 +01:00
|
|
|
---reveal---
|
2018-11-14 14:52:30 +01:00
|
|
|
:banner (-) @NoEcho not 0; drop
|
|
|
|
'RETRO_12_(rx- s:put version $) c:put nl
|
2018-05-07 18:24:36 +02:00
|
|
|
EOM n:put '_MAX,_TIB_@_1025,_Heap_@_ s:put here n:put nl ;
|
2018-04-04 18:11:44 +02:00
|
|
|
:bye (-) #0 unix:exit ;
|
2017-11-15 20:57:17 +01:00
|
|
|
:listen (-)
|
2018-05-07 18:24:36 +02:00
|
|
|
ok repeat s:get valid? [ interpret ok ] [ drop ] choose again ;
|
2017-11-15 20:57:17 +01:00
|
|
|
}}
|
2019-01-13 00:30:52 +01:00
|
|
|
|
|
|
|
&listen #1 store
|
2017-11-15 20:57:17 +01:00
|
|
|
~~~
|