Forth programming and my Volkswagen beetle

I decided to look up an old flame. Both old in terms of how long she has been around, and in how long ago since I had last used her. Yes, the language Forth. Well to show my age, I actually started playing with her on a ZX Spectrum, way back at at the start of the 1980′s. That was when the Speccies came to market in the UK, though I can’t remember exactly when it arrived in our house, as my father bought this one for my little brother. Well he’s now an electrical engineer turned software engineer.

It amazes me that from both the web and even my fellow programmers, I can get quite strong opinions about this awesome little language, and environment, that is Forth. When I started tinkering with Sinclair’s door stopper, my brother’s patience was sorely tested. I had no idea. Really. Or should I say I had no grasp of the essential concepts required to come to grips with one of those new fangled gizmos.

Strangely enough, he got a car before me too. And his patience was also sorely tested when I first started tinkering with one of those :-). Now I have a nice shiny car for daily use, and likewise I also program in other languages, such is the job market these days.

BUT, and that’s a big but, I learnt so much about cars from my ’63 VW Beetle, the Forth of cars one might say. And I still keep it sat in the corner of my garage, for the odd seat of the pants run around the ‘burbs. I’ve pulled the engine right out and repaired stuff. A friend helped me put it back in again. But the point is that I now understand how it all works, I got my hands dirty and I loved it. Ditto Forth.

And maybe that is what Forth is now all about. Complete access, as in you too can have it, yes Mr Java or C# weeny! ;-) Touch all the working parts, which you can pull apart and deconstruct, break and repair, and sometimes get a mate to tell you how.

Anyway, I love Forth. I can’t program in it for toffee now. But, like my old VW, I can still take it out for a spin.

I think the simple concatenative style of Forth, it’s almost functional nature (yes I like emacs Lisp and even Clojure), and the complete system control (at ones own peril) is a facility that I think makes a language truly general in nature. Java’s inability to access anything of the system (ignoring JNI time wasting) has left me wishing for a turn on/off option. Well C# at least did that bit right.

Anyway, enough idle chat. Here’s a simple exercise I did with GForth, simply because there was no Floating point available when I used my speccy Forth implementation all those years ago – so I had to roll my own (with a little help from a good magazine article). Made my lab data handling a breeze back when the college mainframe was the only computing resource.

\ PURPOSE:    GForth experiment
\ Implement:  Centigrade to Fahrenheit
\ Formula:    F = (9/5)C + 32

\ EXPERIMENTATION
\ ---------------
\ use gforth FP (floating point) and FP stack

fvariable C        \ Centigrade FP variable
20e C f!           \ C set to 20.0

\ Check what Centigrade value we have
\ c f@ f.s \ 2.0E1 ok
\ c f@ f.  \ 20. ok

\ Handy stacks cleanup word
clearstacks

\ fractional part
: part ( -- f ) 9.0e 5.0e f/ ;

part f.      \ 1.8 ok

C f@ f* f.   \ 36 ok

32e f+       \ 68. ok  :-)

\ altogether
part C f@ f* 32e f+ f. \ 68.  ok

\ multi level asserts too!
\ level 3 is debugging assertion
assert3( part C f@ f* 32e f+ f. 68e f= ) \ ok

\ RESULT
\ ------
\ helper
: part ( -- f ) 9.0e 5.0e f/ ;

\ simple temp conversion words
\ NB 20 Centigrade is pushed on Float stack and used in the CtoF equation
\ -----------------------------------------------------------------------
: CtoF_ ( C -- ) part fswap f* 32e f+ ." -> " f. ." Fahrenheit" ;

20e CtoF_ \ 20e CtoF -> 68. Fahrenheit ok

\ or return result for further use
\ --------------------------------
: CtoF ( C -- F ) part fswap f* 32e f+ ;

20e CtoF

f.s         \  <1> 6.800000000000E1  ok
f.          \ 68.  ok

If you are interested, I did the experiment not just with GForth, but my trusty Emacs too. I got the setup advice from here. It was nice, very like with Clojure and a REPL, to mark and test bits of code as I wrote them.

It’s a line orientated language, simple left to right. And one can “Ctrl-CR” to send to GForth running in my other emacs window. Priceless.

I’m starting a small Japanese robit (robot kit) which has been sitting in my cupboard for too long. Sound and light sensors no less. Nice to get the soldering iron out for a play. Maybe if I get that done I can try an Atmel Atmega (Arduino) based project. What with the prevalence for such projects at major IT schools, it seems like I should join in the fun.

About these ads

2 Responses to Forth programming and my Volkswagen beetle

  1. Someone wrote a Pong clone in forth for the PowerMac OpenFirmware (g3-g5), it ran beautifully and was much faster than I’d anticipated.
    Sadly, the site it was hosted on was AOL Hometown which recently imploded, and I haven’t been able to find a copy since.

  2. Pingback: GA144, arrayForth, and 144 throbbing cylinders of joy | LEXECORP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s