Forth

Table of Contents

1. Forth

1.1. Forth: notes for newbies

  • a forth has two stacks: the parameter stack and the return stack
  • avoid using then use exit instead
  • .", s", (, \ must all have a space that follows it
  • a lot of words works differently at compile time v.s. run time. like .s, if, loop. For example, you usually can use loops and branches outside of column defintions (: ... ;)

1.2. Forth: miscellaneous

  • the notation ( ... -- ... ) are called stack effects or stack-comment
  • gforth uses .fs as file extension. Elsewhere, I've also seen .fth or just .f. Wikipedia also mentions .forth and .4th.
  • gforth's official release is very old compared to the latest version, all linux distributions I've used (not that many) packages a "very" old version of gforth. The one on Android's app store is fine though.
  • Words usualy have the destination on the TOS ( source destination – )

2. Forth: some conventions

2.1. abbreviations

  • tos stands for "Top Of Stack", it refers to the "first" element on the stack (usually the parameter stack)
  • nos stands for "Next Of Stack", it refers to the "second" element on the stack
  • xt execution token, that's analoguous to a function pointer in C or a symbol in lisp

2.2. naming conventions

These are not strict, this is forth, you do you. But it might still be useful for reading other people's code.

  • @
    • at the start of a word: save a value
      • e.g. @foo could save the ToS into the variable foo
    • at the end of a word: fetch a value
      • e.g. r@ copies a value from the return stack into the parameter stack
      • e.g. c@ fetches a character at c-addr
  • !
    • at the start of a word: initialise or reset a value (but I much prefer 0name)
    • at the end of a word: stores into
      • e.g. c! store a character at c-addr
  • c for characters
  • > and < for "into"/"from"
  • 0name for initialisation or re-initialisation
  • :name for defining words
  • name:, ;name: often used for DSLs
  • slight variants to a word is sometimes denoted by adding a single quote at the end
    • e.g. allocate' could denote a variant of allocate that initialise the allocated memory with 0s
  • surrounding a word with square brackets ([...]) usually means that it is executed at compile-time
    • a.k.a. "immediate words"
    • oftetime, there
  • a word that ends with , means that it puts somethings in the dictionary.
  • an array is usually named using the pluralized name of its elements, like names, people, filenames
  • you can define a word to get the current number of elements in a collection (an array), the convention is to prepend a # to the pluralized name of the elements
    • #names, #people, #filenames
    • "number of …"
  • the current element number is denoted by appending a # to the name (not pluralized)
    • e.g. name#, person#, #filename
    • "… number", or "… id"
  • advance to the next element
    • e.g. +name, +person, +filename
    • increments the variable name# (e.g. name#)

2.3. Stack comment conventions

  • a for address
  • c-addr character-aligned address
  • a-addr "address unit"-aligned address (pointer)

3. Forth: Cheatsheet

3.1. Basics

  • : word ... ;
  • : word ... ; immediate
  • =/ line comment =
  • ( block comment )

3.2. Return stack

  • >r, r>
  • 2>r, 2r>
  • r@, 2r@
  • n>r, nr>

3.3. Input/Output

Word Stack Effect Note
. ( a – ) print the TOS and drop it
emit ( x – ) display the character
type ( c-addr u – ) display a string
cr ( – ) display a new line (*C*arriage *R*eturn)
bl ( – char ) puts the character value for space on the stack
space ( – ) display one space
spaces ( n – ) display n spaces
[char] c ( "<spaces>name" – ) e.g. [char] a would put the value 97 on the stack
." ( "ccc<quote>" – ) display a constant string
s" ( – c-addr u ) returns an address to a constant string

3.4. Memory access

Word Stack Effect Note
! ( w a-addr – ) Pronounced "store"
@ ( a-addr – w ) Pronounced "fetch"
? ( a-addr – ) : ? @ . ;

3.5. Data space (Dictionary)

Word Stack Effect Note
here ( – addr )  
allot ( n – ) Increments HERE
forget ( "<spaces>name" – ) Decrements HERE 😅
, ( x – ) stores a cell in the dictionary and increments HERE by 1 cells
variable ( "<spaces>name" – ) creates a word name which puts the address of the cell on the stack
2variable ( "<spaces>name" – ) reserves 2 cells instead of 1
constant ( "<spaces>name" – ) almost the same as variable

3.6. Dynamic memory management

Word Stack Effect Note
allocate ( u – a-addr ior ) analog to malloc
free ( a-addr – ior )  
resize ( a-addr1 u – a-addr2 ior )  

3.7. GForth specifics

Word Stack Effect Note
throw ( u – a-addr ior ) a must for check the ior return value of syscalls and libc calls (e.g. allocate, open-pipe)

3.7.1. TODO stack manipulation words

Word Stack Effect Note
drop ( a – )  
nip ( a b – b )  
dup ( a – a a )  
swap ( a b – b a )  
over ( a b – a b a )  
rot    
-rot   equivalent to rot rot
roll   is often a code smell
pick   is often a code smell

3.8. Some identities

  • 0= = invert
  • >r r@ r> = dup
  • 2@ = dup cell+ @ swap @

3.8.1. Bunch of things that does nothing

Those are useful for learning, for detecting issues or simply for factoring out (they might occur when factoring something else, then you can easily factor it even more).

  • swap swap, and any even numbers of =swap=s
  • dup drop, dup nip

4. Forth examples

ANEW ~= if already define forget it

5. TODO Forth

  • if and loops
  • see
  • locate
  • parse v.s. word
  • counted strings, cell-counted strings
  • chars, cells, char+, cell+
  • find
  • compile,
  • enter, next
  • quit
  • literal, postpone
  • immediate words, code words, definig words, parsing words, etc.
  • interpretation v.s. execution v.s compilation semantics
  • input buffer, >in
  • control stack
  • the pictured numeric output string buffer #>
  • PAD
  • base, hex, etc.
  • evaluate

5.1. TODO the interpreters

5.2. TODO meta compilation

5.3. TODO writing an assembler

  • e.g. use a buffer to store the result
  • "target words=

5.4. TODO bits about the philosophy

in short: YAGNI

5.5. TODO Forth from the perspective of a lisper??

e.g. how to funcall a word?

execute

5.6. TODO The interpreters

  • inner (next?) v.s. outer

5.7. TODO The threading models

See "moving forth"

5.8. TODO Example of data structures

5.8.1. TODO cons list

ofc

6. Forth Resources

6.1. Moving forth

Moving Forth: a series on writing Forth kernels

6.1.1. TODO Moving Forth Part 1: Design Decisions in the Forth Kernel

Outline

  • choose the cpu
  • threading techniques
    • itc
    • dtc
    • jump to next in each code words v.s. inline next
    • stc
    • ttc
    • segment threaded code
  • register allocation
    • classical forth registers
    • hardware stack
    • ToS in register

7. Backlinks

Created: 2025-03-20 Thu 01:16