/* 翻译case 语句 */ static void TranslateCaseStatement(AstStatement stmt) { AstCaseStatement caseStmt = AsCase(stmt); /* 在switch 中已经创建 */ StartBBlock (caseStmt->respBB); TranslateStatement (caseStmt->stmt); }
static void DumpStatement(AstStatement stmt, int pos) { switch (stmt->kind) { case NK_ExpressionStatement: DumpExpression(AsExpr(stmt)->expr, pos); break; case NK_LabelStatement: fprintf(ASTFile, "(label %s:\n", AsLabel(stmt)->id); LeftAlign(ASTFile, pos + 2); DumpStatement(AsLabel(stmt)->stmt, pos + 2); LeftAlign(ASTFile, pos); fprintf(ASTFile, "end-label)"); break; case NK_CaseStatement: fprintf(ASTFile, "(case "); DumpExpression(AsCase(stmt)->expr, pos + 7); LeftAlign(ASTFile, pos + 2); DumpStatement(AsCase(stmt)->stmt, pos + 2); LeftAlign(ASTFile, pos); fprintf(ASTFile, "end-case)"); break; case NK_DefaultStatement: fprintf(ASTFile, "(default "); DumpStatement(AsDef(stmt)->stmt, pos + 10); LeftAlign(ASTFile, pos); fprintf(ASTFile, "end-default)"); break; case NK_IfStatement: fprintf(ASTFile, "(if "); DumpExpression(AsIf(stmt)->expr, pos + 5); LeftAlign(ASTFile, pos + 2); fprintf(ASTFile, "(then "); LeftAlign(ASTFile, pos + 4); DumpStatement(AsIf(stmt)->thenStmt, pos + 4); LeftAlign(ASTFile, pos + 2); fprintf(ASTFile, "end-then)"); if (AsIf(stmt)->elseStmt != NULL) { LeftAlign(ASTFile, pos + 2); fprintf(ASTFile, "(else "); LeftAlign(ASTFile, pos + 4); DumpStatement(AsIf(stmt)->elseStmt, pos + 4); LeftAlign(ASTFile, pos + 2); fprintf(ASTFile, "end-else)"); } LeftAlign(ASTFile, pos); fprintf(ASTFile, "end-if)"); break; case NK_SwitchStatement: fprintf(ASTFile, "(switch "); DumpExpression(AsSwitch(stmt)->expr, pos + 9); LeftAlign(ASTFile, pos + 2); DumpStatement(AsSwitch(stmt)->stmt, pos + 2); LeftAlign(ASTFile, pos); fprintf(ASTFile, "end-switch)"); break; case NK_WhileStatement: fprintf(ASTFile, "(while "); DumpExpression(AsLoop(stmt)->expr, pos + 8); LeftAlign(ASTFile, pos + 2); DumpStatement(AsLoop(stmt)->stmt, pos + 2); LeftAlign(ASTFile, pos); fprintf(ASTFile, "end-while)"); break; case NK_DoStatement: fprintf(ASTFile, "(do "); DumpExpression(AsLoop(stmt)->expr, pos + 5); LeftAlign(ASTFile, pos + 2); DumpStatement(AsLoop(stmt)->stmt, pos + 2); LeftAlign(ASTFile, pos); fprintf(ASTFile, ")"); break; case NK_ForStatement: fprintf(ASTFile, "(for "); DumpExpression(AsFor(stmt)->initExpr, pos + 6); LeftAlign(ASTFile, pos + 6); DumpExpression(AsFor(stmt)->expr, pos + 6); LeftAlign(ASTFile, pos + 6); DumpExpression(AsFor(stmt)->incrExpr, pos + 6); LeftAlign(ASTFile, pos + 2); DumpStatement(AsFor(stmt)->stmt, pos + 2); LeftAlign(ASTFile, pos); fprintf(ASTFile, "end-for)"); break; case NK_GotoStatement: fprintf(ASTFile, "(goto %s)", AsGoto(stmt)->id); break; case NK_ContinueStatement: fprintf(ASTFile, "(continue)"); break; case NK_BreakStatement: fprintf(ASTFile, "(break)"); break; case NK_ReturnStatement: fprintf(ASTFile, "(ret "); DumpExpression(AsRet(stmt)->expr, pos + 5); fprintf(ASTFile, ")"); break; case NK_CompoundStatement: { AstNode p = ((AstCompoundStatement)stmt)->stmts; fprintf(ASTFile, "{"); while (p != NULL) { LeftAlign(ASTFile, pos + 2); DumpStatement((AstStatement)p, pos + 2); if (p->next != NULL) fprintf(ASTFile, "\n"); p = p->next; } LeftAlign(ASTFile, pos); fprintf(ASTFile, "}"); break; } default: assert(0); } }