You don't have to implement too much in Java.
Even S-expressions parsing can be implemented in Lisp, with the Java part being only responsible for reading some simple serialised form. For example, in a Forth-like syntax, as in (a (b c))
-> a b c NIL . . NIL . .
, where .
makes a cons cell.
Java runtime should provide the very minimal set of primitives. No need to support full Lisp semantics, it's easier to implement a compiler from a "full" Lisp into a simplified bootstrap language in Lisp itself. The bootstrap language may support following primitives:
FUNCTION
, Making a function object, with a given number of arguments and a given body
ARG
, Accessing the n-th argument of a function
VAR
, Accessing the n-th slot of a closure
IF
primitive
APPLY
primitive
CONST
primitive
CLOSURE
primitive - similar to a function object, but initialising a number of closure environment slots
It's easy to implement such an interpreter in Java, with all of the primitives above being classes implementing the same AstNode
interface with a method Run(Environment env)
.
On the Lisp side, a simple "compiler" must be implemented, which will perform lambda lifting and variables enumeration. Things like let
, let*
, etc., can be implemented as simple macros (with macro expander also implemented in Lisp itself).
A minimal set of runtime library functions provided by Java should contain car, cdr, cons, listp, nullp, symbol to string and string to symbol conversions, string concatenation, and an access to the Java reflection for implementing everything else from the Lisp side.
Of course, such interpreter is not going to be very efficient - for example, you'd have to implement recursive functions via Y-combinator. But, you'll only need it to bootstrap the next iteration of a language, of course implemented in Lisp as well, which will compile the same source language into Java bytecode.
Variations are possible - one may implement, say, a simple graph reduction engine instead of an interpreter above, it's even simpler, but is even less efficient.