2014-10-07 18:15:46 +00:00
|
|
|
#ifndef RESOURCECOMPILER_H
|
|
|
|
#define RESOURCECOMPILER_H
|
|
|
|
|
|
|
|
#include "Expression.h"
|
2014-10-07 22:41:40 +00:00
|
|
|
#include "ResourceDefinitions.h"
|
2014-10-07 18:15:46 +00:00
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
class Field;
|
2014-10-30 01:56:49 +00:00
|
|
|
class RezWorld;
|
|
|
|
class Diagnostic;
|
2014-10-07 22:41:40 +00:00
|
|
|
|
|
|
|
class Subscripts
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
std::vector<int> subscripts;
|
2014-10-07 22:41:40 +00:00
|
|
|
public:
|
2019-08-18 11:21:00 +00:00
|
|
|
Subscripts();
|
|
|
|
~Subscripts();
|
2014-10-07 22:41:40 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
void addSubscript(int x);
|
|
|
|
void popSubscript();
|
|
|
|
bool operator<(const Subscripts& other) const;
|
|
|
|
bool empty() const { return subscripts.empty(); }
|
2014-10-07 22:41:40 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 00:29:12 +00:00
|
|
|
class BinaryOutput
|
|
|
|
{
|
|
|
|
protected:
|
2019-08-18 11:21:00 +00:00
|
|
|
int currentOffset;
|
|
|
|
std::vector<unsigned char> data;
|
|
|
|
std::vector<unsigned char> prePassData;
|
|
|
|
bool verboseFlag;
|
|
|
|
bool prePass;
|
2014-10-16 00:29:12 +00:00
|
|
|
public:
|
2019-08-18 11:21:00 +00:00
|
|
|
BinaryOutput();
|
|
|
|
void reset(bool prePass);
|
2014-10-16 00:29:12 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
std::string resourceData();
|
2014-10-16 00:29:12 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
void reserve(int nBits) { write(nBits, 0); }
|
|
|
|
void write(int nBits, int value);
|
|
|
|
int tell() { return currentOffset; }
|
2014-10-16 00:29:12 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
int peek(int bitPos, int size);
|
2014-10-16 00:29:12 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
bool isPrePass() { return prePass; }
|
2014-10-16 00:29:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ResourceCompiler : public BinaryOutput
|
2014-10-07 18:15:46 +00:00
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
RezWorld& world;
|
|
|
|
TypeDefinitionPtr typeDefinition;
|
|
|
|
CompoundExprPtr body;
|
|
|
|
std::map<std::pair<std::string, Subscripts>, ExprPtr> labelValues;
|
|
|
|
std::map<std::pair<std::string, Subscripts>, int> arrayCounts;
|
|
|
|
std::map<std::string, int> curArrayIndices;
|
|
|
|
Field* currentField;
|
|
|
|
Subscripts currentSubscripts;
|
|
|
|
|
|
|
|
void beginArrayScope(std::string& arrayName, int index);
|
2014-10-07 18:15:46 +00:00
|
|
|
public:
|
2019-08-18 11:21:00 +00:00
|
|
|
ResourceCompiler(RezWorld& world, TypeDefinitionPtr type, CompoundExprPtr body, bool verboseFlag);
|
2014-10-13 22:11:21 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
ExprPtr lookupIdentifier(std::string name, const Subscripts& sub = Subscripts());
|
2014-10-07 22:41:40 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
void defineLabel(const std::string& name);
|
|
|
|
void compile();
|
2014-10-07 22:41:40 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
int getArrayCount(const std::string& arrayName);
|
|
|
|
int getArrayIndex(const std::string& arrayName);
|
2014-10-07 22:41:40 +00:00
|
|
|
|
2014-10-13 22:11:21 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
class FieldScope
|
|
|
|
{
|
|
|
|
ResourceCompiler *compiler;
|
|
|
|
public:
|
|
|
|
FieldScope(ResourceCompiler* compiler, Field *field)
|
|
|
|
: compiler(compiler) { compiler->currentField = field; }
|
|
|
|
~FieldScope() { compiler->currentField = nullptr; }
|
|
|
|
};
|
2014-10-07 22:41:40 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
class ArrayScope
|
|
|
|
{
|
|
|
|
ResourceCompiler *compiler;
|
|
|
|
public:
|
|
|
|
ArrayScope(ResourceCompiler* compiler, std::string& arrayName, int index)
|
|
|
|
: compiler(compiler) { compiler->beginArrayScope(arrayName, index); }
|
|
|
|
~ArrayScope() { compiler->currentSubscripts.popSubscript(); }
|
|
|
|
};
|
2014-10-30 01:56:49 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
void problem(Diagnostic d);
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
|
|
|
|
|
2014-10-07 18:15:46 +00:00
|
|
|
#endif // RESOURCECOMPILER_H
|