try to add == support
This commit is contained in:
parent
a65dfdf6d2
commit
8957251808
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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 = "");
|
||||
};
|
Loading…
Reference in New Issue
Block a user