2022-09-07 23:11:24 +02:00
|
|
|
#include <content/helpers/math_evaluator.hpp>
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/helpers/concepts.hpp>
|
|
|
|
|
2020-11-28 21:55:52 +01:00
|
|
|
#include <string>
|
|
|
|
#include <queue>
|
|
|
|
#include <stack>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
i16 MathEvaluator<T>::comparePrecedence(const Operator &a, const Operator &b) {
|
2022-03-04 19:20:21 +01:00
|
|
|
return static_cast<i16>((static_cast<i8>(a) & 0x0F0) - (static_cast<i8>(b) & 0x0F0));
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
2022-09-07 23:11:24 +02:00
|
|
|
bool MathEvaluator<T>::isLeftAssociative(const Operator &op) {
|
2020-11-28 21:55:52 +01:00
|
|
|
return (static_cast<u32>(op) & 0xF00) == 0;
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
std::pair<typename MathEvaluator<T>::Operator, size_t> MathEvaluator<T>::toOperator(const std::string &input) {
|
2022-01-24 20:53:17 +01:00
|
|
|
if (input.starts_with("##")) return { Operator::Combine, 2 };
|
|
|
|
if (input.starts_with("==")) return { Operator::Equals, 2 };
|
|
|
|
if (input.starts_with("!=")) return { Operator::NotEquals, 2 };
|
|
|
|
if (input.starts_with(">=")) return { Operator::GreaterThanOrEquals, 2 };
|
|
|
|
if (input.starts_with("<=")) return { Operator::LessThanOrEquals, 2 };
|
|
|
|
if (input.starts_with(">>")) return { Operator::ShiftRight, 2 };
|
|
|
|
if (input.starts_with("<<")) return { Operator::ShiftLeft, 2 };
|
|
|
|
if (input.starts_with("||")) return { Operator::Or, 2 };
|
|
|
|
if (input.starts_with("^^")) return { Operator::Xor, 2 };
|
|
|
|
if (input.starts_with("&&")) return { Operator::And, 2 };
|
|
|
|
if (input.starts_with("**")) return { Operator::Exponentiation, 2 };
|
|
|
|
if (input.starts_with(">")) return { Operator::GreaterThan, 1 };
|
|
|
|
if (input.starts_with("<")) return { Operator::LessThan, 1 };
|
|
|
|
if (input.starts_with("!")) return { Operator::Not, 1 };
|
|
|
|
if (input.starts_with("|")) return { Operator::BitwiseOr, 1 };
|
|
|
|
if (input.starts_with("^")) return { Operator::BitwiseXor, 1 };
|
|
|
|
if (input.starts_with("&")) return { Operator::BitwiseAnd, 1 };
|
|
|
|
if (input.starts_with("~")) return { Operator::BitwiseNot, 1 };
|
|
|
|
if (input.starts_with("+")) return { Operator::Addition, 1 };
|
|
|
|
if (input.starts_with("-")) return { Operator::Subtraction, 1 };
|
|
|
|
if (input.starts_with("*")) return { Operator::Multiplication, 1 };
|
|
|
|
if (input.starts_with("/")) return { Operator::Division, 1 };
|
|
|
|
if (input.starts_with("%")) return { Operator::Modulus, 1 };
|
|
|
|
if (input.starts_with("=")) return { Operator::Assign, 1 };
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
return { Operator::Invalid, 0 };
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
std::optional<std::queue<typename MathEvaluator<T>::Token>> MathEvaluator<T>::toPostfix(std::queue<Token> inputQueue) {
|
2022-03-04 19:20:21 +01:00
|
|
|
std::queue<Token> outputQueue;
|
|
|
|
std::stack<Token> operatorStack;
|
|
|
|
|
|
|
|
while (!inputQueue.empty()) {
|
|
|
|
Token currToken = inputQueue.front();
|
|
|
|
inputQueue.pop();
|
|
|
|
|
|
|
|
if (currToken.type == TokenType::Number || currToken.type == TokenType::Variable || currToken.type == TokenType::Function)
|
|
|
|
outputQueue.push(currToken);
|
|
|
|
else if (currToken.type == TokenType::Operator) {
|
2022-03-27 00:01:28 +01:00
|
|
|
while ((!operatorStack.empty()) && ((operatorStack.top().type == TokenType::Operator && currToken.type == TokenType::Operator && (comparePrecedence(operatorStack.top().op, currToken.op) > 0)) || (comparePrecedence(operatorStack.top().op, currToken.op) == 0 && isLeftAssociative(currToken.op))) && operatorStack.top().type != TokenType::Bracket) {
|
2022-03-04 19:20:21 +01:00
|
|
|
outputQueue.push(operatorStack.top());
|
|
|
|
operatorStack.pop();
|
|
|
|
}
|
|
|
|
operatorStack.push(currToken);
|
|
|
|
} else if (currToken.type == TokenType::Bracket) {
|
|
|
|
if (currToken.bracketType == BracketType::Left)
|
|
|
|
operatorStack.push(currToken);
|
|
|
|
else {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (operatorStack.empty()) {
|
|
|
|
this->setError("Mismatching parenthesis!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-03-04 19:20:21 +01:00
|
|
|
|
|
|
|
while (operatorStack.top().type != TokenType::Bracket || (operatorStack.top().type == TokenType::Bracket && operatorStack.top().bracketType != BracketType::Left)) {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (operatorStack.empty()) {
|
|
|
|
this->setError("Mismatching parenthesis!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-03-04 19:20:21 +01:00
|
|
|
|
|
|
|
outputQueue.push(operatorStack.top());
|
|
|
|
operatorStack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
operatorStack.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!operatorStack.empty()) {
|
|
|
|
auto top = operatorStack.top();
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
if (top.type == TokenType::Bracket) {
|
|
|
|
this->setError("Mismatching parenthesis!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-03-04 19:20:21 +01:00
|
|
|
|
|
|
|
outputQueue.push(top);
|
|
|
|
operatorStack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputQueue;
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
std::optional<std::queue<typename MathEvaluator<T>::Token>> MathEvaluator<T>::parseInput(std::string input) {
|
2020-11-28 21:55:52 +01:00
|
|
|
std::queue<Token> inputQueue;
|
|
|
|
|
2022-03-04 19:20:21 +01:00
|
|
|
char *prevPos = input.data();
|
2020-11-28 21:55:52 +01:00
|
|
|
for (char *pos = prevPos; *pos != 0x00;) {
|
|
|
|
if (std::isdigit(*pos) || *pos == '.') {
|
2022-05-27 20:42:07 +02:00
|
|
|
auto number = [&] {
|
2022-06-25 12:19:59 +02:00
|
|
|
if constexpr (std::floating_point<T>)
|
2022-05-27 20:42:07 +02:00
|
|
|
return std::strtold(pos, &pos);
|
2022-06-25 12:19:59 +02:00
|
|
|
else if constexpr (std::signed_integral<T>)
|
2022-05-27 20:42:07 +02:00
|
|
|
return std::strtoll(pos, &pos, 10);
|
2022-06-25 12:19:59 +02:00
|
|
|
else if constexpr (std::unsigned_integral<T>)
|
2022-05-27 20:42:07 +02:00
|
|
|
return std::strtoull(pos, &pos, 10);
|
|
|
|
else
|
|
|
|
static_assert(hex::always_false<T>::value, "Can't parse literal of this type");
|
|
|
|
}();
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
if (*pos == 'x') {
|
|
|
|
pos--;
|
|
|
|
number = std::strtoull(pos, &pos, 0);
|
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
inputQueue.push(Token { .type = TokenType::Number, .number = number, .name = "", .arguments = { } });
|
2020-11-28 21:55:52 +01:00
|
|
|
} else if (*pos == '(') {
|
2022-03-27 00:01:28 +01:00
|
|
|
inputQueue.push(Token { .type = TokenType::Bracket, .bracketType = BracketType::Left, .name = "", .arguments = { } });
|
2020-11-28 21:55:52 +01:00
|
|
|
pos++;
|
|
|
|
} else if (*pos == ')') {
|
2022-03-27 00:01:28 +01:00
|
|
|
inputQueue.push(Token { .type = TokenType::Bracket, .bracketType = BracketType::Right, .name = "", .arguments = { } });
|
2020-11-28 21:55:52 +01:00
|
|
|
pos++;
|
|
|
|
} else if (std::isspace(*pos)) {
|
|
|
|
pos++;
|
|
|
|
} else {
|
|
|
|
auto [op, width] = toOperator(pos);
|
|
|
|
|
|
|
|
if (op != Operator::Invalid) {
|
2022-03-27 00:01:28 +01:00
|
|
|
inputQueue.push(Token { .type = TokenType::Operator, .op = op, .name = "", .arguments = { } });
|
2020-11-28 21:55:52 +01:00
|
|
|
pos += width;
|
|
|
|
} else {
|
|
|
|
Token token;
|
|
|
|
|
|
|
|
while (std::isalpha(*pos) || *pos == '_') {
|
|
|
|
token.name += *pos;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pos == '(') {
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
u32 depth = 1;
|
|
|
|
std::vector<std::string> expressions;
|
|
|
|
expressions.emplace_back();
|
|
|
|
|
|
|
|
while (*pos != 0x00) {
|
|
|
|
if (*pos == '(') depth++;
|
|
|
|
else if (*pos == ')') depth--;
|
|
|
|
|
|
|
|
if (depth == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (depth == 1 && *pos == ',') {
|
|
|
|
expressions.emplace_back();
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
expressions.back() += *pos;
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
for (const auto &expression : expressions) {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (expression.empty() && expressions.size() > 1) {
|
|
|
|
this->setError("Invalid function call syntax!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-03-04 19:20:21 +01:00
|
|
|
else if (expression.empty())
|
2020-11-28 21:55:52 +01:00
|
|
|
break;
|
|
|
|
|
2022-03-04 20:52:39 +01:00
|
|
|
auto newInputQueue = parseInput(expression);
|
2022-05-27 20:42:07 +02:00
|
|
|
if (!newInputQueue.has_value())
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
auto postfixTokens = toPostfix(*newInputQueue);
|
|
|
|
if (!postfixTokens.has_value())
|
|
|
|
return std::nullopt;
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
auto result = evaluate(*postfixTokens);
|
|
|
|
if (!result.has_value()) {
|
|
|
|
this->setError("Invalid argument for function!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
token.arguments.push_back(result.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
token.type = TokenType::Function;
|
|
|
|
inputQueue.push(token);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
token.type = TokenType::Variable;
|
|
|
|
inputQueue.push(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
if (prevPos == pos) {
|
|
|
|
this->setError("Invalid syntax!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
prevPos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputQueue;
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
std::optional<T> MathEvaluator<T>::evaluate(std::queue<Token> postfixTokens) {
|
|
|
|
std::stack<T> evaluationStack;
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
while (!postfixTokens.empty()) {
|
|
|
|
auto front = postfixTokens.front();
|
|
|
|
postfixTokens.pop();
|
|
|
|
|
|
|
|
if (front.type == TokenType::Number)
|
|
|
|
evaluationStack.push(front.number);
|
|
|
|
else if (front.type == TokenType::Operator) {
|
2022-05-27 20:42:07 +02:00
|
|
|
T rightOperand, leftOperand;
|
2020-11-28 21:55:52 +01:00
|
|
|
if (evaluationStack.size() < 2) {
|
|
|
|
if ((front.op == Operator::Addition || front.op == Operator::Subtraction || front.op == Operator::Not || front.op == Operator::BitwiseNot) && evaluationStack.size() == 1) {
|
2022-01-24 20:53:17 +01:00
|
|
|
rightOperand = evaluationStack.top();
|
|
|
|
evaluationStack.pop();
|
2020-11-28 21:55:52 +01:00
|
|
|
leftOperand = 0;
|
2022-05-27 20:42:07 +02:00
|
|
|
} else {
|
|
|
|
this->setError("Not enough operands for operator!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
} else {
|
2022-01-24 20:53:17 +01:00
|
|
|
rightOperand = evaluationStack.top();
|
|
|
|
evaluationStack.pop();
|
|
|
|
leftOperand = evaluationStack.top();
|
|
|
|
evaluationStack.pop();
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
T result = [] {
|
|
|
|
if constexpr (std::numeric_limits<T>::has_quiet_NaN)
|
|
|
|
return std::numeric_limits<T>::quiet_NaN();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}();
|
2020-11-28 21:55:52 +01:00
|
|
|
switch (front.op) {
|
2022-02-01 22:09:44 +01:00
|
|
|
default:
|
|
|
|
case Operator::Invalid:
|
2022-05-27 20:42:07 +02:00
|
|
|
this->setError("Invalid operator!");
|
|
|
|
return std::nullopt;
|
2022-02-01 22:09:44 +01:00
|
|
|
case Operator::And:
|
|
|
|
result = static_cast<i64>(leftOperand) && static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::Or:
|
2022-03-04 19:20:21 +01:00
|
|
|
result = static_cast<i64>(leftOperand) || static_cast<i64>(rightOperand);
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case Operator::Xor:
|
|
|
|
result = (static_cast<i64>(leftOperand) ^ static_cast<i64>(rightOperand)) > 0;
|
|
|
|
break;
|
|
|
|
case Operator::GreaterThan:
|
|
|
|
result = leftOperand > rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::LessThan:
|
|
|
|
result = leftOperand < rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::GreaterThanOrEquals:
|
|
|
|
result = leftOperand >= rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::LessThanOrEquals:
|
|
|
|
result = leftOperand <= rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::Equals:
|
|
|
|
result = leftOperand == rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::NotEquals:
|
|
|
|
result = leftOperand != rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::Not:
|
|
|
|
result = !static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::BitwiseOr:
|
|
|
|
result = static_cast<i64>(leftOperand) | static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::BitwiseXor:
|
|
|
|
result = static_cast<i64>(leftOperand) ^ static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::BitwiseAnd:
|
|
|
|
result = static_cast<i64>(leftOperand) & static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::BitwiseNot:
|
|
|
|
result = ~static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::ShiftLeft:
|
|
|
|
result = static_cast<i64>(leftOperand) << static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::ShiftRight:
|
|
|
|
result = static_cast<i64>(leftOperand) >> static_cast<i64>(rightOperand);
|
|
|
|
break;
|
|
|
|
case Operator::Addition:
|
|
|
|
result = leftOperand + rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::Subtraction:
|
|
|
|
result = leftOperand - rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::Multiplication:
|
|
|
|
result = leftOperand * rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::Division:
|
|
|
|
result = leftOperand / rightOperand;
|
|
|
|
break;
|
|
|
|
case Operator::Modulus:
|
2022-05-27 20:42:07 +02:00
|
|
|
if constexpr (std::floating_point<T>)
|
|
|
|
result = std::fmod(leftOperand, rightOperand);
|
|
|
|
else
|
|
|
|
result = leftOperand % rightOperand;
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case Operator::Exponentiation:
|
2022-05-27 20:42:07 +02:00
|
|
|
if constexpr (std::floating_point<T>)
|
|
|
|
result = std::pow(leftOperand, rightOperand);
|
|
|
|
else
|
|
|
|
result = hex::powi(leftOperand, rightOperand);
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case Operator::Combine:
|
|
|
|
result = (static_cast<u64>(leftOperand) << (64 - __builtin_clzll(static_cast<u64>(rightOperand)))) | static_cast<u64>(rightOperand);
|
|
|
|
break;
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
evaluationStack.push(result);
|
|
|
|
} else if (front.type == TokenType::Variable) {
|
|
|
|
if (this->m_variables.contains(front.name))
|
2022-10-02 14:18:56 +02:00
|
|
|
evaluationStack.push(this->m_variables.at(front.name).value);
|
2022-05-27 20:42:07 +02:00
|
|
|
else {
|
|
|
|
this->setError("Unknown variable!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
} else if (front.type == TokenType::Function) {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (!this->m_functions[front.name]) {
|
|
|
|
this->setError("Unknown function called!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
auto result = this->m_functions[front.name](front.arguments);
|
|
|
|
|
|
|
|
if (result.has_value())
|
|
|
|
evaluationStack.push(result.value());
|
2022-05-27 20:42:07 +02:00
|
|
|
} else {
|
|
|
|
this->setError("Parenthesis in postfix expression!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
if (evaluationStack.empty()) {
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
else if (evaluationStack.size() > 1) {
|
|
|
|
this->setError("Undigested input left!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
else {
|
2020-11-28 21:55:52 +01:00
|
|
|
return evaluationStack.top();
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
std::optional<T> MathEvaluator<T>::evaluate(const std::string &input) {
|
2022-03-04 20:52:39 +01:00
|
|
|
auto inputQueue = parseInput(input);
|
2022-10-02 14:18:56 +02:00
|
|
|
if (!inputQueue.has_value() || inputQueue->empty())
|
2022-05-27 20:42:07 +02:00
|
|
|
return std::nullopt;
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
std::string resultVariable = "ans";
|
|
|
|
|
|
|
|
{
|
2022-05-27 20:42:07 +02:00
|
|
|
auto queueCopy = *inputQueue;
|
2022-10-02 14:18:56 +02:00
|
|
|
if (queueCopy.front().type == TokenType::Variable && queueCopy.size() > 2) {
|
2020-11-28 21:55:52 +01:00
|
|
|
resultVariable = queueCopy.front().name;
|
|
|
|
queueCopy.pop();
|
|
|
|
if (queueCopy.front().type != TokenType::Operator || queueCopy.front().op != Operator::Assign)
|
|
|
|
resultVariable = "ans";
|
|
|
|
else {
|
2022-05-27 20:42:07 +02:00
|
|
|
inputQueue->pop();
|
|
|
|
inputQueue->pop();
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
auto postfixTokens = toPostfix(*inputQueue);
|
|
|
|
if (!postfixTokens.has_value())
|
|
|
|
return std::nullopt;
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
auto result = evaluate(*postfixTokens);
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-10-02 14:18:56 +02:00
|
|
|
if (result.has_value() && !this->getVariables()[resultVariable].constant)
|
2020-11-28 21:55:52 +01:00
|
|
|
this->setVariable(resultVariable, result.value());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
2022-10-02 14:18:56 +02:00
|
|
|
void MathEvaluator<T>::setVariable(const std::string &name, T value, bool constant) {
|
|
|
|
this->m_variables[name] = { value, constant };
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
void MathEvaluator<T>::setFunction(const std::string &name, const std::function<std::optional<T>(std::vector<T>)> &function, size_t minNumArgs, size_t maxNumArgs) {
|
|
|
|
this->m_functions[name] = [this, minNumArgs, maxNumArgs, function](auto args) -> std::optional<T> {
|
|
|
|
if (args.size() < minNumArgs || args.size() > maxNumArgs) {
|
|
|
|
this->setError("Invalid number of function arguments!");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
return function(args);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
void MathEvaluator<T>::registerStandardVariables() {
|
2020-11-28 21:55:52 +01:00
|
|
|
this->setVariable("ans", 0);
|
2022-10-02 14:18:56 +02:00
|
|
|
this->setVariable("pi", std::numbers::pi, true);
|
|
|
|
this->setVariable("e", std::numbers::e, true);
|
|
|
|
this->setVariable("phi", std::numbers::phi, true);
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
void MathEvaluator<T>::registerStandardFunctions() {
|
2022-06-25 12:19:59 +02:00
|
|
|
if constexpr (std::floating_point<T>) {
|
2022-05-27 20:42:07 +02:00
|
|
|
this->setFunction(
|
|
|
|
"sin", [](auto args) { return std::sin(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"cos", [](auto args) { return std::cos(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"tan", [](auto args) { return std::tan(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"sqrt", [](auto args) { return std::sqrt(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"ceil", [](auto args) { return std::ceil(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"floor", [](auto args) { return std::floor(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"sign", [](auto args) { return (args[0] > 0) ? 1 : (args[0] == 0) ? 0
|
|
|
|
: -1; }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"abs", [](auto args) { return std::abs(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"ln", [](auto args) { return std::log(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"lb", [](auto args) { return std::log2(args[0]); }, 1, 1);
|
|
|
|
this->setFunction(
|
|
|
|
"log", [](auto args) { return args.size() == 1 ? std::log10(args[0]) : std::log(args[1]) / std::log(args[0]); }, 1, 2);
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template class MathEvaluator<long double>;
|
|
|
|
template class MathEvaluator<i128>;
|
|
|
|
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|