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