From 0dff8628ac870955edd394957a3b362ad3d6c5cf Mon Sep 17 00:00:00 2001 From: mmichlol Date: Mon, 9 Feb 2026 20:00:25 +0100 Subject: [PATCH] Bug Fix --- PCCcompiler/codegen.cpp | 37 ++++++++------------- PCCcompiler/parser.cpp | 72 +++++++++++++++++------------------------ README.md | 2 +- 3 files changed, 44 insertions(+), 67 deletions(-) diff --git a/PCCcompiler/codegen.cpp b/PCCcompiler/codegen.cpp index 9f6cba7..55707e7 100644 --- a/PCCcompiler/codegen.cpp +++ b/PCCcompiler/codegen.cpp @@ -69,7 +69,7 @@ std::string generateAssembly(const CompilerState& state) { result += " push rbp\n"; result += " mov rbp, rsp\n"; - result += " sub rsp, 256\n"; + result += " sub rsp, 288\n"; std::map stackMap; int currentStack = 8; @@ -134,7 +134,7 @@ std::string generateAssembly(const CompilerState& state) { result += " mov rax, [rdx]\n"; // Zapisujemy do zmiennej docelowej - result += " mov " + dst + ", rax\n"; // Lub eax, zależy jak masz + result += " mov " + dst + ", rax\n"; } else if (instr.arg3 == "STRING") { // Przypisanie stringa: ładujemy ADRES (LEA) @@ -276,7 +276,8 @@ std::string generateAssembly(const CompilerState& state) { std::string cond = getVarLocation(condRaw, stackMap); result += " mov eax, " + cond + "\n"; result += " test eax, eax\n"; - result += " jz " + instr.arg1 + "\n"; + result += " je " + instr.arg1 + "\n"; + } break; } @@ -316,18 +317,6 @@ std::string generateAssembly(const CompilerState& state) { result += " mov rax, " + valLoc + "\n"; // Zakładamy 64-bit (lub eax dla 32) } - // 2. Obliczamy adres elementu tablicy - // Adres = [rbp - offset_bazowy - (index * 8)] - // U nas stackMap trzyma offset_bazowy. - // Ponieważ stos rośnie w DÓŁ, a my rezerwowaliśmy w DÓŁ: - // t[0] -> offset - // t[1] -> offset + 8 - // Czekaj, rbp-8, rbp-16... - // Im większy offset w stackMap, tym niżej w pamięci. - // stackMap["t"] = 8. [rbp-8]. - // t[1] powinno być [rbp-16]. - // Czyli Wzór: [rbp - (base_offset + index*8)] - int baseOffset = stackMap[arrName]; // Ładujemy indeks do RCX @@ -361,9 +350,10 @@ std::string generateAssembly(const CompilerState& state) { case OpType::PRINT: { // Instrukcja PRINT zwykła (liczba) std::string val = getVarLocation(instr.arg1, stackMap); - result += " mov edx, " + val + "\n"; - result += " lea rcx, [rel fmt_int]\n"; - result += " call printf\n"; + result += " mov edx, " + val + "\\n"; + result += " lea rcx, [rel fmt_int]\\n"; + result += " xor eax, eax\\n"; // <-- DODAJ TO + result += " call printf\\n"; break; } case OpType::PRINT_STRING: { @@ -371,20 +361,21 @@ std::string generateAssembly(const CompilerState& state) { std::string target = instr.arg1; if (target.find("str_") == 0) { // Literał: print("tekst") - result += " lea rdx, [rel " + target + "]\n"; + result += " lea rdx, [rel " + target + "]\\n"; } else { // Zmienna: print(s) -> s trzyma adres std::string val = getVarLocation(target, stackMap); - result += " mov rdx, " + val + "\n"; + result += " mov rdx, " + val + "\\n"; } - result += " lea rcx, [rel fmt_str]\n"; - result += " call printf\n"; + result += " lea rcx, [rel fmt_str]\\n"; + result += " xor eax, eax\\n"; // <-- DODAJ TO + result += " call printf\\n"; break; } case OpType::CALL: { if (instr.arg1 == "input") { - result += " call getchar\n"; + result += " call _getch\n"; break; } if (instr.arg1 == "read_key") { diff --git a/PCCcompiler/parser.cpp b/PCCcompiler/parser.cpp index 1610732..de07c3f 100644 --- a/PCCcompiler/parser.cpp +++ b/PCCcompiler/parser.cpp @@ -70,14 +70,13 @@ void processSource(const std::string& src, CompilerState& state) { } // ========================================================= - // 2. ZAMYKANIE BLOKU '}' (Koniec funkcji, IF-a lub WHILE) + // 2. ZAMYKANIE BLOKU '}' (Koniec funkcji, IF‑a lub WHILE‑a) // ========================================================= - if (line == "}") { + if (!line.empty() && line.back() == '}') { if (!state.blockStack.empty()) { std::string blockInfo = state.blockStack.top(); state.blockStack.pop(); - // Sprawdzamy czy to pÄ™tla WHILE (znacznik "WHILE|...") bool isWhile = (blockInfo.length() > 6 && blockInfo.substr(0, 6) == "WHILE|"); if (isWhile) { @@ -93,11 +92,11 @@ void processSource(const std::string& src, CompilerState& state) { std::cout << " [PARSER] } End WHILE loop\n"; } else { - // To zwykĹ‚y IF + // To zwykĹ‚y blok (if lub else) if (state.currentFunction) { state.currentFunction->instructions.push_back({ OpType::LABEL, blockInfo, "", "" }); } - std::cout << " [PARSER] } End IF block -> " << blockInfo << "\n"; + std::cout << " [PARSER] } End block -> " << blockInfo << "\n"; } } else { @@ -108,6 +107,7 @@ void processSource(const std::string& src, CompilerState& state) { continue; } + // ========================================================= // JESTEĹšMY W ĹšRODKU FUNKCJI // ========================================================= @@ -192,7 +192,7 @@ void processSource(const std::string& src, CompilerState& state) { continue; } - // E. IF STATEMENT + // E. IF STATEMENT (z ulepszonÄ… obsĹ‚ugÄ… else) else if (line.substr(0, 2) == "if") { size_t openParen = line.find("("); size_t closeParen = line.rfind(")"); @@ -201,47 +201,17 @@ void processSource(const std::string& src, CompilerState& state) { std::string conditionRaw = trim(line.substr(openParen + 1, closeParen - openParen - 1)); std::string finalConditionVar = conditionRaw; - // Logika && i || (uproszczona) - size_t andPos = conditionRaw.find("&&"); - size_t orPos = conditionRaw.find("||"); - - if (andPos != std::string::npos) { - std::string partA = trim(conditionRaw.substr(0, andPos)); - std::string partB = trim(conditionRaw.substr(andPos + 2)); - std::string tempA = "_tmp_and_a_" + std::to_string(state.labelCounter); - std::string tempB = "_tmp_and_b_" + std::to_string(state.labelCounter); - std::string tempRes = "_tmp_and_res_" + std::to_string(state.labelCounter); - - // A - if (partA.find("==") != std::string::npos) { - size_t eq = partA.find("=="); - f.instructions.push_back({ OpType::EQ, tempA, trim(partA.substr(0, eq)), trim(partA.substr(eq + 2)) }); - } - else f.instructions.push_back({ OpType::ASSIGN, tempA, partA, "" }); - - // B - if (partB.find("==") != std::string::npos) { - size_t eq = partB.find("=="); - f.instructions.push_back({ OpType::EQ, tempB, trim(partB.substr(0, eq)), trim(partB.substr(eq + 2)) }); - } - else f.instructions.push_back({ OpType::ASSIGN, tempB, partB, "" }); - - f.instructions.push_back({ OpType::LOGIC_AND, tempRes, tempA, tempB }); - finalConditionVar = tempRes; - } - else if (orPos != std::string::npos) { - // ... (Tutaj moĹĽna dodać logikÄ™ OR analogicznie jak wyĹĽej, skracam dla czytelnoĹ›ci) - } - else if (conditionRaw.find("==") != std::string::npos) { + // ObsĹ‚uga prostych operacji w warunku + if (conditionRaw.find("==") != std::string::npos) { size_t eq = conditionRaw.find("=="); - std::string tempRes = "_tmp_eq_" + std::to_string(state.labelCounter); + std::string tempRes = "_tmp_eq_" + std::to_string(state.labelCounter++); f.instructions.push_back({ OpType::EQ, tempRes, trim(conditionRaw.substr(0, eq)), trim(conditionRaw.substr(eq + 2)) }); finalConditionVar = tempRes; } - std::string labelName = "L_" + std::to_string(state.labelCounter++); - f.instructions.push_back({ OpType::JMP_FALSE, labelName, finalConditionVar, "" }); - state.blockStack.push(labelName); + std::string labelFalse = "L_" + std::to_string(state.labelCounter++); + f.instructions.push_back({ OpType::JMP_FALSE, labelFalse, finalConditionVar, "" }); + state.blockStack.push(labelFalse); } continue; } @@ -292,7 +262,7 @@ void processSource(const std::string& src, CompilerState& state) { state.blockStack.push("WHILE|" + labelStart + "|" + labelEnd); } continue; - } + } // G. MSGBOX else if (line.substr(0, 6) == "msgbox") { @@ -315,6 +285,22 @@ void processSource(const std::string& src, CompilerState& state) { continue; } + else if (line.substr(0, 4) == "else") { + if (!state.blockStack.empty()) { + std::string labelFalse = state.blockStack.top(); + state.blockStack.pop(); + + // Po bloku `if` skocz do koĹ„ca `if‑else`: + std::string labelEnd = "L_" + std::to_string(state.labelCounter++); + f.instructions.push_back({ OpType::JMP, labelEnd, "", "" }); + f.instructions.push_back({ OpType::LABEL, labelFalse, "", "" }); // poczÄ…tek ELSE + state.blockStack.push(labelEnd); + + std::cout << " [PARSER] else block\n"; + } + continue; + } + // ========================================================= // H. PRZYPISANIE / OPERACJE (Linie z "=") // ========================================================= diff --git a/README.md b/README.md index cbf9de4..77b0a7e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PCC Compiler (My C++ Compiler) -![Version](https://img.shields.io/badge/version-0.0.6-blue.svg) +![Version](https://img.shields.io/badge/version-0.0.9-blue.svg) ![Status](https://img.shields.io/badge/status-BETA-orange.svg) ![Platform](https://img.shields.io/badge/platform-Windows%20x64-lightgrey.svg)