String Update

This commit is contained in:
mmichlol
2026-02-07 19:44:03 +01:00
parent c37a122850
commit 3916b4ff7a
3 changed files with 232 additions and 244 deletions

View File

@@ -26,6 +26,20 @@ std::vector<std::string> parseArgs(const std::string& line) {
return args;
}
// Funkcja wyciągająca "tekst" i rejestrująca go w state
// Rejestruje tekst i zwraca jego etykietę (np. str_5)
std::string registerStringLiteral(CompilerState& state, std::string content) {
// Używamy stringCounter z CompilerState
std::string label = "str_" + std::to_string(state.stringCounter++);
// Dodajemy do WEKTORA (push_back działa tylko na wektorze/liście)
state.stringLiterals.push_back({ label, content });
return label;
}
void processSource(const std::string& src, CompilerState& state) {
std::istringstream iss(src);
std::string line;
@@ -106,39 +120,57 @@ void processSource(const std::string& src, CompilerState& state) {
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));
// --- 4. ZMIENNE TYPU STRING ---
// string s = "hello";
else if (line.substr(0, 6) == "string") {
size_t eqPos = line.find("=");
if (eqPos != std::string::npos) {
std::string name = trim(line.substr(7, eqPos - 7));
// 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);
size_t quoteStart = line.find("\"", eqPos);
size_t quoteEnd = line.rfind("\"");
// 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;
}
if (quoteStart != std::string::npos && quoteEnd > quoteStart) {
std::string content = line.substr(quoteStart + 1, quoteEnd - quoteStart - 1);
// 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", "" });
// Rejestracja w wektorze
std::string label = registerStringLiteral(state, content);
// Rejestracja typu w mapie (mapa obsługuje [])
state.varTypes[name] = "string";
// Instrukcja
f.instructions.push_back({ OpType::ASSIGN, name, label, "" });
}
}
}
// --- DRUKOWANIE (PRINT) ---
else if (line.substr(0, 5) == "print") {
size_t open = line.find("(");
size_t close = line.rfind(")");
if (open != std::string::npos && close > open) {
std::string content = trim(line.substr(open + 1, close - open - 1));
// 1. Literał: print("tekst")
if (content.front() == '"' && content.back() == '"') {
std::string text = content.substr(1, content.length() - 2);
std::string label = registerStringLiteral(state, text);
f.instructions.push_back({ OpType::PRINT_STRING, label, "", "" });
}
// 2. Zmienna: print(x) - sprawdzamy typ
// Używamy .count() na mapie varTypes (poprawne)
else if (state.varTypes.count(content) && state.varTypes[content] == "string") {
f.instructions.push_back({ OpType::PRINT_STRING, content, "", "" });
}
// 3. Liczba
else {
f.instructions.push_back({ OpType::PRINT, content, "", "" });
}
}
}
// C. IF STATEMENT
// --- IF (ZAAWANSOWANY) ---
else if (line.substr(0, 2) == "if") {
@@ -352,26 +384,6 @@ void processSource(const std::string& src, CompilerState& state) {
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, "" });