Compare commits
1 Commits
main
...
V0.0.7-bet
| Author | SHA1 | Date | |
|---|---|---|---|
| 7cef3a0bce |
@@ -13,6 +13,7 @@ bool isNumber(const std::string& s) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Zamienia nazwê zmiennej na adres pamiêci [rbp-X] lub liczbê
|
||||
std::string getVarLocation(const std::string& name, const std::map<std::string, int>& locals) {
|
||||
std::string cleanName = name;
|
||||
// Usuwamy ewentualne spacje
|
||||
@@ -26,22 +27,17 @@ std::string getVarLocation(const std::string& name, const std::map<std::string,
|
||||
|
||||
if (cleanName == "RAX") return "eax";
|
||||
|
||||
// USUNIÊTO: if (cleanName == "this") return "[rbp+16]";
|
||||
// Teraz "this" wpadnie do bloku poni¿ej i zostanie znalezione w mapie locals.
|
||||
|
||||
if (locals.count(cleanName)) {
|
||||
int offset = locals.at(cleanName);
|
||||
return "[rbp-" + std::to_string(offset) + "]";
|
||||
}
|
||||
|
||||
// Zwracamy orygina³ (jeœli to np. nazwa etykiety)
|
||||
// Zwracamy orygina³ (jeœli to np. nazwa etykiety), ale to zazwyczaj b³¹d dla zmiennych
|
||||
return cleanName;
|
||||
}
|
||||
|
||||
|
||||
std::string generateAssembly(const CompilerState& state) {
|
||||
// 1. NAG£ÓWEK I DEKLARACJE EXTERN
|
||||
std::string result = "default rel\n";
|
||||
std::string result;
|
||||
result += "global main\n";
|
||||
result += "extern printf\n";
|
||||
result += "extern getchar\n";
|
||||
@@ -49,20 +45,15 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
result += "extern rand\n";
|
||||
result += "extern srand\n";
|
||||
result += "extern time\n";
|
||||
result += "extern MessageBoxA\n";
|
||||
|
||||
// 2. SEKCJA DATA (Tylko raz!)
|
||||
result += "section .data\n";
|
||||
result += " fmt_int db \"%lld\", 10, 0\n"; // Format dla liczb
|
||||
result += " fmt_str db \"%s\", 10, 0\n"; // Format dla stringów
|
||||
result += " fmt db '%d', 10, 0\n";
|
||||
result += "section .data\n";
|
||||
result += " fmt_int db '%d', 10, 0\n"; // Format dla liczb
|
||||
result += " fmt_str db '%s', 10, 0\n"; // Format dla stringów
|
||||
|
||||
// Zrzucamy stringi: ETYKIETA db "TRESC", 0
|
||||
for (const auto& p : state.stringLiterals) {
|
||||
result += " " + p.first + " db \"" + p.second + "\", 0\n";
|
||||
}
|
||||
|
||||
// 3. SEKCJA TEXT (Kod programu)
|
||||
result += "section .text\n";
|
||||
// --- WYPISYWANIE STRINGÓW ---
|
||||
for (const auto& pair : state.stringLiterals) { result += " " + pair.second + " db '" + pair.first + "', 0\n"; }
|
||||
result += "section .text\n\n";
|
||||
|
||||
for (const auto& pair : state.functions) {
|
||||
const Function& func = pair.second;
|
||||
@@ -70,198 +61,57 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
|
||||
result += " push rbp\n";
|
||||
result += " mov rbp, rsp\n";
|
||||
result += " sub rsp, 512\n"; // Zwiêkszy³em stos dla bezpieczeñstwa obiektów
|
||||
result += " sub rsp, 256\n";
|
||||
|
||||
std::map<std::string, int> stackMap;
|
||||
int currentStack = 8;
|
||||
|
||||
// ARGUMENTY FUNKCJI
|
||||
// RCX, RDX, R8, R9 - konwencja Windows x64 (shadow space obs³uguje caller)
|
||||
// Jeœli funkcja jest metod¹, pierwszym argumentem jest 'this' (wskaŸnik na obiekt)
|
||||
|
||||
int argIdx = 0;
|
||||
// 1. ARGUMENTY
|
||||
if (func.args.size() > 0) {
|
||||
stackMap[func.args[0]] = currentStack;
|
||||
result += " mov [rbp-" + std::to_string(currentStack) + "], rcx ; arg " + func.args[0] + "\n";
|
||||
currentStack += 8;
|
||||
argIdx++;
|
||||
}
|
||||
if (func.args.size() > 1) {
|
||||
stackMap[func.args[1]] = currentStack;
|
||||
result += " mov [rbp-" + std::to_string(currentStack) + "], rdx ; arg " + func.args[1] + "\n";
|
||||
currentStack += 8;
|
||||
argIdx++;
|
||||
}
|
||||
if (func.args.size() > 2) {
|
||||
stackMap[func.args[2]] = currentStack;
|
||||
result += " mov [rbp-" + std::to_string(currentStack) + "], r8 ; arg " + func.args[2] + "\n";
|
||||
currentStack += 8;
|
||||
argIdx++;
|
||||
}
|
||||
|
||||
// GENEROWANIE INSTRUKCJI
|
||||
// 2. INSTRUKCJE
|
||||
for (const auto& instr : func.instructions) {
|
||||
|
||||
// Rezerwacja miejsca na stosie dla zmiennych
|
||||
// Rezerwacja miejsca dla nowych zmiennych (wynikowych)
|
||||
// Dodajemy tu OpType::SUB i OpType::MUL
|
||||
bool isWriteOp = (instr.type == OpType::ASSIGN ||
|
||||
instr.type == OpType::ADD ||
|
||||
instr.type == OpType::EQ ||
|
||||
instr.type == OpType::SUB ||
|
||||
instr.type == OpType::MUL ||
|
||||
instr.type == OpType::DIV ||
|
||||
instr.type == OpType::MOD ||
|
||||
instr.type == OpType::LOGIC_AND ||
|
||||
instr.type == OpType::LOGIC_OR ||
|
||||
instr.type == OpType::MSGBOX ||
|
||||
instr.type == OpType::ARRAY_DECLARE ||
|
||||
instr.type == OpType::ARRAY_SET ||
|
||||
instr.type == OpType::ALLOC_OBJECT ||
|
||||
instr.type == OpType::LOAD_FIELD);
|
||||
instr.type == OpType::MOD);
|
||||
|
||||
if (isWriteOp && stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
|
||||
stackMap[instr.arg1] = currentStack;
|
||||
|
||||
// Dla obiektów i tablic alokujemy wiêcej miejsca
|
||||
if (instr.type == OpType::ALLOC_OBJECT) {
|
||||
int size = std::stoi(instr.arg2);
|
||||
currentStack += size;
|
||||
}
|
||||
else if (instr.type == OpType::ARRAY_DECLARE) {
|
||||
int size = std::stoi(instr.arg2);
|
||||
currentStack += (size * 8);
|
||||
}
|
||||
else {
|
||||
currentStack += 8; // domyœlnie zmienna int/ptr
|
||||
}
|
||||
currentStack += 8;
|
||||
}
|
||||
|
||||
switch (instr.type) {
|
||||
case OpType::ASSIGN: {
|
||||
// Jeœli Ÿród³em jest wynik funkcji (RAX), nie generujemy "mov rax, eax"
|
||||
if (instr.arg2 == "RAX") {
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
// POPRAWIONE: Tylko dwa backslashe, tak jak w reszcie kodu
|
||||
result += " mov " + dst + ", rax\n";
|
||||
}
|
||||
else if (instr.arg3.find("ARRAY_IDX:") == 0) {
|
||||
std::string indexStr = instr.arg3.substr(10);
|
||||
std::string arrName = instr.arg2;
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
int baseOffset = stackMap[arrName];
|
||||
|
||||
if (isNumber(indexStr)) result += " mov rcx, " + indexStr + "\\n";
|
||||
else result += " mov rcx, " + getVarLocation(indexStr, stackMap) + "\\n";
|
||||
|
||||
result += " imul rcx, 8\\n";
|
||||
result += " mov rdx, rbp\\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\\n";
|
||||
result += " sub rdx, rcx\\n";
|
||||
result += " mov rax, [rdx]\\n";
|
||||
result += " mov " + dst + ", rax\\n";
|
||||
}
|
||||
else if (instr.arg3 == "STRING") {
|
||||
std::string src = instr.arg2;
|
||||
result += " lea rax, [rel " + src + "]\\n";
|
||||
if (instr.arg3 == "STRING") {
|
||||
result += " lea rax, [rel " + src + "]\n";
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov qword " + dst + ", rax\\n";
|
||||
result += " mov qword " + dst + ", rax\n";
|
||||
}
|
||||
else {
|
||||
std::string srcLoc = getVarLocation(instr.arg2, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
std::string src = instr.arg2;
|
||||
if (isNumber(src)) result += " mov rax, " + src + "\\n";
|
||||
else result += " mov rax, " + srcLoc + "\\n";
|
||||
|
||||
result += " mov " + dst + ", rax\\n";
|
||||
result += " mov eax, " + srcLoc + "\n";
|
||||
result += " mov " + dst + ", eax\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case OpType::ALLOC_OBJECT: {
|
||||
// Miejsce na stosie zosta³o zarezerwowane wy¿ej w pêtli (currentStack += size)
|
||||
// Mo¿emy opcjonalnie wyzerowaæ pamiêæ (memset), ale na razie pomijamy dla prostoty
|
||||
// Komentarz w ASM
|
||||
result += " ; Alloc Object " + instr.arg1 + " size: " + instr.arg2 + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::STORE_FIELD: {
|
||||
// STORE_FIELD objName, offset, value
|
||||
std::string objName = instr.arg1;
|
||||
int offset = std::stoi(instr.arg2);
|
||||
std::string valStr = instr.arg3;
|
||||
|
||||
// 1. Gdzie jest obiekt? (jego baza)
|
||||
// Obiekt na stosie zaczyna siê pod [RBP - stackMap[objName]]
|
||||
// Pola s¹ kolejne w dó³ stosu (bo stos roœnie w dó³, ale struktura ma dodatnie offsety...
|
||||
// W C lokalne struktury: &obj to najni¿szy adres.
|
||||
// U nas stackMap[obj] to "górny" adres (pierwsze zarezerwowane 8 bajtów).
|
||||
// Przyjmijmy: adres_pola = (RBP - stackMap[objName]) - offset
|
||||
|
||||
// Pobierz wartoϾ do zapisania
|
||||
if (isNumber(valStr)) {
|
||||
result += " mov rax, " + valStr + "\n";
|
||||
}
|
||||
else {
|
||||
result += " mov rax, " + getVarLocation(valStr, stackMap) + "\n";
|
||||
}
|
||||
|
||||
int baseOffset = 0;
|
||||
|
||||
// SprawdŸ czy objName to "this"
|
||||
if (objName == "this") {
|
||||
// "this" jest wskaŸnikiem! Trzeba go za³adowaæ
|
||||
std::string thisPtrLoc = getVarLocation("this", stackMap);
|
||||
result += " mov rdx, " + thisPtrLoc + "\n"; // RDX = adres obiektu
|
||||
// Adres pola = RDX - offset (tutaj uwaga: jeœli alokujemy na stosie "w dó³", to pola maj¹ ujemne offsety wzglêdem bazy?)
|
||||
// Zróbmy proœciej: w 'ALLOC_OBJECT' rezerwujemy blok.
|
||||
// [RBP - base] to pocz¹tek (pole 0).
|
||||
// [RBP - base - 8] to pole 1 (offset 8).
|
||||
// Czyli adres = RBP - base - offset.
|
||||
|
||||
// ALE: "this" przekazany do funkcji to wskaŸnik na ten obszar w pamiêci.
|
||||
// Jeœli przekazujemy adres zmiennej lokalnej (LEA), to wskaŸnik pokazuje na [RBP-base].
|
||||
// Wiêc [RDX - offset] powinno zadzia³aæ.
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
result += " mov [rdx], rax\n";
|
||||
}
|
||||
else {
|
||||
// Obiekt lokalny na stosie
|
||||
baseOffset = stackMap[objName];
|
||||
result += " mov rdx, rbp\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n";
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
result += " mov [rdx], rax\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OpType::LOAD_FIELD: {
|
||||
// LOAD_FIELD destVar, objName, offset
|
||||
std::string destVar = instr.arg1; // gdzie zapisaæ wynik
|
||||
std::string objName = instr.arg2; // sk¹d czytaæ
|
||||
int offset = std::stoi(instr.arg3); // offset pola
|
||||
|
||||
// 1. Oblicz adres pola
|
||||
if (objName == "this") {
|
||||
std::string thisPtrLoc = getVarLocation("this", stackMap);
|
||||
result += " mov rdx, " + thisPtrLoc + "\n";
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
}
|
||||
else {
|
||||
int baseOffset = stackMap[objName];
|
||||
result += " mov rdx, rbp\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n";
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
}
|
||||
|
||||
// 2. Pobierz wartoϾ
|
||||
result += " mov rax, [rdx]\n";
|
||||
|
||||
// 3. Zapisz do zmiennej docelowej
|
||||
std::string destLoc = getVarLocation(destVar, stackMap);
|
||||
result += " mov " + destLoc + ", rax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::ADD: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
@@ -272,55 +122,29 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
break;
|
||||
}
|
||||
case OpType::SUB: {
|
||||
// a = b - c
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " sub eax, " + op2 + "\n";
|
||||
result += " sub eax, " + op2 + "\n"; // sub = odejmowanie
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::MUL: {
|
||||
// a = b * c
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
// Mno¿enie w x86 jest specyficzne: imul eax, operand
|
||||
// Wynik l¹duje w eax (i edx jeœli du¿y, ale ignorujemy nadmiar dla prostoty)
|
||||
result += " imul eax, " + op2 + "\n";
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::DIV: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cdq\n";
|
||||
if (isdigit(op2[0]) || op2[0] == '-') {
|
||||
result += " mov ecx, " + op2 + "\n";
|
||||
result += " idiv ecx\n";
|
||||
}
|
||||
else {
|
||||
result += " idiv dword " + op2 + "\n";
|
||||
}
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::MOD: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cdq\n";
|
||||
if (isdigit(op2[0]) || op2[0] == '-') {
|
||||
result += " mov ecx, " + op2 + "\n";
|
||||
result += " idiv ecx\n";
|
||||
}
|
||||
else {
|
||||
result += " idiv dword " + op2 + "\n";
|
||||
}
|
||||
result += " mov " + dst + ", edx\n";
|
||||
break;
|
||||
}
|
||||
case OpType::EQ: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
@@ -332,203 +156,166 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::LOGIC_AND: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cmp eax, 0\n";
|
||||
result += " setne al\n";
|
||||
if (isdigit(op2[0])) result += " mov ecx, " + op2 + "\n";
|
||||
else result += " mov ecx, " + op2 + "\n";
|
||||
result += " cmp ecx, 0\n";
|
||||
result += " setne cl\n";
|
||||
result += " and al, cl\n";
|
||||
result += " movzx eax, al\n";
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::LOGIC_OR: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cmp eax, 0\n";
|
||||
result += " setne al\n";
|
||||
if (isdigit(op2[0])) result += " mov ecx, " + op2 + "\n";
|
||||
else result += " mov ecx, " + op2 + "\n";
|
||||
result += " cmp ecx, 0\n";
|
||||
result += " setne cl\n";
|
||||
result += " or al, cl\n";
|
||||
result += " movzx eax, al\n";
|
||||
result += " mov " + dst + ", eax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::JMP_FALSE: {
|
||||
// PARSOWANIE WARUNKU: np. "suma == 30" lub "test"
|
||||
std::string condRaw = instr.arg2;
|
||||
size_t eqPos = condRaw.find("==");
|
||||
|
||||
if (eqPos != std::string::npos) {
|
||||
// Mamy porównanie w IFie (a == b)
|
||||
std::string leftStr = condRaw.substr(0, eqPos);
|
||||
std::string rightStr = condRaw.substr(eqPos + 2);
|
||||
|
||||
std::string op1 = getVarLocation(leftStr, stackMap);
|
||||
std::string op2 = getVarLocation(rightStr, stackMap);
|
||||
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cmp eax, " + op2 + "\n";
|
||||
result += " jne " + instr.arg1 + " ; jump if NOT equal\n";
|
||||
}
|
||||
else {
|
||||
// Zwyk³a zmienna boolowska (if test)
|
||||
std::string cond = getVarLocation(condRaw, stackMap);
|
||||
result += " mov eax, " + cond + "\n";
|
||||
result += " test eax, eax\n";
|
||||
result += " je " + instr.arg1 + "\n";
|
||||
break;
|
||||
result += " jz " + instr.arg1 + " ; jump if zero\n";
|
||||
}
|
||||
case OpType::JMP: {
|
||||
result += " jmp " + instr.arg1 + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::ARRAY_DECLARE: {
|
||||
// Obs³u¿one przy alokacji stosu
|
||||
break;
|
||||
}
|
||||
case OpType::ARRAY_SET: {
|
||||
std::string arrName = instr.arg1;
|
||||
std::string indexStr = instr.arg2;
|
||||
std::string valStr = instr.arg3;
|
||||
|
||||
if (isNumber(valStr)) result += " mov rax, " + valStr + "\n";
|
||||
else {
|
||||
std::string valLoc = getVarLocation(valStr, stackMap);
|
||||
result += " mov rax, " + valLoc + "\n";
|
||||
}
|
||||
|
||||
int baseOffset = stackMap[arrName];
|
||||
|
||||
if (isNumber(indexStr)) result += " mov rcx, " + indexStr + "\n";
|
||||
else {
|
||||
std::string idxLoc = getVarLocation(indexStr, stackMap);
|
||||
result += " mov rcx, " + idxLoc + "\n";
|
||||
}
|
||||
|
||||
result += " imul rcx, 8\n";
|
||||
result += " mov rdx, rbp\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n";
|
||||
result += " sub rdx, rcx\n";
|
||||
result += " mov [rdx], rax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::LABEL: {
|
||||
result += instr.arg1 + ":\n";
|
||||
break;
|
||||
}
|
||||
case OpType::PRINT: {
|
||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov edx, " + val + "\n";
|
||||
result += " lea rcx, [rel fmt_int]\n";
|
||||
result += " xor eax, eax\n";
|
||||
result += " call printf\n";
|
||||
break;
|
||||
}
|
||||
case OpType::PRINT_STRING: {
|
||||
std::string target = instr.arg1;
|
||||
if (target.rfind("str_", 0) == 0) {
|
||||
result += " lea rdx, [rel " + target + "]\n";
|
||||
}
|
||||
else {
|
||||
std::string val = getVarLocation(target, stackMap);
|
||||
result += " mov rdx, " + val + "\n";
|
||||
}
|
||||
result += " lea rcx, [rel fmt_str]\n";
|
||||
result += " xor eax, eax\n";
|
||||
result += " call printf\n";
|
||||
break;
|
||||
}
|
||||
case OpType::CALL: {
|
||||
if (instr.arg1 == "input" || instr.arg1 == "read_key") {
|
||||
result += " call _getch\n";
|
||||
break;
|
||||
}
|
||||
if (instr.arg1 == "sys_seed") {
|
||||
result += " mov rcx, 0\n";
|
||||
result += " call time\n";
|
||||
result += " mov rcx, rax\n";
|
||||
result += " call srand\n";
|
||||
break;
|
||||
}
|
||||
if (instr.arg1 == "sys_rand") {
|
||||
result += " call rand\n";
|
||||
break;
|
||||
}
|
||||
|
||||
// Call metody/funkcji
|
||||
std::string funcName = instr.arg1;
|
||||
std::string argsRaw = instr.arg2;
|
||||
std::vector<std::string> callArgs;
|
||||
|
||||
if (!argsRaw.empty()) {
|
||||
std::stringstream ss(argsRaw);
|
||||
std::string segment;
|
||||
while (std::getline(ss, segment, ',')) {
|
||||
callArgs.push_back(segment);
|
||||
}
|
||||
}
|
||||
|
||||
// Przygotowanie argumentów dla Windows x64 (RCX, RDX, R8, R9)
|
||||
// Argument 0 (RCX) - ewentualnie 'this'
|
||||
if (callArgs.size() > 0) {
|
||||
// Czy to 'this' (nazwa obiektu)?
|
||||
std::string arg0 = callArgs[0];
|
||||
if (state.varTypes.count(arg0) && state.classes.count(state.varTypes.at(arg0))) {
|
||||
// Przekazujemy ADRES obiektu (pointer)
|
||||
// Obiekt jest na stosie: [RBP - offset]
|
||||
// Adres to: RBP - offset
|
||||
int offset = stackMap.at(arg0);
|
||||
result += " lea rcx, [rbp-" + std::to_string(offset) + "]\n";
|
||||
}
|
||||
else if (arg0 == "this") {
|
||||
// Przekazujemy this dalej
|
||||
result += " mov rcx, [rbp+16]\n"; // zak³adaj¹c ¿e this jest w shadow space? nie, my go kopiujemy na stos
|
||||
// Wróæmy do logiki argumentów: argumenty funkcji s¹ kopiowane na stos lokalny.
|
||||
// arg0 ("this") jest w stackMap["this"].
|
||||
std::string loc = getVarLocation("this", stackMap);
|
||||
result += " mov rcx, " + loc + "\n";
|
||||
}
|
||||
else {
|
||||
// Zwyk³a zmienna / liczba
|
||||
std::string val = getVarLocation(arg0, stackMap);
|
||||
if (isNumber(val)) result += " mov rcx, " + val + "\n";
|
||||
else result += " movsxd rcx, dword " + val + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (callArgs.size() > 1) {
|
||||
std::string val = getVarLocation(callArgs[1], stackMap);
|
||||
if (isNumber(val)) result += " mov rdx, " + val + "\n";
|
||||
else result += " movsxd rdx, dword " + val + "\n";
|
||||
}
|
||||
|
||||
// ... (dalsze argumenty R8, R9 jeœli potrzebujesz)
|
||||
|
||||
result += " call " + funcName + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::RETURN: {
|
||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||
if (!val.empty() && val != ";") {
|
||||
result += " mov eax, " + val + "\n";
|
||||
if (val.empty() || val == ";");
|
||||
else {
|
||||
result += " mov eax, " + val + " ; return value\n";
|
||||
}
|
||||
result += " leave\n";
|
||||
result += " ret\n";
|
||||
break;
|
||||
}
|
||||
case OpType::MSGBOX: {
|
||||
std::string title = instr.arg1;
|
||||
std::string text = instr.arg2;
|
||||
|
||||
if (text.find("str_") == 0) result += " lea rdx, [rel " + text + "]\n";
|
||||
case OpType::PRINT: {
|
||||
if (instr.arg2 == "STRING") {
|
||||
result += " lea rdx, [rel " + instr.arg1 + "]\n";
|
||||
result += " lea rcx, [rel fmt_str]\n";
|
||||
result += " call printf\n";
|
||||
}
|
||||
else {
|
||||
std::string loc = getVarLocation(text, stackMap);
|
||||
result += " mov rdx, " + loc + "\n";
|
||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov edx, " + val + "\n";
|
||||
result += " lea rcx, [rel fmt_int]\n";
|
||||
result += " call printf\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OpType::CALL: {
|
||||
// Parsowanie argumentów
|
||||
std::string argsRaw = instr.arg2;
|
||||
std::vector<std::string> callArgs;
|
||||
// --- SPECJALNE FUNKCJE SYSTEMOWE ---
|
||||
|
||||
// 1. input() - czeka na ENTER (stare)
|
||||
if (instr.arg1 == "input") {
|
||||
result += " call getchar\n";
|
||||
break;
|
||||
}
|
||||
|
||||
if (title.find("str_") == 0) result += " lea r8, [rel " + title + "]\n";
|
||||
// 2. read_key() - zwraca kod wciœniêtego klawisza (NOWOŒÆ)
|
||||
if (instr.arg1 == "read_key") {
|
||||
result += " call _getch\n"; // Zwraca kod znaku w EAX
|
||||
// Jeœli to klawisz specjalny (strza³ki), _getch zwraca 0 lub 224,
|
||||
// a potem trzeba wywo³aæ go drugi raz.
|
||||
// Na razie zróbmy prosto: zwracamy to co zwróci³ pierwszy _getch.
|
||||
break;
|
||||
}
|
||||
if (!argsRaw.empty()) {
|
||||
size_t comma = argsRaw.find(',');
|
||||
if (comma != std::string::npos) {
|
||||
callArgs.push_back(argsRaw.substr(0, comma));
|
||||
callArgs.push_back(argsRaw.substr(comma + 1));
|
||||
}
|
||||
else {
|
||||
std::string loc = getVarLocation(title, stackMap);
|
||||
result += " mov r8, " + loc + "\n";
|
||||
callArgs.push_back(argsRaw);
|
||||
}
|
||||
}
|
||||
|
||||
// Obs³uga RDX (arg 2)
|
||||
if (callArgs.size() > 1) {
|
||||
std::string val = getVarLocation(callArgs[1], stackMap);
|
||||
if (isNumber(val)) {
|
||||
// Jeœli liczba: mov rdx, 100
|
||||
result += " mov rdx, " + val + "\n";
|
||||
}
|
||||
else {
|
||||
// Jeœli zmienna/pamiêæ: movsxd rdx, dword [rbp-8]
|
||||
result += " movsxd rdx, dword " + val + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Obs³uga RCX (arg 1)
|
||||
if (callArgs.size() > 0) {
|
||||
std::string val = getVarLocation(callArgs[0], stackMap);
|
||||
if (isNumber(val)) {
|
||||
result += " mov rcx, " + val + "\n";
|
||||
}
|
||||
else {
|
||||
result += " movsxd rcx, dword " + val + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// ... wewn¹trz case OpType::CALL ...
|
||||
if (instr.arg1 == "sys_seed") {
|
||||
result += " mov rcx, 0\n";
|
||||
result += " mov r9, 0\n";
|
||||
result += " call MessageBoxA\n";
|
||||
result += " call time\n"; // Pobierz czas
|
||||
result += " mov rcx, rax\n"; // Czas jako argument dla srand
|
||||
result += " call srand\n"; // Inicjuj generator
|
||||
break;
|
||||
}
|
||||
if (instr.arg1 == "sys_rand") {
|
||||
result += " call rand\n"; // Wynik w EAX
|
||||
break;
|
||||
}
|
||||
|
||||
result += " call " + instr.arg1 + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::DIV: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cdq\n"; // Rozszerza EAX na EDX:EAX (konieczne przed dzieleniem)
|
||||
|
||||
// IDIV nie przyjmuje sta³ej (np. 10), musi byæ rejestr
|
||||
if (isdigit(op2[0]) || op2[0] == '-') {
|
||||
result += " mov ecx, " + op2 + "\n";
|
||||
result += " idiv ecx\n";
|
||||
}
|
||||
else {
|
||||
result += " idiv dword " + op2 + "\n";
|
||||
}
|
||||
result += " mov " + dst + ", eax\n"; // Wynik dzielenia
|
||||
break;
|
||||
}
|
||||
case OpType::MOD: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
result += " mov eax, " + op1 + "\n";
|
||||
result += " cdq\n"; // Rozszerza EAX
|
||||
|
||||
if (isdigit(op2[0]) || op2[0] == '-') {
|
||||
result += " mov ecx, " + op2 + "\n";
|
||||
result += " idiv ecx\n";
|
||||
}
|
||||
else {
|
||||
result += " idiv dword " + op2 + "\n";
|
||||
}
|
||||
result += " mov " + dst + ", edx\n"; // EDX to reszta z dzielenia!
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,35 +6,25 @@
|
||||
#include <map>
|
||||
#include <stack>
|
||||
|
||||
// Typy operacji
|
||||
// Typy operacji, które nasz kompilator rozumie
|
||||
enum class OpType {
|
||||
ASSIGN, // a = 5
|
||||
ADD, // a = b + c
|
||||
SUB, // a = b - c
|
||||
MUL, // a = b * c
|
||||
DIV, // a = b / c
|
||||
MOD, // a = b % c
|
||||
EQ, // a == b
|
||||
PRINT, // print(int)
|
||||
PRINT_STRING, // print(string)
|
||||
PRINT, // print(a)
|
||||
JMP_FALSE, // if (false) skocz...
|
||||
ARRAY_DECLARE, // int t[10];
|
||||
ARRAY_SET, // t[0] = 5;
|
||||
ARRAY_GET, // x = t[0];
|
||||
JMP, // else / pêtla
|
||||
LOGIC_AND, // &&
|
||||
LOGIC_OR, // ||
|
||||
LABEL, // miejsce skoku
|
||||
CALL, // wywo³anie funkcji
|
||||
RETURN, // return x
|
||||
MSGBOX, // msg box
|
||||
ALLOC_OBJECT, // alokacja obiektu na stosie - NOWE
|
||||
STORE_FIELD, // zapis do pola obiektu - NOWE
|
||||
LOAD_FIELD, // odczyt z pola obiektu - NOWE
|
||||
DIV, // dzielenie
|
||||
MOD, // Reszta z dzelenia (%)
|
||||
NOP // pusta instrukcja
|
||||
};
|
||||
|
||||
// Pojedynczy rozkaz
|
||||
// Pojedynczy rozkaz kompilatora (Intermediate Representation)
|
||||
struct Instruction {
|
||||
OpType type;
|
||||
std::string arg1;
|
||||
@@ -45,63 +35,27 @@ struct Instruction {
|
||||
// Definicja funkcji
|
||||
struct Function {
|
||||
std::string name;
|
||||
std::string returnType;
|
||||
std::vector<std::string> args;
|
||||
std::vector<Instruction> instructions;
|
||||
std::string returnType; // "int", "void", "bool"
|
||||
std::vector<std::string> args; // Nazwy argumentów (np. "a", "b")
|
||||
std::vector<Instruction> instructions; // Lista rozkazów w funkcji
|
||||
};
|
||||
|
||||
// Definicja pola klasy
|
||||
struct ClassField {
|
||||
std::string name;
|
||||
std::string type; // "int", "string", itp.
|
||||
int offset; // offset w bajtach od pocz¹tku obiektu
|
||||
};
|
||||
|
||||
// Definicja metody klasy
|
||||
struct ClassMethod {
|
||||
std::string name;
|
||||
std::string returnType;
|
||||
std::vector<std::string> args;
|
||||
std::string mangledName; // np. "User_setAge"
|
||||
};
|
||||
|
||||
// Definicja klasy
|
||||
struct ClassDef {
|
||||
std::string name;
|
||||
std::vector<ClassField> fields;
|
||||
std::vector<ClassMethod> methods;
|
||||
int totalSize; // rozmiar ca³ego obiektu w bajtach
|
||||
};
|
||||
|
||||
// G£ÓWNY STAN KOMPILATORA
|
||||
struct CompilerState {
|
||||
// Mapa funkcji
|
||||
// Mapa wszystkich funkcji (klucz to nazwa)
|
||||
std::map<std::string, Function> functions;
|
||||
|
||||
// Mapa klas (NOWE)
|
||||
std::map<std::string, ClassDef> classes;
|
||||
|
||||
// Zmienne globalne
|
||||
// Zmienne globalne (tylko nazwa -> wartoœæ pocz¹tkowa)
|
||||
std::map<std::string, int> globals;
|
||||
|
||||
// Zarz¹dzanie stosem
|
||||
std::map<std::string, int> stackMap;
|
||||
int stackOffset = 0;
|
||||
|
||||
// Stan parsera
|
||||
Function* currentFunction = nullptr;
|
||||
std::string currentClass = ""; // (NOWE) - nazwa aktualnie parsowanej klasy
|
||||
int labelCounter = 0;
|
||||
Function* currentFunction = nullptr; // WskaŸnik na aktualnie parsuj¹c¹ siê funkcjê
|
||||
int labelCounter = 0; // Do generowania unikalnych nazw etykiet (L1, L2...)
|
||||
std::stack<std::string> loopStack; // Do break/continue (przysz³oœciowo)
|
||||
|
||||
// Stosy bloków
|
||||
std::stack<std::string> loopStack;
|
||||
std::stack<std::string> blockStack;
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> stringLiterals;
|
||||
|
||||
// Licznik do generowania nazw str_0, str_1...
|
||||
int stringCounter = 0;
|
||||
std::map<std::string, std::string> varTypes;
|
||||
std::map<std::string, std::string> stringLiterals;
|
||||
int stringCounter = 0; // Licznik do generowania nazw str_1, str_2...
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -19,7 +19,7 @@ std::string getExecutablePath() {
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::string inputFile, outputName;
|
||||
std::string Version = "v0.0.7-beta";
|
||||
std::string Version = "v0.0.5-beta";
|
||||
bool showHelp = false, showVersion = false, showCredits = false;
|
||||
|
||||
// --- PARSOWANIE ARGUMENTÓW ---
|
||||
|
||||
@@ -4,17 +4,6 @@
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
// ===================================================================
|
||||
// DEKLARACJE WSTĘPNE
|
||||
// ===================================================================
|
||||
// Musimy zadeklarować tę funkcję wcześniej, bo używają jej i klasy i main
|
||||
bool handleAssignment(const std::string& line, Function& f, CompilerState& state);
|
||||
bool parseInstruction(const std::string& line, Function& f, CompilerState& state);
|
||||
|
||||
// ===================================================================
|
||||
// POMOCNICZE FUNKCJE PARSOWANIA
|
||||
// ===================================================================
|
||||
|
||||
std::vector<std::string> parseArgs(const std::string& line) {
|
||||
std::vector<std::string> args;
|
||||
size_t open = line.find('(');
|
||||
@@ -28,7 +17,8 @@ std::vector<std::string> parseArgs(const std::string& line) {
|
||||
std::string segment;
|
||||
while (std::getline(ss, segment, ',')) {
|
||||
segment = trim(segment);
|
||||
size_t space = segment.find_last_of(" \\t");
|
||||
// segment to np. "int a". Szukamy ostatniej spacji, by wziąć nazwę "a"
|
||||
size_t space = segment.find_last_of(" \t");
|
||||
if (space != std::string::npos) {
|
||||
args.push_back(trim(segment.substr(space + 1)));
|
||||
}
|
||||
@@ -36,584 +26,19 @@ std::vector<std::string> parseArgs(const std::string& line) {
|
||||
return args;
|
||||
}
|
||||
|
||||
std::string registerStringLiteral(CompilerState& state, std::string content) {
|
||||
std::string label = "str_" + std::to_string(state.stringCounter++);
|
||||
state.stringLiterals.push_back({ label, content });
|
||||
return label;
|
||||
}
|
||||
|
||||
int getClassFieldOffset(CompilerState& state, const std::string& fieldName) {
|
||||
if (state.currentClass.empty()) return -1;
|
||||
if (state.classes.count(state.currentClass) == 0) return -1;
|
||||
|
||||
const auto& fields = state.classes[state.currentClass].fields;
|
||||
for (const auto& f : fields) {
|
||||
if (f.name == fieldName) return f.offset;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// FUNKCJE POMOCNICZE DO OBSŁUGI WARUNKÓW
|
||||
// ===================================================================
|
||||
|
||||
std::string processCondition(const std::string& conditionRaw, Function& f, CompilerState& state) {
|
||||
std::string cond = trim(conditionRaw);
|
||||
|
||||
int fieldOffset = getClassFieldOffset(state, cond);
|
||||
if (fieldOffset != -1) {
|
||||
std::string tmp = "_cond_fld_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, tmp, "this", std::to_string(fieldOffset) });
|
||||
cond = tmp;
|
||||
}
|
||||
|
||||
if (cond.find("==") != std::string::npos) {
|
||||
size_t opPos = cond.find("==");
|
||||
std::string a = trim(cond.substr(0, opPos));
|
||||
std::string b = trim(cond.substr(opPos + 2));
|
||||
|
||||
int offA = getClassFieldOffset(state, a);
|
||||
if (offA != -1) {
|
||||
std::string t = "_c_a_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, t, "this", std::to_string(offA) });
|
||||
a = t;
|
||||
}
|
||||
int offB = getClassFieldOffset(state, b);
|
||||
if (offB != -1) {
|
||||
std::string t = "_c_b_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, t, "this", std::to_string(offB) });
|
||||
b = t;
|
||||
}
|
||||
|
||||
std::string tmp = "_cond_tmp_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::EQ, tmp, a, b });
|
||||
return tmp;
|
||||
}
|
||||
else if (cond.find("!=") != std::string::npos) {
|
||||
size_t opPos = cond.find("!=");
|
||||
std::string a = trim(cond.substr(0, opPos));
|
||||
std::string b = trim(cond.substr(opPos + 2));
|
||||
std::string tmp = "_cond_tmp_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::EQ, tmp, a, b });
|
||||
return tmp;
|
||||
}
|
||||
|
||||
return cond;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// FUNKCJE OBSŁUGI BLOKÓW
|
||||
// ===================================================================
|
||||
|
||||
void handleBlockClose(CompilerState& state, const std::vector<std::string>& lines, size_t i) {
|
||||
if (state.blockStack.empty()) {
|
||||
state.currentFunction = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
bool nextIsElse = false;
|
||||
size_t j = i + 1;
|
||||
while (j < lines.size()) {
|
||||
std::string nl = trim(lines[j]);
|
||||
if (nl.empty() || nl.rfind("//", 0) == 0) { j++; continue; }
|
||||
if (nl.rfind("else", 0) == 0) nextIsElse = true;
|
||||
break;
|
||||
}
|
||||
|
||||
std::string blockInfo = state.blockStack.top();
|
||||
|
||||
if (blockInfo.rfind("WHILE|", 0) == 0) {
|
||||
state.blockStack.pop();
|
||||
size_t p1 = blockInfo.find('|');
|
||||
size_t p2 = blockInfo.rfind('|');
|
||||
std::string labelStart = blockInfo.substr(p1 + 1, p2 - p1 - 1);
|
||||
std::string labelEnd = blockInfo.substr(p2 + 1);
|
||||
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::JMP, labelStart, "", "" });
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, labelEnd, "", "" });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (blockInfo.rfind("IF|", 0) == 0) {
|
||||
state.blockStack.pop();
|
||||
size_t p1 = blockInfo.find('|');
|
||||
size_t p2 = blockInfo.rfind('|');
|
||||
std::string labelElse = blockInfo.substr(p1 + 1, p2 - p1 - 1);
|
||||
std::string labelEnd = blockInfo.substr(p2 + 1);
|
||||
|
||||
if (nextIsElse) {
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::JMP, labelEnd, "", "" });
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, labelElse, "", "" });
|
||||
}
|
||||
state.blockStack.push(labelEnd);
|
||||
}
|
||||
else {
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, labelElse, "", "" });
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
state.blockStack.pop();
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, blockInfo, "", "" });
|
||||
}
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// FUNKCJE OBSŁUGI INSTRUKCJI
|
||||
// ===================================================================
|
||||
|
||||
bool handleReturn(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.substr(0, 6) != "return") return false;
|
||||
std::string val = trim(line.substr(6));
|
||||
if (!val.empty() && val.back() == ';') val.pop_back();
|
||||
|
||||
int fieldOffset = getClassFieldOffset(state, val);
|
||||
if (fieldOffset != -1) {
|
||||
std::string tmp = "_ret_tmp_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, tmp, "this", std::to_string(fieldOffset) });
|
||||
val = tmp;
|
||||
}
|
||||
f.instructions.push_back({ OpType::RETURN, val, "", "" });
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handleStringDeclaration(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.substr(0, 6) != "string") return false;
|
||||
size_t eqPos = line.find("=");
|
||||
if (eqPos == std::string::npos) return false;
|
||||
std::string name = trim(line.substr(7, eqPos - 7));
|
||||
size_t quoteStart = line.find("\"", eqPos);
|
||||
size_t quoteEnd = line.rfind("\"");
|
||||
|
||||
if (quoteStart != std::string::npos && quoteEnd > quoteStart) {
|
||||
std::string content = line.substr(quoteStart + 1, quoteEnd - quoteStart - 1);
|
||||
std::string label = registerStringLiteral(state, content);
|
||||
state.varTypes[name] = "string";
|
||||
f.instructions.push_back({ OpType::ASSIGN, name, label, "" });
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool handleArrayDeclaration(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.substr(0, 3) != "int" || line.find("[") == std::string::npos || line.find("=") != std::string::npos) return false;
|
||||
size_t openBracket = line.find("[");
|
||||
size_t closeBracket = line.find("]");
|
||||
|
||||
if (openBracket != std::string::npos && closeBracket > openBracket) {
|
||||
std::string name = trim(line.substr(3, openBracket - 3));
|
||||
std::string sizeStr = trim(line.substr(openBracket + 1, closeBracket - openBracket - 1));
|
||||
state.varTypes[name] = "array";
|
||||
f.instructions.push_back({ OpType::ARRAY_DECLARE, name, sizeStr, "" });
|
||||
std::cout << " [PARSER] Array Decl: " << name << "[" << sizeStr << "]\\n";
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool handlePrint(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.substr(0, 5) != "print") return false;
|
||||
size_t open = line.find("(");
|
||||
size_t close = line.rfind(")");
|
||||
if (open == std::string::npos || close <= open) return false;
|
||||
std::string content = trim(line.substr(open + 1, close - open - 1));
|
||||
|
||||
if (content.find("[") != std::string::npos && content.back() == ']') {
|
||||
size_t opIdx = content.find("[");
|
||||
std::string arrName = content.substr(0, opIdx);
|
||||
std::string arrIdx = content.substr(opIdx + 1, content.length() - opIdx - 2);
|
||||
std::string tmp = "_p_tmp_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::ASSIGN, tmp, arrName, "ARRAY_IDX:" + arrIdx });
|
||||
f.instructions.push_back({ OpType::PRINT, tmp, "", "" });
|
||||
}
|
||||
else if (content.find(".") != std::string::npos) {
|
||||
size_t dotPos = content.find(".");
|
||||
std::string objName = content.substr(0, dotPos);
|
||||
std::string fieldName = content.substr(dotPos + 1);
|
||||
if (state.varTypes.count(objName) && state.classes.count(state.varTypes[objName])) {
|
||||
std::string className = state.varTypes[objName];
|
||||
int offset = -1;
|
||||
for (auto& field : state.classes[className].fields) {
|
||||
if (field.name == fieldName) { offset = field.offset; break; }
|
||||
}
|
||||
if (offset != -1) {
|
||||
std::string tmp = "_p_tmp_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, tmp, objName, std::to_string(offset) });
|
||||
f.instructions.push_back({ OpType::PRINT, tmp, "", "" });
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (getClassFieldOffset(state, content) != -1) {
|
||||
int offset = getClassFieldOffset(state, content);
|
||||
std::string tmp = "_p_tmp_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, tmp, "this", std::to_string(offset) });
|
||||
f.instructions.push_back({ OpType::PRINT, tmp, "", "" });
|
||||
}
|
||||
else if (content.front() == '"' && content.back() == '"') {
|
||||
std::string text = content.substr(1, content.length() - 2);
|
||||
std::string label = registerStringLiteral(state, text);
|
||||
f.instructions.push_back({ OpType::PRINT_STRING, label, "", "" });
|
||||
}
|
||||
else if (state.varTypes.count(content) && state.varTypes[content] == "string") {
|
||||
f.instructions.push_back({ OpType::PRINT_STRING, content, "", "" });
|
||||
}
|
||||
else {
|
||||
f.instructions.push_back({ OpType::PRINT, content, "", "" });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handleIf(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.rfind("if", 0) != 0) return false;
|
||||
size_t openParen = line.find("(");
|
||||
size_t closeParen = line.rfind(")");
|
||||
if (openParen == std::string::npos) return false;
|
||||
std::string conditionRaw = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
||||
std::string finalCond = processCondition(conditionRaw, f, state);
|
||||
std::string labelElse = "L_" + std::to_string(state.labelCounter++);
|
||||
std::string labelEnd = "L_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::JMP_FALSE, labelElse, finalCond, "" });
|
||||
state.blockStack.push("IF|" + labelElse + "|" + labelEnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handleWhile(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.substr(0, 5) != "while") return false;
|
||||
size_t openParen = line.find("(");
|
||||
size_t closeParen = line.rfind(")");
|
||||
if (openParen == std::string::npos) return false;
|
||||
std::string conditionRaw = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
||||
std::string labelStart = "L_" + std::to_string(state.labelCounter++);
|
||||
std::string labelEnd = "L_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::LABEL, labelStart, "", "" });
|
||||
std::string finalCond = processCondition(conditionRaw, f, state);
|
||||
f.instructions.push_back({ OpType::JMP_FALSE, labelEnd, finalCond, "" });
|
||||
state.blockStack.push("WHILE|" + labelStart + "|" + labelEnd);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handleMsgbox(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.substr(0, 6) != "msgbox") return false;
|
||||
size_t open = line.find("(");
|
||||
size_t close = line.rfind(")");
|
||||
if (open == std::string::npos) return false;
|
||||
std::string args = line.substr(open + 1, close - open - 1);
|
||||
size_t comma = args.find(",");
|
||||
if (comma == std::string::npos) return false;
|
||||
std::string arg1 = trim(args.substr(0, comma));
|
||||
std::string arg2 = trim(args.substr(comma + 1));
|
||||
std::string l1 = (arg1.front() == '"') ? registerStringLiteral(state, arg1.substr(1, arg1.size() - 2)) : arg1;
|
||||
std::string l2 = (arg2.front() == '"') ? registerStringLiteral(state, arg2.substr(1, arg2.size() - 2)) : arg2;
|
||||
f.instructions.push_back({ OpType::MSGBOX, l1, l2, "" });
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handleAssignment(const std::string& line, Function& f, CompilerState& state) {
|
||||
// Specjalny przypadek: samodzielne wywołanie metody/funkcji bez "="
|
||||
if (line.find("=") == std::string::npos) {
|
||||
if (line.find("(") != std::string::npos && line.find(")") != std::string::npos) {
|
||||
// Metoda: u.setAge(5);
|
||||
if (line.find(".") != std::string::npos) {
|
||||
size_t dotPos = line.find(".");
|
||||
size_t openParen = line.find("(");
|
||||
std::string objName = trim(line.substr(0, dotPos));
|
||||
std::string methodName = trim(line.substr(dotPos + 1, openParen - dotPos - 1));
|
||||
std::string argsContent = line.substr(openParen + 1, line.rfind(")") - openParen - 1);
|
||||
|
||||
if (state.varTypes.count(objName)) {
|
||||
std::string className = state.varTypes[objName];
|
||||
std::string mangledName = className + "_" + methodName;
|
||||
std::string fullArgs = objName;
|
||||
if (!argsContent.empty()) fullArgs += "," + argsContent;
|
||||
f.instructions.push_back({ OpType::CALL, mangledName, fullArgs, "" });
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Zwykła funkcja: func();
|
||||
size_t open = line.find('(');
|
||||
std::string funcName = trim(line.substr(0, open));
|
||||
std::string argsContent = line.substr(open + 1, line.find(')') - open - 1);
|
||||
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t eqPos = line.find('=');
|
||||
std::string leftSide = trim(line.substr(0, eqPos));
|
||||
std::string rightSide = trim(line.substr(eqPos + 1));
|
||||
if (!rightSide.empty() && rightSide.back() == ';') rightSide.pop_back();
|
||||
|
||||
// 1. Zapis do pola: u.age = 5
|
||||
if (leftSide.find(".") != std::string::npos && leftSide.find("[") == std::string::npos) {
|
||||
size_t dotPos = leftSide.find(".");
|
||||
std::string objName = trim(leftSide.substr(0, dotPos));
|
||||
std::string fieldName = trim(leftSide.substr(dotPos + 1));
|
||||
std::string className = state.varTypes[objName];
|
||||
ClassDef& cls = state.classes[className];
|
||||
int offset = -1;
|
||||
for (auto& field : cls.fields) {
|
||||
if (field.name == fieldName) { offset = field.offset; break; }
|
||||
}
|
||||
f.instructions.push_back({ OpType::STORE_FIELD, objName, std::to_string(offset), rightSide });
|
||||
return true;
|
||||
}
|
||||
// 2. Zapis do pola wewnątrz metody: age = 5
|
||||
else {
|
||||
int fieldOffset = getClassFieldOffset(state, leftSide);
|
||||
if (fieldOffset != -1) {
|
||||
f.instructions.push_back({ OpType::STORE_FIELD, "this", std::to_string(fieldOffset), rightSide });
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Tablice
|
||||
if (leftSide.find("[") != std::string::npos) {
|
||||
size_t open = leftSide.find("[");
|
||||
size_t close = leftSide.find("]");
|
||||
std::string arrName = trim(leftSide.substr(0, open));
|
||||
std::string index = trim(leftSide.substr(open + 1, close - open - 1));
|
||||
f.instructions.push_back({ OpType::ARRAY_SET, arrName, index, rightSide });
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string varName = leftSide;
|
||||
if (leftSide.rfind("int ", 0) == 0) varName = trim(leftSide.substr(4));
|
||||
else if (leftSide.rfind("bool ", 0) == 0) varName = trim(leftSide.substr(5));
|
||||
else if (leftSide.rfind("string ", 0) == 0) varName = trim(leftSide.substr(7));
|
||||
|
||||
// Odczyt pola: x = u.age
|
||||
if (rightSide.find(".") != std::string::npos && rightSide.find("(") == std::string::npos) {
|
||||
size_t dot = rightSide.find(".");
|
||||
std::string obj = rightSide.substr(0, dot);
|
||||
std::string fld = rightSide.substr(dot + 1);
|
||||
if (state.varTypes.count(obj)) {
|
||||
std::string cName = state.varTypes[obj];
|
||||
int off = -1;
|
||||
for (auto& ff : state.classes[cName].fields) if (ff.name == fld) off = ff.offset;
|
||||
if (off != -1) {
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, varName, obj, std::to_string(off) });
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tablice odczyt
|
||||
if (rightSide.find("[") != std::string::npos && rightSide.back() == ']') {
|
||||
size_t open = rightSide.find("[");
|
||||
size_t close = rightSide.find("]");
|
||||
std::string arrName = trim(rightSide.substr(0, open));
|
||||
std::string index = trim(rightSide.substr(open + 1, close - open - 1));
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, arrName, "ARRAY_IDX:" + index });
|
||||
return true;
|
||||
}
|
||||
|
||||
// Wywołanie metody po prawej stronie: x = u.getAge()
|
||||
if (rightSide.find("(") != std::string::npos && rightSide.find(")") != std::string::npos) {
|
||||
if (rightSide.find(".") != std::string::npos) {
|
||||
size_t dot = rightSide.find(".");
|
||||
size_t op = rightSide.find("(");
|
||||
std::string obj = trim(rightSide.substr(0, dot));
|
||||
std::string met = trim(rightSide.substr(dot + 1, op - dot - 1));
|
||||
std::string args = rightSide.substr(op + 1, rightSide.find(")") - op - 1);
|
||||
std::string cName = state.varTypes[obj];
|
||||
std::string mangled = cName + "_" + met;
|
||||
std::string fullArgs = obj;
|
||||
if (!args.empty()) fullArgs += "," + args;
|
||||
f.instructions.push_back({ OpType::CALL, mangled, fullArgs, "" });
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, "RAX", "" });
|
||||
return true;
|
||||
}
|
||||
// Zwykła funkcja
|
||||
size_t open = rightSide.find('(');
|
||||
std::string funcName = trim(rightSide.substr(0, open));
|
||||
std::string argsContent = rightSide.substr(open + 1, rightSide.find(')') - open - 1);
|
||||
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, "RAX", "" });
|
||||
return true;
|
||||
}
|
||||
|
||||
// Arytmetyka
|
||||
struct Operator { std::string symbol; OpType type; size_t length; };
|
||||
std::vector<Operator> operators = {
|
||||
{"&&", OpType::LOGIC_AND, 2}, {"||", OpType::LOGIC_OR, 2}, {"==", OpType::EQ, 2},
|
||||
{"+", OpType::ADD, 1}, {"-", OpType::SUB, 1}, {"*", OpType::MUL, 1}, {"/", OpType::DIV, 1}, {"%", OpType::MOD, 1}
|
||||
};
|
||||
for (const auto& op : operators) {
|
||||
size_t opPos = rightSide.find(op.symbol);
|
||||
if (opPos != std::string::npos) {
|
||||
std::string a = trim(rightSide.substr(0, opPos));
|
||||
std::string b = trim(rightSide.substr(opPos + op.length));
|
||||
int offA = getClassFieldOffset(state, a);
|
||||
if (offA != -1) { std::string t = "_opa" + std::to_string(state.labelCounter++); f.instructions.push_back({ OpType::LOAD_FIELD, t, "this", std::to_string(offA) }); a = t; }
|
||||
int offB = getClassFieldOffset(state, b);
|
||||
if (offB != -1) { std::string t = "_opb" + std::to_string(state.labelCounter++); f.instructions.push_back({ OpType::LOAD_FIELD, t, "this", std::to_string(offB) }); b = t; }
|
||||
f.instructions.push_back({ op.type, varName, a, b });
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (rightSide.size() >= 2 && rightSide.front() == '"') {
|
||||
std::string content = rightSide.substr(1, rightSide.size() - 2);
|
||||
std::string label = registerStringLiteral(state, content);
|
||||
state.varTypes[varName] = "string";
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, label, "" });
|
||||
return true;
|
||||
}
|
||||
|
||||
int fOff = getClassFieldOffset(state, rightSide);
|
||||
if (fOff != -1) {
|
||||
f.instructions.push_back({ OpType::LOAD_FIELD, varName, "this", std::to_string(fOff) });
|
||||
}
|
||||
else {
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, rightSide, "" });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handleFunctionCall(const std::string& line, Function& f) {
|
||||
if (line.find("(") == std::string::npos || line.find(")") == std::string::npos) return false;
|
||||
size_t open = line.find('(');
|
||||
std::string funcName = trim(line.substr(0, open));
|
||||
std::string argsContent = line.substr(open + 1, line.find(')') - open - 1);
|
||||
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
|
||||
return true;
|
||||
}
|
||||
|
||||
// Funkcja zbiorcza parsująca jedną linię (dla processSource i dla metody w klasie)
|
||||
bool parseInstruction(const std::string& line, Function& f, CompilerState& state) {
|
||||
if (line.rfind("else", 0) == 0) return true;
|
||||
|
||||
if (handleReturn(line, f, state)) return true;
|
||||
if (handleStringDeclaration(line, f, state)) return true;
|
||||
if (handleArrayDeclaration(line, f, state)) return true;
|
||||
if (handlePrint(line, f, state)) return true;
|
||||
if (handleIf(line, f, state)) return true;
|
||||
if (handleWhile(line, f, state)) return true;
|
||||
if (handleMsgbox(line, f, state)) return true;
|
||||
|
||||
// handleAssignment obsługuje też wywołania metod i operacje
|
||||
if (handleAssignment(line, f, state)) return true;
|
||||
|
||||
// Deklaracja obiektu: User u;
|
||||
if (state.classes.count(line.substr(0, line.find(" "))) > 0) {
|
||||
size_t spacePos = line.find(" ");
|
||||
std::string className = line.substr(0, spacePos);
|
||||
std::string varName = trim(line.substr(spacePos + 1));
|
||||
if (varName.back() == ';') varName.pop_back();
|
||||
state.varTypes[varName] = className;
|
||||
int size = state.classes[className].totalSize;
|
||||
f.instructions.push_back({ OpType::ALLOC_OBJECT, varName, std::to_string(size), "" });
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// PARSOWANIE KLASY (TERAZ PARSUJE CIAŁA METOD!)
|
||||
// ===================================================================
|
||||
|
||||
void parseClassDefinition(const std::vector<std::string>& lines, size_t& i, CompilerState& state) {
|
||||
std::string line = trim(lines[i]);
|
||||
size_t bracePos = line.find("{");
|
||||
std::string className = trim(line.substr(6, bracePos - 6));
|
||||
|
||||
ClassDef classDef;
|
||||
classDef.name = className;
|
||||
int currentOffset = 0;
|
||||
i++;
|
||||
|
||||
while (i < lines.size()) {
|
||||
line = trim(lines[i]);
|
||||
if (line == "}") break;
|
||||
if (line.empty() || line.rfind("//", 0) == 0) { i++; continue; }
|
||||
|
||||
if (line.back() == ';' && line.find("(") == std::string::npos && line.find("=") == std::string::npos) {
|
||||
size_t spacePos = line.find(" ");
|
||||
std::string type = line.substr(0, spacePos);
|
||||
std::string name = trim(line.substr(spacePos + 1, line.size() - spacePos - 2));
|
||||
ClassField field; field.name = name; field.type = type; field.offset = currentOffset;
|
||||
if (type == "int" || type == "bool") currentOffset += 8;
|
||||
else if (type == "string") currentOffset += 8;
|
||||
classDef.fields.push_back(field);
|
||||
std::cout << " [CLASS] Field: " << type << " " << name << " @ offset " << field.offset << "\n";
|
||||
}
|
||||
else if (line.find("(") != std::string::npos && line.find("{") != std::string::npos) {
|
||||
size_t openParen = line.find("(");
|
||||
size_t spacePos = line.find(" ");
|
||||
std::string returnType = line.substr(0, spacePos);
|
||||
std::string methodName = trim(line.substr(spacePos + 1, openParen - spacePos - 1));
|
||||
|
||||
ClassMethod method;
|
||||
method.name = methodName; method.returnType = returnType;
|
||||
method.args = parseArgs(line);
|
||||
method.mangledName = className + "_" + methodName;
|
||||
classDef.methods.push_back(method);
|
||||
|
||||
Function newFunc;
|
||||
newFunc.name = method.mangledName;
|
||||
newFunc.returnType = returnType;
|
||||
newFunc.args = method.args;
|
||||
newFunc.args.insert(newFunc.args.begin(), "this");
|
||||
state.functions[method.mangledName] = newFunc;
|
||||
|
||||
// --- TUTAJ PARSUJEMY WNĘTRZE METODY ---
|
||||
state.currentFunction = &state.functions[method.mangledName];
|
||||
state.currentClass = className;
|
||||
state.classes[className] = classDef; // Rejestrujemy tymczasowo, by widzieć pola
|
||||
|
||||
i++; // Wejdź do środka metody
|
||||
while (i < lines.size()) {
|
||||
std::string mLine = trim(lines[i]);
|
||||
if (mLine == "}") break; // Koniec metody
|
||||
if (!mLine.empty() && mLine.rfind("//", 0) != 0) {
|
||||
parseInstruction(mLine, *state.currentFunction, state);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
state.currentFunction = nullptr;
|
||||
state.currentClass = "";
|
||||
}
|
||||
i++;
|
||||
}
|
||||
classDef.totalSize = currentOffset;
|
||||
state.classes[className] = classDef;
|
||||
std::cout << "[PARSER] Class " << className << " registered size=" << currentOffset << "\n";
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// GŁÓWNA PĘTLA
|
||||
// ===================================================================
|
||||
|
||||
void processSource(const std::string& src, CompilerState& state) {
|
||||
std::vector<std::string> lines;
|
||||
std::istringstream iss(src);
|
||||
std::string t;
|
||||
while (std::getline(iss, t)) lines.push_back(trim(t));
|
||||
std::string line;
|
||||
|
||||
for (size_t i = 0; i < lines.size(); i++) {
|
||||
std::string line = trim(lines[i]);
|
||||
while (std::getline(iss, line)) {
|
||||
line = trim(line);
|
||||
if (line.empty() || line.substr(0, 2) == "//" || line[0] == '#') continue;
|
||||
|
||||
if (line.find("} else") != std::string::npos) {
|
||||
handleBlockClose(state, lines, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.rfind("class ", 0) == 0 && line.find("{") != std::string::npos) {
|
||||
parseClassDefinition(lines, i, state);
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- 1. DEFINICJA FUNKCJI ---
|
||||
// Warunki: zaczyna się od typu, ma '(', ma '{' i NIE ma '=' (żeby nie mylić ze zmienną)
|
||||
bool startsWithType = (line.rfind("int ", 0) == 0 || line.rfind("void ", 0) == 0 || line.rfind("bool ", 0) == 0);
|
||||
if (startsWithType && line.find("(") != std::string::npos && line.find("{") != std::string::npos && line.find("=") == std::string::npos) {
|
||||
|
||||
size_t openParen = line.find('(');
|
||||
std::string typeRaw = line.substr(0, line.find(' '));
|
||||
std::string nameRaw = line.substr(typeRaw.length(), openParen - typeRaw.length());
|
||||
@@ -623,21 +48,200 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
newFunc.name = funcName;
|
||||
newFunc.returnType = typeRaw;
|
||||
newFunc.args = parseArgs(line);
|
||||
|
||||
state.functions[funcName] = newFunc;
|
||||
state.currentFunction = &state.functions[funcName];
|
||||
|
||||
std::cout << "[PARSER] New Function: " << funcName << "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- 2. ZAMYKANIE BLOKU '}' ---
|
||||
if (line == "}") {
|
||||
handleBlockClose(state, lines, i);
|
||||
// Najpierw sprawdzamy, czy zamykamy IF-a (czy jest coś na stosie bloków)
|
||||
if (!state.blockStack.empty()) {
|
||||
std::string label = state.blockStack.top();
|
||||
state.blockStack.pop();
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, label, "", "" });
|
||||
}
|
||||
std::cout << " [PARSER] } End IF block -> " << label << "\n";
|
||||
}
|
||||
else {
|
||||
// Jeśli stos pusty, to koniec funkcji
|
||||
state.currentFunction = nullptr;
|
||||
std::cout << " [PARSER] } End Function\n";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- JESTEŚMY W ŚRODKU FUNKCJI ---
|
||||
if (state.currentFunction) {
|
||||
parseInstruction(line, *state.currentFunction, state);
|
||||
Function& f = *state.currentFunction;
|
||||
|
||||
// A. RETURN
|
||||
if (line.substr(0, 6) == "return") {
|
||||
std::string val = trim(line.substr(6));
|
||||
if (!val.empty() && val.back() == ';') val.pop_back();
|
||||
f.instructions.push_back({ OpType::RETURN, val, "", "" });
|
||||
std::cout << " [PARSER] Return: " << val << "\n";
|
||||
}
|
||||
// B. PRINT
|
||||
else if (line.substr(0, 5) == "print") {
|
||||
size_t start = line.find('(') + 1;
|
||||
size_t end = line.find(')');
|
||||
if (start != std::string::npos && end != std::string::npos) {
|
||||
std::string arg = trim(line.substr(start, end - start));
|
||||
|
||||
// Czy to bezpośredni napis? np. print("Hello")
|
||||
if (arg.size() >= 2 && arg.front() == '"' && arg.back() == '"') {
|
||||
std::string content = arg.substr(1, arg.size() - 2);
|
||||
|
||||
// Rejestrujemy
|
||||
std::string label;
|
||||
if (state.stringLiterals.count(content)) {
|
||||
label = state.stringLiterals[content];
|
||||
}
|
||||
else {
|
||||
label = "str_" + std::to_string(state.stringCounter++);
|
||||
state.stringLiterals[content] = label;
|
||||
}
|
||||
|
||||
// Dajemy znać generatorowi, że to typ STRING
|
||||
f.instructions.push_back({ OpType::PRINT, label, "STRING", "" });
|
||||
}
|
||||
else {
|
||||
// Zwykła zmienna (int lub string - generator musi zgadnąć lub my musimy wiedzieć)
|
||||
// Na razie załóżmy, że jeśli zmienna ma w nazwie "msg" lub "txt", to string
|
||||
// (To hack, w przyszłości dodamy tabelę typów zmiennych)
|
||||
f.instructions.push_back({ OpType::PRINT, arg, "VAR", "" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// C. IF STATEMENT
|
||||
else if (line.substr(0, 2) == "if") {
|
||||
size_t openParen = line.find("(");
|
||||
size_t closeParen = line.find(")");
|
||||
if (openParen != std::string::npos && closeParen > openParen) {
|
||||
std::string condition = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
||||
std::string labelName = "L_" + std::to_string(state.labelCounter++);
|
||||
|
||||
// Skok warunkowy
|
||||
f.instructions.push_back({ OpType::JMP_FALSE, labelName, condition, "" });
|
||||
state.blockStack.push(labelName);
|
||||
|
||||
std::cout << " [PARSER] IF (" << condition << ") -> Jump to " << labelName << "\n";
|
||||
}
|
||||
}
|
||||
// D. PRZYPISANIE ZMIENNEJ (LUB DEKLARACJA)
|
||||
// np. "int a = 5;" LUB "a = b + c;"
|
||||
else if (line.find("=") != std::string::npos) {
|
||||
size_t eqPos = line.find('=');
|
||||
std::string leftSide = trim(line.substr(0, eqPos));
|
||||
std::string rightSide = trim(line.substr(eqPos + 1));
|
||||
|
||||
bool isStringDecl = false; // Flaga, czy to string
|
||||
if (!rightSide.empty() && rightSide.back() == ';') rightSide.pop_back();
|
||||
|
||||
// Obsługa nazwy zmiennej (usuwanie "int ", "bool ")
|
||||
std::string varName = leftSide;
|
||||
if (leftSide.rfind("int ", 0) == 0) varName = trim(leftSide.substr(4));
|
||||
else if (leftSide.rfind("bool ", 0) == 0) varName = trim(leftSide.substr(5));
|
||||
else if (leftSide.rfind("string ", 0) == 0) { // NOWOŚĆ
|
||||
varName = trim(leftSide.substr(7));
|
||||
isStringDecl = true;
|
||||
}
|
||||
|
||||
|
||||
// 1. Czy to wywołanie funkcji? int x = func();
|
||||
if (rightSide.find("(") != std::string::npos && rightSide.find(")") != std::string::npos) {
|
||||
size_t open = rightSide.find('(');
|
||||
std::string funcName = trim(rightSide.substr(0, open));
|
||||
std::string argsContent = rightSide.substr(open + 1, rightSide.find(')') - open - 1);
|
||||
|
||||
// CALL func
|
||||
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
|
||||
// ASSIGN result (RAX) to variable
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, "RAX", "" });
|
||||
std::cout << " [PARSER] Call & Assign: " << varName << " = " << funcName << "()\n";
|
||||
}
|
||||
// 2. Czy to dodawanie? a + b
|
||||
else if (rightSide.find("+") != std::string::npos) {
|
||||
size_t opPos = rightSide.find("+");
|
||||
std::string a = trim(rightSide.substr(0, opPos));
|
||||
std::string b = trim(rightSide.substr(opPos + 1));
|
||||
f.instructions.push_back({ OpType::ADD, varName, a, b });
|
||||
}
|
||||
// 3. NOWOŚĆ: Czy to odejmowanie? a - b
|
||||
else if (rightSide.find("-") != std::string::npos) {
|
||||
size_t opPos = rightSide.find("-");
|
||||
std::string a = trim(rightSide.substr(0, opPos));
|
||||
std::string b = trim(rightSide.substr(opPos + 1));
|
||||
f.instructions.push_back({ OpType::SUB, varName, a, b }); // <--- Używamy SUB
|
||||
}
|
||||
// 4. NOWOŚĆ: Czy to mnożenie? a * b
|
||||
else if (rightSide.find("*") != std::string::npos) {
|
||||
size_t opPos = rightSide.find("*");
|
||||
std::string a = trim(rightSide.substr(0, opPos));
|
||||
std::string b = trim(rightSide.substr(opPos + 1));
|
||||
f.instructions.push_back({ OpType::MUL, varName, a, b }); // <--- Używamy MUL
|
||||
}
|
||||
else if (rightSide.find("/") != std::string::npos) {
|
||||
size_t opPos = rightSide.find("/");
|
||||
std::string a = trim(rightSide.substr(0, opPos));
|
||||
std::string b = trim(rightSide.substr(opPos + 1));
|
||||
f.instructions.push_back({ OpType::DIV, varName, a, b });
|
||||
}
|
||||
// MODULO: a % b
|
||||
else if (rightSide.find("%") != std::string::npos) {
|
||||
size_t opPos = rightSide.find("%");
|
||||
std::string a = trim(rightSide.substr(0, opPos));
|
||||
std::string b = trim(rightSide.substr(opPos + 1));
|
||||
f.instructions.push_back({ OpType::MOD, varName, a, b });
|
||||
}
|
||||
// 3. Czy to porównanie? a == b (Ważne: == może być w IFie, ale tu jesteśmy w linii z '=')
|
||||
// UWAGA: To rzadkie w C++ (bool x = a == b), ale obsłużmy proste przypisanie wartości logicznej
|
||||
else if (rightSide.find("==") != std::string::npos) {
|
||||
size_t opPos = rightSide.find("==");
|
||||
std::string a = trim(rightSide.substr(0, opPos));
|
||||
std::string b = trim(rightSide.substr(opPos + 2));
|
||||
f.instructions.push_back({ OpType::EQ, varName, a, b });
|
||||
}
|
||||
else if (rightSide.size() >= 2 && rightSide.front() == '"' && rightSide.back() == '"')
|
||||
{
|
||||
{
|
||||
// Wyciągamy treść bez cudzysłowów
|
||||
std::string content = rightSide.substr(1, rightSide.size() - 2);
|
||||
|
||||
// Rejestrujemy stringa w sekcji danych, jeśli jeszcze go nie ma
|
||||
std::string label;
|
||||
if (state.stringLiterals.count(content)) {
|
||||
label = state.stringLiterals[content];
|
||||
}
|
||||
else {
|
||||
label = "str_" + std::to_string(state.stringCounter++);
|
||||
state.stringLiterals[content] = label;
|
||||
}
|
||||
|
||||
// Generujemy instrukcję przypisania ADRESU etykiety do zmiennej
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, label, "STRING" });
|
||||
}
|
||||
}
|
||||
// 4. Zwykłe przypisanie: a = 5
|
||||
else {
|
||||
f.instructions.push_back({ OpType::ASSIGN, varName, rightSide, "" });
|
||||
}
|
||||
}
|
||||
// E. SAMODZIELNE WYWOŁANIE FUNKCJI (bez =)
|
||||
// np. func();
|
||||
else if (line.find("(") != std::string::npos && line.find(")") != std::string::npos) {
|
||||
size_t open = line.find('(');
|
||||
std::string funcName = trim(line.substr(0, open));
|
||||
std::string argsContent = line.substr(open + 1, line.find(')') - open - 1);
|
||||
|
||||
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
|
||||
std::cout << " [PARSER] Call void: " << funcName << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void calculateExpressions(CompilerState& state) {}
|
||||
|
||||
@@ -3,18 +3,13 @@
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <unordered_set>
|
||||
#include <regex>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static std::string ltrim(std::string s) {
|
||||
const size_t first = s.find_first_not_of(" \t");
|
||||
if (first == std::string::npos) return "";
|
||||
return s.substr(first);
|
||||
}
|
||||
|
||||
static std::string loadFileContent(const std::string& path) {
|
||||
std::ifstream in(path, std::ios::binary);
|
||||
// Funkcja pomocnicza do wczytania pliku
|
||||
std::string loadFileContent(const std::string& path) {
|
||||
std::ifstream in(path);
|
||||
if (!in) {
|
||||
std::cerr << "[PREPROCESSOR] Error: Could not open included file: " << path << "\n";
|
||||
return "";
|
||||
@@ -22,109 +17,63 @@ static std::string loadFileContent(const std::string& path) {
|
||||
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
// Próbuje sparsowaæ #include "x" albo #include <x>
|
||||
// Zwraca: true jeœli linia to include i uda³o siê wyci¹gn¹æ œcie¿kê.
|
||||
static bool parseIncludeLine(const std::string& line, std::string& includePath, bool& isStdLib) {
|
||||
includePath.clear();
|
||||
isStdLib = false;
|
||||
|
||||
std::string t = ltrim(line);
|
||||
if (t.rfind("#include", 0) != 0) return false;
|
||||
|
||||
const size_t openQuote = t.find('"');
|
||||
const size_t closeQuote = t.rfind('"');
|
||||
const size_t openAngle = t.find('<');
|
||||
const size_t closeAngle = t.rfind('>');
|
||||
|
||||
if (openQuote != std::string::npos && closeQuote != std::string::npos && closeQuote > openQuote) {
|
||||
includePath = t.substr(openQuote + 1, closeQuote - openQuote - 1);
|
||||
isStdLib = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (openAngle != std::string::npos && closeAngle != std::string::npos && closeAngle > openAngle) {
|
||||
includePath = t.substr(openAngle + 1, closeAngle - openAngle - 1);
|
||||
isStdLib = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// To jest #include ale w z³ym formacie (np. brak cudzys³owu / nawiasów)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Wewnêtrzna funkcja z guardem na rekurencjê.
|
||||
static std::string preprocessSourceImpl(
|
||||
const std::string& src,
|
||||
const std::string& projectDir,
|
||||
const std::string& compilerDir,
|
||||
std::unordered_set<std::string>& includeGuard
|
||||
) {
|
||||
std::string preprocessSource(const std::string& src, const std::string& projectDir, const std::string& compilerDir) {
|
||||
std::istringstream iss(src);
|
||||
std::string line;
|
||||
std::stringstream output;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
// Szukamy: #include "..." lub #include <...>
|
||||
// U¿ywamy prostego find, ¿eby by³o szybko
|
||||
std::string trimLine = line;
|
||||
// Usuwamy bia³e znaki z pocz¹tku
|
||||
size_t first = trimLine.find_first_not_of(" \t");
|
||||
if (first != std::string::npos) trimLine = trimLine.substr(first);
|
||||
|
||||
if (trimLine.rfind("#include", 0) == 0) {
|
||||
// Mamy include!
|
||||
size_t openQuote = trimLine.find('"');
|
||||
size_t closeQuote = trimLine.rfind('"');
|
||||
size_t openAngle = trimLine.find('<');
|
||||
size_t closeAngle = trimLine.rfind('>');
|
||||
|
||||
std::string includePath;
|
||||
bool isStdLib = false;
|
||||
|
||||
const bool isIncludeLine = parseIncludeLine(line, includePath, isStdLib);
|
||||
|
||||
if (!isIncludeLine) {
|
||||
output << line << "\n";
|
||||
continue;
|
||||
// Wersja: #include "plik.pcc"
|
||||
if (openQuote != std::string::npos && closeQuote > openQuote) {
|
||||
includePath = trimLine.substr(openQuote + 1, closeQuote - openQuote - 1);
|
||||
}
|
||||
// Wersja: #include <plik.pcc>
|
||||
else if (openAngle != std::string::npos && closeAngle > openAngle) {
|
||||
includePath = trimLine.substr(openAngle + 1, closeAngle - openAngle - 1);
|
||||
isStdLib = true;
|
||||
}
|
||||
|
||||
if (includePath.empty()) {
|
||||
output << "\n// [PREPROCESSOR] Invalid include (no path): " << line << "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// Z³ó¿ fullPath
|
||||
fs::path fullPath;
|
||||
if (!includePath.empty()) {
|
||||
std::string fullPath;
|
||||
if (isStdLib) {
|
||||
fullPath = fs::path(compilerDir) / "std" / includePath;
|
||||
std::cout << "[PREPROCESSOR] Including STD lib: " << fullPath.string() << "\n";
|
||||
fullPath = compilerDir + "/std/" + includePath;
|
||||
std::cout << "[PREPROCESSOR] Including STD lib: " << fullPath << "\n";
|
||||
}
|
||||
else {
|
||||
if (projectDir.empty()) fullPath = fs::path(includePath);
|
||||
else fullPath = fs::path(projectDir) / includePath;
|
||||
std::cout << "[PREPROCESSOR] Including local file: " << fullPath.string() << "\n";
|
||||
// Plik lokalny: Szukamy w folderze projektu
|
||||
if (projectDir.empty()) fullPath = includePath;
|
||||
else fullPath = projectDir + "/" + includePath;
|
||||
std::cout << "[PREPROCESSOR] Including local file: " << fullPath << "\n";
|
||||
}
|
||||
|
||||
// Normalizacja œcie¿ki (jeœli siê da)
|
||||
std::string guardKey;
|
||||
try {
|
||||
guardKey = fs::weakly_canonical(fullPath).string();
|
||||
}
|
||||
catch (...) {
|
||||
guardKey = fullPath.lexically_normal().string();
|
||||
}
|
||||
|
||||
// Guard przeciw include-loop oraz wielokrotnemu includowaniu
|
||||
if (includeGuard.find(guardKey) != includeGuard.end()) {
|
||||
output << "\n// [PREPROCESSOR] Skipped include (already included): " << includePath << "\n";
|
||||
continue;
|
||||
}
|
||||
includeGuard.insert(guardKey);
|
||||
|
||||
// Wczytaj + przetwórz rekurencyjnie
|
||||
std::string content = loadFileContent(fullPath.string());
|
||||
if (content.empty()) {
|
||||
output << "\n// [PREPROCESSOR] FAILED INCLUDE: " << includePath << "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string processedContent = preprocessSourceImpl(content, projectDir, compilerDir, includeGuard);
|
||||
std::string content = loadFileContent(fullPath);
|
||||
std::string processedContent = preprocessSource(content, projectDir, compilerDir);
|
||||
|
||||
output << "\n// --- BEGIN INCLUDE: " << includePath << " ---\n";
|
||||
output << processedContent;
|
||||
output << "\n// --- END INCLUDE ---\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
output << line << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
return output.str();
|
||||
}
|
||||
|
||||
std::string preprocessSource(const std::string& src, const std::string& projectDir, const std::string& compilerDir) {
|
||||
std::unordered_set<std::string> includeGuard;
|
||||
return preprocessSourceImpl(src, projectDir, compilerDir, includeGuard);
|
||||
}
|
||||
|
||||
@@ -2,12 +2,7 @@
|
||||
#define PREPROCESSOR_H
|
||||
|
||||
#include <string>
|
||||
std::string preprocessSource(
|
||||
const std::string& src,
|
||||
const std::string& projectDir,
|
||||
const std::string& compilerDir
|
||||
);
|
||||
|
||||
std::string preprocessSource(const std::string& src, const std::string& projectDir, const std::string& compilerDir);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# PCC Compiler (My C++ Compiler)
|
||||
|
||||

|
||||

|
||||

|
||||

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