1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*
Copyright (C) 2006 - 2015 Evan Teran
evan.teran@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EXPRESSION_H_20070402_
#define EXPRESSION_H_20070402_
#include "Status.h"
#include <QString>
#include <functional>
struct ExpressionError {
public:
enum ErrorMessage {
None,
Syntax,
UnbalancedParens,
UnbalancedBraces,
DivideByZero,
InvalidNumber,
UnknownVariable,
CannotReadMemory,
UnexpectedOperator,
UnexpectedNumber,
VariableLargerThanAddress
};
public:
ExpressionError() = default;
explicit ExpressionError(ErrorMessage type)
: error_(type) {
}
const char *what() const noexcept {
switch (error_) {
case Syntax:
return "Syntax Error";
case UnbalancedParens:
return "Unbalanced Parenthesis";
case DivideByZero:
return "Divide By Zero";
case InvalidNumber:
return "Invalid Numerical Constant";
case UnknownVariable:
return "Unknown Variable";
case UnbalancedBraces:
return "Unbalanced Braces";
case CannotReadMemory:
return "Cannot Read Memory At the Effective Address";
case UnexpectedOperator:
return "Unexpected Operator";
case UnexpectedNumber:
return "Unexpected Numerical Constant";
case VariableLargerThanAddress:
return "Variable Does Not Fit Into An Address. Is it an FPU Register?";
default:
return "Unknown Error";
}
}
private:
ErrorMessage error_ = None;
};
template <class T>
class Expression {
public:
using variable_getter_t = std::function<T(const QString &, bool *, ExpressionError *)>;
using memoryReader_t = std::function<T(T, bool *, ExpressionError *)>;
public:
Expression(const QString &s, variable_getter_t vg, memoryReader_t mr);
~Expression() = default;
private:
struct Token {
Token() = default;
Token(const Token &other) = default;
enum Operator {
NONE,
AND,
OR,
XOR,
LSHFT,
RSHFT,
PLUS,
MINUS,
MUL,
DIV,
MOD,
CMP,
LPAREN,
RPAREN,
LBRACE,
RBRACE,
NOT,
LT,
LE,
GT,
GE,
EQ,
NE,
LOGICAL_AND,
LOGICAL_OR
};
enum Type {
UNKNOWN,
OPERATOR,
NUMBER,
VARIABLE
};
void set(const QString &data, Operator oper, Type type) {
data_ = data;
operator_ = oper;
type_ = type;
}
QString data_;
Operator operator_ = NONE;
Type type_ = UNKNOWN;
};
private:
T evalInternal() {
T result;
getToken();
evalExp(result);
return result;
}
public:
Result<T, ExpressionError> evaluate() noexcept {
try {
return evalInternal();
} catch (const ExpressionError &e) {
return make_unexpected(e);
}
}
private:
void evalExp(T &result);
void evalExp0(T &result);
void evalExp1(T &result);
void evalExp2(T &result);
void evalExp3(T &result);
void evalExp4(T &result);
void evalExp5(T &result);
void evalExp6(T &result);
void evalExp7(T &result);
void evalAtom(T &result);
void getToken();
private:
QString expression_;
QString::const_iterator expressionPtr_;
Token token_;
variable_getter_t variableReader_;
memoryReader_t memoryReader_;
};
#include "Expression.tcc"
#endif
|