In this project, you will write your first interpreter in this class, for the very small language (MathEx) we saw in class. 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. In fact, the only hard part of this assignment is getting used to using ML for this purpose.
In a directory for this class and project, copy the files from
~tvandrun/Public/cs365/proj1-mathex
This includes a variety of files. The follownig files provide some base code for the interpreter, mainly implementing the parser. You won't be modifying these:
mathex.grm.sml mathex.lex.sml parse.sml sources.cm
The following contains the datatypes for the representation of the syntax:
absyn.sml
The following contains the code for the pretty-printer, as we saw in class:
prettyPrint.sml
This is the one you will be modifying:
interpret.sml
These test the interpreter and pretty-printer, respectively:
mathex-main.sml mathex-print.sml
These are sample "programs" in MathEx.
a.mex b.mex c.mex d.mex e.mex z.mex
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.
Remember that in the file mathex-print.sml
,
the line
runTests(["z.mathex", "a.mathex", "b.mathex", "c.mathex", "d.mathex", "e.mathex"]);
executes all the test files in the list.
It works the same way in mathex-main.sml
.
Then to run the interpreter on the listed test files,
just enter at the command line
sml < mathex-main.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.
Copy your interpret.sml
to
/cslab/class/cs365/turnin/proj1/xxxxxx
where "xxxxxx" is [lily|chet|christopher|tim|david|ben|caleb]. I will grade your project by running it against a collection of test files.
DUE: Tuesday, Jan 26, 5:00 pm.