Project 1: A simple interpreter

In this project, you will write your first interpreter in this class, for the very small language (Mathex) we saw in class on Wednesday. This will allow you to practice thinking about how the representation of a language is used in an interpreter, and also to practice using ML.

1. Setup

In a directory for this class and project, copy the files from

/cslab/class/cs365/proj1

This includes a variety of files:

errormsg.sml
mathex.grm.sig
matex.grm.sml
mathex.lex.sml
mathex.sml
parse.sml
sources.cm

These, above, provide some some base code for the interpreter, mainly implementing the parser. You won't be modifying these.

absyn.sml

This contains the datatypes for the representation of the syntax.

prettyPrint.sml

This contains the code for the pretty-printer, as we saw in class.

interpret.sml

This is where the code for the interpreter will go, and this is the main file you will be modifying.

test-i.sml
test-p.sml

These test the interpreter and pretty-printer, respectively.

a.mex

This is a sample "program" in Mathex.

2. Writing the interpreter

The file interpreter.sml contains the code for the interpreter. Only the function interpret is seen by the outside world-- it takes in a string (the name of a file) and returns an int, the result of executing the Mathex program in the file.

The work is done by the function interpretExpr and its helper functions interpetTerm and interpretFactor. As the file is, they just return zero. Your task is to write these functions the way they are supposed to be.

You will want to use pattern-matching. The patterns that interpretExpr, for example, can take include things like

interpretExpr(Expr(t, r))
interpretExpr(Expr(t, []))
interpretExpr(Expr(t1, (a, t2)::rest))
interpretExpr(Expr(t1, (Plus, t2)::rest))

You may write additional functions inside the struct, but it is not necessary in as simple a program as this.

3. Testing

Remember that in the file test-p.sml, the line

runTests(["a.mex"])

executes all the test files in the list (in this case, just a.mex). To add (or remove) tests, just edit this line. It works the same way in test-i.sml. Then to run the interpreter on the listed test files, just enter at the command line

sml < test-i.sml

ML will spit out a verbose list of processing information, but you'll also see the result-- or a list of your mistakes.

You should also write testfiles of your own and test them.

4. Turn-in

Copy your interpret.sml to

/cslab/class/cs365/turnin/proj1/xxxxxx

where "xxxxxx" is [andy|david|tyler|yelemis]. I will grade your project by running it against a collection of test files.

DUE: Wednesday, Jan 30, 5:00 pm.


Thomas VanDrunen
Last modified: Wed Jan 16 13:28:16 CST 2008