[chbot] Forth for embedded programming.

Charles Manning cdhmanning at gmail.com
Sun Nov 6 20:55:31 GMT 2011


On Sat, Nov 5, 2011 at 5:58 PM, Paul Davey <plmdvy at gmail.com> wrote:
> On Sat, Nov 5, 2011 at 1:58 PM, Carl Ranson <carl.ranson at gmail.com> wrote:
>>
>> My impression is that its one of those languages that looks good at
>> first glance but rapidly degrades as the project goes.
>>
>> Even something as simple as generating the fibonacci sequence becomes
>> unreadable
>>
>> : fib ( n -- fib )
>>   0 1 rot 0 ?do  over + swap  loop drop ;
>>
>> It seems that it is impossible to read the code without also considering the stack at each step.
>>
>
> Forth does have a reputation for being a write only language, it is
> difficult to write properly readable forth.  The stack has to be
> considered at every step as this is the equivalent of the variable
> state in C like languages, the difference is that we can deal with the
> C like abstraction better without practise.
>
> I kinda like forth and I do like it on micro-controllers as you can
> have the compiler on the device and develop it over a serial console.
> The language is hard to write cleanly in though and requires a lot of
> commenting and careful structuring to make it readable.
>
> I do like forth as a concept though it is very simple and elegant in a
> way, threaded code is an interesting idea as well.
>
> Just a few fractured thoughts on Forth.
>

I used Forth in earnest a while back as well as helping doing some
Forth porting and work on a Forth debugger.

Forth is supposed to be written in small chunks, a function at a time
(words in Forth-speak) and tested as you go. Keeping track of stack
behaviour is (relatively) trivial if the functions are short. If you
have a function longer than 5 lines or so then you are doing something
wrong.

Forth is incredibly small. I had a whole forth development suite for a
PC (including editor, compiler, assembler,...) that fitted in less
than 30k. In fact, forth code can be even smaller than assembler code.

IMHO one of the coolest parts of forth is that it has a default
grammar which you can change. That makes Forth not so much a language
as a language framework.

Would I use Forth in the future? I doubt it.  One benefit of Forth is
that the whole compiler and everything else can be easily stuffed onto
the target.  With laptops and jtag debuggers and such that is no
longer compelling and certainly not enough to compensate for the loss
of type checking etc.

The spirit of Forth still lives on in postscript and RPL (used to
program HP calculators). Both are basically type-checked Forth.



More information about the Chchrobotics mailing list