fix: Multiple issues with the calculator
This commit is contained in:
parent
6a07a2f85d
commit
b365e16cc9
@ -16,15 +16,20 @@ namespace hex {
|
|||||||
public:
|
public:
|
||||||
MathEvaluator() = default;
|
MathEvaluator() = default;
|
||||||
|
|
||||||
|
struct Variable {
|
||||||
|
T value;
|
||||||
|
bool constant;
|
||||||
|
};
|
||||||
|
|
||||||
std::optional<T> evaluate(const std::string &input);
|
std::optional<T> evaluate(const std::string &input);
|
||||||
|
|
||||||
void registerStandardVariables();
|
void registerStandardVariables();
|
||||||
void registerStandardFunctions();
|
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);
|
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 {
|
[[nodiscard]] bool hasError() const {
|
||||||
return this->m_lastError.has_value();
|
return this->m_lastError.has_value();
|
||||||
@ -106,7 +111,7 @@ namespace hex {
|
|||||||
std::optional<std::queue<Token>> toPostfix(std::queue<Token> inputQueue);
|
std::optional<std::queue<Token>> toPostfix(std::queue<Token> inputQueue);
|
||||||
std::optional<T> evaluate(std::queue<Token> postfixTokens);
|
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::unordered_map<std::string, std::function<std::optional<T>(std::vector<T>)>> m_functions;
|
||||||
|
|
||||||
std::optional<std::string> m_lastError;
|
std::optional<std::string> m_lastError;
|
||||||
|
@ -345,7 +345,7 @@ namespace hex {
|
|||||||
evaluationStack.push(result);
|
evaluationStack.push(result);
|
||||||
} else if (front.type == TokenType::Variable) {
|
} else if (front.type == TokenType::Variable) {
|
||||||
if (this->m_variables.contains(front.name))
|
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 {
|
else {
|
||||||
this->setError("Unknown variable!");
|
this->setError("Unknown variable!");
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
@ -382,14 +382,14 @@ namespace hex {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
std::optional<T> MathEvaluator<T>::evaluate(const std::string &input) {
|
std::optional<T> MathEvaluator<T>::evaluate(const std::string &input) {
|
||||||
auto inputQueue = parseInput(input);
|
auto inputQueue = parseInput(input);
|
||||||
if (!inputQueue.has_value())
|
if (!inputQueue.has_value() || inputQueue->empty())
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
|
||||||
std::string resultVariable = "ans";
|
std::string resultVariable = "ans";
|
||||||
|
|
||||||
{
|
{
|
||||||
auto queueCopy = *inputQueue;
|
auto queueCopy = *inputQueue;
|
||||||
if (queueCopy.front().type == TokenType::Variable) {
|
if (queueCopy.front().type == TokenType::Variable && queueCopy.size() > 2) {
|
||||||
resultVariable = queueCopy.front().name;
|
resultVariable = queueCopy.front().name;
|
||||||
queueCopy.pop();
|
queueCopy.pop();
|
||||||
if (queueCopy.front().type != TokenType::Operator || queueCopy.front().op != Operator::Assign)
|
if (queueCopy.front().type != TokenType::Operator || queueCopy.front().op != Operator::Assign)
|
||||||
@ -407,16 +407,15 @@ namespace hex {
|
|||||||
|
|
||||||
auto result = evaluate(*postfixTokens);
|
auto result = evaluate(*postfixTokens);
|
||||||
|
|
||||||
if (result.has_value()) {
|
if (result.has_value() && !this->getVariables()[resultVariable].constant)
|
||||||
this->setVariable(resultVariable, result.value());
|
this->setVariable(resultVariable, result.value());
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void MathEvaluator<T>::setVariable(const std::string &name, T value) {
|
void MathEvaluator<T>::setVariable(const std::string &name, T value, bool constant) {
|
||||||
this->m_variables[name] = value;
|
this->m_variables[name] = { value, constant };
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -435,6 +434,9 @@ namespace hex {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void MathEvaluator<T>::registerStandardVariables() {
|
void MathEvaluator<T>::registerStandardVariables() {
|
||||||
this->setVariable("ans", 0);
|
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>
|
template<typename T>
|
||||||
|
@ -385,7 +385,12 @@ namespace hex::plugin::builtin {
|
|||||||
ImGui::TableSetupColumn("hex.builtin.tools.value"_lang);
|
ImGui::TableSetupColumn("hex.builtin.tools.value"_lang);
|
||||||
|
|
||||||
ImGui::TableHeadersRow();
|
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::TableNextRow();
|
||||||
ImGui::TableNextColumn();
|
ImGui::TableNextColumn();
|
||||||
ImGui::TextUnformatted(name.c_str());
|
ImGui::TextUnformatted(name.c_str());
|
||||||
@ -428,10 +433,9 @@ namespace hex::plugin::builtin {
|
|||||||
if (evaluate) {
|
if (evaluate) {
|
||||||
try {
|
try {
|
||||||
auto result = mathEvaluator.evaluate(mathInput);
|
auto result = mathEvaluator.evaluate(mathInput);
|
||||||
|
mathInput.clear();
|
||||||
if (result.has_value()) {
|
if (result.has_value()) {
|
||||||
mathHistory.push_back(result.value());
|
mathHistory.push_back(result.value());
|
||||||
mathInput.clear();
|
|
||||||
lastMathError.clear();
|
lastMathError.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user