A quick introduction to the Camelot compiler

Compiling Camelot programs

The Camelot compiler compiles Camelot programs to JVM bytecode via the intermediate language Grail. Here's a simple example of a Camelot program:
 let rev l acc =
     match l with 
	[] -> acc
      | h::t -> rev t (h::acc)

 let reverse l = rev l []

 let print_list l = 
	match l with
	   [] -> print_string "\n"
	 | h::t -> 
              let _ = print_string (h ^ " ")
	      in print_list t

 let start args = 
	let _ = print_string "Input list:    "
	in let _ = print_list args
	in let revargs = reverse args
	in let _ = print_string "Reversed list: "
	in print_list revargs
To compile this program, put it in a file called "reverse.cmlt", say. To invoke the compiler on a DICE machine, type
   /group/project/mrg/bin/camelot reverse
or
   /group/project/mrg/bin/camelot reverse.cmlt
(You can make a copy of the compiler or set up an alias to avoid having to type such a long command). The compiler should create a Java classfile called "reverse.class" along with another classfile which you can ignore for the time being.

Running Camelot programs

Camelot programs call some standard functions in a class called "Camelotlib", and this must be accessible via your CLASSPATH environment variable: this can be done by typing
  export CLASSPATH=$CLASSPATH:/group/project/mrg/bin:.
or by putting this command in your .bprofile file. Once the classpath has been set up properly the classfile is executed in the usual way:
  java reverse 8 9 677 99 64 6 79 00 87 5
This should run the program and print out the command-line arguments in reverse order.

For more information about Camelot, see the Camelot manual or a Gentle Introduction to Camelot (not yet complete).

Grail

As mentioned above, the Camelot compiler uses an intermediate language called Grail, which is a functional form of JVM bytecode. You can get the Camelot compiler to save the Grail version of a program by using the -g option:
   /group/project/mrg/bin/camelot -g reverse
This will place the Grail translation of the program in the file "reverse.gr".

In addition, there are standalone programs called gdf and gf which will convert Grail to and from bytecode. For example, typing

   /group/project/mrg/bin/gdf reverse.gr
will compile the Grail program into JVM bytecode in the classfile reverse.class. This process can be reversed using the gf program:
   /group/project/mrg/bin/gf reverse
This will decompile the classfile and write the Grail equivalent to the terminal. Note that you should leave out the .class extension to the filename when using gf. Note also that gf will usually only work with classfiles which have been compiled from Grail; the bytecode in classfiles compiled from Java is too irregular to allow decompilation to Grail.

If you wish to examine the actual JVM bytecode in a classfile, use the command "javap -c".


Kenneth MacKenzie