Compare commits

..

No commits in common. "b034b061b38d8a6ba42f418967751a2dd82a20c1" and "d93b2c39bbbd90de3be1b9385a5c131177e49ca3" have entirely different histories.

View File

@ -19,11 +19,11 @@ enum class VarType {
struct Variable {
VarType type;
variant<int, double, string, bool> value;
variant<int, double, string> value;
Variable() : type(VarType::UNKNOWN) {}
Variable(VarType t, const variant<int, double, string, bool>& v) : type(t), value(v) {}
Variable(VarType t, const variant<int, double, string>& v) : type(t), value(v) {}
string toString() const {
switch(type) {
@ -33,8 +33,6 @@ struct Variable {
return to_string(get<double>(value));
case VarType::STRING:
return get<string>(value);
case VarType::BOOLEAN:
return get<bool>(value) ? "true" : "false";
default:
return "unknown";
}
@ -64,8 +62,6 @@ private:
const vector<string> comparitors = {
"==", ">", ">=", "=<", "!=", "!<", "!>", "!=<", "!>="
};
const vector<string> bools = { "true", "false" };
bool checkForDec(const string& str) {
try {
@ -191,10 +187,8 @@ private:
for (const auto& comp : comparitors) {
if (token == comp) return "comparitor";
}
for (const auto& boolean : bools) {
if (token == boolean) return "boolean";
}
if (variables.find(token) != variables.end()) return "variable";
if (token == "=") return "equals";
if (token[0] == '"') return "str";
if (checkForInt(token)) return "int";
@ -216,51 +210,13 @@ private:
return "Error";
}
double doDecimalMath(const vector<pair<string, string>>& tokens) {
verbose("Var type is decimal");
if (tokens.size() == 2 && tokens[1].second == "incrementor") {
verbose("Incrementing...");
auto var = handleVariable(tokens[0].first);
if (!std::holds_alternative<double>(var)) {
error("Only numbers can be incremented");
return 0;
}
if (tokens[1].first == "++") return get<double>(var) + 1;
if (tokens[1].first == "--") return get<double>(var) - 1;
return 0;
}
if (tokens.size() == 5 && tokens[1].first == "=" && tokens[3].second == "operator") {
verbose("Detected an operator");
int left = (tokens[2].second == "variable") ?
get<int>(handleVariable(tokens[2].first)) :
stoi(tokens[2].first);
int right = (tokens[4].second == "variable") ?
get<int>(handleVariable(tokens[4].first)) :
stoi(tokens[4].first);
if (tokens[3].first == "+") return left + right;
if (tokens[3].first == "-") return left - right;
if (tokens[3].first == "*") return left * right;
if (tokens[3].first == "/") {
if (right == 0) {
error("Division by zero");
return 0;
}
return left / right;
}
}
error("Invalid operation");
return 0;
}
int doIntMath(const vector<pair<string, string>>& tokens) {
verbose("Var type is integer");
if (tokens.size() == 2 && tokens[1].second == "incrementor") {
verbose("Incrementing...");
auto var = handleVariable(tokens[0].first);
if (!std::holds_alternative<int>(var)) {
error("Only numbers can be incremented");
error("Only integers can be incremented");
return 0;
}
if (tokens[1].first == "++") return get<int>(var) + 1;
@ -269,6 +225,7 @@ private:
}
if (tokens.size() == 5 && tokens[1].first == "=" && tokens[3].second == "operator") {
verbose("Detected an operator");
int left = (tokens[2].second == "variable") ?
get<int>(handleVariable(tokens[2].first)) :
stoi(tokens[2].first);
@ -318,7 +275,7 @@ public:
error("Log requires an argument");
}
const auto& [token, type] = tokens[1];
if (type == "str" || type == "int" || type == "dec" || type == "boolean") {
if (type == "str" || type == "int" || type == "dec") {
cout << token << endl;
}
else {
@ -410,37 +367,7 @@ public:
verbose("String variable " + varName + " defined as " + value);
return true;
}
else if (tokens[0].first == "bool") {
verbose("Boolean function run");
if (tokens.size() < 2) {
error("variable must have a name");
}
if (tokens.size() < 4) {
error("when defining a variable, set what the variable means");
}
const auto& varName = tokens[1].first;
if (tokens[2].first != "=") {
error("when defining a variable, use '='");
}
if (variables.find(varName) != variables.end()) {
error("variable is already initialized");
}
if (tokens[3].second != "boolean") {
error("you've initialized a boolean value, but set it's value to a different type");
}
if (tokens[3].first == "true" || tokens[3].first == "false") {
bool value = (tokens[3].first == "true");
variables[varName] = Variable(VarType::BOOLEAN, value);
} else {
error("Something went wrong");
}
verbose("Boolean variable " + varName + " defined as " + tokens[3].first);
return true;
}
else if (tokens[0].first == "int") {
verbose("Integer function run");
if (tokens.size() < 2) {
@ -519,8 +446,141 @@ public:
verbose("Mathing...");
var.value = doIntMath(tokens);
} else if (var.type == VarType::DECIMAL) {
verbose("Mathing...");
var.value = doDecimalMath(tokens);
if (tokens[1].second == "incrementor") {
if (tokens[1].first == "++") {
double currentValue = get<double>(var.value);
var.value = currentValue + 1;
} else if (tokens[1].second == "--") {
double currentValue = get<double>(var.value);
var.value = currentValue - 1;
}
} else if (tokens[3].second == "operator" && tokens[1].second == "equals") {
verbose("Detected an operator");
if (tokens[3].first == "+") {
verbose("Adding...");
if (tokens[2].second == "variable") {
verbose("Editing variable in 2nd token");
auto& varAdd = variables[tokens[2].first];
if (varAdd.type == VarType::INTEGER || varAdd.type == VarType::DECIMAL) {
tokens[2].first = to_string(get<double>(varAdd.value));
tokens[2].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
if (tokens[4].second == "variable") {
verbose("Editing variable in 4th token");
auto& varAdd = variables[tokens[4].first];
if (varAdd.type == VarType::INTEGER || varAdd.type == VarType::DECIMAL) {
tokens[4].first = to_string(get<double>(varAdd.value));
tokens[4].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
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");
}
verbose("Trying to add variables");
var.value = stod(tokens[2].first) + stoi(tokens[4].first);
return true;
}
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 || varAdd.type == VarType::DECIMAL) {
tokens[2].first = to_string(get<double>(varAdd.value));
tokens[2].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
if (tokens[4].second == "variable") {
verbose("Editing variable in 4th token");
auto& varAdd = variables[tokens[4].first];
if (varAdd.type == VarType::INTEGER || varAdd.type == VarType::DECIMAL) {
tokens[4].first = to_string(get<double>(varAdd.value));
tokens[4].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
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");
}
verbose("Trying to subtract variables");
var.value = stod(tokens[2].first) - stoi(tokens[4].first);
return true;
}
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 || varAdd.type == VarType::DECIMAL) {
tokens[2].first = to_string(get<double>(varAdd.value));
tokens[2].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
if (tokens[4].second == "variable") {
verbose("Editing variable in 4th token");
auto& varAdd = variables[tokens[4].first];
if (varAdd.type == VarType::INTEGER || varAdd.type == VarType::DECIMAL) {
tokens[4].first = to_string(get<double>(varAdd.value));
tokens[4].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
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");
}
verbose("Trying to multiply variables");
var.value = stod(tokens[2].first) * stoi(tokens[4].first);
return true;
}
if (tokens[3].first == "/") {
verbose("Dividing...");
if (tokens[2].second == "variable") {
verbose("Editing variable in 2nd token");
auto& varAdd = variables[tokens[2].first];
if (varAdd.type == VarType::INTEGER || varAdd.type == VarType::DECIMAL) {
tokens[2].first = to_string(get<double>(varAdd.value));
tokens[2].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
if (tokens[4].second == "variable") {
verbose("Editing variable in 4th token");
auto& varAdd = variables[tokens[4].first];
if (varAdd.type == VarType::INTEGER || varAdd.type == VarType::DECIMAL) {
tokens[4].first = to_string(get<double>(varAdd.value));
tokens[4].second = "int";
} else {
error("not all the variables you're adding are integers");
}
}
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");
}
if (tokens[2].first == "0" || tokens[4].first == "0") {
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)");
}
verbose("Trying to divide variables");
var.value = stod(tokens[2].first) / stoi(tokens[4].first);
return true;
}
}
}
return true;
}