Acheron Lisp
A Lisp for the Web
meetings blogs news home
<< Using the REPL | Home | LispNYC presentation slides >>

Compiling a Hello World program

In this blog post I explain how to compile a "Hello World" program to a JavaScript program that can be executed in the browser.

This is "Hello World" in Acheron Lisp:

 

(defun main ()
  (write "Hello World!"))

(defnative write (str) base #{
  document.write(str);
}#)

 

The main function will be called when the program starts executing. There is currently no write function in the standard library of Acheron Lisp, but we can easily define this function as a native function. The function declaration part of native functions is like normal functions defined with defun, but the function body of native functions consists of JavaScript code. Since JavaScript usage might depend on a particular browser implementation, native functions can be defined for each browser variant. In the example above, the write function is valid for any browser variant, denoted with the base keyword. I will explain native functions and the variant concept in a later blog post in more details.

The example program aboved is saved in the file HelloWorld.lisp. For simplicity, I assume that this file is stored in the folder /files.
To compile this program to JavaScript you have to first change the directory to the folder where you unzipped the compiler. To compile the program you have to type the following command:

java -cp acheron.jar de.defmacro.acheron.compiler.CompilerMain 
     /files/HelloWorld.lisp -o /files

If you have more than one source file to compile you can list them after ...CompilerMain. The folder where to put the compiled files is specified with the -o argument.

 

If everything goes well the compiled *.js files are stored in the folder /files. You will get a JavaScript file for each supported browser variant. In the current version of the compiler you will get these files:

  • compiled_file_IE_v7.js
  • compiled_file_IE_v7_compressed.js
  • compiled_file_IE_v8.js
  • compiled_file_IE_v8_compressed.js
  • compiled_file_MOZILLA_gecko1.8.js
  • compiled_file_MOZILLA_gecko1.8_compressed.js
  • compiled_file_MOZILLA_gecko1.9.js
  • compiled_file_MOZILLA_gecko1.9_compressed.js

The compiler addtionally outputs a compressed version for each browser variant.

 

For a future version there is a automatic browser detection planned. The browser detection will then load the appropriate JavaScript file for the detected browser. Having a separate variant for each browser reduces the runtime overhead for dynamic browser detection. This is the reason, why the compiler outputs a distinct JavaScript file for each browser variant.

If we want to execute the compiled file, we have to manually load it in a HTML file, for example like this:

<html> 
  <head>
   <script type="text/javascript" src="compiled_file_IE_v7_compressed.js">
   </script> 
  </head>
  <body onload="acheron.initFunction();">
  </body>
</html>

Here the version for Internet Explorer 7 is loaded. It doesn't matter which file we load here, because they are all the same (for the example program). By calling the initFunction() we start executing the compiled main function.

 

You can download the example program here:
Source file
And test the compiled application here:
HTML file




Add a comment Send a TrackBack