JavaForth.jar |
[ Download ] | [ JavaDoc ] | [ Change Log ] |
test.forth |
[ Download ] |
This is a very early attempt at a Forth package for Java. It is a fairly traditional ITC based Forth compiler with primitives written in Java. It uses a 32-bit (aligned) cell with a 16-bit character.
You can run the console based interpreter directly from the command line:
java -jar JavaForth.jar
The interpreter has a number of command line arguments:
-d
<size>-r
<size>-e
<word>-e bye
" as the last
argument.
.forth
, but the following
extensions are supported:
.forth
,
.fth
,
.fs
, and
.fh
.
-
All other parameters are passed to the Forth application via the non-standard
next-arg
word.
So, to run the interpreter on the test suite, give the command:
java -jar JavaForth.jar test
This will run the test suite and then prompt for further input.
The prompt will display the current input line number and
"OK
" followed by an indication of the data stack depth.
The following word sets are currently implemented:
Note:
The Java terminal interface is line based and does not support single key
entry. Thus the word KEY
must read a whole line from the
terminal, which must be terminated with the <return> or
<enter> key. Only the first character of this line is
returned by KEY
.
In effect KEY
is defined as:
: KEY ( -- char ) HERE DUP 1 ACCEPT DROP C@ ;
This can be resolved by using an application specific console, another item for the todo list.
The following non-standard word is provided:
2VARIABLE
and DU<
.
[IF]
,
[ELSE]
and
[THEN]
.BYE
,
.S
,
?
and
DUMP
.
The following non standard words have also been defined:
*/
".
The Jar can also be used as a Forth interpreter package. The following Java program uses the package to implement a simple Forth Interpreter:
import org.rigwit.Forth; import java.io.*; public class Sample { public static void main(String[] args) throws IOException { Forth forth = new Forth(); forth.interpret(".( Free Memory: ) unused 1024 / 1 U.R .( Kb) CR"); File file = new File("test.forth"); forth.interpret(file); forth.interpret(); } }
Let's take this a line at a time:
6. | Forth forth = new Forth(); |
Creates a new Forth interpreter. A program may have as many different interpreters as it requires. | |
7. | forth.interpret(".( Free Memory: ) unused 1024 / 1 U.R .( Kb) CR"); |
Interpret a string of forth source code. | |
10. | forth.interpret(file); |
Read and interpret the file as forth source.
This may throw an | |
12. | forth.interpret(); |
Enter into the standard interpret loop:
BYE word
is executed by the forth system.
|
The full details of the API can be found in the Java Doc
for the Forth
class.