/* Generate code for a statement list */ void genStmtList(node *stmtlist, codelabel *next) { if (stmtlist != NULL) { codelabel nextstmt; while (stmtlist->internal.child[1] != NULL){ initLabel(&nextstmt,"stmt"); genStmt(stmtlist->internal.child[0], &nextstmt); placeLabel(&nextstmt); stmtlist = stmtlist->internal.child[1]; } // do the last statement genStmt(stmtlist->internal.child[0], next); } }
/* generate code for an if/elseif statement */ void genIf(node *stmt, codelabel *endif) { node *expr = stmt->internal.child[0]; node *thenstmts = stmt->internal.child[1]; /* stmtlist */ node *elsestmt = stmt->internal.child[2]; /* Nelseif/Nelse */ codelabel elselabel; if(elsestmt) { //make sure there is an else initLabel(&elselabel, "else"); genCondExpr(expr, FALSE, &elselabel); } else { genCondExpr(expr, FALSE, endif); } genStmtList(thenstmts, endif); if(elsestmt) { outputCmd("jmp", 0); outputLabel(endif); placeLabel(&elselabel); genStmt(elsestmt, endif); } }
/* Procedure cGen recursively generates code by * tree traversal */ static void cGen( TreeNode * tree) { if (tree != NULL) { switch (tree->nodekind) { case StmtK: genStmt(tree); break; case ExpK: genExp(tree); break; default: break; } cGen(tree->sibling); } }