diff --git a/src/midend/SysYIRGenerator.cpp b/src/midend/SysYIRGenerator.cpp index d095a97..812bd39 100644 --- a/src/midend/SysYIRGenerator.cpp +++ b/src/midend/SysYIRGenerator.cpp @@ -33,6 +33,9 @@ namespace sysy { Value *SysYIRGenerator::promoteType(Value *value, Type *targetType) { //如果是常量则直接返回相应的值 + if (targetType == nullptr) { + return value; // 如果值为空,那就不需要转换 + } ConstantInteger* constInt = dynamic_cast(value); ConstantFloating *constFloat = dynamic_cast(value); if (constInt) { @@ -385,9 +388,9 @@ Value* SysYIRGenerator::computeExp(SysYParser::ExpContext *ctx, Type* targetType BinaryExpLenStack.push_back(0); // 进入新的层次时Push 0 visitAddExp(ctx->addExp()); - if(targetType == nullptr) { - targetType = Type::getIntType(); // 默认目标类型为int - } + // if(targetType == nullptr) { + // targetType = Type::getIntType(); // 默认目标类型为int + // } compute(); // 最后一个Value应该是最终结果 @@ -428,9 +431,9 @@ Value* SysYIRGenerator::computeAddExp(SysYParser::AddExpContext *ctx, Type* targ visitMulExp(ctx->mulExp(i)); // BinaryValueStack.push_back(operand); } - if(targetType == nullptr) { - targetType = Type::getIntType(); // 默认目标类型为int - } + // if(targetType == nullptr) { + // targetType = Type::getIntType(); // 默认目标类型为int + // } // 根据后缀表达式的逻辑计算 compute(); // 最后一个Value应该是最终结果 @@ -1620,13 +1623,13 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) { } std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) { - Value* result = computeAddExp(ctx->addExp(0), Type::getIntType()); + Value* result = computeAddExp(ctx->addExp(0)); for (int i = 1; i < ctx->addExp().size(); i++) { auto opNode = dynamic_cast(ctx->children[2*i-1]); int opType = opNode->getSymbol()->getType(); - Value* operand = computeAddExp(ctx->addExp(i), Type::getIntType()); + Value* operand = computeAddExp(ctx->addExp(i)); Type* resultType = result->getType(); Type* operandType = operand->getType();