An overview of AST related code in the Adap compiler
The AST interface is defined and mostly implemented by classes in ast.parser package. See javadoc for their data and function members. As a research tool, it has little written documentation. See the TA for questions. AST tree comprises of nodes. The nodes would correspond to function definitions and declaration, Variable declaration, assignments, jumps, labels, variables operands etc. For most of the project you would be just moving nodes from one place to another, creating new nodes, copying nodes and deleting them. All these basic operations can be done by function provided in the base class of all nodes AstNode. Following is a brief description of each of them:
AstNode class: the base class of AST. It supports common functions as follows:
· Tree traversal includes GetChildren(), GetNumChildren() and GetParentAst()
· Tree modification includes insertions such as InsertMeBefore(AstNode), InsertMeAfter(AstNode), InsertMeAsChildOf(AstNode)
· Move operations as well as the deletion operation, DetachMe(), MoveMeAfter(AstNode), MoveMeAsChildOf(AstNode), MoveMeAsChildOf(AstNode, int)
FuncAst class: the root AST node for a C function definition. It has a function body (a CodeBlock object) and a function type (a FuncType object).
BaseType class: A base type represents basic storage types, which include primitive types (e.g. int and float, implemented by PrimitiveType class), array type (e.g. A[100], ArrayType class), and function declarations (FuncType class). In this project, you need to handle just single-dimension arrays.
DataType class: The type of data, which is a base type (zero indirection) or a pointer to a base type. You do not need to deal with pointer type in this project, so the indirection is always zero.
VarAst class: declaration of variables, which includes a name and a data type (a DataType object).
SegAst class: a segment of C code. A segment is a lexical scope. It has a type table and symbol table for types and variables defined inside the scope. Three subclasses are ProgAst (the root node for a program), FileAst (the root node for a C file), and CodeBlock (the root node for a C block). All programs in this project will be in a single file. So a ProgAst node would contain only one element, which is a FileAst node. The parser and code generation interface is supported by ProgAst. To parse a program into an AST tree, construct a ProgAst by giving it the file name of the input program, for example, ProgAst inputProgram = new ProgAst("/u/cding/test/foo.adap"). For a valid AST, you can call ProgAst.GenCode() to generate the program in C. For example, invoking inputProgram.GenCode() would generate a C file named "foo.out.c". The code generation checks some but not all syntax and type consistency. You need to use gcc to compile the generated program to check for errors.
OperatorAst class: the root class for all expressions. Operators include data-access operators in DataAccAst and computation operators in ExprAst. The only data access operator we consider is array access operator in ArrayAcc class, a subclass of DataAccAst. Arithmetic and comparison expressions are represented by ExprAst with the C-style operator (e.g. +, -, *, /, %, >>). Function calls are also ExprAst with the function name as the operator (e.g. printf). If an ExprAst node is a statement, e.g. "foo(a+b);", it also belongs to the subclass, ExprStmt.
StatAst class: four other types of statements (in addition to ExprStmt) are assignment (AssignStat), goto and if-goto (GotoStat), label (LabelStat), and return statement (ReturnStat). To test whether an AstNode n is a statement, for example, you can use "(n instanceof StatAst || n instanceof ExprStmt)".
VarAccAst class: access to a variable, for example, an access to a variable "v".
ConstAst class: an integer, real, or string constant. This project does not need real constants, but it may have string constants for printfs.