<religious-statement>
</religious-statement>
. <religious-statement>
</religious-statement>
, <abcl-dir>\dist\abcl.jar
abcl.jar
to your class path for ABCL projects. C:\abcl-src-0.15.0>cd dist C:\abcl-src-0.15.0\dist>java -jar abcl.jarThis will run the Lisp REPL.
CL-USER(1): (format t "Hello, world!") Hello, world! NIL
CL-USER(2): (exit)
Cons
and LispObject
classes Cons
car()
and cdr()
methods if you want to write Java code in a Lisp style. Cons
objects into arrays, if you wish by using the copyToArray()
method which returns LispObject[]
. LispObject
LispObject
s to Java primitives with methods such as intValue()
which returns (surprise!) an int
. org.armedbear.lisp
package:
Interpreter
createInstance()
: Creates a Lisp interpreter. eval(String expression)
: Evaluates a Lisp expression. Often used with load
to load a Lisp file.Packages
findPackage(String packageName)
: Finds a Lisp package. Package
findAccessibleSymbol(String symbolName)
: Finds a symbol such as that for a function. Symbol
getSymbolFunction()
: Returns the function for a corresponding symbol. Function
execute()
: Executes a function taking a variable number of LispObject
s as arguments. JavaObject
: A subclass of LispObject
for objects coming from Java. Interpreter interpreter = Interpreter.createInstance(); interpreter.eval("(load \"my-lisp-code.lisp\")");
Package defaultPackage = Packages.findPackage("CL-USER");
my-function
defined in my-lisp-code.lisp
(which was loaded above). We obtain it in two steps like this:
Symbol myFunctionSym = defaultPackage.findAccessibleSymbol( "MY-FUNCTION"); Function myFunction = myFunctionSym.getSymbolFunction();
Cons list = (Cons) myFunction.execute( Fixnum.getInstance(64), Fixnum.getInstance(64));
Cons
Java class corresponds to a Lisp list. Note also that we wrap the int
s (in this example) as Fixnum
s. (defun my-function (n1 n2) ...)
Since the user can't be expected to know how to map every Java type to Lisp and vice-versa, there are a couple
of nice methods you can use in all cases:
public static LispObject JavaObject.getInstance(Object, boolean)
: Converts (or wraps) a Java object to a Lisp object, if the boolean is true (else it just wraps it in a JavaObject
).public Object LispObject.javaInstance()
: Converts (or unwraps) a Lisp object to Java. You can invoke this on any Lisp object; if it can't be converted, it will be returned as-is.This code sample is by Ville Voutilainen.
public class Main { public int addTwoNumbers(int a, int b) { return a + b; } }See the entire code sample here.
We need to get the
Main
)
int
)
After that we can invoke the function with jcall
,
giving the method reference, the object and the parameters.
The result is a Lisp object (no need to do jobject-lisp-value
,
unless we invoke the method
with jcall-raw
).
(defun void-function (param) (let* ((class (jclass "Main")) (intclass (jclass "int")) (method (jmethod class "addTwoNumbers" intclass intclass)) (result (jcall method param 2 4))) (format t "in void-function, result of calling addTwoNumbers(2, 4): ~a~%" result)))See the entire code sample here.
life.lisp
: Lisp code for simulating Conway's Game of Life cellular automaton. LifeGUI.java
: A subclass of JApplet for showing a Life universe. Calls life.lisp
for all Life functionality. This documentation was written by Paul Reiners (except where otherwise noted). Helpful suggestions and corrections were given by Alessio Stalla and others on the ABCL mailing list. Please email me with any suggestions or corrections.