173 lines
7.9 KiB
C++
173 lines
7.9 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 '}' ---
|
|
if (line == "}") {
|
|
// Najpierw sprawdzamy, czy zamykamy IF-a (czy jest coś na stosie bloków)
|
|
if (!state.blockStack.empty()) {
|
|
std::string label = state.blockStack.top();
|
|
state.blockStack.pop();
|
|
if (state.currentFunction) {
|
|
state.currentFunction->instructions.push_back({ OpType::LABEL, label, "", "" });
|
|
}
|
|
std::cout << " [PARSER] } End IF block -> " << label << "\n";
|
|
}
|
|
else {
|
|
// Jeśli stos pusty, to 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 var = trim(line.substr(start, end - start));
|
|
f.instructions.push_back({ OpType::PRINT, var, "", "" });
|
|
}
|
|
}
|
|
// C. IF STATEMENT
|
|
else if (line.substr(0, 2) == "if") {
|
|
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));
|
|
std::string labelName = "L_" + std::to_string(state.labelCounter++);
|
|
|
|
// Skok warunkowy
|
|
f.instructions.push_back({ OpType::JMP_FALSE, labelName, condition, "" });
|
|
state.blockStack.push(labelName);
|
|
|
|
std::cout << " [PARSER] IF (" << condition << ") -> Jump to " << labelName << "\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));
|
|
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));
|
|
|
|
// 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. 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 });
|
|
}
|
|
// 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) {}
|