Retro68/Rez/ResourceDefinitions.h

167 lines
3.6 KiB
C
Raw Normal View History

2014-10-06 15:03:25 +00:00
#ifndef RESOURCEDEFINITIONS_H
#define RESOURCEDEFINITIONS_H
#include <iosfwd>
#include <memory>
#include <map>
#include "Expression.h"
2014-10-08 00:52:34 +00:00
#include "ResType.h"
2014-10-06 15:03:25 +00:00
class TypeSpec
{
ResType type;
int id;
public:
static const int noID = 65536;
TypeSpec() : id(noID) {}
TypeSpec(ResType type) : type(type), id(noID) {}
TypeSpec(ResType type, int id) : type(type), id(id) {}
ResType getType() const { return type; }
int getID() const { return id; }
bool hasID() const { return id != noID; }
bool operator<(TypeSpec y) const
{
if(type < y.type)
return true;
else if(y.type < type)
return false;
else
return id < y.id;
}
2014-10-06 15:03:25 +00:00
};
std::ostream& operator<<(std::ostream& out, TypeSpec ts);
2014-10-06 15:03:25 +00:00
class ResourceCompiler;
2014-10-06 15:03:25 +00:00
class Field
{
public:
virtual bool needsValue() { return true; }
virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass) = 0;
2014-10-07 22:41:40 +00:00
virtual ExprPtr lookupNamedValue(std::string) { return nullptr; }
2014-10-06 15:03:25 +00:00
};
typedef std::shared_ptr<Field> FieldPtr;
class SimpleField : public Field
{
public:
enum class Type
{
boolean, byte, integer, longint, rect, point, char_,
pstring, wstring, string, bitstring
};
enum class Attrs
{
none = 0, hex = 1, key = 2, unsigned_ = 4, literal = 8
};
Type type;
Attrs attrs = Attrs::none;
ExprPtr arrayCount;
ExprPtr value;
std::map<std::string, ExprPtr> namedValues;
2014-10-07 22:41:40 +00:00
ExprPtr lastNamedValue;
2014-10-06 15:03:25 +00:00
2014-10-07 22:41:40 +00:00
void addNamedValue(std::string n);
void addNamedValue(std::string n, ExprPtr val);
ExprPtr lookupNamedValue(std::string);
virtual bool needsValue();
virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
2014-10-07 23:17:39 +00:00
private:
void compileString(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
void compileInt(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
2014-10-07 23:55:54 +00:00
void compileCompound(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
2014-10-06 15:03:25 +00:00
};
typedef std::shared_ptr<SimpleField> SimpleFieldPtr;
2014-10-07 23:37:28 +00:00
class FillAlignField : public Field
{
public:
enum class Type
{
bit, nibble, byte, word, long_
};
FillAlignField(Type type, bool isAlign, ExprPtr count = ExprPtr());
virtual bool needsValue();
virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
private:
Type type;
ExprPtr count;
bool isAlign;
};
2014-10-06 15:03:25 +00:00
inline SimpleField::Attrs operator|(SimpleField::Attrs a, SimpleField::Attrs b)
{
return SimpleField::Attrs( int(a) | int(b) );
}
2014-10-07 22:41:40 +00:00
class LabelField : public Field
{
std::string name;
public:
LabelField(std::string name);
virtual bool needsValue();
virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
};
typedef std::shared_ptr<LabelField> LabelFieldPtr;
class FieldList : public Field
2014-10-06 15:03:25 +00:00
{
protected:
std::vector<FieldPtr> fields;
2014-10-06 15:03:25 +00:00
public:
virtual ~FieldList();
void addField(FieldPtr field);
void addLabel(std::string name);
virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
};
typedef std::shared_ptr<FieldList> FieldListPtr;
2014-10-07 18:44:40 +00:00
class ArrayField : public FieldList
{
std::string name;
ExprPtr arrayCount;
public:
ArrayField(std::string name /* or empty */, ExprPtr count /* may be null*/);
virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
};
typedef std::shared_ptr<ArrayField> ArrayFieldPtr;
2014-10-07 23:17:17 +00:00
class SwitchField : public Field
{
std::map<std::string, FieldListPtr> cases;
public:
void addCase(const std::string name, FieldListPtr alternative);
virtual void compile(ExprPtr expr, ResourceCompiler *compiler, bool prePass);
};
typedef std::shared_ptr<SwitchField> SwitchFieldPtr;
class TypeDefinition : public FieldList
{
2014-10-06 15:03:25 +00:00
};
typedef std::shared_ptr<TypeDefinition> TypeDefinitionPtr;
2014-10-06 15:03:25 +00:00
#endif // RESOURCEDEFINITIONS_H