diff --git a/scripts/mir_test1.sh b/scripts/mir_test1.sh index fa2931a..a8f1871 100755 --- a/scripts/mir_test1.sh +++ b/scripts/mir_test1.sh @@ -115,31 +115,38 @@ for test_file in "${test_files[@]}"; do rm -f "$tmp_out" if [ -f "$expected_file" ]; then - expected=$(cat "$expected_file" | tr -d '\n') + expected_output=$(sed '$d' "$expected_file" | tr -d '\n') + expected_exit_code=$(tail -1 "$expected_file") + # 先比较输出 + output_match=true + if [ "$program_output" != "$expected_output" ]; then + output_match=false + fi + + # 再比较退出码 + exit_match=true + if [ $exit_code -ne "$expected_exit_code" ] 2>/dev/null; then + exit_match=false + fi - if [[ "$expected" =~ ^[0-9]+$ ]] && [ "$expected" -ge 0 ] && [ "$expected" -le 255 ] && [ -z "$program_output" ]; then - # 期望退出码(且没有输出) - if [ $exit_code -eq "$expected" ] 2>/dev/null; then - echo -e " ${GREEN}✓${NC} $relative_path (退出码: $exit_code)" - ((pass_run++)) - else - echo -e " ${RED}✗${NC} $relative_path (退出码: 期望 $expected, 实际 $exit_code)" - ((fail_run++)) - fi + if [ "$output_match" = true ] && [ "$exit_match" = true ]; then + echo -e " ${GREEN}✓${NC} $relative_path" + ((pass_run++)) else - # 期望输出内容 - if [ "$program_output" = "$expected" ]; then - echo -e " ${GREEN}✓${NC} $relative_path (输出匹配)" - ((pass_run++)) - else - echo -e " ${RED}✗${NC} $relative_path (输出不匹配: 期望 '$expected', 实际 '$program_output')" - ((fail_run++)) - fi + echo -ne " ${RED}✗${NC} $relative_path" + [ "$output_match" = false ] && echo -n " (输出不匹配: 期望 '$expected_output', 实际 '$program_output')" + [ "$exit_match" = false ] && echo -n " (退出码不匹配: 期望 $expected_exit_code, 实际 $exit_code)" + echo + ((fail_run++)) fi else - # 没有期望文件 - echo -e " ${GREEN}✓${NC} $relative_path (退出码: $exit_code, 输出: '$program_output')" - ((pass_run++)) + if [ $exit_code -eq 0 ]; then + echo -e " ${GREEN}✓${NC} $relative_path (退出码: $exit_code, 输出: '$program_output')" + ((pass_run++)) + else + echo -e " ${RED}✗${NC} $relative_path (退出码: $exit_code, 输出: '$program_output')" + ((fail_run++)) + fi fi done diff --git a/src/irgen/IRGenStmt.cpp b/src/irgen/IRGenStmt.cpp index ab51ab7..0142a90 100644 --- a/src/irgen/IRGenStmt.cpp +++ b/src/irgen/IRGenStmt.cpp @@ -33,21 +33,6 @@ IRGenImpl::BlockFlow IRGenImpl::VisitStmt(SysYParser::StmtContext& s) { } else if (func_->GetType()->IsInt32() && v->IsInt1()) { v = ToI32(v); } - if (func_->GetName() == "main" && !func_->GetType()->IsVoid()) { - auto* nl = builder_.CreateConstInt(10); - // ((v % 256) + 256) % 256 - auto* mod256 = builder_.CreateMod(v, builder_.CreateConstInt(256), - module_.GetContext().NextTemp()); - auto* add256 = builder_.CreateAdd(mod256, builder_.CreateConstInt(256), - module_.GetContext().NextTemp()); - auto* masked = builder_.CreateMod(add256, builder_.CreateConstInt(256), - module_.GetContext().NextTemp()); - - std::vector args1 = {masked}; - builder_.CreateCallExternal("putint", ir::Type::GetVoidType(), args1, ""); - std::vector args2 = {nl}; - builder_.CreateCallExternal("putch", ir::Type::GetVoidType(), args2, ""); - } builder_.CreateRet(v); } else { builder_.CreateRetVoid(); @@ -238,4 +223,4 @@ IRGenImpl::BlockFlow IRGenImpl::VisitStmt(SysYParser::StmtContext& s) { } throw std::runtime_error(FormatError("irgen", "不支持的语句类型")); -} +} \ No newline at end of file diff --git a/test/test_case/testdata/functional/test_riscv.sy b/test/test_case/testdata/functional/test_riscv.sy deleted file mode 100644 index 3cb2bdf..0000000 --- a/test/test_case/testdata/functional/test_riscv.sy +++ /dev/null @@ -1,7 +0,0 @@ -// test/test_case/functional/test_riscv.sy -int main() { - int a = 10; - int b = 20; - int c = a + b; - return c; // 应该返回30 -} \ No newline at end of file