Bug Fix
This commit is contained in:
@@ -69,7 +69,7 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
|
|
||||||
result += " push rbp\n";
|
result += " push rbp\n";
|
||||||
result += " mov rbp, rsp\n";
|
result += " mov rbp, rsp\n";
|
||||||
result += " sub rsp, 256\n";
|
result += " sub rsp, 288\n";
|
||||||
|
|
||||||
std::map<std::string, int> stackMap;
|
std::map<std::string, int> stackMap;
|
||||||
int currentStack = 8;
|
int currentStack = 8;
|
||||||
@@ -134,7 +134,7 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
result += " mov rax, [rdx]\n";
|
result += " mov rax, [rdx]\n";
|
||||||
|
|
||||||
// Zapisujemy do zmiennej docelowej
|
// 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") {
|
else if (instr.arg3 == "STRING") {
|
||||||
// Przypisanie stringa: ³adujemy ADRES (LEA)
|
// Przypisanie stringa: ³adujemy ADRES (LEA)
|
||||||
@@ -276,7 +276,8 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
std::string cond = getVarLocation(condRaw, stackMap);
|
std::string cond = getVarLocation(condRaw, stackMap);
|
||||||
result += " mov eax, " + cond + "\n";
|
result += " mov eax, " + cond + "\n";
|
||||||
result += " test eax, eax\n";
|
result += " test eax, eax\n";
|
||||||
result += " jz " + instr.arg1 + "\n";
|
result += " je " + instr.arg1 + "\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -316,18 +317,6 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
result += " mov rax, " + valLoc + "\n"; // Zak³adamy 64-bit (lub eax dla 32)
|
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];
|
int baseOffset = stackMap[arrName];
|
||||||
|
|
||||||
// £adujemy indeks do RCX
|
// £adujemy indeks do RCX
|
||||||
@@ -361,9 +350,10 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
case OpType::PRINT: {
|
case OpType::PRINT: {
|
||||||
// Instrukcja PRINT zwyk³a (liczba)
|
// Instrukcja PRINT zwyk³a (liczba)
|
||||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||||
result += " mov edx, " + val + "\n";
|
result += " mov edx, " + val + "\\n";
|
||||||
result += " lea rcx, [rel fmt_int]\n";
|
result += " lea rcx, [rel fmt_int]\\n";
|
||||||
result += " call printf\n";
|
result += " xor eax, eax\\n"; // <-- DODAJ TO
|
||||||
|
result += " call printf\\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OpType::PRINT_STRING: {
|
case OpType::PRINT_STRING: {
|
||||||
@@ -371,20 +361,21 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
std::string target = instr.arg1;
|
std::string target = instr.arg1;
|
||||||
if (target.find("str_") == 0) {
|
if (target.find("str_") == 0) {
|
||||||
// Litera³: print("tekst")
|
// Litera³: print("tekst")
|
||||||
result += " lea rdx, [rel " + target + "]\n";
|
result += " lea rdx, [rel " + target + "]\\n";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Zmienna: print(s) -> s trzyma adres
|
// Zmienna: print(s) -> s trzyma adres
|
||||||
std::string val = getVarLocation(target, stackMap);
|
std::string val = getVarLocation(target, stackMap);
|
||||||
result += " mov rdx, " + val + "\n";
|
result += " mov rdx, " + val + "\\n";
|
||||||
}
|
}
|
||||||
result += " lea rcx, [rel fmt_str]\n";
|
result += " lea rcx, [rel fmt_str]\\n";
|
||||||
result += " call printf\n";
|
result += " xor eax, eax\\n"; // <-- DODAJ TO
|
||||||
|
result += " call printf\\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OpType::CALL: {
|
case OpType::CALL: {
|
||||||
if (instr.arg1 == "input") {
|
if (instr.arg1 == "input") {
|
||||||
result += " call getchar\n";
|
result += " call _getch\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (instr.arg1 == "read_key") {
|
if (instr.arg1 == "read_key") {
|
||||||
|
|||||||
@@ -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()) {
|
if (!state.blockStack.empty()) {
|
||||||
std::string blockInfo = state.blockStack.top();
|
std::string blockInfo = state.blockStack.top();
|
||||||
state.blockStack.pop();
|
state.blockStack.pop();
|
||||||
|
|
||||||
// Sprawdzamy czy to pętla WHILE (znacznik "WHILE|...")
|
|
||||||
bool isWhile = (blockInfo.length() > 6 && blockInfo.substr(0, 6) == "WHILE|");
|
bool isWhile = (blockInfo.length() > 6 && blockInfo.substr(0, 6) == "WHILE|");
|
||||||
|
|
||||||
if (isWhile) {
|
if (isWhile) {
|
||||||
@@ -93,11 +92,11 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
std::cout << " [PARSER] } End WHILE loop\n";
|
std::cout << " [PARSER] } End WHILE loop\n";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// To zwykły IF
|
// To zwykły blok (if lub else)
|
||||||
if (state.currentFunction) {
|
if (state.currentFunction) {
|
||||||
state.currentFunction->instructions.push_back({ OpType::LABEL, blockInfo, "", "" });
|
state.currentFunction->instructions.push_back({ OpType::LABEL, blockInfo, "", "" });
|
||||||
}
|
}
|
||||||
std::cout << " [PARSER] } End IF block -> " << blockInfo << "\n";
|
std::cout << " [PARSER] } End block -> " << blockInfo << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -108,6 +107,7 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// JESTEŚMY W ŚRODKU FUNKCJI
|
// JESTEŚMY W ŚRODKU FUNKCJI
|
||||||
// =========================================================
|
// =========================================================
|
||||||
@@ -192,7 +192,7 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// E. IF STATEMENT
|
// E. IF STATEMENT (z ulepszoną obsługą else)
|
||||||
else if (line.substr(0, 2) == "if") {
|
else if (line.substr(0, 2) == "if") {
|
||||||
size_t openParen = line.find("(");
|
size_t openParen = line.find("(");
|
||||||
size_t closeParen = line.rfind(")");
|
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 conditionRaw = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
||||||
std::string finalConditionVar = conditionRaw;
|
std::string finalConditionVar = conditionRaw;
|
||||||
|
|
||||||
// Logika && i || (uproszczona)
|
// Obsługa prostych operacji w warunku
|
||||||
size_t andPos = conditionRaw.find("&&");
|
if (conditionRaw.find("==") != std::string::npos) {
|
||||||
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) {
|
|
||||||
size_t eq = conditionRaw.find("==");
|
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)) });
|
f.instructions.push_back({ OpType::EQ, tempRes, trim(conditionRaw.substr(0, eq)), trim(conditionRaw.substr(eq + 2)) });
|
||||||
finalConditionVar = tempRes;
|
finalConditionVar = tempRes;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string labelName = "L_" + std::to_string(state.labelCounter++);
|
std::string labelFalse = "L_" + std::to_string(state.labelCounter++);
|
||||||
f.instructions.push_back({ OpType::JMP_FALSE, labelName, finalConditionVar, "" });
|
f.instructions.push_back({ OpType::JMP_FALSE, labelFalse, finalConditionVar, "" });
|
||||||
state.blockStack.push(labelName);
|
state.blockStack.push(labelFalse);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -315,6 +285,22 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
continue;
|
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 "=")
|
// H. PRZYPISANIE / OPERACJE (Linie z "=")
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# PCC Compiler (My C++ Compiler)
|
# PCC Compiler (My C++ Compiler)
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user