Files
PCCCompiler/PCCcompiler/parser.cpp
2026-02-07 18:47:07 +01:00

394 lines
20 KiB
C++

#include "parser.h"
#include "utils.h"
#include <iostream>
#include <sstream>
#include <vector>
std::vector<std::string> parseArgs(const std::string& line) {
std::vector<std::string> args;
size_t open = line.find('(');
size_t close = line.find(')');
if (open == std::string::npos || close == std::string::npos) return args;
std::string inside = line.substr(open + 1, close - open - 1);
if (inside.empty()) return args;
std::stringstream ss(inside);
std::string segment;
while (std::getline(ss, segment, ',')) {
segment = trim(segment);
// segment to np. "int a". Szukamy ostatniej spacji, by wziąć nazwę "a"
size_t space = segment.find_last_of(" \t");
if (space != std::string::npos) {
args.push_back(trim(segment.substr(space + 1)));
}
}
return args;
}
void processSource(const std::string& src, CompilerState& state) {
std::istringstream iss(src);
std::string line;
while (std::getline(iss, line)) {
line = trim(line);
if (line.empty() || line.substr(0, 2) == "//" || line[0] == '#') continue;
// --- 1. DEFINICJA FUNKCJI ---
// 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);
if (startsWithType && line.find("(") != std::string::npos && line.find("{") != std::string::npos && line.find("=") == std::string::npos) {
size_t openParen = line.find('(');
std::string typeRaw = line.substr(0, line.find(' '));
std::string nameRaw = line.substr(typeRaw.length(), openParen - typeRaw.length());
std::string funcName = trim(nameRaw);
Function newFunc;
newFunc.name = funcName;
newFunc.returnType = typeRaw;
newFunc.args = parseArgs(line);
state.functions[funcName] = newFunc;
state.currentFunction = &state.functions[funcName];
std::cout << "[PARSER] New Function: " << funcName << "\n";
continue;
}
// --- 2. ZAMYKANIE BLOKU '}' ---
// --- 2. ZAMYKANIE BLOKU '}' ---
if (line == "}") {
if (!state.blockStack.empty()) {
std::string blockInfo = state.blockStack.top();
state.blockStack.pop();
// Sprawdzamy czy to WHILE (czy zaczyna się od "WHILE|")
// Bezpieczniejsza metoda:
bool isWhile = (blockInfo.length() > 6 && blockInfo.substr(0, 6) == "WHILE|");
if (isWhile) {
// To jest pętla!
size_t firstPipe = blockInfo.find('|');
size_t secondPipe = blockInfo.rfind('|');
std::string labelStart = blockInfo.substr(firstPipe + 1, secondPipe - firstPipe - 1);
std::string labelEnd = blockInfo.substr(secondPipe + 1);
if (state.currentFunction) {
state.currentFunction->instructions.push_back({ OpType::JMP, labelStart, "", "" });
state.currentFunction->instructions.push_back({ OpType::LABEL, labelEnd, "", "" });
}
std::cout << " [PARSER] } End WHILE loop\n";
}
else {
// To zwykły IF
if (state.currentFunction) {
state.currentFunction->instructions.push_back({ OpType::LABEL, blockInfo, "", "" });
}
std::cout << " [PARSER] } End IF block -> " << blockInfo << "\n";
}
}
else {
// Koniec funkcji
state.currentFunction = nullptr;
std::cout << " [PARSER] } End Function\n";
}
continue;
}
// --- JESTEŚMY W ŚRODKU FUNKCJI ---
if (state.currentFunction) {
Function& f = *state.currentFunction;
// A. RETURN
if (line.substr(0, 6) == "return") {
std::string val = trim(line.substr(6));
if (!val.empty() && val.back() == ';') val.pop_back();
f.instructions.push_back({ OpType::RETURN, val, "", "" });
std::cout << " [PARSER] Return: " << val << "\n";
}
// B. PRINT
else if (line.substr(0, 5) == "print") {
size_t start = line.find('(') + 1;
size_t end = line.find(')');
if (start != std::string::npos && end != std::string::npos) {
std::string arg = trim(line.substr(start, end - start));
// 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
// --- IF (ZAAWANSOWANY) ---
else if (line.substr(0, 2) == "if") {
size_t openParen = line.find("(");
size_t closeParen = line.rfind(")"); // rfind! Żeby łapać ostatni nawias
if (openParen != std::string::npos && closeParen > openParen) {
std::string conditionRaw = trim(line.substr(openParen + 1, closeParen - openParen - 1));
// Zmienna, która będzie trzymać ostateczny wynik warunku
std::string finalConditionVar = conditionRaw;
// Sprawdzamy czy są operatory logiczne && lub ||
// (Na razie obsłużymy jeden poziom: A && B)
size_t andPos = conditionRaw.find("&&");
size_t orPos = conditionRaw.find("||");
if (andPos != std::string::npos) {
// Mamy AND: "partA && partB"
std::string partA = trim(conditionRaw.substr(0, andPos));
std::string partB = trim(conditionRaw.substr(andPos + 2));
// Generujemy nazwy zmiennych pomocniczych
std::string tempA = "_tmp_and_a_" + std::to_string(state.labelCounter);
std::string tempB = "_tmp_and_b_" + std::to_string(state.labelCounter);
std::string tempRes = "_tmp_and_res_" + std::to_string(state.labelCounter);
// Część A
if (partA.find("==") != std::string::npos) {
size_t eq = partA.find("==");
std::string l = trim(partA.substr(0, eq));
std::string r = trim(partA.substr(eq + 2));
f.instructions.push_back({ OpType::EQ, tempA, l, r });
}
else {
// Jeśli to po prostu zmienna "a"
f.instructions.push_back({ OpType::ASSIGN, tempA, partA, "" });
}
// Część B
if (partB.find("==") != std::string::npos) {
size_t eq = partB.find("==");
std::string l = trim(partB.substr(0, eq));
std::string r = trim(partB.substr(eq + 2));
f.instructions.push_back({ OpType::EQ, tempB, l, r });
}
else {
f.instructions.push_back({ OpType::ASSIGN, tempB, partB, "" });
}
// Wykonujemy AND
f.instructions.push_back({ OpType::LOGIC_AND, tempRes, tempA, tempB });
finalConditionVar = tempRes;
}
else if (orPos != std::string::npos) {
// To samo dla OR
std::string partA = trim(conditionRaw.substr(0, orPos));
std::string partB = trim(conditionRaw.substr(orPos + 2));
std::string tempA = "_tmp_or_a_" + std::to_string(state.labelCounter);
std::string tempB = "_tmp_or_b_" + std::to_string(state.labelCounter);
std::string tempRes = "_tmp_or_res_" + std::to_string(state.labelCounter);
// A
if (partA.find("==") != std::string::npos) {
size_t eq = partA.find("==");
f.instructions.push_back({ OpType::EQ, tempA, trim(partA.substr(0, eq)), trim(partA.substr(eq + 2)) });
}
else f.instructions.push_back({ OpType::ASSIGN, tempA, partA, "" });
// B
if (partB.find("==") != std::string::npos) {
size_t eq = partB.find("==");
f.instructions.push_back({ OpType::EQ, tempB, trim(partB.substr(0, eq)), trim(partB.substr(eq + 2)) });
}
else f.instructions.push_back({ OpType::ASSIGN, tempB, partB, "" });
f.instructions.push_back({ OpType::LOGIC_OR, tempRes, tempA, tempB });
finalConditionVar = tempRes;
}
else if (conditionRaw.find("==") != std::string::npos) {
size_t eq = conditionRaw.find("==");
std::string l = trim(conditionRaw.substr(0, eq));
std::string r = trim(conditionRaw.substr(eq + 2));
std::string tempRes = "_tmp_eq_" + std::to_string(state.labelCounter);
f.instructions.push_back({ OpType::EQ, tempRes, l, r });
finalConditionVar = tempRes;
}
// --- GENEROWANIE SKOKU ---
std::string labelName = "L_" + std::to_string(state.labelCounter++);
// Teraz JMP_FALSE dostaje zawsze już obliczoną zmienną (finalConditionVar)
f.instructions.push_back({ OpType::JMP_FALSE, labelName, finalConditionVar, "" });
state.blockStack.push(labelName);
std::cout << " [PARSER] IF (" << finalConditionVar << ") -> Jump to " << labelName << "\n";
}
}
// --- PĘTLA WHILE ---
else if (line.substr(0, 5) == "while") {
size_t openParen = line.find("(");
size_t closeParen = line.find(")");
if (openParen != std::string::npos && closeParen > openParen) {
std::string condition = trim(line.substr(openParen + 1, closeParen - openParen - 1));
// 1. Generujemy etykiety
std::string labelStart = "L_" + std::to_string(state.labelCounter++);
std::string labelEnd = "L_" + std::to_string(state.labelCounter++);
// 2. Wstawiamy etykietę START (tu będziemy wracać)
f.instructions.push_back({ OpType::LABEL, labelStart, "", "" });
// 3. Sprawdzamy warunek -> jak fałsz, skaczemy do END
f.instructions.push_back({ OpType::JMP_FALSE, labelEnd, condition, "" });
// 4. Wrzucamy info na stos, żeby '}' wiedziało co robić
// Format specjalny: "WHILE|Start|End"
state.blockStack.push("WHILE|" + labelStart + "|" + labelEnd);
std::cout << " [PARSER] WHILE (" << condition << ") -> Loop between " << labelStart << " and " << labelEnd << "\n";
}
}
// D. PRZYPISANIE ZMIENNEJ (LUB DEKLARACJA)
// np. "int a = 5;" LUB "a = b + c;"
else if (line.find("=") != std::string::npos) {
size_t eqPos = line.find('=');
std::string leftSide = trim(line.substr(0, eqPos));
std::string rightSide = trim(line.substr(eqPos + 1));
bool isStringDecl = false; // Flaga, czy to string
if (!rightSide.empty() && rightSide.back() == ';') rightSide.pop_back();
// Obsługa nazwy zmiennej (usuwanie "int ", "bool ")
std::string varName = leftSide;
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("string ", 0) == 0) {
varName = trim(leftSide.substr(7));
isStringDecl = true;
}
// 1. Czy to wywołanie funkcji? int x = func();
if (rightSide.find("(") != std::string::npos && rightSide.find(")") != std::string::npos) {
size_t open = rightSide.find('(');
std::string funcName = trim(rightSide.substr(0, open));
std::string argsContent = rightSide.substr(open + 1, rightSide.find(')') - open - 1);
// CALL func
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
// ASSIGN result (RAX) to variable
f.instructions.push_back({ OpType::ASSIGN, varName, "RAX", "" });
std::cout << " [PARSER] Call & Assign: " << varName << " = " << funcName << "()\n";
}
// 2. Czy to dodawanie? 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::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 });
}
// LOGICZNE AND: 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 + 2)); // +2 bo && ma 2 znaki
f.instructions.push_back({ OpType::LOGIC_AND, varName, a, b });
}
// LOGICZNE OR: 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 + 2));
f.instructions.push_back({ OpType::LOGIC_OR, varName, a, b });
}
// 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
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 + 2));
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
else {
f.instructions.push_back({ OpType::ASSIGN, varName, rightSide, "" });
}
}
// E. SAMODZIELNE WYWOŁANIE FUNKCJI (bez =)
// np. func();
else if (line.find("(") != std::string::npos && line.find(")") != std::string::npos) {
size_t open = line.find('(');
std::string funcName = trim(line.substr(0, open));
std::string argsContent = line.substr(open + 1, line.find(')') - open - 1);
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
std::cout << " [PARSER] Call void: " << funcName << "\n";
}
}
}
}
void calculateExpressions(CompilerState& state) {}