From 636bfcc6d5fd5ee51948994700ad1c70592bbd7f Mon Sep 17 00:00:00 2001 From: saqut Date: Fri, 19 Jun 2026 16:52:30 +0300 Subject: [PATCH] =?UTF-8?q?fix(ir):=20%=3D=20bile=C5=9Fik=20atama=20IR=20?= =?UTF-8?q?=C3=BCretiminde=20eksikti=20(B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Teşhis (a): Sadece %= unutulmuş — +=, -=, *=, /= doğru çalışıyordu. PERCENT_EQUAL MOD opcode'una yönlendirildi; sıfıra mod runtime hatası zaten interpreter'da mevcut, yeni kod gerekmedi. CMakeLists.txt'e .runtime_error uzantısı eklendi (çalışma zamanı hatası golden testleri için; aynı run_golden_error.cmake altyapısı). Testler: compound_mod (17%5=2 zinciri) + mod_by_zero (runtime hata). 21/21 geçti. Co-Authored-By: Claude Sonnet 4.6 --- CMakeLists.txt | 14 ++++++++++ src/ir/ir_generator.cpp | 20 +++++++------- tests/golden/arithmetic/compound_mod.expected | 4 +++ tests/golden/arithmetic/compound_mod.sqt | 27 +++++++++++++++++++ .../arithmetic/mod_by_zero.runtime_error | 1 + tests/golden/arithmetic/mod_by_zero.sqt | 8 ++++++ 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 tests/golden/arithmetic/compound_mod.expected create mode 100644 tests/golden/arithmetic/compound_mod.sqt create mode 100644 tests/golden/arithmetic/mod_by_zero.runtime_error create mode 100644 tests/golden/arithmetic/mod_by_zero.sqt diff --git a/CMakeLists.txt b/CMakeLists.txt index 285d5fc..3e6bf64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,4 +101,18 @@ foreach(SQT_FILE ${ALL_GOLDEN_SQT}) -P "${CMAKE_SOURCE_DIR}/cmake/run_golden_error.cmake" ) endif() + + # Negatif test: .runtime_error — çalışma zamanı hatası beklenen golden testler. + # Aynı altyapı: exit code != 0 + stderr içeriği kontrolü. + set(RUNTIME_ERROR_EXPECTED "${CMAKE_SOURCE_DIR}/tests/golden/${BASE}.runtime_error") + if(EXISTS "${RUNTIME_ERROR_EXPECTED}") + add_test( + NAME "golden_${TNAME}_runtime_error" + COMMAND ${CMAKE_COMMAND} + -DBINARY=${SAQUT_BINARY} + -DSOURCE=${SQT_FILE} + -DEXPECTED=${RUNTIME_ERROR_EXPECTED} + -P "${CMAKE_SOURCE_DIR}/cmake/run_golden_error.cmake" + ) + endif() endforeach() diff --git a/src/ir/ir_generator.cpp b/src/ir/ir_generator.cpp index b45e4af..ba7ef7d 100644 --- a/src/ir/ir_generator.cpp +++ b/src/ir/ir_generator.cpp @@ -376,12 +376,13 @@ int IRGenerator::generateExpression(ASTNode* node) { return varSlot; } - // Birleşik atama: += -= *= /= - // x += y ≡ x = x + y - if (bin->Operator == TokenType::PLUS_EQUAL || - bin->Operator == TokenType::MINUS_EQUAL || - bin->Operator == TokenType::STAR_EQUAL || - bin->Operator == TokenType::SLASH_EQUAL) { + // Birleşik atama: += -= *= /= %= + // x OP= y ≡ x = x OP y + if (bin->Operator == TokenType::PLUS_EQUAL || + bin->Operator == TokenType::MINUS_EQUAL || + bin->Operator == TokenType::STAR_EQUAL || + bin->Operator == TokenType::SLASH_EQUAL || + bin->Operator == TokenType::PERCENT_EQUAL) { auto* lhsId = (IdentifierNode*)bin->Left; std::string varName = lhsId->parserToken.token->token; @@ -389,9 +390,10 @@ int IRGenerator::generateExpression(ASTNode* node) { int rhsSlot = generateExpression(bin->Right); Opcode arithOp = Opcode::ADD; - if (bin->Operator == TokenType::MINUS_EQUAL) arithOp = Opcode::SUB; - else if (bin->Operator == TokenType::STAR_EQUAL) arithOp = Opcode::MUL; - else if (bin->Operator == TokenType::SLASH_EQUAL) arithOp = Opcode::DIV; + if (bin->Operator == TokenType::MINUS_EQUAL) arithOp = Opcode::SUB; + else if (bin->Operator == TokenType::STAR_EQUAL) arithOp = Opcode::MUL; + else if (bin->Operator == TokenType::SLASH_EQUAL) arithOp = Opcode::DIV; + else if (bin->Operator == TokenType::PERCENT_EQUAL) arithOp = Opcode::MOD; int resultSlot = freshSlot(); emitBinaryOp(arithOp, resultSlot, varSlot, rhsSlot); diff --git a/tests/golden/arithmetic/compound_mod.expected b/tests/golden/arithmetic/compound_mod.expected new file mode 100644 index 0000000..9efd8d1 --- /dev/null +++ b/tests/golden/arithmetic/compound_mod.expected @@ -0,0 +1,4 @@ +2 +1 +0 +2 diff --git a/tests/golden/arithmetic/compound_mod.sqt b/tests/golden/arithmetic/compound_mod.sqt new file mode 100644 index 0000000..05a8172 --- /dev/null +++ b/tests/golden/arithmetic/compound_mod.sqt @@ -0,0 +1,27 @@ +// B8: %= bileşik atama doğru IR üretmeli. +// Elle hesap: 17%5=2, 10%3=1, 0%4=0, zincir: 20+5=25-3=22*2=44/4=11%3=2 + +int main() { + int a = 17; + a %= 5; + print(a); // 2 + + int b = 10; + b %= 3; + print(b); // 1 + + int c = 0; + c %= 4; + print(c); // 0 + + // Tüm bileşik atamalar zinciri (regresyon) + int x = 20; + x += 5; // 25 + x -= 3; // 22 + x *= 2; // 44 + x /= 4; // 11 + x %= 3; // 2 + print(x); // 2 + + return 0; +} diff --git a/tests/golden/arithmetic/mod_by_zero.runtime_error b/tests/golden/arithmetic/mod_by_zero.runtime_error new file mode 100644 index 0000000..db826d9 --- /dev/null +++ b/tests/golden/arithmetic/mod_by_zero.runtime_error @@ -0,0 +1 @@ +sıfıra bölme \(mod\) diff --git a/tests/golden/arithmetic/mod_by_zero.sqt b/tests/golden/arithmetic/mod_by_zero.sqt new file mode 100644 index 0000000..2866db9 --- /dev/null +++ b/tests/golden/arithmetic/mod_by_zero.sqt @@ -0,0 +1,8 @@ +// Runtime test: a %= 0 → çalışma zamanı sıfıra bölme hatası vermeli. + +int main() { + int a = 7; + a %= 0; + print(a); + return 0; +}