added int func(int x, int y), return int
This commit is contained in:
@@ -1,102 +1,209 @@
|
|||||||
#include "codegen.h"
|
#include "codegen.h"
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// Pomocnicza funkcja: sprawdza czy string to czysta liczba
|
||||||
|
bool isNumber(const std::string& s) {
|
||||||
|
if (s.empty()) return false;
|
||||||
|
size_t start = (s[0] == '-') ? 1 : 0;
|
||||||
|
for (size_t i = start; i < s.length(); i++) {
|
||||||
|
if (!isdigit(s[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zamienia nazwê zmiennej na adres pamiêci [rbp-X] lub liczbê
|
||||||
|
std::string getVarLocation(const std::string& name, const std::map<std::string, int>& locals) {
|
||||||
|
std::string cleanName = name;
|
||||||
|
// Usuwamy ewentualne spacje
|
||||||
|
size_t first = cleanName.find_first_not_of(" \t");
|
||||||
|
if (first != std::string::npos) cleanName = cleanName.substr(first);
|
||||||
|
size_t last = cleanName.find_last_not_of(" \t");
|
||||||
|
if (last != std::string::npos) cleanName = cleanName.substr(0, last + 1);
|
||||||
|
|
||||||
|
if (cleanName.empty()) return "0";
|
||||||
|
if (isNumber(cleanName)) return cleanName;
|
||||||
|
|
||||||
|
if (cleanName == "RAX") return "eax";
|
||||||
|
|
||||||
|
if (locals.count(cleanName)) {
|
||||||
|
int offset = locals.at(cleanName);
|
||||||
|
return "[rbp-" + std::to_string(offset) + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zwracamy orygina³ (jeœli to np. nazwa etykiety), ale to zazwyczaj b³¹d dla zmiennych
|
||||||
|
return cleanName;
|
||||||
|
}
|
||||||
|
|
||||||
std::string generateAssembly(const CompilerState& state) {
|
std::string generateAssembly(const CompilerState& state) {
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
result += "global main\n";
|
result += "global main\n";
|
||||||
result += "extern printf\n";
|
result += "extern printf\n";
|
||||||
result += "extern GetAsyncKeyState\n\n";
|
result += "extern getchar\n"; // <--- DODAJ TO
|
||||||
|
|
||||||
result += "section .data\n";
|
result += "section .data\n";
|
||||||
for (const auto& v : state.variables) {
|
|
||||||
result += " " + v.first + " dd " + std::to_string(v.second) + "\n";
|
|
||||||
}
|
|
||||||
result += " fmt db '%d', 10, 0\n";
|
result += " fmt db '%d', 10, 0\n";
|
||||||
result += " pause_msg db 'Nacisnij ESC aby zamknac...', 10, 0\n\n";
|
result += "section .text\n\n";
|
||||||
|
|
||||||
result += "section .text\n";
|
for (const auto& pair : state.functions) {
|
||||||
|
const Function& func = pair.second;
|
||||||
|
result += func.name + ":\n";
|
||||||
|
|
||||||
// --- FUNKCJE ---
|
result += " push rbp\n";
|
||||||
for (const auto& func : state.functions) {
|
result += " mov rbp, rsp\n";
|
||||||
result += func.first + ":\n";
|
result += " sub rsp, 256\n";
|
||||||
result += " sub rsp, 40\n";
|
|
||||||
|
|
||||||
for (const std::string& cmd : func.second) {
|
std::map<std::string, int> stackMap;
|
||||||
|
int currentStack = 8;
|
||||||
|
|
||||||
result += " ; CMD: " + cmd + "\n";
|
// 1. ARGUMENTY
|
||||||
|
if (func.args.size() > 0) {
|
||||||
if (cmd.find("PRINT:") == 0) {
|
stackMap[func.args[0]] = currentStack;
|
||||||
std::string varName = cmd.substr(6);
|
result += " mov [rbp-" + std::to_string(currentStack) + "], rcx ; arg " + func.args[0] + "\n";
|
||||||
if (state.variables.count(varName)) {
|
currentStack += 8;
|
||||||
result += " mov edx, [" + varName + "]\n";
|
|
||||||
result += " lea rcx, [rel fmt]\n";
|
|
||||||
result += " call printf\n";
|
|
||||||
}
|
}
|
||||||
|
if (func.args.size() > 1) {
|
||||||
|
stackMap[func.args[1]] = currentStack;
|
||||||
|
result += " mov [rbp-" + std::to_string(currentStack) + "], rdx ; arg " + func.args[1] + "\n";
|
||||||
|
currentStack += 8;
|
||||||
}
|
}
|
||||||
else if (cmd.find("IF:") == 0) {
|
|
||||||
int ifIndex = std::stoi(cmd.substr(3));
|
|
||||||
if (ifIndex >= 0 && ifIndex < state.ifBlocks.size()) {
|
|
||||||
const auto& ifBlock = state.ifBlocks[ifIndex];
|
|
||||||
|
|
||||||
if (state.variables.count(ifBlock.conditionVar)) {
|
// 2. INSTRUKCJE
|
||||||
std::string labelSkip = "skip_if_" + std::to_string(ifIndex);
|
for (const auto& instr : func.instructions) {
|
||||||
|
|
||||||
result += " ; --- IF START [" + ifBlock.conditionVar + "] ---\n";
|
// Rezerwacja miejsca dla nowych zmiennych
|
||||||
result += " mov eax, [" + ifBlock.conditionVar + "]\n";
|
if ((instr.type == OpType::ASSIGN || instr.type == OpType::ADD || instr.type == OpType::EQ)
|
||||||
|
&& stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
|
||||||
|
stackMap[instr.arg1] = currentStack;
|
||||||
|
currentStack += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (instr.type) {
|
||||||
|
case OpType::ASSIGN: {
|
||||||
|
std::string src = getVarLocation(instr.arg2, stackMap);
|
||||||
|
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||||
|
result += " mov eax, " + src + "\n";
|
||||||
|
result += " mov " + dst + ", eax\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OpType::ADD: {
|
||||||
|
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||||
|
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||||
|
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||||
|
result += " mov eax, " + op1 + "\n";
|
||||||
|
result += " add eax, " + op2 + "\n";
|
||||||
|
result += " mov " + dst + ", eax\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OpType::EQ: {
|
||||||
|
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||||
|
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||||
|
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||||
|
result += " mov eax, " + op1 + "\n";
|
||||||
|
result += " cmp eax, " + op2 + "\n";
|
||||||
|
result += " sete al\n";
|
||||||
|
result += " movzx eax, al\n";
|
||||||
|
result += " mov " + dst + ", eax\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OpType::JMP_FALSE: {
|
||||||
|
// PARSOWANIE WARUNKU: np. "suma == 30" lub "test"
|
||||||
|
std::string condRaw = instr.arg2;
|
||||||
|
size_t eqPos = condRaw.find("==");
|
||||||
|
|
||||||
|
if (eqPos != std::string::npos) {
|
||||||
|
// Mamy porównanie w IFie (a == b)
|
||||||
|
std::string leftStr = condRaw.substr(0, eqPos);
|
||||||
|
std::string rightStr = condRaw.substr(eqPos + 2);
|
||||||
|
|
||||||
|
std::string op1 = getVarLocation(leftStr, stackMap);
|
||||||
|
std::string op2 = getVarLocation(rightStr, stackMap);
|
||||||
|
|
||||||
|
result += " mov eax, " + op1 + "\n";
|
||||||
|
result += " cmp eax, " + op2 + "\n";
|
||||||
|
result += " jne " + instr.arg1 + " ; jump if NOT equal\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Zwyk³a zmienna boolowska (if test)
|
||||||
|
std::string cond = getVarLocation(condRaw, stackMap);
|
||||||
|
result += " mov eax, " + cond + "\n";
|
||||||
result += " test eax, eax\n";
|
result += " test eax, eax\n";
|
||||||
result += " jz " + labelSkip + "\n";
|
result += " jz " + instr.arg1 + " ; jump if zero\n";
|
||||||
|
|
||||||
for (const std::string& innerPrint : ifBlock.prints) {
|
|
||||||
if (state.variables.count(innerPrint)) {
|
|
||||||
result += " mov edx, [" + innerPrint + "]\n";
|
|
||||||
result += " lea rcx, [rel fmt]\n";
|
|
||||||
result += " call printf\n";
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
result += labelSkip + ":\n";
|
case OpType::LABEL: {
|
||||||
|
result += instr.arg1 + ":\n";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case OpType::RETURN: {
|
||||||
}
|
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||||
}
|
result += " mov eax, " + val + " ; return value\n";
|
||||||
}
|
result += " leave\n";
|
||||||
result += " add rsp, 40\n";
|
|
||||||
result += " ret\n\n";
|
|
||||||
}
|
|
||||||
// ==main==
|
|
||||||
result += "main:\n";
|
|
||||||
result += " sub rsp, 40\n";
|
|
||||||
|
|
||||||
for (const std::string& call : state.printCalls) {
|
|
||||||
if (call.find("CALL_") == 0) {
|
|
||||||
std::string funcName = call.substr(5);
|
|
||||||
if (state.functions.count(funcName)) {
|
|
||||||
result += " call " + funcName + "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const std::string& var : state.globalPrints) {
|
|
||||||
if (state.variables.count(var)) {
|
|
||||||
result += " mov edx, [" + var + "]\n";
|
|
||||||
result += " lea rcx, [rel fmt]\n";
|
|
||||||
result += " call printf\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
result += " lea rcx, [rel pause_msg]\n";
|
|
||||||
result += " call printf\n";
|
|
||||||
result += "pause_loop:\n";
|
|
||||||
result += " mov ecx, 27\n";
|
|
||||||
result += " call GetAsyncKeyState\n";
|
|
||||||
result += " test ax, 8000h\n";
|
|
||||||
result += " jz pause_loop\n";
|
|
||||||
result += " add rsp, 40\n";
|
|
||||||
result += " ret\n";
|
result += " ret\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OpType::PRINT: {
|
||||||
|
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||||
|
result += " mov edx, " + val + "\n";
|
||||||
|
result += " lea rcx, [rel fmt]\n";
|
||||||
|
result += " call printf\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OpType::CALL: {
|
||||||
|
// Parsowanie argumentów
|
||||||
|
std::string argsRaw = instr.arg2;
|
||||||
|
std::vector<std::string> callArgs;
|
||||||
|
if (instr.arg1 == "input") {
|
||||||
|
result += " call getchar\n"; // Czeka na enter
|
||||||
|
break; // Wychodzimy, ¿eby nie robiæ standardowego call
|
||||||
|
}
|
||||||
|
if (!argsRaw.empty()) {
|
||||||
|
size_t comma = argsRaw.find(',');
|
||||||
|
if (comma != std::string::npos) {
|
||||||
|
callArgs.push_back(argsRaw.substr(0, comma));
|
||||||
|
callArgs.push_back(argsRaw.substr(comma + 1));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callArgs.push_back(argsRaw);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obs³uga RDX (arg 2)
|
||||||
|
if (callArgs.size() > 1) {
|
||||||
|
std::string val = getVarLocation(callArgs[1], stackMap);
|
||||||
|
if (isNumber(val)) {
|
||||||
|
// Jeœli liczba: mov rdx, 100
|
||||||
|
result += " mov rdx, " + val + "\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Jeœli zmienna/pamiêæ: movsxd rdx, dword [rbp-8]
|
||||||
|
result += " movsxd rdx, dword " + val + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obs³uga RCX (arg 1)
|
||||||
|
if (callArgs.size() > 0) {
|
||||||
|
std::string val = getVarLocation(callArgs[0], stackMap);
|
||||||
|
if (isNumber(val)) {
|
||||||
|
result += " mov rcx, " + val + "\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result += " movsxd rcx, dword " + val + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result += " call " + instr.arg1 + "\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (func.returnType == "void") {
|
||||||
|
result += " leave\n ret\n";
|
||||||
|
}
|
||||||
|
result += "\n";
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,28 +6,51 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
struct Expression {
|
// Typy operacji, które nasz kompilator rozumie
|
||||||
std::string leftVar;
|
enum class OpType {
|
||||||
std::string op;
|
ASSIGN, // a = 5
|
||||||
std::string rightVar;
|
ADD, // a = b + c
|
||||||
std::string resultVar;
|
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
|
||||||
|
NOP // pusta instrukcja
|
||||||
};
|
};
|
||||||
|
|
||||||
struct IfBlock {
|
// Pojedynczy rozkaz kompilatora (Intermediate Representation)
|
||||||
std::string conditionVar;
|
struct Instruction {
|
||||||
std::vector<std::string> prints;
|
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 {
|
struct CompilerState {
|
||||||
std::vector<Expression> expressions;
|
// Mapa wszystkich funkcji (klucz to nazwa)
|
||||||
std::vector<IfBlock> ifBlocks;
|
std::map<std::string, Function> functions;
|
||||||
std::map<std::string, int> variables;
|
|
||||||
std::vector<std::string> printCalls;
|
// Zmienne globalne (tylko nazwa -> wartoœæ pocz¹tkowa)
|
||||||
std::vector<std::string> globalPrints;
|
std::map<std::string, int> globals;
|
||||||
std::map<std::string, std::vector<std::string>> functions;
|
|
||||||
bool inFunction = false;
|
// Stan parsera
|
||||||
std::string currentFunction;
|
Function* currentFunction = nullptr; // WskaŸnik na aktualnie parsuj¹c¹ siê funkcjê
|
||||||
std::stack<bool> braceStack;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,212 +2,171 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include "compiler_types.h"
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<std::string> parseArgs(const std::string& line) {
|
||||||
|
std::vector<std::string> args;
|
||||||
|
size_t open = line.find('(');
|
||||||
|
size_t close = line.find(')');
|
||||||
|
if (open == std::string::npos || close == std::string::npos) return args;
|
||||||
|
|
||||||
|
std::string inside = line.substr(open + 1, close - open - 1);
|
||||||
|
if (inside.empty()) return args;
|
||||||
|
|
||||||
|
std::stringstream ss(inside);
|
||||||
|
std::string segment;
|
||||||
|
while (std::getline(ss, segment, ',')) {
|
||||||
|
segment = trim(segment);
|
||||||
|
// segment to np. "int a". Szukamy ostatniej spacji, by wziąć nazwę "a"
|
||||||
|
size_t space = segment.find_last_of(" \t");
|
||||||
|
if (space != std::string::npos) {
|
||||||
|
args.push_back(trim(segment.substr(space + 1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
void processSource(const std::string& src, CompilerState& state) {
|
void processSource(const std::string& src, CompilerState& state) {
|
||||||
std::istringstream iss(src);
|
std::istringstream iss(src);
|
||||||
std::string line;
|
std::string line;
|
||||||
|
|
||||||
std::cout << "Parsuje kod:\n";
|
|
||||||
|
|
||||||
while (std::getline(iss, line)) {
|
while (std::getline(iss, line)) {
|
||||||
line = trim(line);
|
line = trim(line);
|
||||||
if (line.empty()) continue;
|
if (line.empty() || line.substr(0, 2) == "//" || line[0] == '#') continue;
|
||||||
|
|
||||||
// FUNKCJE
|
// --- 1. DEFINICJA FUNKCJI ---
|
||||||
if (line.length() > 4 && line.substr(0, 4) == "void") {
|
// Warunki: zaczyna się od typu, ma '(', ma '{' i NIE ma '=' (żeby nie mylić ze zmienną)
|
||||||
size_t openParen = line.find("(", 4);
|
bool startsWithType = (line.rfind("int ", 0) == 0 || line.rfind("void ", 0) == 0 || line.rfind("bool ", 0) == 0);
|
||||||
size_t closeParen = line.find(")", openParen);
|
|
||||||
if (openParen != std::string::npos && closeParen != std::string::npos) {
|
|
||||||
size_t nameStart = 4;
|
|
||||||
while (nameStart < openParen && (line[nameStart] == ' ' || line[nameStart] == '\t')) nameStart++;
|
|
||||||
size_t nameEnd = openParen;
|
|
||||||
while (nameEnd > nameStart && (line[nameEnd - 1] == ' ' || line[nameEnd - 1] == '\t')) nameEnd--;
|
|
||||||
|
|
||||||
// ZMIANA: używamy 'state' zamiast 'compilerState'
|
if (startsWithType && line.find("(") != std::string::npos && line.find("{") != std::string::npos && line.find("=") == std::string::npos) {
|
||||||
state.currentFunction = line.substr(nameStart, nameEnd - nameStart);
|
|
||||||
state.functions[state.currentFunction] = std::vector<std::string>();
|
size_t openParen = line.find('(');
|
||||||
state.inFunction = true;
|
std::string typeRaw = line.substr(0, line.find(' '));
|
||||||
std::cout << " FUNC " << state.currentFunction << "\n";
|
std::string nameRaw = line.substr(typeRaw.length(), openParen - typeRaw.length());
|
||||||
state.braceStack.push(true);
|
std::string funcName = trim(nameRaw);
|
||||||
|
|
||||||
|
Function newFunc;
|
||||||
|
newFunc.name = funcName;
|
||||||
|
newFunc.returnType = typeRaw;
|
||||||
|
newFunc.args = parseArgs(line);
|
||||||
|
|
||||||
|
state.functions[funcName] = newFunc;
|
||||||
|
state.currentFunction = &state.functions[funcName];
|
||||||
|
|
||||||
|
std::cout << "[PARSER] New Function: " << funcName << "\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// } ZAMYKANIE KLAMRY
|
|
||||||
else if (line.find("}") != std::string::npos) {
|
|
||||||
if (!state.braceStack.empty()) {
|
|
||||||
// Sprawdzamy co zamykamy PRZED zdjęciem ze stosu
|
|
||||||
bool wasFunction = state.braceStack.top();
|
|
||||||
state.braceStack.pop();
|
|
||||||
|
|
||||||
// Jeśli zdjęliśmy 'true' (czyli znacznik funkcji) ORAZ stos jest pusty
|
// --- 2. ZAMYKANIE BLOKU '}' ---
|
||||||
// to znaczy, że zamknęliśmy funkcję główną.
|
if (line == "}") {
|
||||||
if (wasFunction && state.braceStack.empty()) {
|
// Najpierw sprawdzamy, czy zamykamy IF-a (czy jest coś na stosie bloków)
|
||||||
state.inFunction = false;
|
if (!state.blockStack.empty()) {
|
||||||
state.currentFunction.clear();
|
std::string label = state.blockStack.top();
|
||||||
std::cout << " END FUNC\n";
|
state.blockStack.pop();
|
||||||
|
if (state.currentFunction) {
|
||||||
|
state.currentFunction->instructions.push_back({ OpType::LABEL, label, "", "" });
|
||||||
|
}
|
||||||
|
std::cout << " [PARSER] } End IF block -> " << label << "\n";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Zamknęliśmy IF-a (lub inny blok wewnętrzny)
|
// Jeśli stos pusty, to koniec funkcji
|
||||||
std::cout << " END BLOCK (IF)\n";
|
state.currentFunction = nullptr;
|
||||||
}
|
std::cout << " [PARSER] } End Function\n";
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// INT
|
// --- JESTEŚMY W ŚRODKU FUNKCJI ---
|
||||||
if (line.length() > 3 && line.substr(0, 3) == "int") {
|
if (state.currentFunction) {
|
||||||
size_t eqPos = line.find("=", 4);
|
Function& f = *state.currentFunction;
|
||||||
if (eqPos != std::string::npos) {
|
|
||||||
size_t semiPos = line.find(";", eqPos);
|
|
||||||
if (semiPos != std::string::npos) {
|
|
||||||
std::string name = trim(line.substr(4, eqPos - 4));
|
|
||||||
std::string rightSide = trim(line.substr(eqPos + 1, semiPos - eqPos - 1));
|
|
||||||
|
|
||||||
std::string op;
|
// A. RETURN
|
||||||
size_t opPos;
|
if (line.substr(0, 6) == "return") {
|
||||||
|
std::string val = trim(line.substr(6));
|
||||||
size_t eqPos2 = rightSide.find("==");
|
if (!val.empty() && val.back() == ';') val.pop_back();
|
||||||
if (eqPos2 != std::string::npos && (eqPos2 + 1 < rightSide.length()) && rightSide[eqPos2 + 2] != '=') {
|
f.instructions.push_back({ OpType::RETURN, val, "", "" });
|
||||||
op = "==";
|
std::cout << " [PARSER] Return: " << val << "\n";
|
||||||
opPos = eqPos2;
|
|
||||||
}
|
}
|
||||||
else {
|
// B. PRINT
|
||||||
opPos = rightSide.find('+');
|
else if (line.substr(0, 5) == "print") {
|
||||||
if (opPos == std::string::npos) opPos = rightSide.find('-');
|
size_t start = line.find('(') + 1;
|
||||||
if (opPos != std::string::npos) op = rightSide.substr(opPos, 1);
|
size_t end = line.find(')');
|
||||||
}
|
if (start != std::string::npos && end != std::string::npos) {
|
||||||
|
std::string var = trim(line.substr(start, end - start));
|
||||||
if (opPos != std::string::npos && !op.empty()) {
|
f.instructions.push_back({ OpType::PRINT, var, "", "" });
|
||||||
std::string leftVar = trim(rightSide.substr(0, opPos));
|
|
||||||
std::string rightVar = trim(rightSide.substr(opPos + op.length()));
|
|
||||||
|
|
||||||
// ZMIANA: używamy 'state'
|
|
||||||
Expression expr{ leftVar, op, rightVar, name };
|
|
||||||
state.expressions.push_back(expr);
|
|
||||||
state.variables[name] = 0;
|
|
||||||
std::cout << " " << op << " EXPR " << name << " = " << leftVar << " " << op << " " << rightVar << "\n";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
int value = std::stoi(rightSide);
|
|
||||||
state.variables[name] = value;
|
|
||||||
std::cout << " VAR " << name << " = " << value << "\n";
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
// Usunięto '❌' żeby uniknąć warningów o kodowaniu (C4566)
|
|
||||||
std::cout << " [X] BLAD: '" << rightSide << "'\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// C. IF STATEMENT
|
||||||
}
|
else if (line.substr(0, 2) == "if") {
|
||||||
}
|
|
||||||
// BOOL
|
|
||||||
else if (!state.inFunction && line.length() > 4 && line.substr(0, 4) == "bool") {
|
|
||||||
size_t eqPos = line.find("=", 5);
|
|
||||||
if (eqPos != std::string::npos) {
|
|
||||||
size_t semiPos = line.find(";", eqPos);
|
|
||||||
if (semiPos != std::string::npos) {
|
|
||||||
std::string nameRaw = line.substr(5, eqPos - 5);
|
|
||||||
size_t nameStart = nameRaw.find_first_not_of(" \t");
|
|
||||||
size_t nameEnd = nameRaw.find_last_not_of(" \t");
|
|
||||||
std::string name = nameRaw.substr(nameStart, nameEnd - nameStart + 1);
|
|
||||||
|
|
||||||
std::string valueRaw = line.substr(eqPos + 1, semiPos - eqPos - 1);
|
|
||||||
size_t valStart = valueRaw.find_first_not_of(" \t");
|
|
||||||
size_t valEnd = valueRaw.find_last_not_of(" \t");
|
|
||||||
std::string valueStr = valueRaw.substr(valStart, valEnd - valStart + 1);
|
|
||||||
|
|
||||||
bool value = (valueStr == "true" || valueStr == "1");
|
|
||||||
state.variables[name] = value ? 1 : 0;
|
|
||||||
std::cout << " BOOL '" << name << "' = " << (value ? "true" : "false") << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// IF
|
|
||||||
else if (line.length() > 3 && line.substr(0, 2) == "if") {
|
|
||||||
size_t openParen = line.find("(");
|
size_t openParen = line.find("(");
|
||||||
size_t closeParen = line.find(")");
|
size_t closeParen = line.find(")");
|
||||||
|
|
||||||
if (openParen != std::string::npos && closeParen > openParen) {
|
if (openParen != std::string::npos && closeParen > openParen) {
|
||||||
std::string condition = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
std::string condition = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
||||||
|
std::string labelName = "L_" + std::to_string(state.labelCounter++);
|
||||||
|
|
||||||
IfBlock newIf;
|
// Skok warunkowy
|
||||||
newIf.conditionVar = condition;
|
f.instructions.push_back({ OpType::JMP_FALSE, labelName, condition, "" });
|
||||||
state.ifBlocks.push_back(newIf);
|
state.blockStack.push(labelName);
|
||||||
|
|
||||||
// WAŻNE: Dodajemy instrukcję "IF:numer" do listy rozkazów funkcji
|
std::cout << " [PARSER] IF (" << condition << ") -> Jump to " << labelName << "\n";
|
||||||
if (state.inFunction) {
|
|
||||||
int ifIndex = state.ifBlocks.size() - 1;
|
|
||||||
state.functions[state.currentFunction].push_back("IF:" + std::to_string(ifIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << " IF [" << condition << "] { <- blok #" << state.ifBlocks.size() - 1 << "\n";
|
|
||||||
state.braceStack.push(false); // false oznacza, że to klamra IFa (a nie funkcji)
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// PRINT
|
// D. PRZYPISANIE ZMIENNEJ (LUB DEKLARACJA)
|
||||||
else if (line.length() > 5 && line.substr(0, 5) == "print") {
|
// np. "int a = 5;" LUB "a = b + c;"
|
||||||
size_t openParen = line.find("(", 5);
|
else if (line.find("=") != std::string::npos) {
|
||||||
size_t closeParen = line.rfind(")");
|
size_t eqPos = line.find('=');
|
||||||
if (openParen != std::string::npos && closeParen > openParen) {
|
std::string leftSide = trim(line.substr(0, eqPos));
|
||||||
std::string varName = line.substr(openParen + 1, closeParen - openParen - 1);
|
std::string rightSide = trim(line.substr(eqPos + 1));
|
||||||
size_t varStart = varName.find_first_not_of(" \t");
|
if (!rightSide.empty() && rightSide.back() == ';') rightSide.pop_back();
|
||||||
size_t varEnd = varName.find_last_not_of(" \t");
|
|
||||||
|
|
||||||
if (varStart != std::string::npos) {
|
// Obsługa nazwy zmiennej (usuwanie "int ", "bool ")
|
||||||
varName = varName.substr(varStart, varEnd - varStart + 1);
|
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));
|
||||||
|
|
||||||
if (state.inFunction && !state.currentFunction.empty()) {
|
// 1. Czy to wywołanie funkcji? int x = func();
|
||||||
// SPRAWDZAMY CZY JESTEŚMY W IFIE
|
if (rightSide.find("(") != std::string::npos && rightSide.find(")") != std::string::npos) {
|
||||||
// (sprawdzamy szczyt stosu klamerek: false = IF, true = FUNC)
|
size_t open = rightSide.find('(');
|
||||||
if (!state.braceStack.empty() && state.braceStack.top() == false) {
|
std::string funcName = trim(rightSide.substr(0, open));
|
||||||
// Jesteśmy w IFie -> dodaj TYLKO do ifBlocks
|
std::string argsContent = rightSide.substr(open + 1, rightSide.find(')') - open - 1);
|
||||||
if (!state.ifBlocks.empty()) {
|
|
||||||
state.ifBlocks.back().prints.push_back(varName);
|
// CALL func
|
||||||
std::cout << " IF PRINT: " << varName << "\n";
|
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
|
||||||
|
// ASSIGN result (RAX) to variable
|
||||||
|
f.instructions.push_back({ OpType::ASSIGN, varName, "RAX", "" });
|
||||||
|
std::cout << " [PARSER] Call & Assign: " << varName << " = " << funcName << "()\n";
|
||||||
}
|
}
|
||||||
|
// 2. Czy to dodawanie? 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::ADD, varName, a, b });
|
||||||
}
|
}
|
||||||
|
// 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) {
|
||||||
|
size_t opPos = rightSide.find("==");
|
||||||
|
std::string a = trim(rightSide.substr(0, opPos));
|
||||||
|
std::string b = trim(rightSide.substr(opPos + 2));
|
||||||
|
f.instructions.push_back({ OpType::EQ, varName, a, b });
|
||||||
|
}
|
||||||
|
// 4. Zwykłe przypisanie: a = 5
|
||||||
else {
|
else {
|
||||||
// Jesteśmy w normalnej funkcji (poza ifem) -> dodaj jako rozkaz PRINT:
|
f.instructions.push_back({ OpType::ASSIGN, varName, rightSide, "" });
|
||||||
state.functions[state.currentFunction].push_back("PRINT:" + varName);
|
|
||||||
std::cout << " FUNC PRINT " << state.currentFunction << ": " << varName << "\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
// E. SAMODZIELNE WYWOŁANIE FUNKCJI (bez =)
|
||||||
// Print globalny (poza funkcjami)
|
// np. func();
|
||||||
state.globalPrints.push_back(varName);
|
else if (line.find("(") != std::string::npos && line.find(")") != std::string::npos) {
|
||||||
std::cout << " PRINT " << varName << "\n";
|
size_t open = line.find('(');
|
||||||
}
|
std::string funcName = trim(line.substr(0, open));
|
||||||
}
|
std::string argsContent = line.substr(open + 1, line.find(')') - open - 1);
|
||||||
}
|
|
||||||
}
|
|
||||||
// WYWOLYWANIE FUNKCJI;
|
|
||||||
else if (line.length() > 3 && line.find("();") != std::string::npos) {
|
|
||||||
size_t openParen = line.find("(");
|
|
||||||
if (openParen != std::string::npos) {
|
|
||||||
std::string funcName = line.substr(0, openParen);
|
|
||||||
size_t nameStart = funcName.find_first_not_of(" \t");
|
|
||||||
size_t nameEnd = funcName.find_last_not_of(" \t");
|
|
||||||
funcName = funcName.substr(nameStart, nameEnd - nameStart + 1);
|
|
||||||
|
|
||||||
state.printCalls.push_back("CALL_" + funcName); // ZMIANA: state
|
f.instructions.push_back({ OpType::CALL, funcName, argsContent, "" });
|
||||||
std::cout << " CALL FUNC " << funcName << "\n";
|
std::cout << " [PARSER] Call void: " << funcName << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void calculateExpressions(CompilerState& state) {}
|
||||||
void calculateExpressions(CompilerState& state) {
|
|
||||||
for (const auto& expr : state.expressions) {
|
|
||||||
if (state.variables.count(expr.leftVar) && state.variables.count(expr.rightVar)) {
|
|
||||||
int left = state.variables[expr.leftVar];
|
|
||||||
int right = state.variables[expr.rightVar];
|
|
||||||
int result = 0;
|
|
||||||
if (expr.op == "+") result = left + right;
|
|
||||||
else if (expr.op == "==") result = (left == right);
|
|
||||||
state.variables[expr.resultVar] = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user