LinuxCommandLibrary

forth

TLDR

Start Forth interpreter (Gforth)

$ gforth
copy
Run Forth script
$ gforth [script.fs]
copy
Evaluate expression
$ gforth -e "[1 2 + .] [bye]"
copy
Include file then go interactive
$ gforth [script.fs] -
copy

SYNOPSIS

gforth [options] [file...]

DESCRIPTION

Forth is a stack-based programming language known for its simplicity and extensibility. Gforth is the GNU implementation of Forth, conforming to the ANS Forth standard.
Forth uses reverse Polish notation (RPN) with an explicit stack. Words (functions) are defined with `:` and `;`. The language is highly interactive and extensible, commonly used in embedded systems.

PARAMETERS

-e code

Evaluate Forth code.
-m size
Dictionary size.
-d size
Data stack size.
-r size
Return stack size.
file
Forth source file to load.
-
Read from stdin.

BASIC OPERATIONS

$ \ Push numbers, add, print
2 3 + .   \ prints 5

\ Define a word
: square ( n -- n^2 ) dup * ;
5 square .   \ prints 25

\ Stack manipulation
1 2 3 swap   \ 1 3 2
dup          \ duplicates top
drop         \ removes top
copy

CAVEATS

Stack-based paradigm differs from most languages. Manual memory management. Limited standard library compared to modern languages. Debugging can be challenging.

HISTORY

Forth was invented by Charles H. Moore in the late 1960s for controlling radio telescopes. It became popular in embedded systems due to its small footprint and efficiency. Gforth was started in 1992 by Anton Ertl and Bernd Paysan.

SEE ALSO

gforth(1), lisp(1), dc(1)

Copied to clipboard