/* @@@@@ @@@@@ @ @ @ @@@@@ @@@@@ @@@@@ @@@@@@ @@@@@@ @ @ @ @ @ @@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @@@@@ @@@@@ @@@@@ @@@@@ @ @ @ @ @ @ @@@@@ @ @ @@@ @ @ @ @ @ @@ @ @ @ @ @ @ @@@ @ @ @ @ @ @ @ @ @ @ @@@@@@ @@@@@@ @@@ * PrintTree.c - prints the abstract statement tree for diagnostics * 08/28/91 ks - reordered printed of PrintStmt * 09/20/90 - change offset to tabsize - since offset means something * 09/01/90 ks - added the if-then */ #include #include "astdef.h" #define PRINTREE_LOCAL #include "printree.h" #include "tracing.h" /* macro to print a number of blanks */ #define PRBLANK(off) for (OI=0; OI < off; OI++) printf(" "); static int OI; static void PrintAddr(ADDRTREE addr, int tabsize); static void PrintExpr(EXPRTREE expr, int tabsize); static void PrintProcName (PROCEDURETYPE proc); static void PrintParam(PARAMPTR param, int tabsize); static void PrintActParams(PARAMLIST param, int tabsize); static void PrintStmt (STMTPTR Stmt, int tabsize); static void PrintStmtList (STMTLIST stmt, int tabsize); /* -------------------------------------------------------------------------- */ /* --------------- PRINTTREE: PRINT AN ABSTRACT SYNTAX TREE ---------------- */ /* -------------------------------------------------------------------------- */ void PrintTree(void) { if (PtraceTree) Entering("PRINTREE"); printf(" \n"); printf("Intermediate Representation - the Abstract Syntax Tree\n"); printf(" Statements\n"); PrintStmtList(Prog.main,6); if (PtraceTree) Leaving("PRINTTREE"); } /* -------------------------------------------------------------------------- */ /* ------------------- PRINTADDR: PRINT AN ADDRESS TREE -------------------- */ /* -------------------------------------------------------------------------- */ static void PrintAddr(ADDRTREE addr, int tabsize) { if (PtraceTree) Entering("PRINTADDR"); switch(addr->type){ case Memory: PRBLANK(tabsize); printf("%s\n",addr->ae.attrs->id); break; case Immediate: PRBLANK(tabsize); printf("Immediate Value %d\n",addr->ae.i); break; } if (PtraceTree) Leaving("PRINTADDR"); }/* PrintAddr */ /* -------------------------------------------------------------------------- */ /* ------------------ PRINTEXPR: PRINT AN EXPRESSION TREE ------------------ */ /* -------------------------------------------------------------------------- */ /* Print an expression tree indented so it looks like a tree on the paper. */ static void PrintExpr(EXPRTREE expr, int tabsize) { if (PtraceTree) Entering("PRINTEXPR"); if(expr->op == Fetch) PrintAddr(expr->ee.addr, tabsize); else{ switch(expr->op){ case UsubOp: PRBLANK(tabsize); printf("neg\n"); PrintExpr(expr->ee.child, tabsize+3); break; case AddOp: PrintExpr(expr->ee.atree.left, tabsize+3); PRBLANK(tabsize); printf("add\n"); PrintExpr(expr->ee.atree.right, tabsize+3); break; case SubOp: PrintExpr(expr->ee.atree.left, tabsize+3); PRBLANK(tabsize); printf("subtract\n"); PrintExpr(expr->ee.atree.right, tabsize+3); break; case MpyOp: PrintExpr(expr->ee.atree.left, tabsize+3); PRBLANK(tabsize); printf("multiply\n"); PrintExpr(expr->ee.atree.right, tabsize+3); break; case DivOp: PrintExpr(expr->ee.atree.left, tabsize+3); PRBLANK(tabsize); printf("divide\n"); PrintExpr(expr->ee.atree.right, tabsize+3); break; case EqOp: PrintExpr(expr->ee.btree.left, tabsize+3); PRBLANK(tabsize); printf("equal\n"); PrintExpr(expr->ee.btree.right, tabsize+3); break; } } if (PtraceTree) Leaving("PRINTEXPR"); } /* PrintExpr */ /* -------------------------------------------------------------------------- */ /* ----------------- PRINTPROCNAME: PRINT A PROCEDURE NAME ----------------- */ /* -------------------------------------------------------------------------- */ static void PrintProcName(PROCEDURETYPE proc) { if (PtraceTree) Entering("PRINTPROCNAME"); switch(proc){ case ReadProc: printf("Read\n"); break; case ReadlnProc: printf("Readln\n"); break; case WriteProc: printf("Write\n"); break; case WritelnProc: printf("Writeln\n"); break; } if (PtraceTree) Leaving("PRINTPROCNAME"); } /* PrintProcName */ /* -------------------------------------------------------------------------- */ /* --------------------- PRINTPARAM: PRINT A PARAMETER --------------------- */ /* -------------------------------------------------------------------------- */ static void PrintParam(PARAMPTR param, int tabsize) { if (PtraceTree) Entering("PRINTPARAM"); switch(param->type){ case Reference: PrintAddr(param->pe.loc, tabsize); break; case Value: PrintExpr(param->pe.val, tabsize); break; } if (PtraceTree) Leaving("PRINTPARAM"); } /* PrintParam */ /* -------------------------------------------------------------------------- */ /* ---------------- PRINTACTPARAMS: PRINT A PARAMETER LIST ----------------- */ /* -------------------------------------------------------------------------- */ static void PrintActParms(PARAMLIST param, int tabsize) { if (PtraceTree) Entering("PRINTACTPARAMS"); while(param != NULL){ PrintParam(param, tabsize); param = param->next; } if (PtraceTree) Leaving("PRINTACTPARAMS"); } /* PrintActParms */ /* -------------------------------------------------------------------------- */ /* --------------------- PRINTSTMT: PRINT A STATEMENT ---------------------- */ /* -------------------------------------------------------------------------- */ static void PrintStmt(STMTPTR Stmt, int tabsize) { if (PtraceTree) Entering("PRINTSTMT"); switch(Stmt->type){ case AssignStmt: PRBLANK(tabsize); printf("Assign\n"); PrintAddr(Stmt->se.assign.lhs, tabsize+3); PRBLANK(tabsize+6); printf("gets\n"); PrintExpr(Stmt->se.assign.rhs, tabsize+3); break; case StdProcCall: PRBLANK(tabsize); PrintProcName(Stmt->se.procall.type); PRBLANK(tabsize+3); printf("parameters\n"); PrintActParms(Stmt->se.procall.params, tabsize+6); break; case IfThenStmt: PRBLANK(tabsize); printf("If\n"); PrintExpr(Stmt->se.ifthen.bexpr, tabsize+3); PRBLANK(tabsize+3); printf("Then\n"); PrintStmtList(Stmt->se.ifthen.slist, tabsize+6); PRBLANK(tabsize); printf("End If\n"); break; } if (PtraceTree) Leaving("PRINTSTMT"); } /* PrintStmt */ /* -------------------------------------------------------------------------- */ /* ----------------- PRINTSTMTLIST: PRINT A STATEMENT LIST ----------------- */ /* -------------------------------------------------------------------------- */ static void PrintStmtList(STMTLIST stmt, int tabsize) { if (PtraceTree) Entering("PRINTSTMTLIST"); while(stmt != NULL){ PrintStmt(stmt, tabsize); stmt = stmt->next; } if (PtraceTree) Leaving("PRINTSTMTLIST"); } /* PrintStmtList */