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