Rewrite and fix intmath function

This commit is contained in:
Maxwell Jeffress 2025-01-04 19:41:12 +11:00
parent 5a80bc3f44
commit d8e433ba4a

View File

@ -149,137 +149,43 @@ private:
return "Error"; return "Error";
} }
int doIntMath(vector<pair<string, string>> tokens) { int doIntMath(const vector<pair<string, string>>& tokens) {
verbose("Var type is integer"); verbose("Var type is integer");
if (tokens[1].second == "incrementor") { if (tokens.size() == 2 && tokens[1].second == "incrementor") {
verbose("Incrementing..."); verbose("Incrementing...");
if (tokens[1].first == "++") { auto var = handleVariable(tokens[0].first);
auto var = handleVariable(tokens[0].first); if (!std::holds_alternative<int>(var)) {
if (std::holds_alternative<int>(var)) return get<int>(var) + 1; error("Only integers can be incremented");
else error("Only integers can be incremented"); return 0;
} else if (tokens[1].second == "--") {
auto var = handleVariable(tokens[0].first);
if (std::holds_alternative<int>(var)) return get<int>(var) - 1;
else error("Only integers can be incremented");
} }
} else if (tokens[3].second == "operator" && tokens[1].second == "equals") { if (tokens[1].first == "++") return get<int>(var) + 1;
if (tokens[1].first == "--") return get<int>(var) - 1;
return 0;
}
if (tokens.size() == 5 && tokens[1].first == "=" && tokens[3].second == "operator") {
verbose("Detected an operator"); verbose("Detected an operator");
if (tokens[2].second != "int" || tokens[4].second != "int") {
verbose("Detected types are " + tokens[2].second + " and " + tokens[4].second); int left = (tokens[2].second == "variable") ?
error("make sure you're using integers and integers when setting an integer"); get<int>(handleVariable(tokens[2].first)) :
return 0; stoi(tokens[2].first);
} int right = (tokens[4].second == "variable") ?
if (tokens[3].first == "+") { get<int>(handleVariable(tokens[4].first)) :
verbose("Adding..."); stoi(tokens[4].first);
verbose("Trying to add variables");
return stoi(tokens[2].first) + stoi(tokens[4].first); if (tokens[3].first == "+") return left + right;
return 0; if (tokens[3].first == "-") return left - right;
} if (tokens[3].first == "*") return left * right;
if (tokens[3].first == "-") {
verbose("Subtracting...");
if (tokens[2].second == "variable") {
verbose("Editing variable in 2nd token");
auto& varAdd = variables[tokens[2].first];
if (varAdd.type == VarType::INTEGER) {
tokens[2].first = to_string(get<int>(varAdd.value));
tokens[2].second = "int";
} else {
error("not all the variables you're adding are integers");
return 0;
}
}
if (tokens[4].second == "variable") {
verbose("Editing variable in 4th token");
auto& varAdd = variables[tokens[4].first];
if (varAdd.type == VarType::INTEGER) {
tokens[4].first = to_string(get<int>(varAdd.value));
tokens[4].second = "int";
} else {
error("not all the variables you're adding are integers");
return 0;
}
}
if (tokens[2].second != "int" || tokens[4].second != "int") {
verbose("Detected types are " + tokens[2].second + " and " + tokens[4].second);
error("make sure you're adding integers and integers when setting an integer");
return 0;
}
verbose("Trying to subtract variables");
return stoi(tokens[2].first) - stoi(tokens[4].first);
return 0;
}
if (tokens[3].first == "*") {
verbose("Multiplying...");
if (tokens[2].second == "variable") {
verbose("Editing variable in 2nd token");
auto& varAdd = variables[tokens[2].first];
if (varAdd.type == VarType::INTEGER) {
tokens[2].first = to_string(get<int>(varAdd.value));
tokens[2].second = "int";
} else {
error("not all the variables you're adding are integers");
return 0;
}
}
if (tokens[4].second == "variable") {
verbose("Editing variable in 4th token");
auto& varAdd = variables[tokens[4].first];
if (varAdd.type == VarType::INTEGER) {
tokens[4].first = to_string(get<int>(varAdd.value));
tokens[4].second = "int";
} else {
error("not all the variables you're adding are integers");
return 0;
}
}
if (tokens[2].second != "int" || tokens[4].second != "int") {
verbose("Detected types are " + tokens[2].second + " and " + tokens[4].second);
error("make sure you're adding integers and integers when setting an integer");
return 0;
}
verbose("Trying to multiply variables");
return stoi(tokens[2].first) * stoi(tokens[4].first);
return 0;
}
if (tokens[3].first == "/") { if (tokens[3].first == "/") {
verbose("Dividing..."); if (right == 0) {
if (tokens[2].second == "variable") { error("Division by zero");
verbose("Editing variable in 2nd token");
auto& varAdd = variables[tokens[2].first];
if (varAdd.type == VarType::INTEGER) {
tokens[2].first = to_string(get<int>(varAdd.value));
tokens[2].second = "int";
} else {
error("not all the variables you're adding are integers");
return 0;
}
}
if (tokens[4].second == "variable") {
verbose("Editing variable in 4th token");
auto& varAdd = variables[tokens[4].first];
if (varAdd.type == VarType::INTEGER) {
tokens[4].first = to_string(get<int>(varAdd.value));
tokens[4].second = "int";
} else {
error("not all the variables you're adding are integers");
return 0;
}
}
if (tokens[2].second != "int" || tokens[4].second != "int") {
verbose("Detected types are " + tokens[2].second + " and " + tokens[4].second);
error("make sure you're adding integers and integers when setting an integer");
return 0; return 0;
} }
if (tokens[2].first == "0" || tokens[4].first == "0") { return left / right;
error("Don't divide by zero or the end of the universe will be upon us you idiot");
verbose("(please don't try any funny business i'm begging you)");
return 0;
}
verbose("Trying to divide variables");
return stoi(tokens[2].first) / stoi(tokens[4].first);
return 0;
} }
} }
error("Invalid operation");
return 0;
} }
public: public:
@ -467,11 +373,8 @@ public:
} }
auto& var = variables[tokens[0].first]; auto& var = variables[tokens[0].first];
if (var.type == VarType::INTEGER) { if (var.type == VarType::INTEGER) {
tokens.erase(tokens.begin() + 0);
tokens.erase(tokens.begin() + 1);
verbose("Mathing..."); verbose("Mathing...");
var.value = doIntMath(tokens); var.value = doIntMath(tokens);
} else if (var.type == VarType::DECIMAL) { } else if (var.type == VarType::DECIMAL) {
if (tokens[1].second == "incrementor") { if (tokens[1].second == "incrementor") {
if (tokens[1].first == "++") { if (tokens[1].first == "++") {