Assignment 3, CSC431, Spring 2010
For the next assignment, you must write the static checking and rewriting pass for your compiler. Its job is to transform the AST that is the result of parsing into a form suitable for code generation, and to perform certain simple syntactic checks.
The input to the static pass will be an exp, as defined by the AST module.
You must produce the StaticPass module containing two functions:
flattenBegins : AST.exp -> AST.exp: accepts an AST.exp and produces an AST.exp that eliminates redundant begins. specifically, no BeginExp may contain another BeginExp except when it’s an empty one, and appears as the outer one’s last element. The code should be otherwise unchanged. So, for instance, the code BeginExp [BeginExp[X;Y];BeginExp[A;B]] (where A, B, X, and Y refer to non-Begin expressions) should be flattened into BeginExp[X;Y;A;B].
transform : AST.exp -> doubleTable * stringTable * funTable * fieldNameTable: This function must translate an exp in the form produced by the parser into an exp as defined by AST2.fs. Also, it must split the program apart into separate functions, and populate various tables with string, double, and field name constants.
1 The new AST definition
Here’s the definition of AST2.fs:
module AST2 |
|
type exp = |
ID of (string * int * int) // string for debugging |
| BoolExp of bool |
| IntExp of int |
| DoubleExp of int |
| StringExp of int |
| PrimExp of (AST.prim * exp list) |
| IfExp of (exp * exp * exp) |
| WhileExp of (exp * exp) |
| ReturnExp of exp |
| SetExp of ((string * int * int) * exp) |
| BeginExp of (exp list) |
| FieldRefExp of (exp * int) |
| FieldSetExp of (exp * int * exp) |
| MethodCallExp of (exp * int * exp list) |
| NewExp of (exp * exp list) |
| AppExp of (exp * exp list) |
| CloExp of (string * int) // string for debugging |
| ScopeExp of (int * string list * exp) |
with override self.ToString () = (sprintf "%A" self) |
|
type funEntry = string * string list * exp * bool |
|
type doubleTable = Map<double,int> |
type stringTable = Map<string,int> |
type funTable = Map<funEntry,int> |
type fieldNameTable = Map<string,int> |
|
type sframe = (Map<string,int> * int ref) |
type senv = (sframe * sframe list) |
We’ve discussed the meaning of each of these AST2 forms in class, but feel free to ask for clarification on the newsgroup.
2 Errors to Detect
Unbound identifiers
Attempts to bind variables named this.
Attempts to mutate this.
3 Handin
Hand in this assignment by creating an Assignment3 subdirectory in the private section of the subversion repository. You should include a makefile that produces StaticPass.dll, and a TEAM file as before, along with all source files needed to compile it.
4 Errors and Corrections
As always, let me know about any errors or ambiguities in the assignment specification.