Fix loops

This commit is contained in:
2025-05-11 13:55:52 +10:00
parent fe3002e661
commit ae858894b9
2 changed files with 22 additions and 17 deletions

View File

@@ -1,8 +1,8 @@
let int myNum 1; let int myNum 1;
while myNum != 1001 { while myNum != 1000 {
println myNum; println myNum;
myNum = myNum + 1; myNum = myNum + 1;
} }
println "The loop has ended"; println "Finished";

View File

@@ -249,20 +249,17 @@ void Interpreter::executeCode(vector<Token> tokens) {
if (skipScope > 0) { if (skipScope > 0) {
int braceCount = 0; int braceCount = 0;
while (i < tokens.size()) { while (i < tokens.size()) {
int braceCount = 0; if (tokens[i].keyword == keywords::OBRAC) {
while (i < tokens.size()) { braceCount ++;
if (tokens[i].keyword == keywords::OBRAC) { } else if (tokens[i].keyword == keywords::CBRAC) {
braceCount ++; if (braceCount == 0) {
} else if (tokens[i].keyword == keywords::CBRAC) { skipScope --;
if (braceCount == 0) { break;
skipScope --; } else {
break; braceCount --;
} else {
braceCount --;
}
} }
i++;
} }
i++;
} }
} }
if (repeatScope > 0) { if (repeatScope > 0) {
@@ -414,10 +411,18 @@ void Interpreter::executeCode(vector<Token> tokens) {
loop = tokenIndex - 2 - lengthOfLine; loop = tokenIndex - 2 - lengthOfLine;
repeatScope ++; repeatScope ++;
log.debug("While loop condition is true, will skip back to " + to_string(loop)); log.debug("While loop condition is true, will skip back to " + to_string(loop));
} else if (!get<bool>(tokens[i].value.value)) {
int braceLevel = 0;
while (++i < tokens.size()) {
if (tokens[i].keyword == keywords::OBRAC) braceLevel ++;
else if (tokens[i].keyword == keywords::CBRAC) {
if (braceLevel == 0) break;
braceLevel --;
}
break;
}
} else { } else {
log.debug("Condition is false, skipping scope"); syntaxError.generalError("Achievement get: How did we get here?");
skipScope ++;
return;
} }
} else { } else {
if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get<string>(tokens[i].value.value)) != variables.end()) { if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get<string>(tokens[i].value.value)) != variables.end()) {