JVM Languages

JForth: Implementing Forth in Java

By Craig A. Lindley, May 21, 2008

Java Implementation

You might first ask why I was motivated to implement Forth in Java and the answer would be to see how it might be done. Before being flamed, I should state the implementation I provide should really be called "Forth-like" as it stems from my recollection of how Forth works rather than a strict adherence to Forth standards. Also it was not done with any practical purpose in mind other than as a feasibility demonstration for fun. Finally, the Java implementation which I call "JForth," is not particularly efficient in terms of speed, memory usage, or anything else, but it served its purpose. The complete source code for JForth (packaged in a jar file which contains all of the javadocs, compiled classes, and source files) is available online.

Running JForth

Running JForth requires a Java 1.5 or newer JRE being available on your target machine. The jar file, Jforth.jar, available from DDJ, contains all of the javadocs, source code, test code and executable code required to run JForth. The code can either be run in a command shell directly out of the jar file using the command:

java -jar com.craigl.jforth.JForth

or the code can be extracted from the jar file and compiled in your preferred programming environment. Either way, once the main JForth class is executed you will be given the prompt > and the program will wait for you to enter Forth expressions. Type the Forth word "bye" to end your Forth session.

The file of test code, "testcode" must be extracted from the jar file to be executed. To compile and run this test code at the JForth prompt type:

> "path_to_testcode_file/testcode" load <Enter>

In JForth, all words are derived from the abstract class BaseWord which implements the ExecuteIF interface. The BaseWord class maintains the name of the Forth word, a flag indicating whether the word is immediate or not and another flag indicating whether this word is a primitive.

The ExecuteIF interface consists of a single method that all words must implement:

public int execute(OStack dataStack, OStack variableStack);

This method takes references to the data and the variable stacks and returns a non-zero int value on success.

Derived from BaseWord are the PrimitiveWord and NonPrimitiveWord classes. Primitive words are defined in Java code; non-primitive words contain a list of BaseWords (both primitive and non-primitive) that make up the definition. A primitive word is executed by calling its execute method to run the Java code which makes up its definition. The execute method in a non-primitive word traverses its list of BaseWords calling the execute method of each sequentially. Listing Two shows the definition of the Forth multiplication operator as an example of how primitive words are implemented.

The Forth dictionary is provided by the WordsList class which encapsulates a typed LinkedList. Helper methods in the class provide required functionality. Both the inner and the outer interpreters are methods in the JForth main class.

Two stacks are used in this implementation -- the data stack and the variable stack. Both use the stock Java Stack class. These stacks only contain objects. Currently the allowed objects on the stack are: Integers, Strings, BaseWords, and various types of control words used for conditional expressions and loop constructs. (Note: Since generics are used in this JForth implementation, Java 1.5 or newer must be used to compile and run the provided code.)

See the sidebar Running JForth to see how to run this code.

Typing "words" at the command line prompt will show you all available JForth words as shown below:

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!