There are three basic ways one might implement some programming language X using Java:
One could write an interpreter for X in Java.
First parse the source into an internal
“abstract syntax tree”,
and then evaluate it using a recursive eval
function.
The advantage of using Java rather than C or C++ is having garbage collection,
classes, and the standard Java class library makes it easier.
The obvious down-side of the interpreter solution is speed. If your interpreter for language X is written in Java, which is in turn interpreted by a Java VM, then you get double interpretation overhead.
You could write a compiler to translate language X into Java source code. You need to define a mapping for language X constructs into equivalent Java constructs, and then write a program that writes out a parsed X program using corresponding Java constructs. A number of implementations take this approach, including NetRexx, and various “extended Java” dialects.
This only gives you the single a single (Java VM) layer of interpretation. On the other hand, most of the efforts that people are making into improving Java performance will benefit your implementation, since you use standard Java bytecodes.
The biggest problem with that approach is that it is an inherently batch process, and has poor responsiveness. Consider a read-eval-print-loop, that is the ability for a user to type in an expression, and have it be immediately read, evaluated, and the result printed. If evaluating an expression requires converting it to a Java program, writing it to a disk file, invoking a separate java compiler, and then loading the resulting class file into the running environment, then response time will be inherently poor. This hurts “exploratory programing”, that is the ability to define and update functions on the fly.
A lesser disadvantage is that Java source code is not quite as expressive as Java bytecodes. While bytecodes are very close to Java source, there are some useful features not available in the Java language, such as goto. Debugging information is also an issue.
Alternatively, you could directly generate Java bytecode.
You can write out a .class file, which can be saved for later.
You also have the option of writing to an internal byte array,
which can be immediately loaded as a class using the
java.lang.ClassLoader.defineClass
method.
In that case you can by-pass the file system entirely,
yield a fast load-and-go solution, which enables a very responsive
read-eval-print loop.
This solution is the best of both worlds. The main problem is that more code needs to be written. Fortunately, by using Kawa, much of that work has already been done.
I will discuss the compiler later, but first we will give an overview of the run-time environment of Kawa, and the classes used to implement Scheme values.