added Random Int Generator

This commit is contained in:
mmichlol
2026-02-07 15:53:25 +01:00
parent 99b9ae450e
commit 5edf6ed382
4 changed files with 103 additions and 12 deletions

View File

@@ -41,19 +41,18 @@ std::string generateAssembly(const CompilerState& state) {
result += "global main\n"; result += "global main\n";
result += "extern printf\n"; result += "extern printf\n";
result += "extern getchar\n"; result += "extern getchar\n";
result += "extern _getch\n"; // ZMIANA: Dodajemy _getch (zamiast lub obok getchar) result += "extern _getch\n";
result += "extern rand\n";
result += "extern srand\n";
result += "extern time\n";
result += "section .data\n"; result += "section .data\n";
result += " fmt db '%d', 10, 0\n"; result += " fmt db '%d', 10, 0\n";
result += "section .data\n"; result += "section .data\n";
result += " fmt_int db '%d', 10, 0\n"; // Format dla liczb result += " fmt_int db '%d', 10, 0\n"; // Format dla liczb
result += " fmt_str db '%s', 10, 0\n"; // NOWOŒÆ: Format dla stringów result += " fmt_str db '%s', 10, 0\n"; // Format dla stringów
// --- WYPISYWANIE STRINGÓW --- // --- WYPISYWANIE STRINGÓW ---
for (const auto& pair : state.stringLiterals) { for (const auto& pair : state.stringLiterals) { result += " " + pair.second + " db '" + pair.first + "', 0\n"; }
// Nazwa etykiety: db 'Tresc', 0
// Uwaga: ASM nie lubi pewnych znaków, ale zak³adamy proste litery
result += " " + pair.second + " db '" + pair.first + "', 0\n";
}
result += "section .text\n\n"; result += "section .text\n\n";
for (const auto& pair : state.functions) { for (const auto& pair : state.functions) {
@@ -88,7 +87,9 @@ std::string generateAssembly(const CompilerState& state) {
instr.type == OpType::ADD || instr.type == OpType::ADD ||
instr.type == OpType::EQ || instr.type == OpType::EQ ||
instr.type == OpType::SUB || instr.type == OpType::SUB ||
instr.type == OpType::MUL); instr.type == OpType::MUL ||
instr.type == OpType::DIV ||
instr.type == OpType::MOD);
if (isWriteOp && stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") { if (isWriteOp && stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
stackMap[instr.arg1] = currentStack; stackMap[instr.arg1] = currentStack;
@@ -264,9 +265,59 @@ std::string generateAssembly(const CompilerState& state) {
} }
} }
// ... wewn¹trz case OpType::CALL ...
if (instr.arg1 == "sys_seed") {
result += " mov rcx, 0\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"; result += " call " + instr.arg1 + "\n";
break; 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;
}
} }
} }
@@ -277,4 +328,4 @@ std::string generateAssembly(const CompilerState& state) {
} }
return result; return result;
} }

View File

@@ -19,6 +19,8 @@ enum class OpType {
LABEL, // miejsce skoku LABEL, // miejsce skoku
CALL, // wywo³anie funkcji CALL, // wywo³anie funkcji
RETURN, // return x RETURN, // return x
DIV, // dzielenie
MOD, // Reszta z dzelenia (%)
NOP // pusta instrukcja NOP // pusta instrukcja
}; };

View File

@@ -37,7 +37,6 @@ void processSource(const std::string& src, CompilerState& state) {
// --- 1. DEFINICJA FUNKCJI --- // --- 1. DEFINICJA FUNKCJI ---
// Warunki: zaczyna się od typu, ma '(', ma '{' i NIE ma '=' (żeby nie mylić ze zmienną) // 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); 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) { if (startsWithType && line.find("(") != std::string::npos && line.find("{") != std::string::npos && line.find("=") == std::string::npos) {
size_t openParen = line.find('('); size_t openParen = line.find('(');
@@ -56,7 +55,6 @@ void processSource(const std::string& src, CompilerState& state) {
std::cout << "[PARSER] New Function: " << funcName << "\n"; std::cout << "[PARSER] New Function: " << funcName << "\n";
continue; continue;
} }
// --- 2. ZAMYKANIE BLOKU '}' --- // --- 2. ZAMYKANIE BLOKU '}' ---
if (line == "}") { if (line == "}") {
// Najpierw sprawdzamy, czy zamykamy IF-a (czy jest coś na stosie bloków) // Najpierw sprawdzamy, czy zamykamy IF-a (czy jest coś na stosie bloków)
@@ -75,7 +73,6 @@ void processSource(const std::string& src, CompilerState& state) {
} }
continue; continue;
} }
// --- JESTEŚMY W ŚRODKU FUNKCJI --- // --- JESTEŚMY W ŚRODKU FUNKCJI ---
if (state.currentFunction) { if (state.currentFunction) {
Function& f = *state.currentFunction; Function& f = *state.currentFunction;
@@ -188,6 +185,19 @@ void processSource(const std::string& src, CompilerState& state) {
std::string b = trim(rightSide.substr(opPos + 1)); std::string b = trim(rightSide.substr(opPos + 1));
f.instructions.push_back({ OpType::MUL, varName, a, b }); // <--- Używamy MUL 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 '=') // 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 // 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) { else if (rightSide.find("==") != std::string::npos) {

28
push.bat Normal file
View File

@@ -0,0 +1,28 @@
@echo off
echo --- AUTO GIT PUSHER ---
echo.
:: 1. Dodajemy wszystkie pliki
echo [1/3] Adding files...
git add .
:: 2. Pytamy o nazwę commita
echo.
set /p commit_msg="Enter commit message: "
:: Sprawdzamy czy wpisano wiadomość, jak nie to dajemy domyślną
if "%commit_msg%"=="" set commit_msg="Auto update"
:: 3. Robimy commit
echo.
echo [2/3] Committing with message: "%commit_msg%"...
git commit -m "%commit_msg%"
:: 4. Wysyłamy (zakładając, że masz już ustawiony 'origin')
echo.
echo [3/3] Pushing to repository...
git push
echo.
echo DONE!
pause