fixed IF statment
This commit is contained in:
@@ -1,51 +1,83 @@
|
|||||||
#include "codegen.h"
|
#include "codegen.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
std::string generateAssembly(const CompilerState& state) {
|
std::string generateAssembly(const CompilerState& state) {
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
// --- NAG£ÓWEK I SEKCJA DATA ---
|
// --- NAG£ÓWEK ---
|
||||||
result += "global main\n";
|
result += "global main\n";
|
||||||
result += "extern printf\n";
|
result += "extern printf\n";
|
||||||
result += "extern GetAsyncKeyState\n\n";
|
result += "extern GetAsyncKeyState\n\n";
|
||||||
|
|
||||||
result += "section .data\n";
|
result += "section .data\n";
|
||||||
|
|
||||||
// Generowanie zmiennych
|
|
||||||
for (const auto& v : state.variables) {
|
for (const auto& v : state.variables) {
|
||||||
result += " " + v.first + " dd " + std::to_string(v.second) + "\n";
|
result += " " + v.first + " dd " + std::to_string(v.second) + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sta³e stringi
|
|
||||||
result += " fmt db '%d', 10, 0\n";
|
result += " fmt db '%d', 10, 0\n";
|
||||||
result += " pause_msg db 'Nacisnij ESC aby zamknac...', 10, 0\n\n";
|
result += " pause_msg db 'Nacisnij ESC aby zamknac...', 10, 0\n\n";
|
||||||
|
|
||||||
result += "section .text\n";
|
result += "section .text\n";
|
||||||
|
|
||||||
// --- DEFINICJE FUNKCJI U¯YTKOWNIKA ---
|
// --- GENEROWANIE FUNKCJI ---
|
||||||
for (const auto& func : state.functions) {
|
for (const auto& func : state.functions) {
|
||||||
result += func.first + ":\n";
|
result += func.first + ":\n";
|
||||||
result += " sub rsp, 40\n"; // Shadow space
|
result += " sub rsp, 40\n";
|
||||||
|
|
||||||
// Printy wewn¹trz funkcji
|
// Iterujemy przez LISTÊ ROZKAZÓW danej funkcji
|
||||||
for (const std::string& var : func.second) {
|
// Rozkazy mog¹ byæ typu "PRINT:x" albo "IF:index"
|
||||||
if (state.variables.count(var)) {
|
for (const std::string& cmd : func.second) {
|
||||||
result += " mov edx, [" + var + "]\n";
|
|
||||||
|
// 1. ZWYK£Y PRINT (format: "PRINT:nazwa_zmiennej")
|
||||||
|
if (cmd.find("PRINT:") == 0) {
|
||||||
|
std::string varName = cmd.substr(6); // utnij "PRINT:"
|
||||||
|
if (state.variables.count(varName)) {
|
||||||
|
result += " mov edx, [" + varName + "]\n";
|
||||||
result += " lea rcx, [rel fmt]\n";
|
result += " lea rcx, [rel fmt]\n";
|
||||||
result += " call printf\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
|
||||||
|
|
||||||
|
if (ifIndex >= 0 && ifIndex < state.ifBlocks.size()) {
|
||||||
|
const auto& ifBlock = state.ifBlocks[ifIndex];
|
||||||
|
|
||||||
|
if (state.variables.count(ifBlock.conditionVar)) {
|
||||||
|
std::string labelSkip = "skip_if_" + std::to_string(ifIndex);
|
||||||
|
|
||||||
|
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 += " lea rcx, [rel fmt]\n";
|
||||||
|
result += " call printf\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result += labelSkip + ":\n";
|
||||||
|
result += " ; --- IF END ---\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result += " add rsp, 40\n";
|
result += " add rsp, 40\n";
|
||||||
result += " ret\n\n";
|
result += " ret\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- FUNKCJA MAIN ---
|
// --- MAIN ---
|
||||||
result += "main:\n";
|
result += "main:\n";
|
||||||
result += " sub rsp, 40\n";
|
result += " sub rsp, 40\n";
|
||||||
|
|
||||||
// Globalne printy (poza funkcjami)
|
// Globalne printy (bez zmian)
|
||||||
for (const std::string& var : state.globalPrints) {
|
for (const std::string& var : state.globalPrints) {
|
||||||
if (state.variables.count(var)) {
|
if (state.variables.count(var)) {
|
||||||
result += " mov edx, [" + var + "]\n";
|
result += " mov edx, [" + var + "]\n";
|
||||||
@@ -54,26 +86,24 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wywo³ania funkcji zdefiniowanych wczeœniej
|
// Wywo³ania funkcji
|
||||||
for (const std::string& call : state.printCalls) {
|
for (const std::string& call : state.printCalls) {
|
||||||
if (call.find("CALL_") == 0) {
|
if (call.find("CALL_") == 0) {
|
||||||
std::string funcName = call.substr(5); // Usuñ prefiks "CALL_"
|
std::string funcName = call.substr(5);
|
||||||
if (state.functions.count(funcName)) {
|
if (state.functions.count(funcName)) {
|
||||||
result += " call " + funcName + "\n";
|
result += " call " + funcName + "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- PAUSE LOOP (Czekanie na ESC) ---
|
// Pauza
|
||||||
result += " lea rcx, [rel pause_msg]\n";
|
result += " lea rcx, [rel pause_msg]\n";
|
||||||
result += " call printf\n";
|
result += " call printf\n";
|
||||||
result += "pause_loop:\n";
|
result += "pause_loop:\n";
|
||||||
result += " mov ecx, 27\n"; // VK_ESCAPE
|
result += " mov ecx, 27\n";
|
||||||
result += " call GetAsyncKeyState\n";
|
result += " call GetAsyncKeyState\n";
|
||||||
result += " test ax, 8000h\n";
|
result += " test ax, 8000h\n";
|
||||||
result += " jz pause_loop\n";
|
result += " jz pause_loop\n";
|
||||||
|
|
||||||
// Wyjœcie z programu
|
|
||||||
result += " add rsp, 40\n";
|
result += " add rsp, 40\n";
|
||||||
result += " ret\n";
|
result += " ret\n";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user