added more functions

This commit is contained in:
mmichlol
2026-02-07 11:33:50 +01:00
parent 5fbd0f98a2
commit aa6d16bc52
8 changed files with 264 additions and 17 deletions

View File

@@ -22,12 +22,14 @@
<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">

View File

@@ -27,6 +27,9 @@
<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">
@@ -41,5 +44,8 @@
<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>

View File

@@ -40,9 +40,19 @@ 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"; // <--- DODAJ TO result += "extern getchar\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"; // NOWOŒÆ: 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";
}
result += "section .text\n\n"; result += "section .text\n\n";
for (const auto& pair : state.functions) { for (const auto& pair : state.functions) {
@@ -71,19 +81,33 @@ 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 // Rezerwacja miejsca dla nowych zmiennych (wynikowych)
if ((instr.type == OpType::ASSIGN || instr.type == OpType::ADD || instr.type == OpType::EQ) // Dodajemy tu OpType::SUB i OpType::MUL
&& stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") { bool isWriteOp = (instr.type == OpType::ASSIGN ||
instr.type == OpType::ADD ||
instr.type == OpType::EQ ||
instr.type == OpType::SUB ||
instr.type == OpType::MUL);
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 = getVarLocation(instr.arg2, stackMap); std::string src = instr.arg2;
if (instr.arg3 == "STRING") {
result += " lea rax, [rel " + src + "]\n";
std::string dst = getVarLocation(instr.arg1, stackMap); std::string dst = getVarLocation(instr.arg1, stackMap);
result += " mov eax, " + src + "\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"; result += " mov " + dst + ", eax\n";
}
break; break;
} }
case OpType::ADD: { case OpType::ADD: {
@@ -95,6 +119,30 @@ 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);
@@ -144,10 +192,17 @@ std::string generateAssembly(const CompilerState& state) {
break; break;
} }
case OpType::PRINT: { case OpType::PRINT: {
if (instr.arg2 == "STRING") {
result += " lea rdx, [rel " + instr.arg1 + "]\n";
result += " lea rcx, [rel fmt_str]\n";
result += " call printf\n";
}
else {
std::string val = getVarLocation(instr.arg1, stackMap); std::string val = getVarLocation(instr.arg1, stackMap);
result += " mov edx, " + val + "\n"; result += " mov edx, " + val + "\n";
result += " lea rcx, [rel fmt]\n"; result += " lea rcx, [rel fmt_int]\n";
result += " call printf\n"; result += " call printf\n";
}
break; break;
} }
case OpType::CALL: { case OpType::CALL: {

View File

@@ -51,6 +51,9 @@ 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

View File

@@ -2,14 +2,16 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstdlib> // do std::system #include <cstdlib>
#include <filesystem>
#include "compiler_types.h" #include "compiler_types.h"
#include "parser.h" #include "parser.h"
#include "codegen.h" #include "codegen.h"
#include "preprocessor.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::string inputFile, outputName; std::string inputFile, outputName;
std::string Version = "v1.5.0-modular"; // Zaktualizowałem wersję :) std::string Version = "v0.0.3-beta"; // Zaktualizowałem wersję :)
bool showHelp = false, showVersion = false, showCredits = false; bool showHelp = false, showVersion = false, showCredits = false;
// --- PARSOWANIE ARGUMENTÓW --- // --- PARSOWANIE ARGUMENTÓW ---
@@ -68,10 +70,24 @@ 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();
// --- LOGIKA KOMPILATORA --- // --- PREPROCESSOR START ---
std::cout << "[INFO] Preprocessing...\n";
std::filesystem::path p(inputFile);
std::string basePath = p.parent_path().string();
// Uruchamiamy preprocesor - podmieni #include na kod
src = preprocessSource(src, basePath);
// Opcjonalnie: pokaż kod po złączeniu (do debugowania)
// std::cout << "--- FINAL CODE ---\n" << src << "\n------------------\n";
// --- 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

@@ -92,10 +92,34 @@ 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 var = trim(line.substr(start, end - start)); std::string arg = 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("(");
@@ -117,12 +141,19 @@ 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) {
@@ -143,6 +174,20 @@ 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
}
// 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) {
@@ -151,6 +196,26 @@ 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, "" });

View File

@@ -0,0 +1,89 @@
#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& basePath) {
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) {
// Biblioteka standardowa: szukamy w folderze "std" obok exe/projektu
// U¿ywamy current_path() + "std"
fullPath = "std/" + includePath;
std::cout << "[PREPROCESSOR] Including STD lib: " << fullPath << "\n";
}
else {
// Plik lokalny: szukamy w tym samym folderze co plik Ÿród³owy
// Jeœli basePath jest pusty, szukamy lokalnie
if (basePath.empty()) fullPath = includePath;
else fullPath = basePath + "/" + includePath;
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);
// 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 << processedContent;
output << "\n// --- END INCLUDE ---\n";
}
}
else {
// Zwyk³a linia kodu - przepisujemy bez zmian
output << line << "\n";
}
}
return output.str();
}

View File

@@ -0,0 +1,11 @@
#ifndef PREPROCESSOR_H
#define PREPROCESSOR_H
#include <string>
// 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