add syntax
This commit is contained in:
116
src/util.c
116
src/util.c
@@ -137,6 +137,36 @@ TreeNode *newExpNode(ExpKind kind) {
|
||||
return t;
|
||||
}
|
||||
|
||||
TreeNode *newDeclNode(DeclKind kind) {
|
||||
TreeNode *t = (TreeNode *) malloc(sizeof(TreeNode));
|
||||
int i;
|
||||
if (t == NULL)
|
||||
fprintf(listing, "Out of memory error at line %d\n", lineno);
|
||||
else {
|
||||
for (i = 0; i < MAXCHILDREN; i++) t->child[i] = NULL;
|
||||
t->sibling = NULL;
|
||||
t->nodekind = DeclK;
|
||||
t->kind.decl = kind;
|
||||
t->lineno = lineno;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
TreeNode *newTypeNode(TypeKind kind) {
|
||||
TreeNode *t = (TreeNode *) malloc(sizeof(TreeNode));
|
||||
int i;
|
||||
if (t == NULL)
|
||||
fprintf(listing, "Out of memory error at line %d\n", lineno);
|
||||
else {
|
||||
for (i = 0; i < MAXCHILDREN; i++) t->child[i] = NULL;
|
||||
t->sibling = NULL;
|
||||
t->nodekind = TypeK;
|
||||
t->kind.type = kind;
|
||||
t->lineno = lineno;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
/* Function copyString allocates and makes a new
|
||||
* copy of an existing string
|
||||
*/
|
||||
@@ -169,6 +199,22 @@ static void printSpaces(void) {
|
||||
fprintf(listing, " ");
|
||||
}
|
||||
|
||||
void printType(TreeNode *tree) {
|
||||
switch (tree->type) {
|
||||
case Void:
|
||||
fprintf(listing, "void");
|
||||
break;
|
||||
case Integer:
|
||||
fprintf(listing, "int");
|
||||
break;
|
||||
case IntegerArray:
|
||||
fprintf(listing, "int[]");
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* procedure printTree prints a syntax tree to the
|
||||
* listing file using indentation to indicate subtrees
|
||||
*/
|
||||
@@ -179,27 +225,28 @@ void printTree(TreeNode *tree) {
|
||||
printSpaces();
|
||||
if (tree->nodekind == StmtK) {
|
||||
switch (tree->kind.stmt) {
|
||||
case CompK:
|
||||
fprintf(listing, "Compound Statement:\n");
|
||||
break;
|
||||
case IfK:
|
||||
fprintf(listing, "If\n");
|
||||
fprintf(listing, "%s:\n",
|
||||
(tree->child[2] != NULL) ? "If-Else Statement" : "If Statement");
|
||||
break;
|
||||
case RepeatK:
|
||||
fprintf(listing, "Repeat\n");
|
||||
case IterK:
|
||||
fprintf(listing, "While Statement:\n");
|
||||
break;
|
||||
case AssignK:
|
||||
fprintf(listing, "Assign to: %s\n", tree->attr.name);
|
||||
break;
|
||||
case ReadK:
|
||||
fprintf(listing, "Read: %s\n", tree->attr.name);
|
||||
break;
|
||||
case WriteK:
|
||||
fprintf(listing, "Write\n");
|
||||
case ReturnK:
|
||||
fprintf(listing, "Return Statement:\n");
|
||||
break;
|
||||
default:
|
||||
fprintf(listing, "Unknown ExpNode kind\n");
|
||||
fprintf(listing, "Unknown StmtNode kind\n");
|
||||
break;
|
||||
}
|
||||
} else if (tree->nodekind == ExpK) {
|
||||
switch (tree->kind.exp) {
|
||||
case AssignK:
|
||||
fprintf(listing, "Assign:\n");
|
||||
break;
|
||||
case OpK:
|
||||
fprintf(listing, "Op: ");
|
||||
printToken(tree->attr.op, "\0");
|
||||
@@ -208,12 +255,55 @@ void printTree(TreeNode *tree) {
|
||||
fprintf(listing, "Const: %d\n", tree->attr.val);
|
||||
break;
|
||||
case IdK:
|
||||
fprintf(listing, "Id: %s\n", tree->attr.name);
|
||||
fprintf(listing, "Variable: name = %s\n", tree->attr.name);
|
||||
break;
|
||||
case ArrIdK:
|
||||
fprintf(listing, "Variable: name = %s\n", tree->attr.name);
|
||||
break;
|
||||
case CallK:
|
||||
fprintf(listing, "Call: function name = %s\n", tree->attr.name);
|
||||
break;
|
||||
default:
|
||||
fprintf(listing, "Unknown ExpNode kind\n");
|
||||
break;
|
||||
}
|
||||
} else if (tree->nodekind == DeclK) {
|
||||
switch (tree->kind.decl) {
|
||||
case FuncK:
|
||||
fprintf(listing, "Function Declaration: name = %s, return type = ", tree->attr.name);
|
||||
printType(tree);
|
||||
fprintf(listing, "\n");
|
||||
break;
|
||||
case VarK:
|
||||
fprintf(listing, "Variable Declaration: name = %s, type = ", tree->attr.name);
|
||||
printType(tree);
|
||||
fprintf(listing, "\n");
|
||||
break;
|
||||
case ArrVarK:
|
||||
fprintf(listing, "Variable Declaration: name = %s, type = ", tree->attr.name);
|
||||
printType(tree);
|
||||
fprintf(listing, "\n");
|
||||
break;
|
||||
case NonArrParamK:
|
||||
if (tree->type == Void)
|
||||
fprintf(listing, "Void Parameter\n");
|
||||
else {
|
||||
fprintf(listing, "Parameter: name = %s, type = ", tree->attr.name);
|
||||
printType(tree);
|
||||
fprintf(listing, "\n");
|
||||
}
|
||||
break;
|
||||
case ArrParamK:
|
||||
fprintf(listing, "Parameter: name = %s, type = ", tree->attr.name);
|
||||
printType(tree);
|
||||
fprintf(listing, "\n");
|
||||
break;
|
||||
default:
|
||||
fprintf(listing, "Unknown DeclNode kind\n");
|
||||
break;
|
||||
}
|
||||
} else if (tree->nodekind == TypeK) {
|
||||
|
||||
} else
|
||||
fprintf(listing, "Unknown node kind\n");
|
||||
for (i = 0; i < MAXCHILDREN; i++)
|
||||
|
||||
Reference in New Issue
Block a user