/monkey

the Monkey compiler for a light-weighted functional language.

Primary LanguageStandard ML

The Monkey Compiler

The Monkey Compiler is a small open sourced compiling system. The goal of this project is to assist understanding the phases in compiling functional programming languages. You can explore the compiling procedures and intermediate representations by specifying and examining the output. In particular, the Monkey Compiler provides two parallel methods to eliminate higher-order functions, which may reveal some aspects to improve the efficiency of the target program and facilitate further study.

###Source Language

The source language of Monkey Compiler is basically a subset of Standard ML. Chapter 1 (CPS Conversion) of the document describes the abstract syntax of the source language (basically the same with the concrete syntax). Chapter 0 (Front End) also provides some details about source language syntax.

###Structure

The figure below descirbes the basic structure of the compiler.

###How to use

You may compile the source code to some certain platform using tool such as MLton or SML/NJ. The Monkey Compiler cannot compile itself for now.

Once you have the executive version of the Monkey Compiler, you may run it with command (given that the compiler's executive file is named "monkey", and it can be found in Path):

monkey [option ...] filename

Then the corresponding .c file will be generated. (Currently it needs to be compiled together with runtime.c and runtime.h by some C compiler to obtain an executive. As an alternative, we can encode the garbage collection code into the generated .c file. I guess this is one of the reasons why choosing Java bytecode as the target language will be a good idea.)

###Options

The following options are supported:

  • -defunc

    Use the defunctionalization strategy for higher-order function elimination. This is the default setting.

  • -closure

    Use closure conversion strategy for higher-order function elimination.

  • -all

    Output the codes of all the intermediate representations. Note that the output for CPS representaion, closure passing representaion and defunctionalized representaion (all ends with ".sml") cannot be compiled and run, for SML compilers cannot infer the type information.

  • -t

    The executive file you generated will print its run time to the console.

Multiple options can be selected.