Farth - is an attempt to implement Forth

forth, implementation, language
pip install Farth==0.7.1



Forth-like compilable programming language written in Python (which is not recommended for these things as any other scripting language).

You can guess everything about Farth by thinking of its name...

Table of contents

  1. Installation
  2. Syntax
  3. Hello world
  5. Arithmetic operations
  6. Comparison
  7. Custom words
  8. Stack manipulation
  9. Branching
  10. Loops
  11. Includes
  12. String manipulation
  13. How to compile code


Run python setup.py install
After that You will be able to run interpreter by running following command:

python -m Farth


Syntax is almost the same as in typical Forth language.

Hello world

"Hello, world!" print


There's # word for commenting everything after it on the same line. Example:

# This is a comment
# 123 print

Arithmetic operations

5 3 + print # Will display 8
4 5 - print # Will display 1
5 4 * print # Will display 20
2 10 / print # Will display 5
10 % 5 print # Will display 0


Comparison operators are the same as in typical Forth.
= - equal
!= - not equal
<= - less or equal
>= - greater or equal
< - less
> - greater

If condition is true it will push 1 to the stack or 0 if it's false.

Custom words

: word starts word definition and ; ends it.

: plus + ;
: plus10 10 plus ;
8 plus10 print # Will display 18

Words are more like macros.
For example, You can define word that defines another word.

Stack manipulation

dup - (n -- n n) duplicates last value on the stack
drop - (n -- ) removes last value from stack
over - (n1 n2 -- n1 n2 n1) duplicates previous value
rot - (n1 n2 n3 -- n2 n3 n1) 'rotates' 3 last values
swap - (n1 n2 -- n2 n1) swaps 2 last values
2dup - (d -- d d) duplicates last pair of values
2drop - (d -- ) removes last pair of values
2swap - (d1 d2 -- d2 d1) swaps 2 last pairs of values
2over - (d1 d2 -- d1 d2 d1) duplicates previous pair of values
reverse - (n1 n2 ... -- ... n2 n1) reverses stack
.s - print current stack contents


1 means true. Any other value means false.
The following example defines a new word that prints "true" if condition is true and "false" if it's false:

: testif if "true" print else "false" print endif ;
0 testif # "false"
1 testif # "true"


Farth loop is similiar to loop in Forth. You have to put value on the loop stack with do, right after that write some code to be repeated and call loop word
The following example prints digits from 10 to 1:

10 do i print loop

i copies current value from loop stack and pushes it to the data stack.

You can use la word to put values on loop stack.


You can include Farth code into Your program from files.
To do that You can use include as in following example:

"some_file.forth" include

This will execute code from some_file.forth.

Note: include is not the same as import.

String manipulation

lower - ("SOME STRING" -- "some string") converts string to lowercase
upper - ("some string" -- "SOME STRING") converts string to uppercase
tostr - (n -- "n") converts number to string
strlen - pushes length of string on the stack
stri - (string index -- string[index]) takes string and index as arguments, pushes to stack string[index]
slice - (string index1 index2 -- string[index1:index2]) slices string
strrs - ("123" -- "321") reverses string

How to compile code

Let's say You have file called hello.farth that contains this code:

"Hello, world!" print

To compile it, run the following command:

python -m Farth hello.farth -b -o hello

Where hello is an output filename.
After running this command You will find another file (hello in this case). You can run that file with following command:

python -m Farth hello

You should see Hello, world! after that.
Also, You can dump FarthVM code into file with -d or --dump options.