]> gitweb.ps.run Git - toc/blob - src/repr.h
generic functions and structs
[toc] / src / repr.h
1 #pragma once\r
2 \r
3 #include <vector>\r
4 #include <string>\r
5 #include <memory>\r
6 \r
7 #include "TocParser.h"\r
8 \r
9 using namespace std;\r
10 \r
11 struct Type;\r
12 struct Variable;\r
13 struct Body;\r
14 struct Function;\r
15 struct Struct;\r
16 struct Namespace;\r
17 struct Program;\r
18 \r
19 struct FuncExpr;\r
20 struct MethodExpr;\r
21 struct LitExpr;\r
22 struct ParenExpr;\r
23 struct DotExpr;\r
24 struct PrefixOperatorExpr;\r
25 struct PostfixOperatorExpr;\r
26 struct BinaryOperatorExpr;\r
27 struct TernaryOperatorExpr;\r
28 struct BracketsExpr;\r
29 struct IdentifierExpr;\r
30 struct Expr;\r
31 \r
32 struct IfStmt;\r
33 struct SwitchStmt;\r
34 struct ForStmt;\r
35 struct WhileStmt;\r
36 struct AssignStmt;\r
37 struct ReturnStmt;\r
38 struct Stmt;\r
39 \r
40 struct Context\r
41 {\r
42   std::shared_ptr<Context> parent;\r
43   std::vector<Variable> variables;\r
44 };\r
45 \r
46 enum class TypeModifierType\r
47 {\r
48   Pointer, Array\r
49 };\r
50 \r
51 struct TypeModifier\r
52 {\r
53   TypeModifierType type;\r
54 \r
55   bool _staticArray;\r
56   int _arraySize;\r
57 };\r
58 \r
59 struct Type\r
60 {\r
61   std::vector<std::string> namespacePrefixes;\r
62   std::string name;\r
63   std::vector<TypeModifier> modifiers;\r
64   std::vector<Type> genericInstantiation;\r
65 };\r
66 \r
67 struct Variable\r
68 {\r
69   std::string name;\r
70   Type type;\r
71 };\r
72 \r
73 struct Body\r
74 {\r
75   std::shared_ptr<Context> ctx;\r
76   std::vector<Stmt> statements;\r
77 };\r
78 \r
79 struct Function\r
80 {\r
81   std::string name;\r
82   std::vector<std::string> genericTypeNames;\r
83   std::vector<std::vector<Type>> genericInstantiations;\r
84   Type returnType;\r
85   std::vector<Variable> parameters;\r
86   bool defined;\r
87   Body body;\r
88 };\r
89 \r
90 template<typename T>\r
91 struct StructMember\r
92 {\r
93   T t;\r
94   bool isPublic;\r
95   operator T() { return t; }\r
96 };\r
97 \r
98 struct Struct\r
99 {\r
100   std::string name;\r
101   std::vector<std::string> genericTypeNames;\r
102   std::vector<std::vector<Type>> genericInstantiations;\r
103   std::vector<StructMember<Variable>> members;\r
104   std::vector<StructMember<Function>> methods;\r
105 };\r
106 \r
107 struct Namespace\r
108 {\r
109   std::string name;\r
110   std::shared_ptr<Context> ctx;\r
111   std::vector<Struct> structs;\r
112   std::vector<Function> functions;\r
113   std::vector<Namespace> namespaces;\r
114 };\r
115 \r
116 struct Program\r
117 {\r
118   std::shared_ptr<Context> ctx;\r
119   std::vector<Struct> structs;\r
120   std::vector<Function> functions;\r
121   std::vector<Namespace> namespaces;\r
122 };\r
123 \r
124 enum class ExprType\r
125 {\r
126   Func, Method, Lit, Paren, Dot, PrefixOp, PostfixOp, BinaryOp, TernaryOp, Bracket, Identifier\r
127 };\r
128 \r
129 struct FuncExpr\r
130 {\r
131   std::vector<std::string> namespacePrefixes;\r
132   std::string functionName;\r
133   std::vector<Expr> arguments;\r
134   std::vector<Type> genericInstantiation;\r
135 };\r
136 \r
137 struct MethodExpr\r
138 {\r
139   std::shared_ptr<Expr> expr;\r
140   std::string methodName;\r
141   std::vector<Expr> arguments;\r
142   std::vector<Type> genericInstantiation;\r
143 };\r
144 \r
145 enum class LitType\r
146 {\r
147   Int, Decimal, String, Bool\r
148 };\r
149 \r
150 struct LitExpr\r
151 {\r
152   LitType type;\r
153 \r
154   int _int;\r
155   double _decimal;\r
156   std::string _string;\r
157   bool _bool;\r
158 };\r
159 \r
160 struct ParenExpr\r
161 {\r
162   std::shared_ptr<Expr> expr;\r
163 };\r
164 \r
165 struct DotExpr\r
166 {\r
167   bool isPointer;\r
168   std::shared_ptr<Expr> expr;\r
169   std::string identifier;\r
170 };\r
171 \r
172 enum class PrefixOperatorType\r
173 {\r
174   Plus, Minus, Increment, Decrement,\r
175   LogicalNot, BitwiseNot, Dereference, AddressOf,\r
176   COUNT\r
177 };\r
178 static std::string PrefixOperatorTypeStrings[] =\r
179 {\r
180   "+", "-", "++", "--", "!", "~", "*", "&" };\r
181 \r
182 struct PrefixOperatorExpr\r
183 {\r
184   PrefixOperatorType type;\r
185   std::shared_ptr<Expr> expr;\r
186 };\r
187 \r
188 enum class PostfixOperatorType\r
189 {\r
190   Increment, Decrement,\r
191   COUNT\r
192 };\r
193 static std::string PostfixOperatorTypeStrings[] =\r
194 {\r
195   "++", "--" };\r
196 \r
197 struct PostfixOperatorExpr\r
198 {\r
199   PostfixOperatorType type;\r
200   std::shared_ptr<Expr> expr;\r
201 };\r
202 \r
203 enum class BinaryOperatorType\r
204 {\r
205   Plus, Minus, Multiply, Divide, Modulo, BitwiseAnd, BitwiseOr, BitwiseXor, LessThan, GreaterThan,\r
206   LeftShift, RightShift, LogicalAnd, LogicalOr, Equals, NotEquals, LessThanEquals, GreaterThanEquals, BitwiseAndEquals, BitwiseOrEquals, BitwiseXorEquals,\r
207   PlusEquals, MinusEquals, MultiplyEquals, DivideEquals, ModuloEquals,\r
208   LeftShiftEquals, RightShiftEquals,\r
209   COUNT\r
210 };\r
211 static std::string BinaryOperatorTypeStrings[] =\r
212 {\r
213   "+", "-", "*", "/", "%", "&", "|", "^", "<", ">",\r
214   "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",\r
215   "+=","-=","*=","/=","%=",\r
216   "<<=",">>=" };\r
217 \r
218 struct BinaryOperatorExpr\r
219 {\r
220   BinaryOperatorType type;\r
221   std::shared_ptr<Expr> lexpr;\r
222   std::shared_ptr<Expr> rexpr;\r
223 };\r
224 \r
225 struct TernaryOperatorExpr\r
226 {\r
227   std::shared_ptr<Expr> lexpr;\r
228   std::shared_ptr<Expr> rexprTrue;\r
229   std::shared_ptr<Expr> rexprFalse;\r
230 };\r
231 \r
232 struct BracketsExpr\r
233 {\r
234   std::shared_ptr<Expr> lexpr;\r
235   std::shared_ptr<Expr> rexpr;\r
236 };\r
237 \r
238 struct IdentifierExpr\r
239 {\r
240   std::vector<std::string> namespacePrefixes;\r
241   std::string identifier;\r
242 };\r
243 \r
244 struct Expr\r
245 {\r
246   ExprType type;\r
247 \r
248   FuncExpr            _func;\r
249   MethodExpr          _method;\r
250   LitExpr             _lit;\r
251   ParenExpr           _paren;\r
252   DotExpr             _dot;\r
253   PrefixOperatorExpr  _prefixOp;\r
254   PostfixOperatorExpr _postfixOp;\r
255   BinaryOperatorExpr  _binaryOp;\r
256   TernaryOperatorExpr _ternaryOp;\r
257   BracketsExpr        _brackets;\r
258   IdentifierExpr      _identifier;\r
259 };\r
260 \r
261 enum class StmtType\r
262 {\r
263   If, Switch, For, While, Assign, Return, Expr\r
264 };\r
265 \r
266 struct ElseStmt\r
267 {\r
268   bool _if;\r
269   std::shared_ptr<Expr> expr;\r
270   Body body;\r
271 };\r
272 struct IfStmt\r
273 {\r
274   Expr condition;\r
275   Body body;\r
276   std::vector<ElseStmt> elses;\r
277 };\r
278 \r
279 struct SwitchCase\r
280 {\r
281   std::shared_ptr<Expr> expr;\r
282   Body body;\r
283 };\r
284 \r
285 struct SwitchStmt\r
286 {\r
287   std::shared_ptr<Expr> ident;\r
288   std::vector<SwitchCase> cases;\r
289 };\r
290 \r
291 // TODO: int i = 0 (var decl)\r
292 struct ForStmt\r
293 {\r
294   std::shared_ptr<AssignStmt> init;\r
295   std::shared_ptr<Expr> condition;\r
296   std::shared_ptr<Expr> action;\r
297   Body body;\r
298 };\r
299 \r
300 struct WhileStmt\r
301 {\r
302   Expr condition;\r
303   Body body;\r
304 };\r
305 \r
306 struct AssignStmt\r
307 {\r
308   Expr lexpr, rexpr;\r
309 };\r
310 \r
311 struct ReturnStmt\r
312 {\r
313   Expr expr;\r
314 };\r
315 \r
316 struct Stmt\r
317 {\r
318   StmtType type;\r
319   \r
320   IfStmt      _if;\r
321   SwitchStmt  _switch;\r
322   ForStmt     _for;\r
323   WhileStmt   _while;\r
324   AssignStmt  _assign;\r
325   ReturnStmt  _return;\r
326   Expr        _expr;\r
327 };\r
328 \r
329 \r