Lectures in Forth 15: code highlighting. 
Author Message
 Lectures in Forth 15: code highlighting.

This lecture may also be found :


Linux owners can download a version to experiment with high lighting
following that link.

Forth lecture 14.

Forth high lighting.


The syntax high lighting of othere languages showing the ``type'' of a
word is pretty useless in Forth because words have no type. To make color
highlighting potentially valuable we have to rethink what information we
want most. That information is of course the stack effect. Since there is
an -- albeit experimental -- way to add the stack information to
ciforth, a rather simple program can add color high lighting to the word
ID. that displays a Forth word. ciforth, as an indirect threaded Forth,
allows to retrofit a colored ID. onto a system. This is complemented by a
few modifications to the decompilation system.

Start color high lighting by DO-COLOR.

Stop color high lighting by NO-COLOR.

View a colored decompilation by CRACK WORD.

You can download the system to experiment with it on Linux.

The example below is described in a way that if you don't run it, you still
get the idea.


The color high lighting has as main aspects

    The first letter in a word is colored to indicate stack input.
    The last letter of a word is colored to indicate stack output.
    The body of the letter is printed white, which is also the color for no
    stack I/O.
    The words is printed on a blue black ground if it has variable stack
    effect, or is otherwise disconcerting (to be decided upon.).
    The order of the stack depth is white, aqua, green, yellow and red.
    Red is used for everything over 3. Pink is used for unknown or

The bottom line: if the code is nice, it looks nice.


To make it possible for everybody to assess the possibilities of color high
lighting in Forth I have made a binary distribution that you can download
here. It only runs on Intel Linux, and it runs best from the console. The
black screen background is essential. In KDE you can run a shell into a so
called ``Linux console'' that has the same color settings. It is a saved
system from a prerelease snapshot of unstable version 4.1 of ciforth with
an as yet experimental program, loaded. This program is an analyser
that adds the information about stack effect to the headers.

The sole purpose of all this it to find out whether you like the color high
lighting and to allow you to experiment with it.


You may want to run through this step by step :

tar xfz linacolor.gz
80386 ciforth beta $RCSfile: ci86.gnr,v $ $Revision: $
INCLUDE color.frt

Type WORDS to have a comparison. Now type


This doesn't look too good. All new words in the decompilation and a
large part of the dictionary is pink, i.e. unknown.

Now try the following sequence of commands:


After FILL-ALL-SE all stack effects have been filled in. However the
decompilation looks disconcerting, because of a word with variable stack

After inspection of the source it is clear that that word always takes one
parameter, and this is filled in by !SE. The stack effect is 1-0 . The hex
value 21 is passed, because since 0 indicates unknown, the values are
offset by 1.


We are still not happy because COLOR-ID. is highlighted with a blue
background as a severe offender, while in fact it has a regular stack
behaviour. Now reset its stack effect byte and run FILL-ALL-SE again.


    SWAP  1+  SWAP  1-  DUP

BRANCH  [ 0000,0008t , ] 2DROP  SHOWING-CHAR
R> SELECT-COLOR  EMIT  black  white  2t  SPACES

Now it looks nice! Only the white-aqua-green colors are left. The strange
character after one letter words are place holders (in the console it shows
up as a small block).

Some more words over how to read this.
We can see here some pipes. A pipe is a sequence of words where each
word consumes the outputs of the previous one.
A white-white pipe is very reassuring and, what is more, you can think it


A slightly more advanced pattern is the start DUP ... 0BRANCH. This is

with a green white pipe ``AND 0BRANCH '' . There is no total stack
On the second line ``DUP .. R'' features a white-green pipe ``DUP SE
SWAP '' followed by two aqua-white pipes ``SELECT-COLOR '' and
``>R '' . This too has no net stack effect.
The result is that you almost can visually connect the input of the total to

Interesting is also the sequence ``SWAP 1+ SWAP 1- ''. They may have
not good connections between the words, but all of them have a net stack
effect of zero. They are working on the stack in place.

Another example may be more practical.


(WORD)  FOUND  DUP  0=  0000,000Bt ?ERROR  CRACKED

This is more indicative of the situation where you are inspecting
somebody elses code. Four out of seven words are probably unknown to
you. With color highlighting you can think away the ``DUP ... ?ERROR''
sequence, because of the now familiar white-aqua, white-aqua
green-white pattern. What is left is an aqua-white chain. More over it is
obvious (from an idiomatic point of view) that WORD leaves a Forth
string constant and FOUND passes an execution token (aka DEA) to
Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS
To suffer is the prerogative of the strong. The weak -- perish.

Fri, 12 Nov 2004 22:18:02 GMT  
 [ 1 post ] 

 Relevant Pages 

1. comp.lang.forth FAQ: books (15 June 1996), part 5/6

2. Forth Day in San Francisco area (Nov. 15)

3. Forth On-line Resource/Technical Help (v1.6, 15-Mar-92)

4. Forth On-line Resource/Technical Help (v1.6, 15-Mar-92)

5. Forth On-line Resource/Technical Help (v1.6, 15-Mar-92)

6. Forth On-line Resource/Technical Help (v1.6, 15-Mar-92)

7. Forth On-line Resource/Technical Help (v1.6, 15-Mar-92)

8. Forth On-line Resource/Technical Help (v1.6, 15-Mar-92)

9. Forth On-line Resource/Technical Help (v1.6, 15-Mar-92)

10. ANNOUNCE: pygtk-1.99.15, pyorbit-1.99.3, gnome-python-1.99.15

11. Reuse News - Week Ending: December 15, 1995 - 95-12-15.txt [1/1]

12. Failed file.test 15-4 through 15-8 on SCO Xenix 2.3.4


Powered by phpBB® Forum Software