merging
This commit is contained in:
@ -219,9 +219,13 @@ std::any LLVMIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext* ctx) {
|
||||
}
|
||||
std::any LLVMIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx)
|
||||
{
|
||||
irStream << "|||Start ";
|
||||
std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this));
|
||||
irStream << "End||| \n";
|
||||
std::string lhsType = symbolTable[ctx->lValue()->Ident()->getText()].second;
|
||||
irStream << "[[[Start ";
|
||||
std::string rhs = std::any_cast<std::string>(ctx->exp()->accept(this));
|
||||
irStream << "End]]] \n";
|
||||
|
||||
if (lhsType == "float") {
|
||||
try {
|
||||
@ -397,14 +401,7 @@ std::any LLVMIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx)
|
||||
|
||||
std::any LLVMIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
|
||||
std::string varName = ctx->Ident()->getText();
|
||||
std::string allocaPtr = symbolTable[varName].first;
|
||||
std::string type = symbolTable[varName].second;
|
||||
std::string temp = getNextTemp();
|
||||
irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n";
|
||||
tmpTable[temp] = type;
|
||||
return temp;
|
||||
// std::string varName = ctx->Ident()->getText();
|
||||
// return symbolTable[varName].first;
|
||||
return symbolTable[varName].first;
|
||||
}
|
||||
|
||||
// std::any LLVMIRGenerator::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
|
||||
@ -423,8 +420,36 @@ std::any LLVMIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
|
||||
// }
|
||||
// }
|
||||
|
||||
std::any LLVMIRGenerator::visitPrimExp(SysYParser::PrimaryExpContext* ctx) {
|
||||
return visitChildren(ctx);
|
||||
|
||||
std::any LLVMIRGenerator::visitPrimExp(SysYParser::PrimExpContext *ctx){
|
||||
std::cout << "Type name: " << typeid(*ctx).name() << std::endl;
|
||||
irStream << " PrimExp ";
|
||||
if (auto *lvalCtx = dynamic_cast<SysYParser::LValContext*>(ctx)) {
|
||||
// 是 LValContext,可以安全地调用 lValue()
|
||||
return visit(lvalCtx->lValue());
|
||||
}
|
||||
return visitChildren(ctx);
|
||||
}
|
||||
|
||||
std::any LLVMIRGenerator::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
|
||||
irStream << " Primaryexp \n";
|
||||
if (auto *lvalCtx = dynamic_cast<SysYParser::LValContext*>(ctx)) {
|
||||
std::string allocaPtr = std::any_cast<std::string>(lvalCtx->lValue()->accept(this));
|
||||
std::string varName = lvalCtx->lValue()->Ident()->getText();
|
||||
std::string type = symbolTable[varName].second;
|
||||
std::string temp = getNextTemp();
|
||||
irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n";
|
||||
tmpTable[temp] = type;
|
||||
return temp;
|
||||
} else if (auto *numCtx = dynamic_cast<SysYParser::NumContext*>(ctx)) {
|
||||
return numCtx->number()->accept(this);
|
||||
} else if (auto *strCtx = dynamic_cast<SysYParser::StrContext*>(ctx)) {
|
||||
return strCtx->string()->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
std::any LLVMIRGenerator::visitParenExp(SysYParser::ParenExpContext* ctx) {
|
||||
return ctx->exp()->accept(this);
|
||||
}
|
||||
|
||||
std::any LLVMIRGenerator::visitNumber(SysYParser::NumberContext* ctx) {
|
||||
@ -436,6 +461,29 @@ std::any LLVMIRGenerator::visitNumber(SysYParser::NumberContext* ctx) {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::any LLVMIRGenerator::visitString(SysYParser::StringContext *ctx)
|
||||
{
|
||||
if (ctx->STRING()) {
|
||||
// 处理字符串常量
|
||||
std::string str = ctx->STRING()->getText();
|
||||
// 去掉引号
|
||||
str = str.substr(1, str.size() - 2);
|
||||
// 转义处理
|
||||
std::string escapedStr;
|
||||
for (char c : str) {
|
||||
if (c == '\\') {
|
||||
escapedStr += "\\\\";
|
||||
} else if (c == '"') {
|
||||
escapedStr += "\\\"";
|
||||
} else {
|
||||
escapedStr += c;
|
||||
}
|
||||
}
|
||||
return "\"" + escapedStr + "\"";
|
||||
}
|
||||
return ctx->STRING()->getText();
|
||||
}
|
||||
|
||||
std::any LLVMIRGenerator::visitUnExp(SysYParser::UnExpContext* ctx) {
|
||||
if (ctx->unaryOp()) {
|
||||
std::string operand = std::any_cast<std::string>(ctx->unaryExp()->accept(this));
|
||||
|
||||
Reference in New Issue
Block a user