rmgr's wiki

> / > general > tech

rforth

Output of a Forth loop program which counts down from 10 to 0 and ends with

rforth is an experimental implementation of an interpreter for the Forth programming language written in Python. It's not pretty but it's mine.

Presently it only includes a shell for playing. I don't have any real plans for the future with this thing.

It's pretty brittle, it's really not designed for use by other humans at the moment but feel free to have a play

Example program

The following code counts down from whatever is at the top of the stack.

: loop 1 swap - dup dup . 0 = if else loop then ;

Standard Library

The program has a parameter to preload a file as a standard library of words defined with rforth.

2dup

Pops the top two items on the stack and adds two of both back on.

>=

Combines the output of > or =

<=

Combines the output of < or =

Supported commands

+

Pops the top two items on the stack and then puts the result back on top.

-

Pops the top two items on the stack and then puts the result back on top.

.

Pops the item on top of the stack and prints it as an integer

bye

Quits the interpreter

emit

Prints the item on top of the stack as an ASCII character

:

Enters word compile mode

;

Exits word compile mode.

stack

Prints the stack out for debugging

words

Prints all user defined words for debugging

>

Pops the top two items on the stack, compares the top value (a) to the next value (b) and puts -1 on the stack if a > b, and 0 on the stack if b > a.

<

Pops the top two items on the stack, compares the top value (a) to the next value (b) and puts -1 on the stack if a < b, and 0 on the stack if b < a.

=

Pops the top two items on the stack, compares the top value (a) to the next value (b) and puts -1 on the stack if a = b, and 0 on the stack if b != a.

if

Pops the top item on the stack, compares it to -1 and if they are equal, executes the code between either if and else or if and then, depending on if there is an else statement defined. If the top value on the stack is not -1, it executes the else block.

dup

Pops the top item on the stack and adds two of it back on.

rot

Pops the top three items on the stack (a,b,c) and puts them back in the order (c,a,b)

swap

Pops the top two items on the stack (a,b) and puts them back in the order (b,a)

or

Pops the top two items on the stack and puts -1 on the stack if either value is -1.

and

Pops the top two items on the stack and puts -1 on the stack if both values are -1.

!

Pops the top two item (k,v) on the stack and puts v in position k on the heap.

@

Pops the top item (k) on the stack and then inserts the value in position k on the heap into the top of the stack.

heap

Prints the heap out for debugging.

s" strings "

Sets the "reading string" flag until it reaches a closing quotation mark. Reads the inputted string on to the stack.

Further Reading/Links

https://en.wikipedia.org/wiki/Forth_(programming_language)

https://tildegit.org/rmgr/rforth

Tags

#retrocomputing #forth #interpreted