Interactive ESP8266 Development With PunyForth

Forth is one of those interesting languages that has a cult-like following. If you’ve never looked into it, its strength is that it is dead simple to put on most CPUs, yet it is very powerful and productive. There are two main principles that make this possible. First, parsing is easy because any sequence of non-space characters makes up a legitimate Forth word. So while words like “double” and “solve” are legal Forth words, so is “#$#” if that’s what you want to define.

The other thing that makes Forth both simple and powerful is that it is stack-based. If you are used to a slide rule or an HP calculator, it is very natural to think of “5+2*3” as “5 2 3 * +” but it is also very simple for the computer to interpret.

[Zeroflag] created PunyForth–a Forth-like language for the ESP8266. You can also run PunyForth for cross development purposes on Linux (including the Raspberry Pi). The system isn’t quite proper Forth, but it is close enough that if you know Forth, you’ll have no trouble.

According to the project’s documentation, here’s a list of differences between Forth and PunyForth:

  • Punyforth is case sensitive
  • Strings are null-terminated
  • Strings are created and printed differently
  • Parsing words are ended with a colon character by convention
  • Defining a word in terms of itself results in recursion by default (use the override word to alter this behavior)
  • Curly brackets denote quotations instead of locals

None of that is anything that makes it very strange if you already know Forth. If you don’t know Forth, then none of this will strike you as unusal, anyway.

The system provides a TCP shell. It also  supports exception handling, cooperative multitasking, socket and GPIO APIs.

If you are into Forth, you’ll like this. If you aren’t, you still might like the expereince of developing interactively directly on the hardware. Just remember, the key is to define words (subroutines) that allow you to write easy-to-read (and write and debug) high-level programs.

We’ve covered Forth on breadboard CPUs before. If Forth isn’t your cup of tea, maybe you’d prefer BASIC.


17 thoughts on “Interactive ESP8266 Development With PunyForth

  1. It is very natural to think of “5+2*3” as “5 2 3 * +”

    No, it is not. I think too many people assume this around here. RPN calculators did not die 3 decades ago because they were easier to use. Easier to IMPLEMENT, yes. Easier to use? Hell no. Not for 99.999% of us.

      1. I had several programmable TI calculators but playing with microprocessors too, I was used to calculate by pushing and popping from a stack too. Then I got a HP calculator and either never found the right config settings or that thingy had a far too short stack. I sold it again after a few weeks and never touched a similar toy again… but I still like RPN if the stack is large enough.

        My problem with Forth is the many stacks. I’m used to have return address and data on the same stack. Forth has different stacks for different usages. Starting with my TRS-80 days I repeatedly tried Forth but I never managed to get beyond some trivial programs… but I will retry! Definitely!

    1. If you learned to do math by hand and/or using a slide rule, it was very natural. Because you had to manage the order of operations yourself in those cases, anyway. So you just skip a step by going straight to RPN.


      by hand you mentally do: 5+2 then *3 then you do 9*6 then you do + on the results… so gee, that’s: 5 2 + 3 * 9 6 * +

    2. I loved having an RPN calculator (Sinclair Scientific Kit) as a kid in the mid seventies at school because my mates only ever borrowed it once before declaring it broken and going back to their slide rules.

    3. Yes it is. It is the way people think and takes just a little use to realize how natural it is. And it is easy to prove. Here, perform this task. 3 +. Can’t do it? Because the operator is in the wrong place. To actually compute, say 3+5, you have to stack the arguments mentally then perform the operation. You have to save the + and wait for the 5. This is why all compilers produce postfix output.

      Not being proper Forth is probably a good thing. Forth took a nosedive when a little club called the Forth Standards Team managed to get their dream Forth into ANSI and bloated and generalized it to death. An old fashioned FIG Forth or MVP-Forth tailored to an architecture is much better suited to small systems. I will have to try this out.

    4. if you use assembly language, or even just plain functions, no operators, you use it all the time–RPN is just in reverse order. Assembly has no “a + b”, but it does have “add(a,b)”. So “5+2*3” is really “+(*(3,2),5)” or “add((mul(3,2),5)” .

      Quick steps to decoding RPN:
      1. Mirror it: reverse right-to-left to left-to-right.
      Ex: 5 2 3 * + —–> + * 3 2 5
      2. Assume all operators are 2-value functions, and add parentheses & commas as expected
      Ex: + * 3 2 5 —–> +(*(3,2),5)
      3. Think of operators as functions with non-alphabetic names
      Ex: +() –> add(), *() –> mul(), so add(mul(3,2),5)

      Done! RPN is just function notation reversed!

    1. I’m into it since a week, the Arduino based Yaffa port on the 8266, works on 32 with serial only, I’m looking to add the Telnet server back.(per SDK or wait till arduino libs get more functional). Other C based Forth, like ficl or tile, just crash the esp32 and I don’t get it at the moment (memory access/MP). There is also picoC waiting…

  2. I got addicted to rpn on the fourth grade (I borrowed my dad’s calculator), using an HP 48g up through the middle of high school. Then I got a 50g and used that until the middle of my bachelors (EE). Then I got a Prime and been using it through graduation and ever since at work.

    To say I’m biased is an understatement. I love rpn and find it far less error prone to use than algebraic entry.

  3. Having used Forth 20 + years ago, the best thing I can say about it is that it is easy to implement on tiny computers. I can’t think of any reason to use it today other than just for mental exercise.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.