Half working code

This commit is contained in:
Maxwell 2025-04-25 10:40:18 +10:00
parent 96f93ceb49
commit ab07efcb86
6 changed files with 19 additions and 3 deletions

BIN
mx

Binary file not shown.

View File

@ -239,6 +239,12 @@ void Interpreter::executeCode(vector<Token> tokens) {
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
if (tokens[i + 1].value.type != valtype::INT) syntaxError.mathTypeMismatch("Expected an int when subtracting from an int");
variables[varName].value = get<int>(variables[varName].value) - get<int>(tokens[i + 1].value.value);
} else if (tokens[i].keyword == keywords::SET) {
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
if (tokens[i + 1].value.type != valtype::INT) syntaxError.mathTypeMismatch("Expected an int when changing an int");
variables[varName].value = get<int>(tokens[i + 1].value.value);
} else {
syntaxError.mathCannotDoOperationOnType("unknown", "int");
}
} else if (variables[varName].type == valtype::DEC) {
if (tokens[i].keyword == keywords::INCREMENT) {
@ -253,6 +259,12 @@ void Interpreter::executeCode(vector<Token> tokens) {
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
if (tokens[i + 1].value.type != valtype::DEC) syntaxError.mathTypeMismatch("Expected an dec when subtracting from an dec");
variables[varName].value = get<double>(variables[varName].value) - get<double>(tokens[i + 1].value.value);
} else if (tokens[i].keyword == keywords::SET) {
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
if (tokens[i + 1].value.type != valtype::DEC) syntaxError.mathTypeMismatch("Expected a dec when changing a dec");
variables[varName].value = get<int>(tokens[i + 1].value.value);
} else {
syntaxError.mathCannotDoOperationOnType("unknown", "dec");
}
}
} else {

View File

@ -115,6 +115,7 @@ void Parser::processLines() {
else if (ct == "-") token.keyword = keywords::SUBTRACT;
else if (ct == "*") token.keyword = keywords::MULTIPLY;
else if (ct == "/") token.keyword = keywords::DIVIDE;
else if (ct == "=") token.keyword = keywords::SET;
else if (ct == "++") token.keyword = keywords::INCREMENT;
else if (ct == "--") token.keyword = keywords::DECREMENT;
else if (ct == "+=") token.keyword = keywords::ADDTO;

View File

@ -30,7 +30,7 @@ void SyntaxError::unknownFn() {
}
void SyntaxError::mathTypeMismatch(string notes) {
cerr << "MathError: cannot add two different types together" << endl;
cerr << "MathError: cannot use two different types together" << endl;
if (!notes.empty()) cerr << "Notes: " << notes << endl;
exit(1);
}

View File

@ -16,7 +16,7 @@ enum class valtype {
enum class keywords {
IF, ELSE, WHILE, INT, DEC, STR, BOOL, FUN, RETURN,
OPARE, CPARE, OBRAC, CBRAC,
ADDTO, SUBTRACTFROM, MULTIPLYTO, DIVIDEFROM,
SET, ADDTO, SUBTRACTFROM, MULTIPLYTO, DIVIDEFROM,
ADD, SUBTRACT, MULTIPLY, DIVIDE,
EQUAL, INEQUAL, LESS, GREATER, EQLESS, EQGREATER,
INCREMENT, DECREMENT,

View File

@ -3,3 +3,6 @@ let str bingus "heheheha its bingusing time";
print bingus;
print 1 + 1;
let int myInt 3;
print myInt;
myInt = 5;
print myInt;