o Small dictionary: instead of relying on a fixed cell size, the dictionary is written in variable length cells: small and common numbers take less space then larger, resulting in 30% to 50% space saving
o Portable: zForth is written in 100% ANSI C, and runs on virtually all operating systems and all architectures. Tested on x86 Linux/Win32/MS-DOS (Turbo-C 1.0!), x86_64, ARM, ARM thumb, MIPS, Atmel AVR and the 8051.
o Small footprint: the kernel C code compiles to about 3 or 4 kB of machine code, depending on the architecture and chosen cell data types.
o Tracing: zForth is able to show a nice trace of what it is doing under the hood, see below for an example.
o VM: Implemented as a small virtual machine: not the fastest, but safe and flexible. Instead of having direct access to host memory, the forth VM memory is abstracted, allowing proper boundary checking on memory accesses and stack operations.
o Flexible data types: at compile time the user is free to choose what C data type should be used for the dictionary and the stacks. zForth supports signed integer sizes from 16 to 128 bit, but also works seamlessly with floating point types like float and double (or even the C99 'complex' type!)
o Ease interfacing: calling C code from forth is easy through a host system call primitive, and code has access to the stack for exchanging data between Forth and C. Calling forth from C is easy, just one function to evaluate forth code."
I've seen a lot of embedded Forth stuff recently but I don't get the appeal. Clearly there is some benefit over writing assembly, but why is it preferred over portable C, or even higher level languages such as Python on more powerful mcus?
FWIW I felt the same way about LISP for a long time until I fell in love with it via Emacs. So please educate me!
Forth is not just another language for the sake of it. I am currently learning Forth from Leo Brodie's excellent books "Starting Forth" and "Thinking Forth". It actually changes the way you think. Just as Lisp does - almost a feeling of "a new planet swims into his ken".
The main feature of the Forth style is to write lots of 1 to 2 line functions, in such a way that at the high-level, you have essentially a domain-specific language that looks like exactly what you want a program to look like - for example, "RINSE WASH CYCLE SPIN STOP" for a washing machine program, and "RINSE WASH RINSE WASH CYCLE SPIN STOP" for an extra clean option.
People claim that once you learn Forth, you start programming Forth-style in other languages like C, Python etc. I can already see the appeal.
My C sometimes looks like Lispy Erlangish weirdness, and my Java often looks like my C. Certain languages just have certain ways of worming their way into your brain and improving the way you think, much to the dismay of everyone who can't think that way yet because the idiom isn't native to that language (yet).
I love that effect. My JS changed a lot once I grokked LISP, and I finally understood why there was a Haskell course on my degree track.
One of the most rewarding training experiences was giving my old Haskell textbook to a junior team member and watching his (at the time .NET, w/LINQ) code change over the course of a few weekends.
I don't have recent experience with Forth, but traditionally, Forth didn't require such a "beefy" machine as a C programming environment.
For instance here's a fairly complete Forth environment in around 10 KBytes running on an 8-bit home computer. There was no C compiler at all for that machine, and if there would be, working with it would be a lot more hassle than a Forth REPL.
Aw, input is broken on mobile (Android + of course Chrome).
I'm getting a keyboard popup, and I appear to be typing somewhere offscreen (I can see a typing suggestion containing exactly what I've typed, which wouldn't happen if input wasn't going anywhere), but the text isn't appearing onscreen, then when I press Enter, I get an "OK" as though I'd hit Enter on a blank line... and the input I'd typed isn't cleared. So, "words <Enter> words <Enter>" shows "wordswords" in my keyboard's suggestion, and "OK\nOK\n" on the display.
Yeah no useful mobile support unfortunately. Plan is to replace the standard virtual keyboards (which don't have the necessary keys anyway) with a custom-rendered keyboard eventually.
Forth has a REPL, and tiny Forths like this can work on microcontrollers that are unable to host a Python instance. (A microcontroller that can run Python is pretty rare).
Otherwise, your development cycle in C or assembly is "peer at datasheet, carefully enter bitwise arithmetic, compile, flash, wait several seconds for flashing to complete, debug."
* Forth is tiny. You don't need a preprocessor, a compiler, a linker. Your entire programming environment, beside the text editor, can live directly on the target MCU.
* Forth is interactive. You can run your code, examine the results, define and redefine words, run again, etc right on the target machine (which may be a tiny controller). Yes, you likely will remove the REPL-related words from the final image (or not), but C can't hold a candle to the interactive development experience of Forth.
* Unlike in C, you can build an economical DSL in Forth to closely match your target domain. What Forth takes away in readability due to the need to think in stack terms, it more than pays back in expressiveness and abstraction, provided certain skill and taste.
While Forth does have an execution overhead and thus cannot obviate C completely in some cases, I've seen e.g. an injection controller for an internal combustion engine which did hard realtime completely in Forth. Hard realtime here means that all computations were clocked by the rotation of the shaft, with precise timing of different phases along the way.
C normally involves compiling the entire program at once. Even if you use a interpreted C of some sort, C is a lot more verbose and involves more typing.
This is important when testing and evaluating new hardware. All you need is a serial port or equivalent connection to a forth to allow you to exercise the hardware in a very interactive way. In some cases you might end up writing the final program by accident.
Through a certain lens, a vanilla Forth program is defined purely by composing pure functions (and those compositions are vastly easier to write in Forth vs C or Python). It's a simple idea that has a lot of elegance, giving you high-level properties in a language that can be very close to the metal.
The only way to write a real forth kernel is in assembler. The use of the cpu registers is very particular. It is impossible to do that with a standard compiler.
they leverage off the c compiler to make it easily transportable and able to use different types, everything is a tradeoff and for the right circumstances this is an excellent one.
No one says it has ot be assembler, more the point is it easily could be if you wanted, hence the bringing up of new systems/CPU's often using Forth, you bootstrap more quickly.
I'm not sure I'd go that far, but certainly one of the really nice things about Forth is that it's so easy to bootstrap up from just assembler into something usable.
They're referring to that the 'traditional'/'proper' way to implement Forth is to write the whole thing in assembly and carefully consider how best to make use of the particular instruction-set.
o Small dictionary: instead of relying on a fixed cell size, the dictionary is written in variable length cells: small and common numbers take less space then larger, resulting in 30% to 50% space saving
o Portable: zForth is written in 100% ANSI C, and runs on virtually all operating systems and all architectures. Tested on x86 Linux/Win32/MS-DOS (Turbo-C 1.0!), x86_64, ARM, ARM thumb, MIPS, Atmel AVR and the 8051.
o Small footprint: the kernel C code compiles to about 3 or 4 kB of machine code, depending on the architecture and chosen cell data types.
o Tracing: zForth is able to show a nice trace of what it is doing under the hood, see below for an example.
o VM: Implemented as a small virtual machine: not the fastest, but safe and flexible. Instead of having direct access to host memory, the forth VM memory is abstracted, allowing proper boundary checking on memory accesses and stack operations.
o Flexible data types: at compile time the user is free to choose what C data type should be used for the dictionary and the stacks. zForth supports signed integer sizes from 16 to 128 bit, but also works seamlessly with floating point types like float and double (or even the C99 'complex' type!)
o Ease interfacing: calling C code from forth is easy through a host system call primitive, and code has access to the stack for exchanging data between Forth and C. Calling forth from C is easy, just one function to evaluate forth code."