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
|
2019-03-29 21:03:05 +01:00
|
|
|
RRE 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. This is at address 1024 (the kernel space is fixed
|
|
|
|
at addresses 0 to 1023).
|
|
|
|
|
|
|
|
~~~
|
|
|
|
#1024 'TIB const
|
|
|
|
~~~
|
|
|
|
|
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 ;
|
2019-02-06 14:09:35 +01:00
|
|
|
---reveal---
|
2018-11-23 21:38:46 +01:00
|
|
|
: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 ;
|
2019-03-06 18:22:56 +01:00
|
|
|
:include (s-) identify #2 @io:Scripting io:invoke ;
|
|
|
|
:sys:name (-s) s:empty identify #3 @io:Scripting io:invoke ;
|
2018-11-23 21:38:46 +01:00
|
|
|
}}
|
2017-10-20 03:16:46 +02:00
|
|
|
~~~
|
|
|
|
|
2019-03-29 19:46:22 +01:00
|
|
|
# Fullscreen Interactive Listener
|
2019-02-05 21:22:17 +01:00
|
|
|
|
2019-03-29 19:46:22 +01:00
|
|
|
~~~
|
|
|
|
'FullScreenListener var
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Nearly all of this will be hidden from the user.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
{{
|
|
|
|
~~~
|
|
|
|
|
|
|
|
I'm using the buffer: words to deal with the TIB. To avoid any
|
|
|
|
conflict with user buffers, I'm duplicating the needed words
|
|
|
|
here, privately.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:Buffer `0 ; data
|
|
|
|
:Ptr `0 ; data
|
|
|
|
:terminate (-) #0 @Ptr store ;
|
|
|
|
:buffer:start (-a) @Buffer ;
|
|
|
|
:buffer:end (-a) @Ptr ;
|
|
|
|
:buffer:add (c-) buffer:end store &Ptr v:inc terminate ;
|
|
|
|
:buffer:get (-c) &Ptr v:dec buffer:end fetch terminate ;
|
|
|
|
:buffer:empty (-) buffer:start !Ptr terminate ;
|
|
|
|
:buffer:set (a-) !Buffer buffer:empty ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Color choices are a personal thing. I define the ones I want
|
|
|
|
here.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:white,blue #27 c:put '[1;37;44m s:put ;
|
|
|
|
:white,black #27 c:put '[0;37;40m s:put ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
The top bar displays:
|
|
|
|
|
|
|
|
- stack depth
|
|
|
|
- memory stats
|
|
|
|
- top five stack values
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:dump-stack
|
|
|
|
depth 0; #5 n:min
|
|
|
|
'|_ s:put
|
|
|
|
#1 [ 'a 'aa reorder n:put ] case
|
|
|
|
#2 [ 'ab 'abba reorder #2 [ n:put sp ] times ] case
|
|
|
|
#3 [ 'abc 'abccba reorder #3 [ n:put sp ] times ] case
|
|
|
|
#4 [ 'abcd 'abcddcba reorder #4 [ n:put sp ] times ] case
|
|
|
|
#5 [ 'abcde 'abcdeedcba reorder #5 [ n:put sp ] times ] case ;
|
|
|
|
|
|
|
|
:top-row #27 c:put '[1;0H s:put ;
|
|
|
|
:clear-row #80 [ sp ] times ;
|
|
|
|
|
|
|
|
:top top-row white,blue clear-row top-row
|
|
|
|
FREE depth n:dec 'SP:%n_FREE:%n_ s:format s:put
|
|
|
|
dump-stack ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
|
|
Next is the input bar. This is displayed at the bottom of the screen.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:bottom-row #27 c:put '[24;0H s:put ;
|
|
|
|
:input bottom-row white,blue clear-row bottom-row ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
The other bit is the output area in the middle. I have this as white
|
|
|
|
text on a black background.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:output bottom-row white,black clear-row bottom-row ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Now on to input processing. I have a variable, `In`, which
|
|
|
|
tracks the number of characters read so far. This is used in
|
|
|
|
a few places, to ensure that backspace doesn't overwrite the
|
|
|
|
kernel, and to blank the line if too many characters are
|
|
|
|
input.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
'In var
|
|
|
|
~~~
|
|
|
|
|
|
|
|
For handling backspaces, remove from the buffer and erase the
|
|
|
|
displayed character.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:discard buffer:get drop ;
|
|
|
|
:rub #8 c:put sp #8 c:put ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
I process input on CR, LF, and SPACE.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:evaluate TIB interpret ;
|
|
|
|
:reset #0 !In buffer:empty ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Tie this together into a key handler.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:handle
|
|
|
|
ASCII:SPACE [ @In 0; drop output evaluate nl reset top input ] case
|
|
|
|
ASCII:CR [ @In 0; drop output evaluate nl reset top input ] case
|
|
|
|
ASCII:LF [ @In 0; drop output evaluate nl reset top input ] case
|
|
|
|
ASCII:DEL [ @In 0; drop discard rub &In v:dec ] case
|
|
|
|
ASCII:BS [ @In 0; drop discard rub &In v:dec ] case
|
|
|
|
dup c:put buffer:add &In v:inc ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
And use that a the `process` loop.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:process
|
|
|
|
TIB buffer:set
|
|
|
|
repeat c:get handle @In #80 eq? [ #0 !In input ] if again ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
The last few support words use the stty(1) program to setup and
|
|
|
|
reset the terminal.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:init 'stty_cbreak_-echo unix:system ;
|
|
|
|
:exit 'stty_-cbreak_echo unix:system #0 unix:exit ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Switch to the public words.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
---reveal---
|
|
|
|
~~~
|
|
|
|
|
|
|
|
And expose `bye`, `clear`, and the `listener`.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:clear #27 c:put '[2J s:put #27 c:put '[0;0H s:put ;
|
|
|
|
:listen:fullscreen:bye exit ;
|
|
|
|
:listen:fullscreen init clear top input process ;
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Hide the support words.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
}}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
# Standard Interactive Listener
|
2019-02-05 21:22:17 +01:00
|
|
|
|
|
|
|
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 ;
|
2019-03-29 21:03:05 +01:00
|
|
|
:s:get (-s) [ TIB buffer:set
|
2018-05-07 18:24:36 +02:00
|
|
|
[ 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 ;
|
2019-03-29 19:46:22 +01:00
|
|
|
:bye (-) @FullScreenListener [ listen:fullscreen:bye ] if
|
|
|
|
#0 unix:exit ;
|
2017-11-15 20:57:17 +01:00
|
|
|
:listen (-)
|
2019-03-29 19:46:22 +01:00
|
|
|
@FullScreenListener [ listen:fullscreen ] if;
|
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
|
|
|
~~~
|
2019-02-06 14:09:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:image:save (s-) #1000 io:scan-for io:invoke ;
|
|
|
|
~~~
|