>You Are Here<

Lisp-to-Perl Compiler

by David (hhdave [AT] blueyonder.co.uk)

Introduction

Here you will find a program to translate programs written in a (slightly unusual) dialect of lisp into executable perl programs. The dialect of lisp used is similar to scheme. It differs in having common lisp like macros (defmacro), a different concept of truth (it uses perl's) and a few other things. It does not, unfortunately, support 'proper' garbage collection (it uses perl's refcounting), last call optimisation or continuations. It is designed to produce perl code as efficient, if nor more so than that which someone programming directly in perl would, which is why it does not support these things - it would incur a lot of overhead trying to implement them in perl5.

Download

lisp2perl.tar.gz -- compiler source, compiled version of compiler (perl code), some example lisp scripts and some documentation.

Why?

Why do such a thing? Well, it allows us to combine the advantages of perl with those of lisp. For example, we can have: the power of lisp macros, the lovely, simple syntax of lisp (if you happen to like that kind of thing!), the excellent regular expression support of perl, countless cpan modules (and other benefits of the perl community), the good OS integration of perl, ability to run anywhere perl can, and many more. Also, because I could and because it was fun. I have used it succesfully for a fairly large project at work involving translating XML from one form to another.

Getting Started

I have made available the source code to this. It is written in lisp (it's self hosting). In the tarball (above) there is a makefile, for making the lisp2perl compiler, a compiled version of the compiler (required to compile itself), and a program which implements the classic lisp 'read, eval, print' loop and various examples and other stuff. The quickest way to get started is to [dl, unpack, cd, rep]

In the tarball there are also a few bits of documentation - I started documenting everything, but they are unfinished. The source code is also reasonably well commented and hopefully not too hard to understand (mostly!)

Download the tarball and extract it (tar xzf lisp2perl.tar.gz). It will make a directory called 'Lisp2Perl'. 'cd' into this directory and type:-

perl rep.scm.pl

You will now be in a classic lisp read-eval-print loop. It will wait for you to type a complete lisp expression, then evaluate it and print it. Evaluation is done by first compiling the expression into perl and then using perls 'eval' function to evaluate that perl. Here you can type things like:-

(+ 4 8)

to do simple sums. Exciting eh? To see what functions are supported have a look at all the macro definitions in 'stdMacros.scm'.

Compiling

To compile a simple scheme script (eg the 'hello.scm' script) just type:- perl bin/l2p hello.scm This will make a file called 'hello.scm.pl' which can be executed like any other perl script.

[mention stripping etc.]

Flavour of Lisp

Slightly 'odd'. More info coming soon...

Limitations

No gensyms (which would be nice for macros). No last call optimisation. No mark and sweep garbage collection.

Error reporting when trying to call an undefined function is not good. Because function calls compile as $fn->() the only error message you get is 'Undefined subroutine &main:: called at...'. I think this should be fixed at compile time, which I'm working on.

More information will be available here shortly about how this is implemented. In the meantime have a look at the sourcecode.