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