Stack Sensitive Prompt

Data left on the stack is a common bug, it’s inevitable and was a issue for me when I was learning Forth. Yes … it still happens now, but it’s so infrequent it really catches me out when it occurs.

I began to add “.s” to the end of every new Word just to be sure the Stack was clean, and that worked fine but was a bit messy.

Recently I read a post by Albert Van Der Horst on Comp.Language.Forth (C.L.F) where he suggested colouring the Forth prompt to indicate the Stack status.

What a great idea.

Here is my solution, it is based on code by Glen Worstell; Note that when the stack has NO items, the prompt is the normal colour, but when the stack has one or more items, the prompt is RED.

I think this is a excellent solution as it uses no extra screen real-estate, and red is very eye catching. Who could miss a non empty stack now ?

A stack sensitive prompt

_images/stack-sensitive-prompt.jpg

The code

~/projects/programming-languages/forth/mecrisp-stellaris/mecrisp-unofficial-doc/projects/stack-sensitive-prompt/f103-red-stack-prompt.fs.html
 1 \ Program Name: f103-red-stack-prompt.fs
 2 \ Date: Sat 23 Nov 2019 18:23:59 AEDT
 3 \ Copyright 2019 by t.j.porter <terry@tjporter.com.au>, licensed under the GPLV2
 4 \ For Mecrisp-Stellaris by Matthias Koch.
 5 \ https://sourceforge.net/projects/mecrisp/
 6 \ Chip: STM32F103, Board: Olimex P103
 7 \ Clock: 8 Mhz using the internalRC clock, unless otherwise stated
 8 \ All register names are CMSIS-SVD compliant
 9 \ Note: gpio a,b,c,d,e, and uart1 are enabled by Mecrisp-Stellaris Core.
10 \ Requires preloaded support program(s): 
11 \ Standalone: no preloaded support files required
12 \
13 \ This Program : make the prompt red when stack >= 1
14 \
15 \ ---------------------------------------------------------------------------\
16  compiletoram
17  \ compiletoflash
18 
19 
20  \ designed for a white background with black text, and a red prompt when the
21  \ stack is not empty, but you can change to suit from the table below.
22 
23  : esc      ( -- ) 27 emit ;
24  : black    ( -- cursor colour )  esc ." [30;0m" ;
25  : red      ( -- cursor colour )  esc ." [31;1m" ;
26  : green    ( -- cursor colour )  esc ." [32;1m" ;
27  : yellow   ( -- cursor colour )  esc ." [33;1m" ;
28  : blue     ( -- cursor colour )  esc ." [34;1m" ;
29  : magenta  ( -- cursor colour )  esc ." [35;1m" ;
30  : cyan     ( -- cursor colour )  esc ." [36;1m" ;
31  : white    ( -- cursor colour )  esc ." [37;1m" ;
32 
33  : prompt ( -- )
34    begin
35        depth if
36        red ." ok. "  black cr    \ prompt
37        else ." ok. " cr
38        then
39      query  interpret
40    again
41  ;
42 
43  : init.prompt ['] prompt hook-quit ! ;
44 
45  init.prompt quit

Download: f103-red-stack-prompt.fs