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

@@ -6,27 +6,28 @@
#include <map>
#include <stack>
// Typy operacji, które nasz kompilator rozumie
// Typy operacji
enum class OpType {
ASSIGN, // a = 5
ADD, // a = b + c
SUB, // a = b - c
MUL, // a = b * c
DIV, // a = b / c
MOD, // a = b % c
EQ, // a == b
PRINT, // print(a)
PRINT, // print(int)
PRINT_STRING, // print(string) - NOWE
JMP_FALSE, // if (false) skocz...
JMP, // else / pêtla
LOGIC_AND, // &&
LOGIC_OR, // ||
LOGIC_AND, // &&
LOGIC_OR, // ||
LABEL, // miejsce skoku
CALL, // wywo³anie funkcji
RETURN, // return x
DIV, // dzielenie
MOD, // Reszta z dzelenia (%)
NOP // pusta instrukcja
};
// Pojedynczy rozkaz kompilatora (Intermediate Representation)
// Pojedynczy rozkaz
struct Instruction {
OpType type;
std::string arg1;
@@ -37,27 +38,43 @@ struct Instruction {
// Definicja funkcji
struct Function {
std::string name;
std::string returnType; // "int", "void", "bool"
std::vector<std::string> args; // Nazwy argumentów (np. "a", "b")
std::vector<Instruction> instructions; // Lista rozkazów w funkcji
std::string returnType;
std::vector<std::string> args;
std::vector<Instruction> instructions;
};
// G£ÓWNY STAN KOMPILATORA
struct CompilerState {
// Mapa wszystkich funkcji (klucz to nazwa)
// Mapa funkcji
std::map<std::string, Function> functions;
// Zmienne globalne (tylko nazwa -> wartoœæ pocz¹tkowa)
// Zmienne globalne
std::map<std::string, int> globals;
// Stan parsera
Function* currentFunction = nullptr; // WskaŸnik na aktualnie parsuj¹c¹ siê funkcjê
int labelCounter = 0; // Do generowania unikalnych nazw etykiet (L1, L2...)
std::stack<std::string> loopStack; // Do break/continue (przysz³oœciowo)
// Zarz¹dzanie stosem
std::map<std::string, int> stackMap;
int stackOffset = 0;
// Stan parsera
Function* currentFunction = nullptr;
int labelCounter = 0;
// Stosy bloków
std::stack<std::string> loopStack;
std::stack<std::string> blockStack;
std::map<std::string, std::string> stringLiterals;
// --- SEKCJA STRINGÓW (TU BY£Y B£ÊDY) ---
// Lista litera³ów do sekcji .data (np. "str_0" -> "Hello")
// Musi byæ VECTOR, bo iterujemy po nim.
std::vector<std::pair<std::string, std::string>> stringLiterals;
// Licznik do generowania nazw str_0, str_1...
int stringCounter = 0;
// Mapa typów zmiennych (np. "imie" -> "string")
// Musi byæ MAP, bo szukamy po nazwie.
std::map<std::string, std::string> varTypes;
};
#endif