diff --git a/mx b/mx index 5b66310..5725e58 100755 Binary files a/mx and b/mx differ diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index 3781416..371f825 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -114,6 +114,36 @@ void Interpreter::convertToTokens(vector 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(currentInstruction[i - 1].value.value) == get(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(currentInstruction[i - 1].value.value) == get(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(currentInstruction[i - 1].value.value) == get(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(currentInstruction[i - 1].value.value) == get(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); } diff --git a/src/SyntaxError.cpp b/src/SyntaxError.cpp index 2eb923f..365ae3b 100644 --- a/src/SyntaxError.cpp +++ b/src/SyntaxError.cpp @@ -51,4 +51,8 @@ void SyntaxError::generalError(string notes) { cerr << "GeneralError: Something went awfully wrong and we don't know why lmao" << endl; 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; } \ No newline at end of file diff --git a/src/SyntaxError.h b/src/SyntaxError.h index c41bed7..8cc3341 100644 --- a/src/SyntaxError.h +++ b/src/SyntaxError.h @@ -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 = ""); }; \ No newline at end of file diff --git a/test.mx b/test.mx index eae21cc..0685d91 100644 --- a/test.mx +++ b/test.mx @@ -9,4 +9,6 @@ print myInt; let str myString "dingus"; print myString; myString = "dongus"; -print myString; \ No newline at end of file +print myString; +print 5 == 5; +print 3.14 == 3.13; \ No newline at end of file