2018-09-25 01:04:42 +01:00
|
|
|
#ifndef PARSEUTIL_H
|
|
|
|
#define PARSEUTIL_H
|
|
|
|
|
2018-09-25 01:12:29 +01:00
|
|
|
#include "heallocation.h"
|
2019-05-05 21:11:00 +01:00
|
|
|
#include "log.h"
|
2018-09-25 01:04:42 +01:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMap>
|
|
|
|
|
|
|
|
enum TokenType {
|
|
|
|
Number,
|
|
|
|
Operator,
|
2019-05-05 21:11:00 +01:00
|
|
|
Error,
|
|
|
|
};
|
|
|
|
|
|
|
|
class DebugInfo {
|
|
|
|
public:
|
|
|
|
DebugInfo(QString file, QStringList lines) {
|
|
|
|
this->file = file;
|
|
|
|
this->lines = lines;
|
|
|
|
};
|
|
|
|
QString file;
|
|
|
|
int line;
|
|
|
|
bool err;
|
|
|
|
QStringList lines;
|
|
|
|
void error(QString expression, QString token) {
|
|
|
|
int lineNo = 0;
|
|
|
|
for (QString line_ : lines) {
|
|
|
|
lineNo++;
|
|
|
|
if (line_.contains(expression)) {
|
|
|
|
this->line = lineNo;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logError(QString("%1:%2: unknown identifier found in expression: '%3'.").arg(file).arg(line).arg(token));
|
|
|
|
}
|
2018-09-25 01:04:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Token {
|
|
|
|
public:
|
|
|
|
Token(QString value = "", QString type = "") {
|
|
|
|
this->value = value;
|
|
|
|
this->type = TokenType::Operator;
|
|
|
|
if (type == "decimal" || type == "hex") {
|
|
|
|
this->type = TokenType::Number;
|
|
|
|
this->operatorPrecedence = -1;
|
|
|
|
} else if (type == "operator") {
|
|
|
|
this->operatorPrecedence = precedenceMap[value];
|
2019-05-05 21:11:00 +01:00
|
|
|
} else if (type == "error") {
|
|
|
|
this->type = TokenType::Error;
|
2018-09-25 01:04:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static QMap<QString, int> precedenceMap;
|
|
|
|
QString value;
|
|
|
|
TokenType type;
|
|
|
|
int operatorPrecedence; // only relevant for operator tokens
|
|
|
|
};
|
|
|
|
|
|
|
|
class ParseUtil
|
|
|
|
{
|
|
|
|
public:
|
2019-05-05 21:11:00 +01:00
|
|
|
ParseUtil(QString, QString);
|
2018-09-25 01:04:42 +01:00
|
|
|
void strip_comment(QString*);
|
|
|
|
QList<QStringList>* parseAsm(QString);
|
|
|
|
int evaluateDefine(QString, QMap<QString, int>*);
|
2019-05-05 21:11:00 +01:00
|
|
|
DebugInfo *debug;
|
2018-09-25 01:04:42 +01:00
|
|
|
private:
|
|
|
|
QList<Token> tokenizeExpression(QString expression, QMap<QString, int>* knownIdentifiers);
|
|
|
|
QList<Token> generatePostfix(QList<Token> tokens);
|
|
|
|
int evaluatePostfix(QList<Token> postfix);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PARSEUTIL_H
|