try to add == support

This commit is contained in:
Maxwell 2025-04-25 11:08:29 +10:00
parent a65dfdf6d2
commit 8957251808
5 changed files with 38 additions and 1 deletions

BIN
mx

Binary file not shown.

View File

@ -114,6 +114,36 @@ void Interpreter::convertToTokens(vector<Token> tokenList) {
i -= 1;
}
}
// Detect any comparisons and convert
// Start at 1 to avoid having to do more crappy memory safety stuff
for (int i = 1; i < currentInstruction.size(); i++) {
if (currentInstruction[i].keyword == keywords::EQUAL) {
Token newToken;
newToken.keyword = keywords::VALUE;
newToken.type = valtype::BOOL;
newToken.value.type = valtype::BOOL;
if (currentInstruction.size() < i + 1) syntaxError.mathTooFewArgs();
if (currentInstruction[i - 1].value.type != currentInstruction[i + 1].value.type) syntaxError.mathTypeMismatch();
log.debug("Comparing stuff");
if (currentInstruction[i - 1].value.type == valtype::INT) {
if (get<int>(currentInstruction[i - 1].value.value) == get<int>(currentInstruction[i + 1].value.value)) newToken.value.value = true;
else newToken.value.value = false;
} else if (currentInstruction[i - 1].value.type == valtype::DEC) {
if (get<double>(currentInstruction[i - 1].value.value) == get<double>(currentInstruction[i + 1].value.value)) newToken.value.value = true;
else newToken.value.value = false;
} else if (currentInstruction[i - 1].value.type == valtype::STR) {
if (get<string>(currentInstruction[i - 1].value.value) == get<string>(currentInstruction[i + 1].value.value)) newToken.value.value = true;
else newToken.value.value = false;
} else if (currentInstruction[i - 1].value.type == valtype::BOOL) {
if (get<bool>(currentInstruction[i - 1].value.value) == get<bool>(currentInstruction[i + 1].value.value)) newToken.value.value = true;
else newToken.value.value = false;
}
currentInstruction[i - 1] = newToken;
currentInstruction.erase(currentInstruction.begin() + i);
currentInstruction.erase(currentInstruction.begin() + i);
i -= 1;
}
}
// Execute the instruction
executeCode(currentInstruction);
}

View File

@ -52,3 +52,7 @@ void SyntaxError::generalError(string notes) {
if (!notes.empty()) cerr << "Notes: " << notes << endl;
exit(1);
}
void SyntaxError::cannotCompareDifferentTypes(string notes) {
cerr << "ComparisonError: cannot compare two different types" << endl;
if (!notes.empty()) cerr << "Notes: " << notes << endl;
}

View File

@ -10,5 +10,6 @@ public:
void mathTypeMismatch(string notes = "");
void mathTooFewArgs(string notes = "");
void mathCannotDoOperationOnType(string operatorUsed, string type, string notes = "");
void cannotCompareDifferentTypes(string notes = "");
void generalError(string notes = "");
};

View File

@ -10,3 +10,5 @@ let str myString "dingus";
print myString;
myString = "dongus";
print myString;
print 5 == 5;
print 3.14 == 3.13;