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