public class WordSet
extends java.lang.Object
code
words.
Such classes must extend this class.
The WordList.loadJava()
method will only load Java classes if they extend this class.
To be recognised as a primitive the Java method must have the
signature:
All such methods will be reflected in the current word list with the Forth name method. When the Forth word is executed, the associated Java method is executed, with the invokingpublic void
method(
XT
);
XT
as the argument.
All other Java methods will not be reflected in the Forth word list
but are available to the Java primitive.
Three fields are provided to allow the java primitives access to the
current virtual machine, they are:
vm
–
The current virtual machine.data
–
The data (parameter) stack of the current virtual machine.vm.data
.
mem
–
The user memory area of the current virtual machine.vm.memory
.
import org.rigwit.forth.XT; import org.rigwit.forth.annotation.*; public class ExampleWords extends WordSet { // ... Java primitive definitions go here ... }
@Primitive("
<name>")
@Immediate
@Stack("
<description>")
@Environment
[("
<name>")
]@Forth
annotation. The annotation may be attached to
a class, field or method definition.
As the Java annotation system does not allow an annotation to be
attached to a definition more than once, the @Interpret
annotation has been provided to allow for a collection of
@Forth
annotations. Thus making it possible to attach
multiple forth statement to a definition. The @Forth
annotations within an @Interpret
group are processed in
the order they are written.
The annotations are interpreted after the primitive definitions in the class have been loaded. The annotations are processed in the following order:@Interpret({ @Forth("DECIMAL"), @Forth("32 CONSTANT BL") }) public class ExampleWords extends WordSet { ... }
value
definition
(6.2.2405).
The value
primitive is given as follows:
Note that the@Stack("x '<spaces>name' -- ") public void value(XT xt) { xt = vm.create(); // Create user definition xt.setHandler(this, "valueDef"); // Change XT handler Object item = data.popObj(); // Pop item from data stack mem.addCell(item); // Place item in dictionary }
vm.create()
method returns the
XT
for the definition it has created.
The handler for the XT is changed to the valueDef
method
of this class. The name of the handler method must end with
"Def
".
When the new XT is executed the handler for the XT
(valueDef(XT)
) will be invoked with the XT as its argument.
The address of the next cell after the calling XT is on the top of
the data stack.
The handler method must be public and take an XT as an argument.
It should return an integer which is added to the current IP so
that it can skip over the data associated with the XT (a single
cell value in this case).
The valueDef
method is:
@Stack("a-addr -- x") public int valueDef(XT xt) { int addr = data.popAddr(); // Address of next cell Object value = mem.getCell(addr); // Fetch cell data.pushObj(value); // Push cell return VM.CELL; // Skip one cell }
TO
(6.2.2405) is used to
modify the value returned by a value
definition.
This will parse the next word in the input stream and determine
that the word is a user definition.
It will then look up the words handler (valueDef
in our case)
and replace the "Def
" with "To
".
As with the handler method, the modifier method must be public and
take the XT of the definition to be modified as its argument.
Thus when TO
is used on a value
definition, the java
method valueTo(XT)
is invoked with the XT of the definition
as its argument. The valueTo
method is:
Note that@Stack("x -- ") public int valueTo(XT xt) { Object item = data.popObj(); mem.setCell(xt.here, item); return 0; }
xt.here
holds the address of the data field of
the original valueDef
definition.
By making the method return an integer value, we ensure it is not
reflected in the Forth word list when the primitive file is loaded,
although the return value is ignored.Modifier and Type | Field and Description |
---|---|
protected TypedStack |
data
The data stack for the current virtual machine.
|
protected Memory |
mem
The user memory area for the current virtual machine.
|
protected VM |
vm
The current virtual machine.
|
Constructor and Description |
---|
WordSet() |
Modifier and Type | Method and Description |
---|---|
void |
setVirtualMachine(VM machine)
Set the virtual machine to reflect the vm the wordset has been
loaded into.
|
protected VM vm
protected TypedStack data
vm.data
.protected Memory mem
vm.memory
.public void setVirtualMachine(VM machine)
machine
- the Forth Virtual Machine these words effect.