This commit is contained in:
mmichlol
2026-02-09 20:00:25 +01:00
parent d736f2786e
commit 0dff8628ac
3 changed files with 44 additions and 67 deletions

View File

@@ -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<std::string, int> 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") {

View File

@@ -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, IFa lub WHILEa)
// =========================================================
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 `ifelse`:
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 "=")
// =========================================================