added more functions
This commit is contained in:
@@ -92,10 +92,34 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
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, "", "" });
|
||||
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
|
||||
else if (line.substr(0, 2) == "if") {
|
||||
size_t openParen = line.find("(");
|
||||
@@ -117,12 +141,19 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
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) { // NOWOŚĆ
|
||||
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) {
|
||||
@@ -143,6 +174,20 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
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
|
||||
}
|
||||
// 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) {
|
||||
@@ -151,6 +196,26 @@ 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, "" });
|
||||
|
||||
Reference in New Issue
Block a user