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