2014-10-07 18:15:46 +00:00
|
|
|
#ifndef EXPRESSION_H
|
|
|
|
#define EXPRESSION_H
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
class ResourceCompiler;
|
2014-10-07 18:15:46 +00:00
|
|
|
|
|
|
|
class Expression;
|
|
|
|
class CompoundExpr;
|
2014-10-07 22:41:40 +00:00
|
|
|
class IdentifierExpr;
|
2014-10-07 23:17:17 +00:00
|
|
|
class CaseExpr;
|
2014-10-07 18:15:46 +00:00
|
|
|
typedef std::shared_ptr<Expression> ExprPtr;
|
|
|
|
typedef std::shared_ptr<CompoundExpr> CompoundExprPtr;
|
2014-10-07 22:41:40 +00:00
|
|
|
typedef std::shared_ptr<IdentifierExpr> IdentifierExprPtr;
|
2014-10-07 23:17:17 +00:00
|
|
|
typedef std::shared_ptr<CaseExpr> CaseExprPtr;
|
2014-10-07 18:15:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
enum class BinaryOp
|
|
|
|
{
|
|
|
|
XOR, OR, AND, SHIFTLEFT, SHIFTRIGHT, EQUAL, NOTEQUAL, PLUS, MINUS, MULTIPLY, DIVIDE
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class UnaryOp
|
|
|
|
{
|
|
|
|
MINUS, COMPLEMENT
|
|
|
|
};
|
|
|
|
|
|
|
|
class TypeError
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class Expression
|
|
|
|
{
|
|
|
|
public:
|
2014-10-07 22:41:40 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
2014-10-07 23:17:39 +00:00
|
|
|
virtual std::string evaluateString(ResourceCompiler *ctx);
|
2014-10-07 18:15:46 +00:00
|
|
|
virtual ~Expression();
|
|
|
|
};
|
|
|
|
|
|
|
|
class StringExpr : public Expression
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
public:
|
|
|
|
StringExpr(const std::string& str) : str(str) {}
|
|
|
|
~StringExpr();
|
2014-10-07 23:17:39 +00:00
|
|
|
virtual std::string evaluateString(ResourceCompiler *ctx);
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class IntExpr : public Expression
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
public:
|
|
|
|
IntExpr(int val) : val(val) {}
|
|
|
|
~IntExpr();
|
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CompoundExpr : public Expression
|
|
|
|
{
|
|
|
|
std::vector<ExprPtr> items;
|
|
|
|
public:
|
|
|
|
void addItem(ExprPtr item);
|
2014-10-07 18:44:40 +00:00
|
|
|
ExprPtr getItem(int i) const { return items[i]; }
|
|
|
|
int size() const { return items.size(); }
|
|
|
|
|
2014-10-07 18:15:46 +00:00
|
|
|
~CompoundExpr();
|
|
|
|
};
|
|
|
|
|
2014-10-07 23:17:17 +00:00
|
|
|
class CaseExpr : public Expression
|
|
|
|
{
|
|
|
|
std::string tag;
|
|
|
|
CompoundExprPtr expr;
|
|
|
|
friend class SwitchField;
|
|
|
|
public:
|
|
|
|
CaseExpr(const std::string& tag, CompoundExprPtr expr);
|
|
|
|
};
|
|
|
|
|
2014-10-07 18:15:46 +00:00
|
|
|
class BinaryExpr : public Expression
|
|
|
|
{
|
|
|
|
BinaryOp op;
|
|
|
|
ExprPtr a, b;
|
|
|
|
public:
|
|
|
|
BinaryExpr(BinaryOp op, ExprPtr a, ExprPtr b)
|
|
|
|
: op(op), a(a), b(b) {}
|
|
|
|
~BinaryExpr();
|
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class UnaryExpr : public Expression
|
|
|
|
{
|
|
|
|
UnaryOp op;
|
|
|
|
ExprPtr a;
|
|
|
|
public:
|
|
|
|
UnaryExpr(UnaryOp op, ExprPtr a)
|
|
|
|
: op(op), a(a) {}
|
|
|
|
~UnaryExpr();
|
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
class IdentifierExpr : public Expression
|
|
|
|
{
|
|
|
|
std::string id;
|
|
|
|
std::vector<ExprPtr> arguments;
|
|
|
|
bool isFunction;
|
|
|
|
public:
|
|
|
|
IdentifierExpr(std::string id, bool isFunction = false);
|
2014-10-07 18:15:46 +00:00
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
void addArgument(ExprPtr e);
|
2014-10-07 23:55:54 +00:00
|
|
|
ExprPtr lookup(ResourceCompiler *ctx);
|
2014-10-07 22:41:40 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
2014-10-07 23:37:51 +00:00
|
|
|
virtual std::string evaluateString(ResourceCompiler *ctx);
|
2014-10-07 22:41:40 +00:00
|
|
|
};
|
2014-10-07 18:15:46 +00:00
|
|
|
|
|
|
|
#endif // EXPRESSION_H
|