8th is a secure, cross-platform programming language based on Forth which lets you concentrate on your application’s logic instead of worrying about differences between platforms. It lets you write your code once, and simultaneously produce applications running on multiple platforms. Its built-in encryption helps protect your application from hackers. Its interactive nature makes debugging and testing your code much easier.
As of this writing it supports 32-bit and 64-bit variants of:
Besides all that, 8th is simply fun to use!Before you can use it, you need to get a copy. Follow the installation instructions (unzip the file you've received) and get comfortable in your OS'es console...
Originally, the Forth language was developed in the early 1970s to control telescope hardware in an observatory. It has since found its way into many embedded applications (spacecraft, robotics, industrial control systems, automotive and many more). Its primary advantages historically have been extremely small size (around 2K code+data for a capable interpreter/compiler), interactive nature, and very good performance.
The simplicity and elegance of the Forth REPL has been one of the language's main draws, being extremely easy to implement and extremely useful for testing and debugging. A typical implementation will simply parse whitespace-delimited input (from the keyboard, a file, a socket, etc.) and look up each "word" so parsed in its dictionary of known words. If the word is found, the code it represents it is executed. Otherwise, an attempt is made to convert the word to a number. If that fails, the REPL may complain.
Numbers or other data are placed on a stack, and processed in RPN (or "postfix") manner. So:
10 2 /
results in 5, because the 10 was pushed first, then the 2 was pushed, and the "/" word popped those items off, divided 10 by 2, and pushed the resultant 5 back onto the stack.
There is no fixed syntax. nstead, any whitespace-delimited bit of text is treated as a possible "word" to be looked-up and executed.
New words are added to the dictionary using the somewhat cryptic word ":" and terminated with the equally cryptic ";". For example:
: add2 2 + ;
This sequence creates a new "word" named add2 which adds 2 to the number which was on the top of the stack (TOS, hereafter) when "add2" was invoked. The colon (":") word begins compilation of whatever words are found until the terminating semi-colon (";"), which is why these are sometimes called "colon-defs". The effect is to create a new word in the dictionary which executes the sequence of instructions desired.
8th differs from more traditional Forths in a number of ways. First of all, it is strongly typed and has a plethora of useful types (dynamic strings, arrays, maps, queues and more). Traditional Forths have "cells", which are numbers (typically sized as appropriate for the CPU) which may be treated as addresses (e.g. strings for instance) or just numbers - the programmer has to manage all the complexity and bear in mind what each cell represents.
8th's strings and arrays grow as needed, much as they do in many other high-level languages. Memory is allocated or deallocated as needed, behind the scenes - the programmer cannot allocate memory directly. In traditional Forths, strings are fixed in size and manipulating them properly can be complex. In 8th you can, for example:
"Hello, " "world" + . cr
This will print "Hello, world". The "+" concatenates the two strings and the "." prints whatever is in TOS. "cr" prints out a "carriage-return" (actually it prints the end-of-line sequence appropriate for the platform on which it's running).
8th is designed around the principles of least surprise and ensuring security. Once you grok RPN and the Forthish way of doing things, you'll find it strives to be consistent in how the built-in words behave. When you wish to deploy your application, you can create one which is signed and encrypted such that any modification of the app renders it incapable of being run (rather than being modified in a possibly harmful way).
Glad you asked! As mentioned in the introduction, 8th currently supports Raspberry Pi and other embedded Linux Arm-based systems. On RPI 8th gives you easy access to GPIO and I2C for example:
18 false hw:gpio \ tell the system you want pin 18, not read-only true hw:gpio! \ write '1' or 'true' to the GPIO pin we opened 0.1 sleep \ delay a tenth of a second hw:gpio@ . cr \ read the pin and print what was read
Some of the benefits for embedded work with 8th include:
That's all for this introduction. I hope you're intrigued by 8th and will read the links posted and perhaps browse or even join the 8th forum as well.
In my next blog post, I'll start showing how to use 8th to your advantage. You can read it by clicking on the link below:
[CTAToken URL = "https://community.arm.com/iot/embedded/b/embedded-blog/posts/8th-grokking-the-repl" target="_blank" text="8th: Grokking the REPL" class ="green"]
8th's first AGL (Automotive Grade Linux) version released today.