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