Compare commits
1 Commits
fdcbc31cca
...
V0.0.3-bet
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c4f714565 |
@@ -22,14 +22,12 @@
|
|||||||
<ClCompile Include="codegen.cpp" />
|
<ClCompile Include="codegen.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="parser.cpp" />
|
<ClCompile Include="parser.cpp" />
|
||||||
<ClCompile Include="preprocessor.cpp" />
|
|
||||||
<ClCompile Include="utils.cpp" />
|
<ClCompile Include="utils.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="codegen.h" />
|
<ClInclude Include="codegen.h" />
|
||||||
<ClInclude Include="compiler_types.h" />
|
<ClInclude Include="compiler_types.h" />
|
||||||
<ClInclude Include="parser.h" />
|
<ClInclude Include="parser.h" />
|
||||||
<ClInclude Include="preprocessor.h" />
|
|
||||||
<ClInclude Include="utils.h" />
|
<ClInclude Include="utils.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<PropertyGroup Label="Globals">
|
<PropertyGroup Label="Globals">
|
||||||
|
|||||||
@@ -27,9 +27,6 @@
|
|||||||
<ClCompile Include="codegen.cpp">
|
<ClCompile Include="codegen.cpp">
|
||||||
<Filter>Pliki źródłowe</Filter>
|
<Filter>Pliki źródłowe</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="preprocessor.cpp">
|
|
||||||
<Filter>Pliki źródłowe</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="utils.h">
|
<ClInclude Include="utils.h">
|
||||||
@@ -44,8 +41,5 @@
|
|||||||
<ClInclude Include="compiler_types.h">
|
<ClInclude Include="compiler_types.h">
|
||||||
<Filter>Pliki nagłówkowe</Filter>
|
<Filter>Pliki nagłówkowe</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="preprocessor.h">
|
|
||||||
<Filter>Pliki nagłówkowe</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -40,19 +40,9 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
std::string result;
|
std::string result;
|
||||||
result += "global main\n";
|
result += "global main\n";
|
||||||
result += "extern printf\n";
|
result += "extern printf\n";
|
||||||
result += "extern getchar\n";
|
result += "extern getchar\n"; // <--- DODAJ TO
|
||||||
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 += " fmt_int db '%d', 10, 0\n"; // Format dla liczb
|
|
||||||
result += " fmt_str db '%s', 10, 0\n"; // Format dla stringów
|
|
||||||
|
|
||||||
// --- WYPISYWANIE STRINGÓW ---
|
|
||||||
for (const auto& pair : state.stringLiterals) { 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) {
|
||||||
@@ -81,35 +71,19 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
// 2. INSTRUKCJE
|
// 2. INSTRUKCJE
|
||||||
for (const auto& instr : func.instructions) {
|
for (const auto& instr : func.instructions) {
|
||||||
|
|
||||||
// Rezerwacja miejsca dla nowych zmiennych (wynikowych)
|
// Rezerwacja miejsca dla nowych zmiennych
|
||||||
// Dodajemy tu OpType::SUB i OpType::MUL
|
if ((instr.type == OpType::ASSIGN || instr.type == OpType::ADD || instr.type == OpType::EQ)
|
||||||
bool isWriteOp = (instr.type == OpType::ASSIGN ||
|
&& stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
|
||||||
instr.type == OpType::ADD ||
|
|
||||||
instr.type == OpType::EQ ||
|
|
||||||
instr.type == OpType::SUB ||
|
|
||||||
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;
|
stackMap[instr.arg1] = currentStack;
|
||||||
currentStack += 8;
|
currentStack += 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (instr.type) {
|
switch (instr.type) {
|
||||||
case OpType::ASSIGN: {
|
case OpType::ASSIGN: {
|
||||||
std::string src = instr.arg2;
|
std::string src = getVarLocation(instr.arg2, stackMap);
|
||||||
if (instr.arg3 == "STRING") {
|
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||||
result += " lea rax, [rel " + src + "]\n";
|
result += " mov eax, " + src + "\n";
|
||||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
result += " mov " + dst + ", eax\n";
|
||||||
result += " mov qword " + dst + ", rax\n";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::string srcLoc = getVarLocation(instr.arg2, stackMap);
|
|
||||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
|
||||||
result += " mov eax, " + srcLoc + "\n";
|
|
||||||
result += " mov " + dst + ", eax\n";
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OpType::ADD: {
|
case OpType::ADD: {
|
||||||
@@ -121,30 +95,6 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
result += " mov " + dst + ", eax\n";
|
result += " mov " + dst + ", eax\n";
|
||||||
break;
|
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"; // 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::EQ: {
|
case OpType::EQ: {
|
||||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||||
@@ -188,47 +138,25 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
}
|
}
|
||||||
case OpType::RETURN: {
|
case OpType::RETURN: {
|
||||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||||
if (val.empty() || val == ";");
|
result += " mov eax, " + val + " ; return value\n";
|
||||||
else {
|
|
||||||
result += " mov eax, " + val + " ; return value\n";
|
|
||||||
}
|
|
||||||
result += " leave\n";
|
result += " leave\n";
|
||||||
result += " ret\n";
|
result += " ret\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OpType::PRINT: {
|
case OpType::PRINT: {
|
||||||
if (instr.arg2 == "STRING") {
|
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||||
result += " lea rdx, [rel " + instr.arg1 + "]\n";
|
result += " mov edx, " + val + "\n";
|
||||||
result += " lea rcx, [rel fmt_str]\n";
|
result += " lea rcx, [rel fmt]\n";
|
||||||
result += " call printf\n";
|
result += " call printf\n";
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
|
||||||
result += " mov edx, " + val + "\n";
|
|
||||||
result += " lea rcx, [rel fmt_int]\n";
|
|
||||||
result += " call printf\n";
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OpType::CALL: {
|
case OpType::CALL: {
|
||||||
// Parsowanie argumentów
|
// Parsowanie argumentów
|
||||||
std::string argsRaw = instr.arg2;
|
std::string argsRaw = instr.arg2;
|
||||||
std::vector<std::string> callArgs;
|
std::vector<std::string> callArgs;
|
||||||
// --- SPECJALNE FUNKCJE SYSTEMOWE ---
|
|
||||||
|
|
||||||
// 1. input() - czeka na ENTER (stare)
|
|
||||||
if (instr.arg1 == "input") {
|
if (instr.arg1 == "input") {
|
||||||
result += " call getchar\n";
|
result += " call getchar\n"; // Czeka na enter
|
||||||
break;
|
break; // Wychodzimy, ¿eby nie robiæ standardowego call
|
||||||
}
|
|
||||||
|
|
||||||
// 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()) {
|
if (!argsRaw.empty()) {
|
||||||
size_t comma = argsRaw.find(',');
|
size_t comma = argsRaw.find(',');
|
||||||
@@ -265,59 +193,9 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -328,4 +206,4 @@ std::string generateAssembly(const CompilerState& state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ 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
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -53,9 +51,6 @@ struct CompilerState {
|
|||||||
std::stack<std::string> loopStack; // Do break/continue (przysz³oœciowo)
|
std::stack<std::string> loopStack; // Do break/continue (przysz³oœciowo)
|
||||||
|
|
||||||
std::stack<std::string> blockStack;
|
std::stack<std::string> blockStack;
|
||||||
|
|
||||||
std::map<std::string, std::string> stringLiterals;
|
|
||||||
int stringCounter = 0; // Licznik do generowania nazw str_1, str_2...
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,24 +2,14 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdlib>
|
#include <cstdlib> // do std::system
|
||||||
#include <filesystem>
|
|
||||||
#include <windows.h>
|
|
||||||
#include "compiler_types.h"
|
#include "compiler_types.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "codegen.h"
|
#include "codegen.h"
|
||||||
#include "preprocessor.h"
|
|
||||||
|
|
||||||
std::string getExecutablePath() {
|
|
||||||
char buffer[MAX_PATH];
|
|
||||||
GetModuleFileNameA(NULL, buffer, MAX_PATH);
|
|
||||||
std::string::size_type pos = std::string(buffer).find_last_of("\\/");
|
|
||||||
return std::string(buffer).substr(0, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
std::string inputFile, outputName;
|
std::string inputFile, outputName;
|
||||||
std::string Version = "v0.0.5-beta";
|
std::string Version = "v1.5.0-modular"; // Zaktualizowałem wersję :)
|
||||||
bool showHelp = false, showVersion = false, showCredits = false;
|
bool showHelp = false, showVersion = false, showCredits = false;
|
||||||
|
|
||||||
// --- PARSOWANIE ARGUMENTÓW ---
|
// --- PARSOWANIE ARGUMENTÓW ---
|
||||||
@@ -78,22 +68,10 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... wczytywanie pliku (to co miałeś) ...
|
|
||||||
std::string src((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
std::string src((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
// --- PREPROCESSOR START ---
|
// --- LOGIKA KOMPILATORA ---
|
||||||
std::cout << "[INFO] Preprocessing...\n";
|
|
||||||
|
|
||||||
// 1. Ścieżka projektu (tam gdzie plik wejściowy)
|
|
||||||
std::filesystem::path p(inputFile);
|
|
||||||
std::string projectDir = p.parent_path().string();
|
|
||||||
|
|
||||||
// 2. Ścieżka kompilatora (tam gdzie PCC.exe i folder std)
|
|
||||||
std::string compilerDir = getExecutablePath();
|
|
||||||
|
|
||||||
// Uruchamiamy z obiema ścieżkami
|
|
||||||
src = preprocessSource(src, projectDir, compilerDir);
|
|
||||||
CompilerState state;
|
CompilerState state;
|
||||||
|
|
||||||
std::cout << "[INFO] Parsing code...\n";
|
std::cout << "[INFO] Parsing code...\n";
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ 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('(');
|
||||||
@@ -55,6 +56,7 @@ 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)
|
||||||
@@ -73,6 +75,7 @@ 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;
|
||||||
@@ -89,34 +92,10 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
size_t start = line.find('(') + 1;
|
size_t start = line.find('(') + 1;
|
||||||
size_t end = line.find(')');
|
size_t end = line.find(')');
|
||||||
if (start != std::string::npos && end != std::string::npos) {
|
if (start != std::string::npos && end != std::string::npos) {
|
||||||
std::string arg = trim(line.substr(start, end - start));
|
std::string var = trim(line.substr(start, end - start));
|
||||||
|
f.instructions.push_back({ OpType::PRINT, var, "", "" });
|
||||||
// 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
|
// C. IF STATEMENT
|
||||||
else if (line.substr(0, 2) == "if") {
|
else if (line.substr(0, 2) == "if") {
|
||||||
size_t openParen = line.find("(");
|
size_t openParen = line.find("(");
|
||||||
@@ -138,19 +117,12 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
size_t eqPos = line.find('=');
|
size_t eqPos = line.find('=');
|
||||||
std::string leftSide = trim(line.substr(0, eqPos));
|
std::string leftSide = trim(line.substr(0, eqPos));
|
||||||
std::string rightSide = trim(line.substr(eqPos + 1));
|
std::string rightSide = trim(line.substr(eqPos + 1));
|
||||||
|
|
||||||
bool isStringDecl = false; // Flaga, czy to string
|
|
||||||
if (!rightSide.empty() && rightSide.back() == ';') rightSide.pop_back();
|
if (!rightSide.empty() && rightSide.back() == ';') rightSide.pop_back();
|
||||||
|
|
||||||
// Obsługa nazwy zmiennej (usuwanie "int ", "bool ")
|
// Obsługa nazwy zmiennej (usuwanie "int ", "bool ")
|
||||||
std::string varName = leftSide;
|
std::string varName = leftSide;
|
||||||
if (leftSide.rfind("int ", 0) == 0) varName = trim(leftSide.substr(4));
|
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("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();
|
// 1. Czy to wywołanie funkcji? int x = func();
|
||||||
if (rightSide.find("(") != std::string::npos && rightSide.find(")") != std::string::npos) {
|
if (rightSide.find("(") != std::string::npos && rightSide.find(")") != std::string::npos) {
|
||||||
@@ -171,33 +143,6 @@ 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::ADD, varName, a, b });
|
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 '=')
|
// 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) {
|
||||||
@@ -206,26 +151,6 @@ void processSource(const std::string& src, CompilerState& state) {
|
|||||||
std::string b = trim(rightSide.substr(opPos + 2));
|
std::string b = trim(rightSide.substr(opPos + 2));
|
||||||
f.instructions.push_back({ OpType::EQ, varName, a, b });
|
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
|
// 4. Zwykłe przypisanie: a = 5
|
||||||
else {
|
else {
|
||||||
f.instructions.push_back({ OpType::ASSIGN, varName, rightSide, "" });
|
f.instructions.push_back({ OpType::ASSIGN, varName, rightSide, "" });
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
#include "preprocessor.h"
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <regex>
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
// 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 "";
|
|
||||||
}
|
|
||||||
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
// 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()) {
|
|
||||||
std::string fullPath;
|
|
||||||
if (isStdLib) {
|
|
||||||
fullPath = compilerDir + "/std/" + includePath;
|
|
||||||
std::cout << "[PREPROCESSOR] Including STD lib: " << fullPath << "\n";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Plik lokalny: Szukamy w folderze projektu
|
|
||||||
if (projectDir.empty()) fullPath = includePath;
|
|
||||||
else fullPath = projectDir + "/" + includePath;
|
|
||||||
std::cout << "[PREPROCESSOR] Including local file: " << fullPath << "\n";
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#ifndef PREPROCESSOR_H
|
|
||||||
#define PREPROCESSOR_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
std::string preprocessSource(const std::string& src, const std::string& projectDir, const std::string& compilerDir);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
10
README.md
10
README.md
@@ -1,23 +1,17 @@
|
|||||||
# PCC Compiler (My C++ Compiler)
|
# 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.
|
**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
|
## 🚀 Features
|
||||||
- **Custom Syntax**: C-like syntax easy for beginners.
|
- **Custom Syntax**: C-like syntax easy for beginners.
|
||||||
- **Variables**: Support for `int`, `bool` and `string`.
|
- **Variables**: Support for `int` and `bool`.
|
||||||
- **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.
|
- **Control Flow**: `if` statements support.
|
||||||
- **Functions**: Define and call `void` functions.
|
- **Functions**: Define and call `void` functions.
|
||||||
- **Native Compilation**: Compiles directly to x64 machine code.
|
- **Native Compilation**: Compiles directly to x64 machine code.
|
||||||
- **More Informations**: For more informations check PCC Docs.
|
|
||||||
|
|
||||||
## 🛠️ Usage
|
## 🛠️ Usage
|
||||||
1. Download the latest release from the [Releases](../../releases) page.
|
1. Download the latest release from the [Releases](../../releases) page.
|
||||||
|
|||||||
35
push.bat
35
push.bat
@@ -1,35 +0,0 @@
|
|||||||
@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