CLisp Compiler Sample

Sample Overview

CLisp is a sample Lisp compiler, which compiles to common intermediate language (CIL). CLisp is developed in C# and uses reflection emit to generate CIL. It can also be adapted to function as an interpreter if the generated CIL is executed dynamically instead of persisting to an assembly file.
 
It compiles functions like car, cdr, cons, atom, null, subst. Not all Lisp functions are implemented, but others can be added with ease, as they are implemented as a runtime library. Control flow structures such as if-then-else and do-while have been implemented. Recursive function calls are supported.

The compiler consists of the following modules in separate source files.

Lexer

Converts the input file into a token stream. Strips out the source code comments.

Parser

Parses the token stream outputting an abstract syntax tree (AST). Syntax errors are reported, but error recovery is not robust. Multiple errors are not reported.

Semantic Analyzer

Analyzes the AST and augments it with semantic information using a bottom-up approach. Currently Int and lists are the only types. Floating point numbers are not supported.

Code Generator

Visits the AST and generates corresponding CIL.

Sample Source and Build Output Locations

The sample source is found in sscli\samples\compilers\clisp.  The source files are:

The build output location is %TARGETCOMPLUS%\samples\compilers\clisp.  The output file is an executable assembly named clisp.exe.

Building the Sample

All samples are built by the buildall script. 

You can also build all the samples by switching to the root of the sample directory, sscli\samples, and typing build -c.

You can build this specific sample by switching to the sample directory and typing build -c.

Running the Sample

  1. Run env.bat or source the env.csh or env.sh script files depending on your platform.
  2. Build the SSCLI using the buildall script or batch file.
  3. Switch to the %TARGETCOMPLUS%\samples\compilers\clisp directory.
  4. Type the following command:

    clix clisp inputfile

    where inputfile is the name of a Lisp source file.  For example:

    clix clisp fibo.lisp

  5. Run the resulting assembly:

clix fibo.exe


Copyright (c) 2002 Microsoft Corporation. All rights reserved.