Compare commits
5 Commits
V0.0.5
...
V0.0.7-bet
| Author | SHA1 | Date | |
|---|---|---|---|
| 7cef3a0bce | |||
|
|
fdcbc31cca | ||
|
|
4f1a21e9c2 | ||
|
|
5edf6ed382 | ||
|
|
99b9ae450e |
@@ -41,18 +41,18 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
result += "global main\n";
|
||||
result += "extern printf\n";
|
||||
result += "extern getchar\n";
|
||||
result += "extern _getch\n";
|
||||
result += "extern rand\n";
|
||||
result += "extern srand\n";
|
||||
result += "extern time\n";
|
||||
result += "section .data\n";
|
||||
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"; // NOWOŒÆ: Format dla stringów
|
||||
result += " fmt_str db '%s', 10, 0\n"; // Format dla stringów
|
||||
|
||||
// --- WYPISYWANIE STRINGÓW ---
|
||||
for (const auto& pair : state.stringLiterals) {
|
||||
// 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";
|
||||
}
|
||||
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) {
|
||||
@@ -87,7 +87,9 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
instr.type == OpType::ADD ||
|
||||
instr.type == OpType::EQ ||
|
||||
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") {
|
||||
stackMap[instr.arg1] = currentStack;
|
||||
@@ -186,7 +188,10 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
}
|
||||
case OpType::RETURN: {
|
||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov eax, " + val + " ; return value\n";
|
||||
if (val.empty() || val == ";");
|
||||
else {
|
||||
result += " mov eax, " + val + " ; return value\n";
|
||||
}
|
||||
result += " leave\n";
|
||||
result += " ret\n";
|
||||
break;
|
||||
@@ -209,9 +214,21 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
// 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"; // Czeka na enter
|
||||
break; // Wychodzimy, ¿eby nie robiæ standardowego call
|
||||
result += " call getchar\n";
|
||||
break;
|
||||
}
|
||||
|
||||
// 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(',');
|
||||
@@ -248,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";
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,4 +328,4 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ enum class OpType {
|
||||
LABEL, // miejsce skoku
|
||||
CALL, // wywo³anie funkcji
|
||||
RETURN, // return x
|
||||
DIV, // dzielenie
|
||||
MOD, // Reszta z dzelenia (%)
|
||||
NOP // pusta instrukcja
|
||||
};
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
// --- 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('(');
|
||||
@@ -56,7 +55,6 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
std::cout << "[PARSER] New Function: " << funcName << "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- 2. ZAMYKANIE BLOKU '}' ---
|
||||
if (line == "}") {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// --- JESTEŚMY W ŚRODKU FUNKCJI ---
|
||||
if (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));
|
||||
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) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# PCC Compiler (My C++ Compiler)
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
@@ -13,9 +13,11 @@ https://nodrop.xyz/docs/docs.html
|
||||
- **Custom Syntax**: C-like syntax easy for beginners.
|
||||
- **Variables**: Support for `int`, `bool` and `string`.
|
||||
- **include files**: you can include base files from compiler `#include <main.pcc>` or your own files `#include "myfile.pcc"`.
|
||||
- **KeyBoard and Inputs Support**: now you can control keyboard inputs using `#include <Input.pcc>`.
|
||||
- **Control Flow**: `if` statements support.
|
||||
- **Functions**: Define and call `void` functions.
|
||||
- **Native Compilation**: Compiles directly to x64 machine code.
|
||||
- **More Informations**: For more informations check PCC Docs.
|
||||
|
||||
## 🛠️ Usage
|
||||
1. Download the latest release from the [Releases](../../releases) page.
|
||||
|
||||
Reference in New Issue
Block a user