How to extend Jatha by adding new functions
The easy way
If you can figure out how to implement it in LISP, do that
rather than implement it in Java. It's simpler.
Put the definition in a file and load it.
Extending the Java code
Since the new function is a LISP function,
you need to define it as a method on the LispValue interface
(org.jatha.dynatype.LispValue
) and then implement it on
one or more of the implementations of LispValue
, such as
StandardLispSymbol
or StandardLispNumber
.
Put a default method on StandardLispValue
that returns a default
value or throws an appropriate exception if the argument type(s) are not correct
at runtime.
Extending the LISP interpreter
If you just want to call the function from Java, the instructions above
are sufficient. However, if you want to make the function available
through the LISP interpreter, you also need to do the following:
- The LISP compiler (
org.jatha.compile.LispCompiler.java
)
compiles the code into virtual machine code. The virtual machine is
based on the one in Kogge's book Symbolic Architectures.
- Each compilable function must a subclass of
LispPrimitive
. See the
directory org.jatha.compile
for many examples.
- The function has to have a
compileArgs
method and an
execute
method. For example, look at GethashPrimitive.java
.
The execute
method must push a result on the S register and
pop the C (code) register an appropriate number of times.
- The easiest thing to do is to copy the Primitive class for a function similar
to the one you are adding and modify it to call your new function. This is
very easy.
- To make the new primitive available to the user via the interpreter, you have
to register it. Add a call to Register in
org.jatha.compile.LispCompiler.init()
.