Compare commits
4 Commits
0d016f8ae9
...
V0.0.7-bet
| Author | SHA1 | Date | |
|---|---|---|---|
| 7cef3a0bce | |||
|
|
fdcbc31cca | ||
|
|
4f1a21e9c2 | ||
|
|
5edf6ed382 |
@@ -41,19 +41,18 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
result += "global main\n";
|
||||
result += "extern printf\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 += " 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) {
|
||||
@@ -88,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;
|
||||
@@ -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";
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
29
README.md
Normal file
29
README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# PCC Compiler (My C++ Compiler)
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
## Docs
|
||||
https://nodrop.xyz/docs/docs.html
|
||||
|
||||
**PCC Compiler** is a custom programming language compiler built from scratch in C++. It translates PCC code into x64 Assembly (NASM), which is then linked into a standalone Windows executable.
|
||||
|
||||
## 🚀 Features
|
||||
- **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.
|
||||
2. Unzip the archive to C:/PCC/
|
||||
3. Run `start.bat` as Administrator.
|
||||
4. Compile your code: `PCC.exe code.pcc`.
|
||||
|
||||
---
|
||||
*Created by Michał Lewandowski*
|
||||
Reference in New Issue
Block a user