Files
PCCCompiler/PCCcompiler/compiler_types.h
2026-02-15 12:18:38 +01:00

108 lines
2.6 KiB
C++

#ifndef COMPILER_TYPES_H
#define COMPILER_TYPES_H
#include <string>
#include <vector>
#include <map>
#include <stack>
// 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(int)
PRINT_STRING, // print(string)
JMP_FALSE, // if (false) skocz...
ARRAY_DECLARE, // int t[10];
ARRAY_SET, // t[0] = 5;
ARRAY_GET, // x = t[0];
JMP, // else / pêtla
LOGIC_AND, // &&
LOGIC_OR, // ||
LABEL, // miejsce skoku
CALL, // wywo³anie funkcji
RETURN, // return x
MSGBOX, // msg box
ALLOC_OBJECT, // alokacja obiektu na stosie - NOWE
STORE_FIELD, // zapis do pola obiektu - NOWE
LOAD_FIELD, // odczyt z pola obiektu - NOWE
NOP // pusta instrukcja
};
// Pojedynczy rozkaz
struct Instruction {
OpType type;
std::string arg1;
std::string arg2;
std::string arg3;
};
// Definicja funkcji
struct Function {
std::string name;
std::string returnType;
std::vector<std::string> args;
std::vector<Instruction> instructions;
};
// Definicja pola klasy
struct ClassField {
std::string name;
std::string type; // "int", "string", itp.
int offset; // offset w bajtach od pocz¹tku obiektu
};
// Definicja metody klasy
struct ClassMethod {
std::string name;
std::string returnType;
std::vector<std::string> args;
std::string mangledName; // np. "User_setAge"
};
// Definicja klasy
struct ClassDef {
std::string name;
std::vector<ClassField> fields;
std::vector<ClassMethod> methods;
int totalSize; // rozmiar ca³ego obiektu w bajtach
};
// G£ÓWNY STAN KOMPILATORA
struct CompilerState {
// Mapa funkcji
std::map<std::string, Function> functions;
// Mapa klas (NOWE)
std::map<std::string, ClassDef> classes;
// Zmienne globalne
std::map<std::string, int> globals;
// Zarz¹dzanie stosem
std::map<std::string, int> stackMap;
int stackOffset = 0;
// Stan parsera
Function* currentFunction = nullptr;
std::string currentClass = ""; // (NOWE) - nazwa aktualnie parsowanej klasy
int labelCounter = 0;
// Stosy bloków
std::stack<std::string> loopStack;
std::stack<std::string> blockStack;
std::vector<std::pair<std::string, std::string>> stringLiterals;
// Licznik do generowania nazw str_0, str_1...
int stringCounter = 0;
std::map<std::string, std::string> varTypes;
};
#endif