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
- Hello world
- Arithmetic operations
- Custom words
- Stack manipulation
- String manipulation
- How to compile code
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!" print
# word for commenting everything after it on the same line.
# This is a comment # 123 print
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.
: 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.
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
true. Any other value means
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"
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
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:
This will execute code from
Note: include is not the same as import.
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
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
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