Mal (Make A Lisp) Compiler in C
Mal is Clojure inspired Lisp interpreter created by Joel Martin.
malcc is an incremental compiler implementation for the Mal language. It uses the Tiny C Compiler as the compiler backend and has full support for the Mal language, including macros, tail-call elimination, and even run-time eval.
malcc can also be used as an ahead-of-time compiler for Mal, producing a single binary file for distribution (though using it this way sacrifices run-time eval functionality since the compiler is not shipped in the resulting binary).
malcc has been tested on Ubuntu 18.04 and macOS 10.14 Mojave.
Prerequisites on Mac:
sudo xcode-select --install brew install pcre libgc
On Macos 10.14.4, there is a problem building TinyCC. This fixes that:
cd /usr/local/lib sudo ln -s ../../lib/libSystem.B.dylib libgcc_s.10.4.dylib
Prerequisites on Ubuntu/Debian:
apt-get install libpcre3-dev libedit-dev libgc-dev
git submodule update --init make all
Running the REPL:
→ ./malcc Mal [malcc] user> (+ 1 2) 3 user> ^D
Running a mal file:
→ ./malcc examples/fib.mal 55 12586269025
Ahead-of-Time compiling a mal file:
→ ./malcc --compile examples/fib.mal fib → ./fib 55 12586269025
malcc is fast! Running the microbenchmarks on my Macbook Pro yields an order-of-magnitude speedup for long-running code vs the C++ implementation:
→ ../cpp/stepA_mal perf1.mal "Elapsed time: 1 msecs" → ../cpp/stepA_mal perf2.mal "Elapsed time: 2 msecs" → ../cpp/stepA_mal perf3.mal iters over 10 seconds: 12415
→ ../../stepA_mal perf1.mal "Elapsed time: 0 msecs" → ../../stepA_mal perf2.mal "Elapsed time: 3 msecs" → ../../stepA_mal perf3.mal iters over 10 seconds: 226216
Note: I'm not sure if this is a fair comparison, but I could not coax the C interpreter implementation of mal to run the perf3 test, so I figured the C++ implementation was the next-best thing.
I followed the Mal guide to implement malcc, using the same steps that any other implementation would follow. Naturally, since malcc is a compiler rather than a straight interpreter, there are some differences from other implementations:
gen_and should be fairly self-explanatory.
load-fileis implemented as a special form rather than a simple function. This is because macros defined in a file must be found and compiled during code generation--they cannot be discovered at run-time.
maldirectory to provide test coverage.
Send patches/bug reports to ~firstname.lastname@example.org.
This project's sourcecode is copyrighted by Tim Morgan and licensed under the
MIT license, included in the
LICENSE file in this repository.
mal contains a copy of the Mal language repository and is
copyrighted by Joel Martin and released under the Mozilla Public License 2.0
(MPL 2.0). The text of the MPL 2.0 license is included in the