Go to the first, previous, next, last section, table of contents.


Compiling Scheme code to byte-codes

All Scheme functions and source files are invisibly compiled into internal Java byte-codes. A traditional evaluator is only used for top-level directly entered expressions outside a lambda. (It would have been simpler to also byte-compile top-level expressions by surrounding them by a dummy lambda. However, this would create a new Class object in the Java VM for every top-level expression. This is undesirable unless you have a VM that can garbage collect Class objects.)

To save speed when loading large Scheme source files, you probably want to pre-compile them and save them on your local disk. There are two ways to do this.

You can compile a Scheme source file to a single archive file. You do this using the compile-file function. The result is a single file that you can move around and load just like the .scm source file. You just specify the name of the archive file to the load procedure. Currently, the archive is a "zip" archive and has extension ".zip"; a future release will probably use "Java Archive" (jar) files. The advantage of compiling to an archive is that it is simple and transparent. A minor disadvantage is that it causes the Java "verifier" to be run when functions are loaded from it, which takes a little extra time.

Alternatively, you can compile a Scheme source file to a collection of `.class' files. You then use the standard Java class loading mechanism to load the code. The Java "verifier" does not need to get run, which makes loading a little faster. The compiled class files do have to be installed be installed somewhere in the CLASSPATH.

Compiling Scheme to an archive file

To byte-compile a file `foo.scm' do:

(compile-file "foo.scm" "foo")

This will create `foo.zip', which contains byte-compiled "j-code" that implements `foo.scm'.

You can later do:

(load "foo")

This will load `foo.zip', which should have the same effect as loading `foo.scm', except you will get the byte-compiled versions.

Compiling Scheme to a set of .class files

Invoking `kawa' (or `java kawa.repl') with the `-C' flag will compile a `.scm' source file into one or more `.class' files.

You run it as follows:

kawa [-d outdirectory] [-P prefix] [-T topname] [--main | --spplet] -C infile ...

Note the `-C' must come last, because `Kawa' processes the arguments and options in order,

Here:

`-C infile ...'
The Scheme source files we want to compile.
`-d outdirectory'
The directory under which the resulting `.class' files will be. The default is the current directory.
`-P prefix'
A string to prepend to the generated class names. The default is the empty string.
`-T topname'
The name of the "top" class - i.e. the one that contains the code for the top-level expressions and definitions. The default is generated from the infile and prefix.
`--main'
Generate a main method so that the resulting "top" class can be used as a stand-alone application. See section Compiling Scheme to a standalone application.
`--applet'
The resulting class inherits from java.applet.Applet, and can be used as an applet. See section Compiling Scheme to an applet.

When you actually want to load the classes, the outdirectory must be in your `CLASSPATH'. You can use the standard load function to load the code, by specifying the top-level class, either as a file name (relative to outdirectory) or a class name. E.g. if you did:

kawa -d /usr/local/share/java -P my.lib. -T foo -C foosrc.scm

you can use either:

(load "my.lib.foo")

or:

(load "my/lib/foo.class")

If you are compiling a Scheme source file (say `foosrc.scm') that uses macros defined in some other file (say `macs.scm'), you need to make sure the definitions are visible to the compiler. One way to do that is with the `-f':

kawa -f macs.scm -C foosrc.scm

Compiling Scheme to a standalone application

A Java application is a Java class with a special method (whose name is main). The application can be invoked directly by naming it in the Java command. If you want to generate an application from a Scheme program, create a Scheme source file with the definitions you need, plus the top-level actions that you want the application to execute. You can compile in the regular way decribed in the previous section, but add the --main option. For example, assuming your Scheme file is MyProgram.scm:

kawa --main -C MyProgram.scm

This will create a MyProgram.class which you can either load (as decribed in the previous section), or invoke as an application:

java MyProgram [args]

Your Scheme program can access the command-line arguments args by using the global variable `command-line-arguments'.

Compiling Scheme to an applet

An applet is a Java class that inherits from java.applet.Applet. The applet can be downloaded and run in a Java-capable web-browser. To generate an applet from a Scheme program, write the Scheme program with appropriate definitions of the functions `init', `start', `stop' and `destroy'. You must declare these as zero-argument functions with a <void> return-type.

Here is an example, based on the scribble applet in Flanagan's "Java Examples in a Nutshell" (O'Reilly, 1997):

(define-private last-x 0)
(define-private last-y 0)

(define (init) <void>
  (let ((applet :: <java.applet.Applet> (this)))
    (invoke applet 'addMouseListener
	    (object (<java.awt.event.MouseAdapter>)
		    ((mousePressed (e :: <java.awt.event.MouseEvent>)) <void>
		     (set! last-x (invoke e 'getX))
		     (set! last-y (invoke e 'getY)))))
    (invoke applet 'addMouseMotionListener
	    (object (<java.awt.event.MouseMotionAdapter>)
		    ((mouseDragged (e :: <java.awt.event.MouseEvent>)) <void>
		     (let ((g :: <java.awt.Graphics>
			      (invoke applet 'getGraphics))
			   (x :: <int> (invoke e 'getX))
			   (y :: <int> (invoke e 'getY)))
		       (invoke g 'drawLine last-x last-y x y)
		       (set! last-x x)
		       (set! last-y y)))))))

(define (start) <void> (format #t "called start.~%~!"))
(define (stop) <void> (format #t "called stop.~%~!"))
(define (destroy) <void> (format #t "called destroy.~%~!"))

You compile the program with the `--applet' flag in addition to the normal `-C' flag:

java kawa.repl --applet -C scribble.scm

You can then create a `.jar' archive containing your applet. You also need to include the Kawa classes in the `.jar', or you can include a MANIFEST file that specifies Class-Path to use a Java 2 download extension.

jar cf scribble.jar scribble*.class other-classes ...

Finally, you create an `.html' page referencing your applet:

<html><head><title>Scribble testapp</title></head>
<body><h1>Scribble testapp</h1>
You can scribble here:
<br>
<applet code="scribble.class" archive="scribble.jar" width=200 height=200>
Sorry, Java is needed.</applet>
</body></html>


Go to the first, previous, next, last section, table of contents.