Skip to content

gwasser/lambdatiger

Repository files navigation

lambdatiger

Tiger programming language implementation, with compiler and interpreter frontends

What?

Tiger is a small programming language defined in Modern Compiler Implementation in ML by Andrew Appel (Cambridge University Press, 2004). The base Tiger language consists of nested functions, record values with implicit pointers, integer and string variables, arrays, and simple imperative language control constructs. It can be extended with object-oriented and functional features.

tigerc is a compiler for the Tiger language. I also include an interpreter called tigeri. Both executables utilize a shared library of features for lexing, parsing, analysis, and assembly code generation.

Consider this experimental research quality, not production quality. This is more for my own learning, but I hope it is useful to you if you're interested. I hope to include documentation, comments, and proper git branches/tags to make it easy to study and learn from, as well as simply give an example of a well-written Haskell project.

Also, consider this a work-in-progress, as many of the features described above do not exist yet.

Building

This compiler is itself written in Haskell. Aside from standard ghc compiler libraries, you will need tasty testing framework installed. To compile, you ideally will use stack since it can manage dependencies and the build.

Compile with

stack build 

Then you can test the compiler library with

stack test

You can run the command you prefer (tigerc the compiler, or tigeri the interpreter) with stack using

stack exec (COMMAND_NAME)

You can either redirect a file from stdin, or provide filename(s) as parameters for interpretation. You can pass parameters to the actual executable and not stack by putting them after a --, such as stack exec tigerc -- --help.

Tutorial

I am maintaining a tutorial set of notes about the implementation as an mdBook for easy online viewing. The source for the tutorial is included in this repository.

The tutorial can be viewed online at gwasser.github.io/lambdatiger. It is automatically built using the GitHub Actions pipelines.

To build the book manually, you will need a Rust toolchain installed with the mdbook command install. If you don't have the mdbook command available, install it with:

cargo install mdbook

I also use the mdbook-pdf extension to build to a PDF for printing and publishing, install it with:

cargo install mdbook-pdf

Once you have the prerequisite commands installed, run:

mdbook build

You can also serve and view in web browser for easier viewing and automatic updates:

mdbook serve

By default, the files are served at localhost:3000.

References

The Tiger language and overall design of the compiler is based on the Tiger language and compiler presented in the book:

  • Appel, Andrew W. Modern Compiler Implementation in ML. Cambridge University Press, 2004.

However, the book describes implementation in ML, and does not provide sample code for all details of the compiler (the book website does provide some ML templates for guidance but not the full source of course as that is left as an exercise for the reader).

This project is an attempt at a Haskell implementation rather than ML of a similar Tiger language compiler.

Aside from the main text, the following may be useful:

A larger list that I haven't read much yet is Awesome Compilers, but I'm putting it here to help myself and others.

License

This software and accompanying documentation are Copyright (C) 2016-2024 Garret Wassermann.

This software distribution (including any documentation) is licensed under the GNU General Public License version 3. Please see the included COPYING file with this distribution.