[lab2]implemented while, break, continue

This commit is contained in:
ladev789
2025-04-01 17:50:17 +08:00
parent 9d619b11d7
commit 9f562aa0be
3 changed files with 71 additions and 3 deletions

View File

@@ -205,6 +205,38 @@ std::any SysYIRGenerator::visitStmt(SysYParser::StmtContext* ctx) {
irStream << " br label %" << mergeLabel << "\n";
irStream << mergeLabel << ":\n";
} else if (ctx->WHILE()) {
std::string loop_cond = "while.cond." + std::to_string(tempCounter);
std::string loop_body = "while.body." + std::to_string(tempCounter);
std::string loop_end = "while.end." + std::to_string(tempCounter++);
loopStack.push({loop_end, loop_cond});
irStream << " br label %" << loop_cond << "\n";
irStream << loop_cond << ":\n";
std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
irStream << " br i1 " << cond << ", label %" << loop_body << ", label %" << loop_end << "\n";
irStream << loop_body << ":\n";
ctx->stmt(0)->accept(this);
irStream << " br label %" << loop_cond << "\n";
irStream << loop_end << ":\n";
loopStack.pop();
} else if (ctx->BREAK()) {
if (loopStack.empty()) {
throw std::runtime_error("Break statement outside of a loop.");
}
irStream << " br label %" << loopStack.top().breakLabel << "\n";
} else if (ctx->CONTINUE()) {
if (loopStack.empty()) {
throw std::runtime_error("Continue statement outside of a loop.");
}
irStream << " br label %" << loopStack.top().continueLabel << "\n";
} else if (ctx->blockStmt()) {
ctx->blockStmt()->accept(this);
} else if (ctx->exp()) {
ctx->exp()->accept(this);
}
return nullptr;
}
@@ -289,6 +321,7 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext* ctx) {
irStream << " " << temp << " = srem " << type << " " << left << ", " << right << "\n";
}
left = temp;
tmpTable[temp] = type;
}
return left;
}
@@ -298,6 +331,7 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext* ctx) {
std::string left = std::any_cast<std::string>(mulExps[0]->accept(this));
for (size_t i = 1; i < mulExps.size(); ++i) {
std::string right = std::any_cast<std::string>(mulExps[i]->accept(this));
irStream << "right is " << right << "\n";
std::string op = ctx->children[2*i-1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
@@ -307,6 +341,7 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext* ctx) {
irStream << " " << temp << " = sub nsw " << type << " " << left << ", " << right << "\n";
}
left = temp;
tmpTable[temp] = type;
}
return left;
}

View File

@@ -23,9 +23,10 @@ private:
std::vector<std::string> breakStack;
std::vector<std::string> continueStack;
bool hasReturn = false;
struct LoopLabels {
std::string breakLabel;
std::string continueLabel;
std::string breakLabel; // break跳转的目标标签
std::string continueLabel; // continue跳转的目标标签
};
std::stack<LoopLabels> loopStack; // 用于管理循环的break和continue标签
std::string getNextTemp();

View File

@@ -1,5 +1,37 @@
//test add
int a1 = 1;
int a2 = 2;
int a3 = 3;
int a4 = 4;
int a5 = 5;
int a6 = 6;
int a7 = 7;
int a8 = 8;
int a9 = 9;
int a10 = 10;
int a11 = 11;
int a12 = 12;
int a13 = 13;
int a14 = 14;
int a15 = 15;
int a16 = 16;
int a17 = 1;
int a18 = 2;
int a19 = 3;
int a20 = 4;
int a21 = 5;
int a22 = 6;
int a23 = 7;
int a24 = 8;
int a25 = 9;
int a26 = 10;
int a27 = 11;
int a28 = 12;
int a29 = 13;
int a30 = 14;
int a31 = 15;
int a32 = 16;
int main(int e){
int a, b;
@@ -8,7 +40,7 @@ int main(int e){
b = 2;
a = 11;
c = 1.6 ;
return a+b;
return a+b+1;
}
int add(int a, int b){