#include "preprocessor.h" #include #include #include #include #include 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(in)), std::istreambuf_iterator()); } 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 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(); }