2014-10-07 18:15:46 +00:00
|
|
|
#ifndef EXPRESSION_H
|
|
|
|
#define EXPRESSION_H
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-10-30 01:56:49 +00:00
|
|
|
#include "location.hh"
|
|
|
|
|
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
|
|
|
|
{
|
2014-10-12 17:16:02 +00:00
|
|
|
XOR, OR, AND, SHIFTLEFT, SHIFTRIGHT, EQUAL, NOTEQUAL, PLUS, MINUS, MULTIPLY, DIVIDE, CONCAT
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class UnaryOp
|
|
|
|
{
|
|
|
|
MINUS, COMPLEMENT
|
|
|
|
};
|
|
|
|
|
|
|
|
class TypeError
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class Expression
|
|
|
|
{
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
yy::location location;
|
|
|
|
|
|
|
|
Expression(yy::location loc) : location(loc) {}
|
|
|
|
|
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();
|
2014-10-30 01:56:49 +00:00
|
|
|
|
|
|
|
void error(ResourceCompiler *ctx, std::string err);
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class StringExpr : public Expression
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
StringExpr(const std::string& str, yy::location loc) : Expression(loc), str(str) {}
|
2014-10-07 18:15:46 +00:00
|
|
|
~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:
|
2014-10-30 01:56:49 +00:00
|
|
|
IntExpr(int val, yy::location loc) : Expression(loc), val(val) {}
|
2014-10-07 18:15:46 +00:00
|
|
|
~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:
|
2014-10-30 01:56:49 +00:00
|
|
|
CompoundExpr(yy::location loc) : Expression(loc) {}
|
|
|
|
|
2014-10-07 18:15:46 +00:00
|
|
|
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:
|
2014-10-30 01:56:49 +00:00
|
|
|
CaseExpr(const std::string& tag, CompoundExprPtr expr, yy::location loc);
|
2014-10-07 23:17:17 +00:00
|
|
|
};
|
|
|
|
|
2014-10-07 18:15:46 +00:00
|
|
|
class BinaryExpr : public Expression
|
|
|
|
{
|
|
|
|
BinaryOp op;
|
|
|
|
ExprPtr a, b;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
BinaryExpr(BinaryOp op, ExprPtr a, ExprPtr b, yy::location loc)
|
|
|
|
: Expression(loc), op(op), a(a), b(b) {}
|
2014-10-07 18:15:46 +00:00
|
|
|
~BinaryExpr();
|
|
|
|
|
2014-10-07 22:41:40 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
2014-10-12 17:16:02 +00:00
|
|
|
virtual std::string evaluateString(ResourceCompiler *ctx);
|
2014-10-07 18:15:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class UnaryExpr : public Expression
|
|
|
|
{
|
|
|
|
UnaryOp op;
|
|
|
|
ExprPtr a;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
UnaryExpr(UnaryOp op, ExprPtr a, yy::location loc)
|
|
|
|
: Expression(loc), op(op), a(a) {}
|
2014-10-07 18:15:46 +00:00
|
|
|
~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
|
|
|
|
{
|
2014-10-12 17:16:02 +00:00
|
|
|
public:
|
2014-10-07 22:41:40 +00:00
|
|
|
std::string id;
|
|
|
|
std::vector<ExprPtr> arguments;
|
2014-10-30 01:56:49 +00:00
|
|
|
IdentifierExpr(std::string id, yy::location loc);
|
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
|
|
|
|
2014-10-12 17:16:02 +00:00
|
|
|
class CountOfExpr : public Expression
|
|
|
|
{
|
|
|
|
IdentifierExprPtr arg;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
CountOfExpr(IdentifierExprPtr arg, yy::location loc) : Expression(loc), arg(arg) {}
|
2014-10-12 17:16:02 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
class ArrayIndexExpr : public Expression
|
|
|
|
{
|
|
|
|
IdentifierExprPtr arg;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
ArrayIndexExpr(IdentifierExprPtr arg, yy::location loc) : Expression(loc), arg(arg) {}
|
2014-10-12 17:16:02 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadExpr : public Expression
|
|
|
|
{
|
|
|
|
ExprPtr arg;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
ReadExpr(ExprPtr arg, yy::location loc) : Expression(loc), arg(arg) {}
|
2014-10-12 17:16:02 +00:00
|
|
|
virtual std::string evaluateString(ResourceCompiler *ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
class UnimplementedExpr : public Expression
|
|
|
|
{
|
|
|
|
std::string msg;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
UnimplementedExpr(std::string msg, yy::location loc) : Expression(loc), msg(msg) {}
|
2014-10-12 17:16:02 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
|
|
|
virtual std::string evaluateString(ResourceCompiler *ctx);
|
|
|
|
};
|
|
|
|
|
2014-10-13 22:11:21 +00:00
|
|
|
class PeekExpr : public Expression
|
|
|
|
{
|
|
|
|
ExprPtr addr;
|
|
|
|
ExprPtr offset;
|
|
|
|
ExprPtr size;
|
|
|
|
public:
|
2014-10-30 01:56:49 +00:00
|
|
|
PeekExpr(ExprPtr addr, ExprPtr offset, ExprPtr size, yy::location loc);
|
|
|
|
PeekExpr(ExprPtr addr, int size, yy::location loc);
|
2014-10-13 22:11:21 +00:00
|
|
|
virtual int evaluateInt(ResourceCompiler *ctx);
|
|
|
|
};
|
2014-10-12 17:16:02 +00:00
|
|
|
|
2014-10-07 18:15:46 +00:00
|
|
|
#endif // EXPRESSION_H
|