62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
#ifndef COMPILER_TYPES_H
|
|
#define COMPILER_TYPES_H
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <stack>
|
|
|
|
// Typy operacji, które nasz kompilator rozumie
|
|
enum class OpType {
|
|
ASSIGN, // a = 5
|
|
ADD, // a = b + c
|
|
SUB, // a = b - c
|
|
MUL, // a = b * c
|
|
EQ, // a == b
|
|
PRINT, // print(a)
|
|
JMP_FALSE, // if (false) skocz...
|
|
JMP, // else / pêtla
|
|
LABEL, // miejsce skoku
|
|
CALL, // wywo³anie funkcji
|
|
RETURN, // return x
|
|
DIV, // dzielenie
|
|
MOD, // Reszta z dzelenia (%)
|
|
NOP // pusta instrukcja
|
|
};
|
|
|
|
// Pojedynczy rozkaz kompilatora (Intermediate Representation)
|
|
struct Instruction {
|
|
OpType type;
|
|
std::string arg1;
|
|
std::string arg2;
|
|
std::string arg3;
|
|
};
|
|
|
|
// 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
|
|
};
|
|
|
|
struct CompilerState {
|
|
// Mapa wszystkich funkcji (klucz to nazwa)
|
|
std::map<std::string, Function> functions;
|
|
|
|
// Zmienne globalne (tylko nazwa -> wartoœæ pocz¹tkowa)
|
|
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)
|
|
|
|
std::stack<std::string> blockStack;
|
|
|
|
std::map<std::string, std::string> stringLiterals;
|
|
int stringCounter = 0; // Licznik do generowania nazw str_1, str_2...
|
|
};
|
|
|
|
#endif
|