2020-11-28 21:55:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <functional>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
template<typename T>
|
|
|
|
class MathEvaluator {
|
|
|
|
public:
|
|
|
|
MathEvaluator() = default;
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-10-02 14:18:56 +02:00
|
|
|
struct Variable {
|
|
|
|
T value;
|
|
|
|
bool constant;
|
|
|
|
};
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
std::optional<T> evaluate(const std::string &input);
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
void registerStandardVariables();
|
|
|
|
void registerStandardFunctions();
|
|
|
|
|
2022-10-02 14:18:56 +02:00
|
|
|
void setVariable(const std::string &name, T value, bool constant = false);
|
2022-05-27 20:42:07 +02:00
|
|
|
void setFunction(const std::string &name, const std::function<std::optional<T>(std::vector<T>)> &function, size_t minNumArgs, size_t maxNumArgs);
|
|
|
|
|
2022-10-02 14:18:56 +02:00
|
|
|
std::unordered_map<std::string, Variable> &getVariables() { return this->m_variables; }
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
[[nodiscard]] bool hasError() const {
|
|
|
|
return this->m_lastError.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<std::string> getLastError() const {
|
|
|
|
return this->m_lastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setError(const std::string &error) {
|
|
|
|
this->m_lastError = error;
|
|
|
|
}
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
private:
|
|
|
|
enum class TokenType
|
|
|
|
{
|
|
|
|
Number,
|
|
|
|
Variable,
|
|
|
|
Function,
|
|
|
|
Operator,
|
|
|
|
Bracket
|
2022-03-27 00:01:28 +01:00
|
|
|
};
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
enum class Operator : u16
|
|
|
|
{
|
|
|
|
Invalid = 0x000,
|
|
|
|
Assign = 0x010,
|
|
|
|
Or = 0x020,
|
|
|
|
Xor = 0x030,
|
|
|
|
And = 0x040,
|
|
|
|
BitwiseOr = 0x050,
|
|
|
|
BitwiseXor = 0x060,
|
|
|
|
BitwiseAnd = 0x070,
|
|
|
|
Equals = 0x080,
|
|
|
|
NotEquals = 0x081,
|
|
|
|
GreaterThan = 0x090,
|
|
|
|
LessThan = 0x091,
|
|
|
|
GreaterThanOrEquals = 0x092,
|
|
|
|
LessThanOrEquals = 0x093,
|
|
|
|
ShiftLeft = 0x0A0,
|
|
|
|
ShiftRight = 0x0A1,
|
|
|
|
Addition = 0x0B0,
|
|
|
|
Subtraction = 0x0B1,
|
|
|
|
Multiplication = 0x0C0,
|
|
|
|
Division = 0x0C1,
|
|
|
|
Modulus = 0x0C2,
|
|
|
|
Exponentiation = 0x1D0,
|
|
|
|
Combine = 0x0E0,
|
2023-11-16 21:23:58 +01:00
|
|
|
BitwiseNot = 0x2F0,
|
|
|
|
Not = 0x2F1,
|
|
|
|
Plus = 0x2F2,
|
|
|
|
Minus = 0x2F3
|
2022-05-27 20:42:07 +02:00
|
|
|
};
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
enum class BracketType : std::uint8_t
|
|
|
|
{
|
|
|
|
Left,
|
|
|
|
Right
|
|
|
|
};
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
struct Token {
|
|
|
|
TokenType type;
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
union {
|
|
|
|
T number;
|
|
|
|
Operator op;
|
|
|
|
BracketType bracketType;
|
|
|
|
};
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
std::string name;
|
|
|
|
std::vector<T> arguments;
|
|
|
|
};
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
static i16 comparePrecedence(const Operator &a, const Operator &b);
|
2022-09-07 23:11:24 +02:00
|
|
|
static bool isLeftAssociative(const Operator &op);
|
2023-11-16 21:23:58 +01:00
|
|
|
static bool isUnary(const Operator &op);
|
2022-05-27 20:42:07 +02:00
|
|
|
static std::pair<Operator, size_t> toOperator(const std::string &input);
|
2020-11-28 21:55:52 +01:00
|
|
|
|
|
|
|
private:
|
2022-05-27 20:42:07 +02:00
|
|
|
std::optional<std::queue<Token>> parseInput(std::string input);
|
|
|
|
std::optional<std::queue<Token>> toPostfix(std::queue<Token> inputQueue);
|
|
|
|
std::optional<T> evaluate(std::queue<Token> postfixTokens);
|
|
|
|
|
2022-10-02 14:18:56 +02:00
|
|
|
std::unordered_map<std::string, Variable> m_variables;
|
2022-05-27 20:42:07 +02:00
|
|
|
std::unordered_map<std::string, std::function<std::optional<T>(std::vector<T>)>> m_functions;
|
2020-11-28 21:55:52 +01:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
std::optional<std::string> m_lastError;
|
2020-11-28 21:55:52 +01:00
|
|
|
};
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
extern template class MathEvaluator<long double>;
|
|
|
|
extern template class MathEvaluator<i128>;
|
2022-12-16 11:20:39 +01:00
|
|
|
extern template class MathEvaluator<u128>;
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2020-11-28 21:55:52 +01:00
|
|
|
}
|