this repo has no description
1#ifndef EXPRESSION_H
2#define EXPRESSION_H
3
4#include <memory>
5#include <vector>
6
7#include "location.hh"
8
9class ResourceCompiler;
10
11class Expression;
12class CompoundExpr;
13class IdentifierExpr;
14class CaseExpr;
15typedef std::shared_ptr<Expression> ExprPtr;
16typedef std::shared_ptr<CompoundExpr> CompoundExprPtr;
17typedef std::shared_ptr<IdentifierExpr> IdentifierExprPtr;
18typedef std::shared_ptr<CaseExpr> CaseExprPtr;
19
20
21enum class BinaryOp
22{
23 XOR, OR, AND, SHIFTLEFT, SHIFTRIGHT, EQUAL, NOTEQUAL, PLUS, MINUS, MULTIPLY, DIVIDE, CONCAT
24};
25
26enum class UnaryOp
27{
28 MINUS, COMPLEMENT
29};
30
31class TypeError
32{
33};
34
35class Expression
36{
37public:
38 yy::location location;
39
40 Expression(yy::location loc) : location(loc) {}
41
42 virtual int evaluateInt(ResourceCompiler *ctx);
43 virtual std::string evaluateString(ResourceCompiler *ctx);
44 virtual ~Expression();
45
46 void error(ResourceCompiler *ctx, std::string err);
47};
48
49class StringExpr : public Expression
50{
51 std::string str;
52public:
53 StringExpr(const std::string& str, yy::location loc) : Expression(loc), str(str) {}
54 ~StringExpr();
55 virtual std::string evaluateString(ResourceCompiler *ctx);
56};
57
58class IntExpr : public Expression
59{
60 int val;
61public:
62 IntExpr(int val, yy::location loc) : Expression(loc), val(val) {}
63 ~IntExpr();
64
65 virtual int evaluateInt(ResourceCompiler *ctx);
66};
67
68class CompoundExpr : public Expression
69{
70 std::vector<ExprPtr> items;
71public:
72 CompoundExpr(yy::location loc) : Expression(loc) {}
73
74 void addItem(ExprPtr item);
75 ExprPtr getItem(int i) const { return items[i]; }
76 int size() const { return items.size(); }
77
78 ~CompoundExpr();
79};
80
81class CaseExpr : public Expression
82{
83 std::string tag;
84 CompoundExprPtr expr;
85 friend class SwitchField;
86public:
87 CaseExpr(const std::string& tag, CompoundExprPtr expr, yy::location loc);
88};
89
90class BinaryExpr : public Expression
91{
92 BinaryOp op;
93 ExprPtr a, b;
94public:
95 BinaryExpr(BinaryOp op, ExprPtr a, ExprPtr b, yy::location loc)
96 : Expression(loc), op(op), a(a), b(b) {}
97 ~BinaryExpr();
98
99 virtual int evaluateInt(ResourceCompiler *ctx);
100 virtual std::string evaluateString(ResourceCompiler *ctx);
101};
102
103class UnaryExpr : public Expression
104{
105 UnaryOp op;
106 ExprPtr a;
107public:
108 UnaryExpr(UnaryOp op, ExprPtr a, yy::location loc)
109 : Expression(loc), op(op), a(a) {}
110 ~UnaryExpr();
111
112 virtual int evaluateInt(ResourceCompiler *ctx);
113};
114
115class IdentifierExpr : public Expression
116{
117public:
118 std::string id;
119 std::vector<ExprPtr> arguments;
120 IdentifierExpr(std::string id, yy::location loc);
121
122 void addArgument(ExprPtr e);
123 ExprPtr lookup(ResourceCompiler *ctx);
124 virtual int evaluateInt(ResourceCompiler *ctx);
125 virtual std::string evaluateString(ResourceCompiler *ctx);
126};
127
128class CountOfExpr : public Expression
129{
130 IdentifierExprPtr arg;
131public:
132 CountOfExpr(IdentifierExprPtr arg, yy::location loc) : Expression(loc), arg(arg) {}
133 virtual int evaluateInt(ResourceCompiler *ctx);
134};
135
136class ArrayIndexExpr : public Expression
137{
138 IdentifierExprPtr arg;
139public:
140 ArrayIndexExpr(IdentifierExprPtr arg, yy::location loc) : Expression(loc), arg(arg) {}
141 virtual int evaluateInt(ResourceCompiler *ctx);
142};
143
144class ReadExpr : public Expression
145{
146 ExprPtr arg;
147public:
148 ReadExpr(ExprPtr arg, yy::location loc) : Expression(loc), arg(arg) {}
149 virtual std::string evaluateString(ResourceCompiler *ctx);
150};
151
152class UnimplementedExpr : public Expression
153{
154 std::string msg;
155public:
156 UnimplementedExpr(std::string msg, yy::location loc) : Expression(loc), msg(msg) {}
157 virtual int evaluateInt(ResourceCompiler *ctx);
158 virtual std::string evaluateString(ResourceCompiler *ctx);
159};
160
161class PeekExpr : public Expression
162{
163 ExprPtr addr;
164 ExprPtr offset;
165 ExprPtr size;
166public:
167 PeekExpr(ExprPtr addr, ExprPtr offset, ExprPtr size, yy::location loc);
168 PeekExpr(ExprPtr addr, int size, yy::location loc);
169 virtual int evaluateInt(ResourceCompiler *ctx);
170};
171
172#endif // EXPRESSION_H