ELSE update
This commit is contained in:
@@ -40,12 +40,29 @@ std::string registerStringLiteral(CompilerState& state, std::string content) {
|
||||
|
||||
void processSource(const std::string& src, CompilerState& state) {
|
||||
std::istringstream iss(src);
|
||||
std::string line;
|
||||
std::vector<std::string> lines;
|
||||
{
|
||||
std::istringstream iss(src);
|
||||
std::string t;
|
||||
while (std::getline(iss, t)) lines.push_back(trim(t));
|
||||
}
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
line = trim(line);
|
||||
for (size_t i = 0; i < lines.size(); i++) {
|
||||
std::string line = trim(lines[i]);
|
||||
if (line.empty() || line.substr(0, 2) == "//" || line[0] == '#') continue;
|
||||
|
||||
if (line.find("} else") != std::string::npos) {
|
||||
// najpierw obsłuż zamknięcie bloku
|
||||
std::string saved = line;
|
||||
line = "}";
|
||||
// ... uruchom kod obsługi "}" (najlepiej przenieś go do funkcji pomocniczej)
|
||||
// potem obsłuż else:
|
||||
line = "else";
|
||||
// ... uruchom kod obsługi else
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// =========================================================
|
||||
// 1. DEFINICJA FUNKCJI (np. void main() { )
|
||||
// =========================================================
|
||||
@@ -72,42 +89,79 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
// =========================================================
|
||||
// 2. ZAMYKANIE BLOKU '}' (Koniec funkcji, IF‑a lub WHILE‑a)
|
||||
// =========================================================
|
||||
if (!line.empty() && line.back() == '}') {
|
||||
if (line == "}") {
|
||||
|
||||
// lookahead: czy następna sensowna linia to "else" / "else {"
|
||||
bool nextIsElse = false;
|
||||
size_t j = i + 1;
|
||||
while (j < lines.size()) {
|
||||
std::string nl = trim(lines[j]);
|
||||
if (nl.empty() || nl.rfind("//", 0) == 0) { j++; continue; }
|
||||
if (nl.rfind("else", 0) == 0) nextIsElse = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!state.blockStack.empty()) {
|
||||
std::string blockInfo = state.blockStack.top();
|
||||
state.blockStack.pop();
|
||||
|
||||
bool isWhile = (blockInfo.length() > 6 && blockInfo.substr(0, 6) == "WHILE|");
|
||||
// --- WHILE ---
|
||||
if (blockInfo.rfind("WHILE|", 0) == 0) {
|
||||
state.blockStack.pop();
|
||||
|
||||
if (isWhile) {
|
||||
size_t firstPipe = blockInfo.find('|');
|
||||
size_t secondPipe = blockInfo.rfind('|');
|
||||
std::string labelStart = blockInfo.substr(firstPipe + 1, secondPipe - firstPipe - 1);
|
||||
std::string labelEnd = blockInfo.substr(secondPipe + 1);
|
||||
size_t p1 = blockInfo.find('|');
|
||||
size_t p2 = blockInfo.rfind('|');
|
||||
std::string labelStart = blockInfo.substr(p1 + 1, p2 - p1 - 1);
|
||||
std::string labelEnd = blockInfo.substr(p2 + 1);
|
||||
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::JMP, labelStart, "", "" });
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, labelEnd, "", "" });
|
||||
}
|
||||
std::cout << " [PARSER] } End WHILE loop\n";
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
// To zwykły blok (if lub else)
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, blockInfo, "", "" });
|
||||
|
||||
// --- IF (specjalny wpis IF|else|end) ---
|
||||
if (blockInfo.rfind("IF|", 0) == 0) {
|
||||
state.blockStack.pop();
|
||||
|
||||
size_t p1 = blockInfo.find('|');
|
||||
size_t p2 = blockInfo.rfind('|');
|
||||
std::string labelElse = blockInfo.substr(p1 + 1, p2 - p1 - 1);
|
||||
std::string labelEnd = blockInfo.substr(p2 + 1);
|
||||
|
||||
if (nextIsElse) {
|
||||
// zamykamy blok IF, ale zaraz będzie ELSE:
|
||||
// 1) przeskocz ELSE po wykonaniu IF
|
||||
// 2) wstaw początek ELSE
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::JMP, labelEnd, "", "" });
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, labelElse, "", "" });
|
||||
}
|
||||
// Teraz oczekujemy na '}' kończące ELSE -> ma wstawić LABEL labelEnd
|
||||
state.blockStack.push(labelEnd);
|
||||
}
|
||||
std::cout << " [PARSER] } End block -> " << blockInfo << "\n";
|
||||
else {
|
||||
// if bez else: labelElse jest po prostu "koniec if"
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, labelElse, "", "" });
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- zwykły LABEL na stosie (np. koniec ELSE: labelEnd) ---
|
||||
state.blockStack.pop();
|
||||
if (state.currentFunction) {
|
||||
state.currentFunction->instructions.push_back({ OpType::LABEL, blockInfo, "", "" });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
// Koniec funkcji
|
||||
state.currentFunction = nullptr;
|
||||
std::cout << " [PARSER] } End Function\n";
|
||||
}
|
||||
|
||||
// jeśli stos pusty -> zamykamy funkcję
|
||||
state.currentFunction = nullptr;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// =========================================================
|
||||
// JESTEŚMY W ŚRODKU FUNKCJI
|
||||
// =========================================================
|
||||
@@ -193,29 +247,21 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
}
|
||||
|
||||
// E. IF STATEMENT (z ulepszoną obsługą else)
|
||||
else if (line.substr(0, 2) == "if") {
|
||||
else if (line.rfind("if", 0) == 0) {
|
||||
size_t openParen = line.find("(");
|
||||
size_t closeParen = line.rfind(")");
|
||||
|
||||
if (openParen != std::string::npos && closeParen > openParen) {
|
||||
std::string conditionRaw = trim(line.substr(openParen + 1, closeParen - openParen - 1));
|
||||
std::string finalConditionVar = conditionRaw;
|
||||
|
||||
// Obsługa prostych operacji w warunku
|
||||
if (conditionRaw.find("==") != std::string::npos) {
|
||||
size_t eq = conditionRaw.find("==");
|
||||
std::string tempRes = "_tmp_eq_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::EQ, tempRes, trim(conditionRaw.substr(0, eq)), trim(conditionRaw.substr(eq + 2)) });
|
||||
finalConditionVar = tempRes;
|
||||
}
|
||||
|
||||
std::string labelFalse = "L_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::JMP_FALSE, labelFalse, finalConditionVar, "" });
|
||||
state.blockStack.push(labelFalse);
|
||||
std::string labelElse = "L_" + std::to_string(state.labelCounter++);
|
||||
std::string labelEnd = "L_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::JMP_FALSE, labelElse, conditionRaw, "" });
|
||||
state.blockStack.push("IF|" + labelElse + "|" + labelEnd);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// F. WHILE LOOP
|
||||
else if (line.substr(0, 5) == "while") {
|
||||
size_t openParen = line.find("(");
|
||||
@@ -285,19 +331,7 @@ void processSource(const std::string& src, CompilerState& state) {
|
||||
continue;
|
||||
}
|
||||
|
||||
else if (line.substr(0, 4) == "else") {
|
||||
if (!state.blockStack.empty()) {
|
||||
std::string labelFalse = state.blockStack.top();
|
||||
state.blockStack.pop();
|
||||
|
||||
// Po bloku `if` skocz do końca `if‑else`:
|
||||
std::string labelEnd = "L_" + std::to_string(state.labelCounter++);
|
||||
f.instructions.push_back({ OpType::JMP, labelEnd, "", "" });
|
||||
f.instructions.push_back({ OpType::LABEL, labelFalse, "", "" }); // początek ELSE
|
||||
state.blockStack.push(labelEnd);
|
||||
|
||||
std::cout << " [PARSER] else block\n";
|
||||
}
|
||||
else if (line.rfind("else", 0) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user