/* Recursively print a parse tree as code */ void PrintParseTree(VyParseTree* tree){ int treeType = tree->type; /* If it is a list, then print a parenthesized list */ if(treeType == TREE_LIST){ PrintListGeneric(tree, '(', ')'); } /* Print a reference separated by a colon */ else if(treeType == TREE_REF){ PrintParseTree(GetObj(tree)); /* Delete the previous space before adding the colon */ printf("\b:"); PrintParseTree(GetRef(tree)); } /* If it is a number or identifier, just print the string */ else if(treeType == TREE_IDENT) { printf("%s ", GetStrData(tree)); } else if(treeType == TREE_NUM){ PrintNumber(GetNumberData(tree)); /* Print a space so that the backspace doesn't delete part of the number */ printf(" "); } /* Print strings enclosed in quotes */ else if(treeType == TREE_STR){ printf("\""); printf("%s", GetStrData(tree)); printf("\""); printf("\""); } /* If it is an error, print the error */ else if(treeType == TREE_ERROR){ printf("\n---------------------------------\n"); printf("Error: %s", tree->data->error.message); printf("\n---------------------------------\n"); } else{ printf("\n\n\nWarning: Incorrect parse tree node type: %d! \n\n\n", treeType); } }
void PrintListGeneric(VyParseTree* tree, char oDelim, char cDelim){ printf("%c", oDelim); /* Print each element recursively */ int i; for(i = 0; i < ListTreeSize(tree); i++){ VyParseTree* next = GetListData(tree,i); if(IsQuote(next)){ printf("'"); PrintParseTree(GetListData(next, 1)); } else if(IsSubstitution(next)){ if(IsSplicingSubstitution(next)){ printf("$@"); }else{ printf("$"); } PrintParseTree(GetListData(next, 1)); } else if(next->type == TREE_LIST){ VyParseTree* first = GetListData(next, 0); if(first->type == TREE_IDENT && StrEquals(GetStrData(first), "infix")){ PrintListGeneric(GetListData(next, 1), '{','}'); } else if(first->type == TREE_IDENT && StrEquals(GetStrData(first), "quote-substitutions")){ PrintListGeneric(GetListData(next, 1), '[',']'); }else{ PrintParseTree(next); } } else{ PrintParseTree(next); } } /* If it wasn't an empty list, remove the extra space generated by the item inside */ if(ListTreeSize(tree) > 0){ printf("\b"); } printf("%c ", cDelim); }
/* The controlling function for PrintParseTree(VyParseTree*) */ void PrintTree(VyParseTree* tree){ if(tree != NULL){ printf("\n"); /* Recursively print */ PrintParseTree(tree); linesPrinted = 0; }else{ printf(" NULL TREE "); } }
// Print the tree for debugging void PrintParseTree(struct tree_node* pNode, int depth) { int i; for (i = 0; i < depth; i++) printf(" "); printf("NODE (%d): ", pNode->id); if (pNode->type == TN_INTEGER) printf("INTEGER (%d)\n", pNode->ival); else if (pNode->type == TN_QSTRING) printf("QSTRING\n"); else if (pNode->type == TN_CHAR) printf("CHAR ('%s')\n", pNode->sval); else if (pNode->type == TN_BYTE_IDENT) printf("BYTE IDENTIFIER (\"%s\" offset: %d)\n", pNode->sval, pNode->ival); else if (pNode->type == TN_WORD_IDENT) printf("WORD IDENTIFIER (\"%s\" offset: %d)\n", pNode->sval, pNode->ival); else if (pNode->type == TN_BOOL_IDENT) printf("BOOLEAN IDENTIFIER (\"%s\" offset: %d)\n", pNode->sval, pNode->ival); else if (pNode->type == TN_PTR_IDENT) printf("PTR IDENTIFIER (\"%s\" offset: %d)\n", pNode->sval, pNode->ival); else if (pNode->type == TN_PTR_BYTE_ASSIGN) printf("BYTE POINTER ELEMENT ASSIGNMENT (\"%s\")\n", pNode->sval); else if (pNode->type == TN_PTR_WORD_ASSIGN) printf("WORD POINTER ELEMENT ASSIGNMENT (\"%s\")\n", pNode->sval); else if (pNode->type == TN_PTR_BYTE) printf("BYTE PTR DEREFERENCE (\"%s\")\n", pNode->sval); else if (pNode->type == TN_PTR_WORD) printf("WORD PTR DEREFERENCE (\"%s\")\n", pNode->sval); else if (pNode->type == TN_STRING_LITERAL) printf("STRING LITERAL (\"%s\")\n", pNode->sval); else if (pNode->type == TN_BYTE_BLOCK) printf("BYTE BLOCK\n"); else if (pNode->type == TN_WORD_BLOCK) printf("WORD BLOCK\n"); else if (pNode->type == TN_FUNCTION) printf("FUNCTION\n"); else if (pNode->type == TN_FUNCTIONCALL) printf("FUNCTION CALL (\"%s\")\n", pNode->sval); else if (pNode->type == TN_SEGCALL) printf("SEGMENT() CALL\n"); else if (pNode->type == TN_OFFCALL) printf("OFFSET() CALL\n"); else if (pNode->type == TN_BYTE_ASSIGN) printf("BYTE ASSIGN\n"); else if (pNode->type == TN_WORD_ASSIGN) printf("WORD ASSIGN\n"); else if (pNode->type == TN_BOOL_ASSIGN) printf("BOOL ASSIGN\n"); else if (pNode->type == TN_PTR_ASSIGN) printf("PTR ASSIGN\n"); else if (pNode->type == TN_RET_INT) printf("RETURN INT\n"); else if (pNode->type == TN_RET_BOOL) printf("RETURN BOOL\n"); else if (pNode->type == TN_RET_PTR) printf("RETURN PTR\n"); else if (pNode->type == TN_WHILE) printf("WHILE\n"); else if (pNode->type == TN_TRUE) printf("TRUE\n"); else if (pNode->type == TN_FALSE) printf("FALSE\n"); else if (pNode->type == TN_NULL) printf("NULL\n"); else if (pNode->type == TN_IF) printf("IF\n"); else if (pNode->type == TN_ARGLIST) printf("ARGUMENT LIST\n"); else if (pNode->type == TN_PARAM) printf("PARAM (%s %s)\n", identStr(pNode->ival), pNode->sval); else if (pNode->type == TN_PARAMLIST) printf("PARAMLIST\n"); else if (pNode->type == TN_FDEF) printf("FUNCTION DEFINITION (%s)\n", pNode->sval); else if (pNode->type == TN_ASM) printf("ASM\n"); else if (pNode->type == TN_ASMLOC) printf("MEMLOC\n"); else if (pNode->type == TN_ASMREG) printf("REGISTER (%s)\n", regStr(pNode->ival)); else if (pNode->type == TN_AMOV) printf("MOV\n"); else if (pNode->type == TN_ACALL) printf("CALL\n"); else if (pNode->type == TN_AINT) printf("INT\n"); else if (pNode->type == TN_IADD) printf("ADD\n"); else if (pNode->type == TN_ISUB) printf("SUB\n"); else if (pNode->type == TN_UMINUS) printf("UNARY MINUS\n"); else if (pNode->type == TN_IMUL) printf("MUL\n"); else if (pNode->type == TN_IDIV) printf("DIV\n"); else if (pNode->type == TN_IMOD) printf("MOD\n"); else if (pNode->type == TN_IEQ || pNode->type == TN_BEQ || pNode->type == TN_PEQ) printf("EQ\n"); else if (pNode->type == TN_INEQ || pNode->type == TN_BNEQ || pNode->type == TN_PNEQ || pNode->type == TN_UBNEQ) printf("NEQ\n"); else if (pNode->type == TN_ILT) printf("LT\n"); else if (pNode->type == TN_IGT) printf("GT\n"); else if (pNode->type == TN_ILTE) printf("LTE\n"); else if (pNode->type == TN_IGTE) printf("GTE\n"); else if (pNode->type == TN_REF) printf("MEMORY ADDRESS\n"); else printf("NO TYPE\n"); for (i = 0; i < pNode->numOperands; i++) if (pNode->operands[i] != NULL) PrintParseTree(pNode->operands[i], depth + 1); else printf("WARNING: UNEXPECTED NULL OPERAND!\n"); if (pNode->pNextStatement != NULL) PrintParseTree(pNode->pNextStatement, depth); }