Learning FORTH on Arduino

Some output from Nano running FORTH (no command echo)

FORTH is interesting as a language not as ugly and boring as C, yet more lightweight than lisp. For the uno (328PU) ulisp requires 97% of the program store space, and 74% of your dynamic memory, while Arduino-FVM needs 36% of program storage space and 69% of dynamic memory. There is more for me to explore as far as space efficiency goes, but anyway I’m giving FORTH a try.

There are a few different FORTH implementations for Arduino – I was looking for the easiest path to get set-up. I landed on Arduino-FVM simply because it was the first one I found that was an Arduino sketch/library which I load quickly without having to learn anything new about how to upload to the 328. I just had to install the FVM library, and then compile a sketch which gives me the actual interpreter to load on the microcontroller.

https://github.com/mikaelpatel/Arduino-FVM

You can’t really learn the fundamentals of FORTH programming from just looking at Arduino-FVM documentation; so I installed gforth on my Guix desktop computer and used the gforth info page that has nice reference material and an extensive tutorial. Then I looked at the Arduino-FVM source code, which documents the parameters for the arduino-specific words.

This one liner will blink the LED on and off:

: blink true 13 pinmode begin 500 delay 13 digitaltoggle again ; blink

For those total unfamiliar with FORTH, it is sort of like doing programming on a reverse polish notation calculator, but with a larger stack available.

Advertisement

3 thoughts on “Learning FORTH on Arduino”

  1. For those totally unfamiliar with programming on a reverse polish notation calculator, it has been said that it is like Python and Assembly code had a child that joined a circus as a high wire act.
    Hackaday abbreviated this to say Forth was like a “flamethrower crawling with cobras” because you literally can change the mathematical meaning of 2+2 to equal 5….

    Like

  2. I confirmed that on gforth it is possible:
    : + 1 + + ; redefined + ok
    2 2 ok
    + ok
    . 5 ok
    I tried this on Arduino-FVM and it didn’t work (the interpreter freezes) but I think Arduino-FVM is not fully ANS FORTH compliant anyway.

    Liked by 1 person

Leave a Reply to thequietlearner Cancel 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 )

Facebook photo

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

Connecting to %s