Here's a small
script I wrote while
studying the Forth
language, some time ago.
It is an
ascii graph generator that will - as shown below -
print on the
term stdout,
horizontal representations of
numbers which
values will preferably be within the 0-64
range.
Overflowing numbers are also
superficially
managed but don't expect too much as this is only intended as a personal
introduction to this
delightful language...
First: what's in it ?
mirko@broomstick> cat grapheur.f
: .X ." #" ;
: TERMWIDTH 64 ;
: BAR DUP 0 > IF DUP 0 DO .X LOOP THEN ;
: END ." " TERMWIDTH OVER - DUP 0 > IF 0 DO ." -" LOOP 0 THEN ;
: LABEL ." " DROP . CR ;
: LIMITEND DUP 0 > IF END ELSE 0 END DROP THEN LABEL ;
: LIMITBAR DUP TERMWIDTH > IF TERMWIDTH ELSE DUP THEN BAR DROP LIMITEND ;
: GRAPH CR DEPTH 0 DO LIMITBAR LOOP ;
In Forth, every word is a
command, it is declared within a
colon and a
semicolon, I then
subsequently declare a command
.X which will print one "#".
I then declare one command:
TERMWIDTH which just returns the maximum number of # I'll plot.
Why didn't I declare it as a variable ?
Because:
- I have not gone that far in the manual, yet
- I hope I'll be soon able to extend this prog until I can use this command to automatically find out how many columns my xterm can display.
I then declare the
BAR.
END and
LABEL words which respectively display the #, the subsequent
dashes and the trailing value.
LIMITEND and
LIMITBAR are just respectively extending the
END and
BAR words so that they don't
overflow as in Forth you have to handle your
errors yourself.
GRAPH is the resulting Forth program that will "
plot" any
stacked value.
It is
invoked the following way :
1 2 3 4 54213 0 0 12 34 197 851 12 11 3 4 41 -13 0 -41 -1000 2121 21 34 GRAPH
You may, of course,
script this invocation by doing something like this :
mirko@broomstick>gforth grapheur.f -e bye
################################## ------------------------------ 34
##################### ------------------------------------------- 21
################################################################ 2121
---------------------------------------------------------------- -1000
---------------------------------------------------------------- -41
---------------------------------------------------------------- 0
---------------------------------------------------------------- -13
######################################### ----------------------- 41
#### ------------------------------------------------------------ 4
### ------------------------------------------------------------- 3
########### ----------------------------------------------------- 11
############ ---------------------------------------------------- 12
################################################################ 851
################################################################ 197
################################## ------------------------------ 34
############ ---------------------------------------------------- 12
---------------------------------------------------------------- 0
---------------------------------------------------------------- 0
################################################################ 54213
#### ------------------------------------------------------------ 4
### ------------------------------------------------------------- 3
## -------------------------------------------------------------- 2
# --------------------------------------------------------------- 1
There you are, the plotted values appears in their
popping order.
I know I need much more practice to achieve nice progs such as
the smallest IDE driver ever but, damn'! If you feel like giving me some help, don't hesitate !!!