fix: Multiple issues with the calculator

This commit is contained in:
WerWolv 2022-10-02 14:18:56 +02:00
parent 6a07a2f85d
commit b365e16cc9
3 changed files with 24 additions and 13 deletions

View File

@ -16,15 +16,20 @@ namespace hex {
public:
MathEvaluator() = default;
struct Variable {
T value;
bool constant;
};
std::optional<T> evaluate(const std::string &input);
void registerStandardVariables();
void registerStandardFunctions();
void setVariable(const std::string &name, T value);
void setVariable(const std::string &name, T value, bool constant = false);
void setFunction(const std::string &name, const std::function<std::optional<T>(std::vector<T>)> &function, size_t minNumArgs, size_t maxNumArgs);
std::unordered_map<std::string, T> &getVariables() { return this->m_variables; }
std::unordered_map<std::string, Variable> &getVariables() { return this->m_variables; }
[[nodiscard]] bool hasError() const {
return this->m_lastError.has_value();
@ -106,7 +111,7 @@ namespace hex {
std::optional<std::queue<Token>> toPostfix(std::queue<Token> inputQueue);
std::optional<T> evaluate(std::queue<Token> postfixTokens);
std::unordered_map<std::string, T> m_variables;
std::unordered_map<std::string, Variable> m_variables;
std::unordered_map<std::string, std::function<std::optional<T>(std::vector<T>)>> m_functions;
std::optional<std::string> m_lastError;

View File

@ -345,7 +345,7 @@ namespace hex {
evaluationStack.push(result);
} else if (front.type == TokenType::Variable) {
if (this->m_variables.contains(front.name))
evaluationStack.push(this->m_variables.at(front.name));
evaluationStack.push(this->m_variables.at(front.name).value);
else {
this->setError("Unknown variable!");
return std::nullopt;
@ -382,14 +382,14 @@ namespace hex {
template<typename T>
std::optional<T> MathEvaluator<T>::evaluate(const std::string &input) {
auto inputQueue = parseInput(input);
if (!inputQueue.has_value())
if (!inputQueue.has_value() || inputQueue->empty())
return std::nullopt;
std::string resultVariable = "ans";
{
auto queueCopy = *inputQueue;
if (queueCopy.front().type == TokenType::Variable) {
if (queueCopy.front().type == TokenType::Variable && queueCopy.size() > 2) {
resultVariable = queueCopy.front().name;
queueCopy.pop();
if (queueCopy.front().type != TokenType::Operator || queueCopy.front().op != Operator::Assign)
@ -407,16 +407,15 @@ namespace hex {
auto result = evaluate(*postfixTokens);
if (result.has_value()) {
if (result.has_value() && !this->getVariables()[resultVariable].constant)
this->setVariable(resultVariable, result.value());
}
return result;
}
template<typename T>
void MathEvaluator<T>::setVariable(const std::string &name, T value) {
this->m_variables[name] = value;
void MathEvaluator<T>::setVariable(const std::string &name, T value, bool constant) {
this->m_variables[name] = { value, constant };
}
template<typename T>
@ -435,6 +434,9 @@ namespace hex {
template<typename T>
void MathEvaluator<T>::registerStandardVariables() {
this->setVariable("ans", 0);
this->setVariable("pi", std::numbers::pi, true);
this->setVariable("e", std::numbers::e, true);
this->setVariable("phi", std::numbers::phi, true);
}
template<typename T>

View File

@ -385,7 +385,12 @@ namespace hex::plugin::builtin {
ImGui::TableSetupColumn("hex.builtin.tools.value"_lang);
ImGui::TableHeadersRow();
for (const auto &[name, value] : mathEvaluator.getVariables()) {
for (const auto &[name, variable] : mathEvaluator.getVariables()) {
const auto &[value, constant] = variable;
if (constant)
continue;
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(name.c_str());
@ -428,10 +433,9 @@ namespace hex::plugin::builtin {
if (evaluate) {
try {
auto result = mathEvaluator.evaluate(mathInput);
mathInput.clear();
if (result.has_value()) {
mathHistory.push_back(result.value());
mathInput.clear();
lastMathError.clear();
}