Compare commits
3 Commits
V0.0.6-bet
...
fdcbc31cca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fdcbc31cca | ||
|
|
4f1a21e9c2 | ||
|
|
5edf6ed382 |
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
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*
|
||||||
35
push.bat
Normal file
35
push.bat
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
@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: "
|
||||||
|
|
||||||
|
:: Jeśli użytkownik nic nie wpisze, ustaw domyślną
|
||||||
|
if "%commit_msg%"=="" set commit_msg="Auto update"
|
||||||
|
|
||||||
|
:: 3. Robimy commit
|
||||||
|
echo.
|
||||||
|
echo [2/3] Committing...
|
||||||
|
git commit -m "%commit_msg%"
|
||||||
|
|
||||||
|
:: 4. Wysyłamy (używamy origin, który już skonfigurowałeś)
|
||||||
|
echo.
|
||||||
|
echo [3/3] Pushing to Gitea...
|
||||||
|
git push origin main
|
||||||
|
|
||||||
|
:: Jeśli push się nie uda (np. konflikt), spróbuj wymusić
|
||||||
|
if %errorlevel% neq 0 (
|
||||||
|
echo.
|
||||||
|
echo [WARNING] Standard push failed. Trying force push...
|
||||||
|
git push origin main --force
|
||||||
|
)
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo DONE!
|
||||||
|
pause
|
||||||
Reference in New Issue
Block a user