include Update

This commit is contained in:
mmichlol
2026-02-07 11:57:44 +01:00
parent e250b2f5fb
commit 9bbf8435c6
4 changed files with 26 additions and 30 deletions

View File

@@ -4,14 +4,22 @@
#include <vector> #include <vector>
#include <cstdlib> #include <cstdlib>
#include <filesystem> #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" #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.3-beta"; // Zaktualizowałem wersję :) std::string Version = "v0.0.5-beta";
bool showHelp = false, showVersion = false, showCredits = false; bool showHelp = false, showVersion = false, showCredits = false;
// --- PARSOWANIE ARGUMENTÓW --- // --- PARSOWANIE ARGUMENTÓW ---
@@ -77,17 +85,15 @@ int main(int argc, char* argv[]) {
// --- PREPROCESSOR START --- // --- PREPROCESSOR START ---
std::cout << "[INFO] Preprocessing...\n"; std::cout << "[INFO] Preprocessing...\n";
// 1. Ścieżka projektu (tam gdzie plik wejściowy)
std::filesystem::path p(inputFile); std::filesystem::path p(inputFile);
std::string basePath = p.parent_path().string(); std::string projectDir = p.parent_path().string();
// Uruchamiamy preprocesor - podmieni #include na kod // 2. Ścieżka kompilatora (tam gdzie PCC.exe i folder std)
src = preprocessSource(src, basePath); std::string compilerDir = getExecutablePath();
// Opcjonalnie: pokaż kod po złączeniu (do debugowania) // Uruchamiamy z obiema ścieżkami
// std::cout << "--- FINAL CODE ---\n" << src << "\n------------------\n"; src = preprocessSource(src, projectDir, compilerDir);
// --- PREPROCESSOR END ---
// --- LOGIKA KOMPILATORA (DALEJ BEZ ZMIAN) ---
CompilerState state; CompilerState state;
std::cout << "[INFO] Parsing code...\n"; std::cout << "[INFO] Parsing code...\n";

View File

@@ -17,7 +17,7 @@ std::string loadFileContent(const std::string& path) {
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
} }
std::string preprocessSource(const std::string& src, const std::string& basePath) { std::string preprocessSource(const std::string& src, const std::string& projectDir, const std::string& compilerDir) {
std::istringstream iss(src); std::istringstream iss(src);
std::string line; std::string line;
std::stringstream output; std::stringstream output;
@@ -53,26 +53,17 @@ std::string preprocessSource(const std::string& src, const std::string& basePath
if (!includePath.empty()) { if (!includePath.empty()) {
std::string fullPath; std::string fullPath;
if (isStdLib) { if (isStdLib) {
// Biblioteka standardowa: szukamy w folderze "std" obok exe/projektu fullPath = compilerDir + "/std/" + includePath;
// U¿ywamy current_path() + "std"
fullPath = "std/" + includePath;
std::cout << "[PREPROCESSOR] Including STD lib: " << fullPath << "\n"; std::cout << "[PREPROCESSOR] Including STD lib: " << fullPath << "\n";
} }
else { else {
// Plik lokalny: szukamy w tym samym folderze co plik Ÿród³owy // Plik lokalny: Szukamy w folderze projektu
// Jeœli basePath jest pusty, szukamy lokalnie if (projectDir.empty()) fullPath = includePath;
if (basePath.empty()) fullPath = includePath; else fullPath = projectDir + "/" + includePath;
else fullPath = basePath + "/" + includePath;
std::cout << "[PREPROCESSOR] Including local file: " << fullPath << "\n"; std::cout << "[PREPROCESSOR] Including local file: " << fullPath << "\n";
} }
// Wczytujemy plik i REKURENCYJNIE go przetwarzamy
// (bo plik do³¹czany mo¿e mieæ swoje include!)
std::string content = loadFileContent(fullPath); std::string content = loadFileContent(fullPath);
std::string processedContent = preprocessSource(content, projectDir, compilerDir);
// Dla uproszczenia rekurencji w include'ach lokalnych, przekazujemy ten sam basePath
// W idealnym œwiecie powinniœmy braæ folder nowego pliku.
std::string processedContent = preprocessSource(content, basePath);
output << "\n// --- BEGIN INCLUDE: " << includePath << " ---\n"; output << "\n// --- BEGIN INCLUDE: " << includePath << " ---\n";
output << processedContent; output << processedContent;
@@ -80,7 +71,6 @@ std::string preprocessSource(const std::string& src, const std::string& basePath
} }
} }
else { else {
// Zwyk³a linia kodu - przepisujemy bez zmian
output << line << "\n"; output << line << "\n";
} }
} }

View File

@@ -2,10 +2,7 @@
#define PREPROCESSOR_H #define PREPROCESSOR_H
#include <string> #include <string>
std::string preprocessSource(const std::string& src, const std::string& projectDir, const std::string& compilerDir);
// G³ówna funkcja preprocesora.
// src: kod Ÿród³owy g³ównego pliku
// basePath: œcie¿ka do folderu, w którym jest g³ówny plik (¿eby wiedzieæ sk¹d braæ lokalne include)
std::string preprocessSource(const std::string& src, const std::string& basePath);
#endif #endif

View File

@@ -4,12 +4,15 @@
![Status](https://img.shields.io/badge/status-BETA-orange.svg) ![Status](https://img.shields.io/badge/status-BETA-orange.svg)
![Platform](https://img.shields.io/badge/platform-Windows%20x64-lightgrey.svg) ![Platform](https://img.shields.io/badge/platform-Windows%20x64-lightgrey.svg)
## 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`, `bool` and `string`.
- **include files**: you can include base files from compiler `#include <main.pcc>` or your own files `#include "myfile.pcc`. - **include files**: you can include base files from compiler `#include <main.pcc>` or your own files `#include "myfile.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.