Skip to content

Repository files navigation

Trent Decaf Compiler AMOD-4901

This repo contains a simple programming language for Trent's AMOD-4901 course. The compiler is based off the expresso language from MIT's 2002 version of SMA-5502.

Getting Started

If you are just looking to get started you can find some syntax examples in ./examples/, which includes a few example decaf files ranging from hello world to more complex examples that test various features of the language. You can compile these examples using dotnet run --project=../decaf -- ./<name>.decaf --wat ./<name>.wat in the examples directory where <name> is the file name you are trying to compile. This will produce a WebAssembly text format file that you can then run using a WebAssembly runtime such as WasmTime. We highly recommend using WasmTime for running the generated WebAssembly code as it's important you are using a runtime that supports function references. The code can be run using wasmtime run --wasm reference-types --wasm function-references <name>.wat.

Dependencies

The compiler is built using a minimal set of dependencies defined below:

  • .NET SDK (required)
    • This is required to build and the run the compiler as it's implemented in c#.
  • ANTLR (semi-required)
    • This is used for generating the lexer and parser, if you do not have this installed you can use the pre generated lexer and parser but if you make changes to the grammar and do not have ANTLR installed your changes will not be reflected in the compiler.
  • WasmTime (semi-optional)
    • This is used for running the generated WebAssembly code, both by the task system and in our test suite. It's semi optional however as our cli doesn't have any direct dependency on it and our test suite will skip any tests that require it if it's not installed, however if you want to run the generated WebAssembly code you will need this installed or an alternative WebAssembly runtime.
  • Taskfile (semi-optional)
    • This is used for running shell commands, however if you do not have this installed you can copy the commands from the taskfile directly and run them in bash or the corresponding commands on your platform.
  • Spectre Console (required)
    • This is required for the command line interface of the compiler, it gives us easy command line parsing (which is a non trivial problem) and allows us to have a nice looking command line interface.
    • This is not used in the compiler itself and is only used for the command line interface, if you wanted to use the compiler as a library you could do so without this dependency.
  • Verify
    • This is used for snapshot testing, it allows us to easily test the output of the compiler against expected output without having to write a lot of boilerplate code.

Building

The compiler itself can be built using the command task build.

Usage

By default the compiler can be used by running task -- <opts>, you can run task experiment which will run the compiler with whatever is in ./example.decaf

Testing

The compiler uses MSTest and Verify for testing.

Tests can be run using task test, you can use task test -- --filter <query> to run specific tests.

Extra Commands

  • task format - This can be used to format the entire compiler and test suite.
  • task clean - This can be used to clean up any artifacts generated by the compiler.

WalkThrough

A walkthrough of the compiler and documentation on the components can be found in the docs folder.

Copyright ©️ 2025 Jake Follest, Tony Tran

About

An optimizing compiler targeting WebAsssembly, for the Trent's Amod-4901 reading course.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages