62 lines
1.9 KiB
C
62 lines
1.9 KiB
C
|
|
#ifndef INTERPRETER_H
|
||
|
|
#define INTERPRETER_H
|
||
|
|
|
||
|
|
#include "ast.h"
|
||
|
|
|
||
|
|
#include <QDataStream>
|
||
|
|
#include <QVariantMap>
|
||
|
|
#include <QIODevice>
|
||
|
|
|
||
|
|
struct Runtime {
|
||
|
|
QDataStream* in = nullptr;
|
||
|
|
Module* module = nullptr; // Non-const to allow modifying globals
|
||
|
|
|
||
|
|
ByteOrder order = ByteOrder::LE;
|
||
|
|
QVariantMap vars;
|
||
|
|
|
||
|
|
QString filePath;
|
||
|
|
};
|
||
|
|
|
||
|
|
class Interpreter {
|
||
|
|
public:
|
||
|
|
explicit Interpreter(Module mod);
|
||
|
|
|
||
|
|
// Parse a given type name from a device/stream
|
||
|
|
QVariantMap runType(const QString& typeName, QIODevice* dev, const QString& filePath = QString()) const;
|
||
|
|
|
||
|
|
// Run from existing QDataStream (for nested parse)
|
||
|
|
QVariantMap runType(const QString& typeName, QDataStream& stream, const QString &filePath = QString()) const;
|
||
|
|
|
||
|
|
// Check if the criteria for a certain filetype matches
|
||
|
|
bool checkCriteria(const QString& typeName, QIODevice* dev, const QString filePath) const;
|
||
|
|
|
||
|
|
QVariantMap runTypeInternal(const QString& typeName,
|
||
|
|
QDataStream& stream,
|
||
|
|
const QString& filePath,
|
||
|
|
std::optional<ByteOrder> inheritedOrder) const;
|
||
|
|
|
||
|
|
static void seedFileVars(Runtime &rt, const QString &filePath);
|
||
|
|
|
||
|
|
private:
|
||
|
|
QVariant evalExpr(Runtime& rt, const Expr& e) const;
|
||
|
|
QVariant evalCall(Runtime& rt, const Expr::Call& c) const;
|
||
|
|
QVariant evalPipe(Runtime& rt, const Expr::Pipe& p) const;
|
||
|
|
|
||
|
|
void execStmt(Runtime& rt, const Stmt& s) const;
|
||
|
|
void execBlock(Runtime& rt, const QVector<StmtPtr>& body) const;
|
||
|
|
|
||
|
|
// helpers
|
||
|
|
void applyByteOrder(Runtime& rt) const;
|
||
|
|
QVariant readScalar(Runtime& rt, ScalarType t) const;
|
||
|
|
QByteArray readBytes(Runtime& rt, qint64 n) const;
|
||
|
|
QByteArray readEOF(Runtime& rt) const;
|
||
|
|
|
||
|
|
qint64 toInt(const QVariant& v) const;
|
||
|
|
bool toBool(const QVariant& v) const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
mutable Module m_mod; // Mutable to allow modifying globals from const methods
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif // INTERPRETER_H
|