Forth

From Gentoo Wiki
Jump to:navigation Jump to:search
This article is a stub. Please help out by expanding it - how to get started.

Forth is a heavily stack-oriented self-compiling procedural programming language that is only slightly more abstract than assembly. Forth fist appeared in 1970 and continues to see use in the embedded programming market, behind C and assembly language. Forth interpreters can be written to run on bare metal or on top of a general purpose operating system. Forth code is self-compiling in real-time even when run from bare metal on top of simple machine language primitives. Thus, somewhat like defining a new compiler target in C, Forth is sometimes used to bootstrap entirely new hardware.

Forth programs are divided up into "words" (subroutines). Embedded Forth interpreters are typically written in assembly language and built from Forth-words that call machine language primitives. Forth interpreters built for server use typically are written in C and built from Forth-words that call OS primitives.

Forth is infamous for requiring all mathematical operations be written in reverse Polish notation. That is, 5 + 2 is written as 5 2 +. Needless to say, this syntax takes some getting used to.

Forth code runs on the most minimal of virtual machines, requiring only two CPU registers and a stack to function. Forth has been ported to extremely low spec processors, even to 8 and 16-bit CPU's that can make a difficult C compiler target. Forth applications are built on top of Forth interpreters; that is, the base Forth interpreter essentially becomes the application over the course of development. Consequently, Forth lacks a concept of C-style linking. This makes embedded versions of Forth extremely useful for time-critical embedded applications and even firmware development.

Forth on Gentoo

Gentoo has support for the following Forth interpreters:

See Also

  • Assembly language — the lowest level of all programming languages, typically represented as a series of CPU architecture specific mnemonics and related operands.
  • C — a programming language developed for Bell Labs in the early 1970s
  • C++
  • Open Firmware — an IEEE 1275-1994 standard for hardware independent firmware built on top of a Forth machine.
  • Rust — a general-purpose, multi-paradigm, compiled, programming language.

External Resources

Learning Forth Today

There are many resources for learning Forth. Traditionally, it has been common for a beginner to learn the rudiments of the language and then promptly attempt to implement a Forth interpreter independently. This has the dual effect of cementing key concepts into the learner's mind and proving Forth competency to others. This is not done as frequently today as in the past, but it's still a common occurrence and a well-worn path to Forth mastery.

  • Learn Forth in Y Minutes — a high level Forth tutorial focused on GForth.
  • Starting Forth — the classic Forth Primer.
  • And So Forth... — a more advanced Forth primer.
  • Forth Lessons — a in-depth series of progressive lessons on the Forth programming language from the makers of the OLPC laptop.
  • Tumble Forth — a series of Forth related articles articles written by the author of the post-apocalyptically themed CollapseOS and DuskOS operating systems. Both bare metal operating systems, the former 8-bit and the later 32-bit, are written in Forth.
  • Code Your Own Forth — a YouTube series on how to write your own Forth interpreter using Swift.

Forth Machine Source Code in Low Level Languages

Another common way to learn Forth is by studying the source code of existing implementations for insight. Many interpreters have been written as a learning exercise. Perhaps the best known example of this in recent years is jonesforth, which is written in assembly language. Despite its simplicity it can function both as an interpreter and as a primitive operating system in its own right. There are other lower level examples, including C and Go implementations. High level examples include Forth interpreters written in Bash, Perl and Python.

  • Forth: The programming language that writes itself — An article adapted from a 2023 talk that provides information required to understand a Forth Machine's internal logic.
  • jonesforth.S — Forth interpreter source code, written in heavily commented i386 assembly intended as a learning aid.
  • lbForth.c — A port of jonesforth to ANSI C.
  • Jonesforth ARM — Forth interpreter source code written in heavily commented ARM assembly, a port of the i386 original.
  • Jonesforth RISC-V — Forth interpreter source code written in heavily commented RISC-V assembly, a port of the i386 original.
  • Jonesforth ported to multiple architectures — Forth interpreter source code written in M68k and PowerPC among others, ported from the i386 original.
  • milliForth — A Forth in just 386 bytes; the smallest real programming language ever.
  • sectorforth — a well documented minimalist 16-bit Forth written in X86 assembly targeting the i386. It takes its name from the fact that the self-bootstrapping code fits inside of a 512-byte disk sector.
  • Planckforth — A bootstrapping Forth machine handwritten in x86 assembly squeezed into a 1 kB ELF executable.
  • Forthress — A Forth dialect written for educational purposes that is self-bootstrapping but not intended to run on bare metal without an OS, it runs on Linux and builds using NASM.
  • durexforth — a modern Forth that implements the Forth 2012 standard on the 8-bit 6502. It's open source, actively maintained, and comes bundled with a native vi-like text editor.
  • punyforth — A simple Forth-like programming language that primarily targets the ESP8266, a WiFi chip with its own TCP/IP stack that is a common component of many IoT devices.

Forth Machine Source Code in High Level Languages

  • Bashforth — A slow but fully functional Forth interpreter written entirely in Bash.
  • Yoda — A pure Bash compiling Forth interpreter; Forth functions get converted to their Bash equivalents prior to execution.
  • PGForth — A much faster simple Forth interpreter written in a few hundred lines of Perl 5.
  • cubed4th — A Forth written in Python 3.
  • forth-awk — A interpreter Forth written in Awk.

Forth Package Repositories

Miscellaneous