fixed parser

This commit is contained in:
mmichlol
2026-02-06 18:21:58 +01:00
parent f192c5a367
commit 196140f9b8
3 changed files with 76 additions and 48 deletions

View File

@@ -6,7 +6,6 @@
std::string generateAssembly(const CompilerState& state) {
std::string result;
// --- NAG£ÓWEK ---
result += "global main\n";
result += "extern printf\n";
result += "extern GetAsyncKeyState\n\n";
@@ -20,28 +19,25 @@ std::string generateAssembly(const CompilerState& state) {
result += "section .text\n";
// --- GENEROWANIE FUNKCJI ---
// --- FUNKCJE ---
for (const auto& func : state.functions) {
result += func.first + ":\n";
result += " sub rsp, 40\n";
// Iterujemy przez LISTÊ ROZKAZÓW danej funkcji
// Rozkazy mog¹ byæ typu "PRINT:x" albo "IF:index"
for (const std::string& cmd : func.second) {
// 1. ZWYK£Y PRINT (format: "PRINT:nazwa_zmiennej")
result += " ; CMD: " + cmd + "\n";
if (cmd.find("PRINT:") == 0) {
std::string varName = cmd.substr(6); // utnij "PRINT:"
std::string varName = cmd.substr(6);
if (state.variables.count(varName)) {
result += " mov edx, [" + varName + "]\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
}
}
// 2. BLOK IF (format: "IF:index_w_tablicy")
else if (cmd.find("IF:") == 0) {
int ifIndex = std::stoi(cmd.substr(3)); // pobierz numer IFa
int ifIndex = std::stoi(cmd.substr(3));
if (ifIndex >= 0 && ifIndex < state.ifBlocks.size()) {
const auto& ifBlock = state.ifBlocks[ifIndex];
@@ -50,43 +46,30 @@ std::string generateAssembly(const CompilerState& state) {
result += " ; --- IF START [" + ifBlock.conditionVar + "] ---\n";
result += " mov eax, [" + ifBlock.conditionVar + "]\n";
result += " cmp eax, 1\n";
result += " jne " + labelSkip + "\n"; // Skocz jeœli FA£SZ (0)
// Generuj zawartoœæ œrodka IFa
for (const std::string& innerCmd : ifBlock.prints) {
if (state.variables.count(innerCmd)) {
result += " mov edx, [" + innerCmd + "]\n";
result += " test eax, eax\n";
result += " jz " + labelSkip + "\n";
for (const std::string& innerPrint : ifBlock.prints) {
if (state.variables.count(innerPrint)) {
result += " mov edx, [" + innerPrint + "]\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
}
}
result += labelSkip + ":\n";
result += " ; --- IF END ---\n";
}
}
}
}
result += " add rsp, 40\n";
result += " ret\n\n";
}
// --- MAIN ---
// ==main==
result += "main:\n";
result += " sub rsp, 40\n";
// Globalne printy (bez zmian)
for (const std::string& var : state.globalPrints) {
if (state.variables.count(var)) {
result += " mov edx, [" + var + "]\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
}
}
// Wywo³ania funkcji
for (const std::string& call : state.printCalls) {
if (call.find("CALL_") == 0) {
std::string funcName = call.substr(5);
@@ -96,7 +79,15 @@ std::string generateAssembly(const CompilerState& state) {
}
}
// Pauza
for (const std::string& var : state.globalPrints) {
if (state.variables.count(var)) {
result += " mov edx, [" + var + "]\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
}
}
result += " lea rcx, [rel pause_msg]\n";
result += " call printf\n";
result += "pause_loop:\n";