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.
  • Fortran — a general-purpose, compiled imperative programming language that is especially suited to numeric computation and scientific computing.
  • C — a programming language developed for Bell Labs in the early 1970s
  • C++ — a general-purpose programming language that originated from 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.

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.

jonesforth

Other

  • 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.
  • 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, the Netwide assembler.
  • milliForth — A Forth in just 386 bytes; the smallest real programming language ever.
  • MinForth — Minimalistic but complete standard Forth compiler in C; by design no toolchain is required to adapt or rebuild MinForth.
  • pForth — Public domain, portable ANS Forth based on a kernel written in ANSI C, to make it easy to port pForth to multiple platforms.
  • Planckforth — A bootstrapping Forth machine handwritten in x86 assembly squeezed into a 1 kB ELF executable.
  • 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.
  • 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.
  • zForth — Tiny, embeddable, flexible, compact Forth scripting language for embedded systems.

Forth Machine Source Code in High Level Languages

High level examples include Forth interpreters written in Bash, Perl and Python.

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

Forth Package Repositories

Miscellaneous