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