fixed parser
This commit is contained in:
@@ -6,7 +6,6 @@
|
|||||||
std::string generateAssembly(const CompilerState& state) {
|
std::string generateAssembly(const CompilerState& state) {
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
// --- 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";
|
||||||
@@ -20,28 +19,25 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
|
|
||||||
result += "section .text\n";
|
result += "section .text\n";
|
||||||
|
|
||||||
// --- GENEROWANIE FUNKCJI ---
|
// --- FUNKCJE ---
|
||||||
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";
|
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) {
|
for (const std::string& cmd : func.second) {
|
||||||
|
|
||||||
// 1. ZWYK£Y PRINT (format: "PRINT:nazwa_zmiennej")
|
result += " ; CMD: " + cmd + "\n";
|
||||||
|
|
||||||
if (cmd.find("PRINT:") == 0) {
|
if (cmd.find("PRINT:") == 0) {
|
||||||
std::string varName = cmd.substr(6); // utnij "PRINT:"
|
std::string varName = cmd.substr(6);
|
||||||
if (state.variables.count(varName)) {
|
if (state.variables.count(varName)) {
|
||||||
result += " mov edx, [" + varName + "]\n";
|
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) {
|
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()) {
|
if (ifIndex >= 0 && ifIndex < state.ifBlocks.size()) {
|
||||||
const auto& ifBlock = state.ifBlocks[ifIndex];
|
const auto& ifBlock = state.ifBlocks[ifIndex];
|
||||||
|
|
||||||
@@ -50,43 +46,30 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
|
|
||||||
result += " ; --- IF START [" + ifBlock.conditionVar + "] ---\n";
|
result += " ; --- IF START [" + ifBlock.conditionVar + "] ---\n";
|
||||||
result += " mov eax, [" + 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
|
result += " test eax, eax\n";
|
||||||
for (const std::string& innerCmd : ifBlock.prints) {
|
result += " jz " + labelSkip + "\n";
|
||||||
if (state.variables.count(innerCmd)) {
|
|
||||||
result += " mov edx, [" + innerCmd + "]\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 += " lea rcx, [rel fmt]\n";
|
||||||
result += " call printf\n";
|
result += " call printf\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result += labelSkip + ":\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";
|
||||||
}
|
}
|
||||||
|
// ==main==
|
||||||
// --- MAIN ---
|
|
||||||
result += "main:\n";
|
result += "main:\n";
|
||||||
result += " sub rsp, 40\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) {
|
for (const std::string& call : state.printCalls) {
|
||||||
if (call.find("CALL_") == 0) {
|
if (call.find("CALL_") == 0) {
|
||||||
std::string funcName = call.substr(5);
|
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 += " lea rcx, [rel pause_msg]\n";
|
||||||
result += " call printf\n";
|
result += " call printf\n";
|
||||||
result += "pause_loop:\n";
|
result += "pause_loop:\n";
|
||||||
|
|||||||
@@ -90,24 +90,31 @@ int main(int argc, char* argv[]) {
|
|||||||
// --- ZAPIS I KOMPILACJA ZEWNĘTRZNA ---
|
// --- ZAPIS I KOMPILACJA ZEWNĘTRZNA ---
|
||||||
std::system("if not exist output mkdir output");
|
std::system("if not exist output mkdir output");
|
||||||
|
|
||||||
std::string baseName = outputName.empty() ?
|
// 1. Ustal bazową nazwę (bez rozszerzenia)
|
||||||
inputFile.substr(0, inputFile.find_last_of(".")) : outputName;
|
std::string baseName;
|
||||||
|
|
||||||
// Upewnij się, że nazwa nie ma rozszerzenia .exe w środku ścieżki
|
|
||||||
if (outputName.empty()) {
|
if (outputName.empty()) {
|
||||||
if (baseName.find(".exe") == std::string::npos) baseName += ".exe";
|
// Jeśli nie podano -o, weź nazwę pliku wejściowego i utnij .pcc
|
||||||
|
size_t lastDot = inputFile.find_last_of(".");
|
||||||
|
if (lastDot != std::string::npos)
|
||||||
|
baseName = inputFile.substr(0, lastDot);
|
||||||
|
else
|
||||||
|
baseName = inputFile;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Jeśli użytkownik podał nazwę bez .exe, dodaj ją
|
// Jeśli podano -o, sprawdź czy ma .exe i ewentualnie utnij
|
||||||
if (outputName.find(".exe") == std::string::npos) outputName += ".exe";
|
// (żebyśmy mogli dodać .asm i .obj bez bałaganu)
|
||||||
|
size_t exePos = outputName.find(".exe");
|
||||||
|
if (exePos != std::string::npos)
|
||||||
|
baseName = outputName.substr(0, exePos);
|
||||||
|
else
|
||||||
baseName = outputName;
|
baseName = outputName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ścieżki wyjściowe
|
// Teraz budujemy ścieżki - czysto i ładnie
|
||||||
// Uwaga: zakładam proste nazewnictwo, można tu poprawić usuwanie ścieżek z nazwy pliku
|
|
||||||
std::string asmPath = "output\\" + baseName + ".asm";
|
std::string asmPath = "output\\" + baseName + ".asm";
|
||||||
std::string objPath = "output\\" + baseName + ".obj";
|
std::string objPath = "output\\" + baseName + ".obj";
|
||||||
std::string exePath = "output\\" + baseName;
|
std::string exePath = "output\\" + baseName + ".exe";
|
||||||
|
|
||||||
|
|
||||||
// Zapisz ASM
|
// Zapisz ASM
|
||||||
std::ofstream asmOut(asmPath);
|
std::ofstream asmOut(asmPath);
|
||||||
|
|||||||
@@ -33,18 +33,28 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// }
|
// } ZAMYKANIE KLAMRY
|
||||||
else if (line.find("}") != std::string::npos) {
|
else if (line.find("}") != std::string::npos) {
|
||||||
if (!state.braceStack.empty()) {
|
if (!state.braceStack.empty()) {
|
||||||
|
// Sprawdzamy co zamykamy PRZED zdjęciem ze stosu
|
||||||
|
bool wasFunction = state.braceStack.top();
|
||||||
state.braceStack.pop();
|
state.braceStack.pop();
|
||||||
if (!state.braceStack.empty()) {
|
|
||||||
|
// Jeśli zdjęliśmy 'true' (czyli znacznik funkcji) ORAZ stos jest pusty
|
||||||
|
// to znaczy, że zamknęliśmy funkcję główną.
|
||||||
|
if (wasFunction && state.braceStack.empty()) {
|
||||||
state.inFunction = false;
|
state.inFunction = false;
|
||||||
state.currentFunction.clear();
|
state.currentFunction.clear();
|
||||||
std::cout << " END FUNC\n";
|
std::cout << " END FUNC\n";
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// Zamknęliśmy IF-a (lub inny blok wewnętrzny)
|
||||||
|
std::cout << " END BLOCK (IF)\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// INT
|
// INT
|
||||||
if (line.length() > 3 && line.substr(0, 3) == "int") {
|
if (line.length() > 3 && line.substr(0, 3) == "int") {
|
||||||
size_t eqPos = line.find("=", 4);
|
size_t eqPos = line.find("=", 4);
|
||||||
@@ -124,10 +134,16 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
|
|
||||||
IfBlock newIf;
|
IfBlock newIf;
|
||||||
newIf.conditionVar = condition;
|
newIf.conditionVar = condition;
|
||||||
state.ifBlocks.push_back(newIf); // ZMIANA: state.ifBlocks
|
state.ifBlocks.push_back(newIf);
|
||||||
|
|
||||||
|
// WAŻNE: Dodajemy instrukcję "IF:numer" do listy rozkazów funkcji
|
||||||
|
if (state.inFunction) {
|
||||||
|
int ifIndex = state.ifBlocks.size() - 1;
|
||||||
|
state.functions[state.currentFunction].push_back("IF:" + std::to_string(ifIndex));
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << " IF [" << condition << "] { <- blok #" << state.ifBlocks.size() - 1 << "\n";
|
std::cout << " IF [" << condition << "] { <- blok #" << state.ifBlocks.size() - 1 << "\n";
|
||||||
state.braceStack.push(false);
|
state.braceStack.push(false); // false oznacza, że to klamra IFa (a nie funkcji)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,14 +155,28 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
std::string varName = line.substr(openParen + 1, closeParen - openParen - 1);
|
std::string varName = line.substr(openParen + 1, closeParen - openParen - 1);
|
||||||
size_t varStart = varName.find_first_not_of(" \t");
|
size_t varStart = varName.find_first_not_of(" \t");
|
||||||
size_t varEnd = varName.find_last_not_of(" \t");
|
size_t varEnd = varName.find_last_not_of(" \t");
|
||||||
|
|
||||||
if (varStart != std::string::npos) {
|
if (varStart != std::string::npos) {
|
||||||
varName = varName.substr(varStart, varEnd - varStart + 1);
|
varName = varName.substr(varStart, varEnd - varStart + 1);
|
||||||
|
|
||||||
if (state.inFunction && !state.currentFunction.empty()) {
|
if (state.inFunction && !state.currentFunction.empty()) {
|
||||||
state.functions[state.currentFunction].push_back(varName);
|
// SPRAWDZAMY CZY JESTEŚMY W IFIE
|
||||||
std::cout << " FUNC PRINT " << state.currentFunction << ": " << varName << "\n";
|
// (sprawdzamy szczyt stosu klamerek: false = IF, true = FUNC)
|
||||||
|
if (!state.braceStack.empty() && state.braceStack.top() == false) {
|
||||||
|
// Jesteśmy w IFie -> dodaj TYLKO do ifBlocks
|
||||||
|
if (!state.ifBlocks.empty()) {
|
||||||
|
state.ifBlocks.back().prints.push_back(varName);
|
||||||
|
std::cout << " IF PRINT: " << varName << "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
// Jesteśmy w normalnej funkcji (poza ifem) -> dodaj jako rozkaz PRINT:
|
||||||
|
state.functions[state.currentFunction].push_back("PRINT:" + varName);
|
||||||
|
std::cout << " FUNC PRINT " << state.currentFunction << ": " << varName << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Print globalny (poza funkcjami)
|
||||||
state.globalPrints.push_back(varName);
|
state.globalPrints.push_back(varName);
|
||||||
std::cout << " PRINT " << varName << "\n";
|
std::cout << " PRINT " << varName << "\n";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user