]> gitweb.ps.run Git - toc/blob - src/repr.h
add namespace, private struct member grammar, change bracket style
[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 Program;\r
17 struct FuncExpr;\r
18 struct LitExpr;\r
19 struct IdentifierExpr;\r
20 struct AccessExpr;\r
21 struct BracketsExpr;\r
22 struct UnaryOperatorExpr;\r
23 struct BinaryOperatorExpr;\r
24 struct TernaryOperatorExpr;\r
25 struct DotExpr;\r
26 struct ParenExpr;\r
27 struct Expr;\r
28 struct IfStmt;\r
29 struct SwitchStmt;\r
30 struct ForStmt;\r
31 struct WhileStmt;\r
32 struct AssignStmt;\r
33 struct ReturnStmt;\r
34 struct Stmt;\r
35 \r
36 enum class TypeModifierType\r
37 {\r
38   Pointer, Array\r
39 };\r
40 \r
41 struct TypeModifier\r
42 {\r
43   TypeModifierType type;\r
44 \r
45   bool _staticArray;\r
46   int _arraySize;\r
47 };\r
48 \r
49 struct Type\r
50 {\r
51   std::string name;\r
52   std::vector<TypeModifier> modifiers;\r
53 };\r
54 \r
55 struct Variable\r
56 {\r
57   std::string name;\r
58   Type type;\r
59 };\r
60 \r
61 struct Body\r
62 {\r
63   std::vector<Variable> variables;\r
64   std::vector<Stmt> statements;\r
65 };\r
66 \r
67 struct Function\r
68 {\r
69   Type returnType;\r
70   std::string name;\r
71   std::vector<Variable> parameters;\r
72   Body body;\r
73 };\r
74 \r
75 struct Struct\r
76 {\r
77   std::string name;\r
78   std::vector<Variable> members;\r
79   std::vector<Function> methods;\r
80 };\r
81 \r
82 struct Program\r
83 {\r
84   std::vector<Variable> variables;\r
85   std::vector<Struct> structs;\r
86   std::vector<Function> functions;\r
87 };\r
88 \r
89 enum class ExprType\r
90 {\r
91   Func, Lit, Identifier, Brackets, UnaryOperator, BinaryOperator, TernaryOperator, Dot\r
92 };\r
93 \r
94 struct FuncExpr\r
95 {\r
96   std::string functionName;\r
97   std::vector<Expr> arguments;\r
98 };\r
99 \r
100 enum class LitType\r
101 {\r
102   Int, Decimal, String, Bool\r
103 };\r
104 \r
105 struct LitExpr\r
106 {\r
107   LitType type;\r
108 \r
109   int _int;\r
110   double _decimal;\r
111   std::string _string;\r
112   bool _bool;\r
113 };\r
114 \r
115 // TODO: accessExpr\r
116 struct IdentifierExpr\r
117 {\r
118   std::string name;\r
119 };\r
120 \r
121 struct BracketsExpr\r
122 {\r
123   std::shared_ptr<Expr> lexpr;\r
124   std::shared_ptr<Expr> rexpr;\r
125 };\r
126 \r
127 enum class UnaryOperatorType\r
128 {\r
129   Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost,\r
130   LogicalNot, BitwiseNot, Dereference, AddressOf,\r
131   COUNT\r
132 };\r
133 \r
134 enum class BinaryOperatorType\r
135 {\r
136   Plus, Minus, Multiply, Divide, Modulo, BitwiseAnd, BitwiseOr, BitwiseXor, LessThan, GreaterThan,\r
137   LeftShift, RightShift, LogicalAnd, LogicalOr, Equals, NotEquals, LessThanEquals, GreaterThanEquals, BitwiseAndEquals, BitwiseOrEquals, BitwiseXorEquals,\r
138   PlusEquals, MinusEquals, MultiplyEquals, DivideEquals, ModuloEquals,\r
139   LeftShiftEquals, RightShiftEquals,\r
140   COUNT\r
141 };\r
142 static std::string UnaryOperatorTypeStrings[] =\r
143 {\r
144   "+", "-", "++", "--", "++", "--", "!", "~", "*", "&" };\r
145 \r
146 static std::string BinaryOperatorTypeStrings[] =\r
147 {\r
148   "+", "-", "*", "/", "%", "&", "|", "^", "<", ">",\r
149   "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",\r
150   "+=","-=","*=","/=","%=",\r
151   "<<=",">>=" };\r
152 \r
153 struct UnaryOperatorExpr\r
154 {\r
155   UnaryOperatorType type;\r
156   std::shared_ptr<Expr> expr;\r
157 };\r
158 \r
159 struct BinaryOperatorExpr\r
160 {\r
161   BinaryOperatorType type;\r
162   std::shared_ptr<Expr> lexpr;\r
163   std::shared_ptr<Expr> rexpr;\r
164 };\r
165 \r
166 struct TernaryOperatorExpr\r
167 {\r
168   std::shared_ptr<Expr> lexpr;\r
169   std::shared_ptr<Expr> rexprTrue;\r
170   std::shared_ptr<Expr> rexprFalse;\r
171 };\r
172 \r
173 struct DotExpr\r
174 {\r
175   std::shared_ptr<Expr> expr;\r
176   IdentifierExpr ident;\r
177 };\r
178 \r
179 // TODO: paren expr\r
180 struct Expr\r
181 {\r
182   ExprType type;\r
183 \r
184   bool parenthesized;\r
185 \r
186   FuncExpr            _func;\r
187   LitExpr             _lit;\r
188   IdentifierExpr      _identifier;\r
189   BracketsExpr        _brackets;\r
190   UnaryOperatorExpr   _unaryOperator;\r
191   BinaryOperatorExpr  _binaryOperator;\r
192   TernaryOperatorExpr _ternaryOperator;\r
193   DotExpr             _dot;\r
194 };\r
195 \r
196 enum class StmtType\r
197 {\r
198   If, Switch, For, While, Assign, Return, Expr\r
199 };\r
200 \r
201 struct ElseStmt\r
202 {\r
203   bool _if;\r
204   std::shared_ptr<Expr> expr;\r
205   Body body;\r
206 };\r
207 struct IfStmt\r
208 {\r
209   Expr condition;\r
210   Body body;\r
211   std::vector<ElseStmt> elses;\r
212 };\r
213 \r
214 struct SwitchCase\r
215 {\r
216   std::shared_ptr<Expr> expr;\r
217   Body body;\r
218 };\r
219 \r
220 struct SwitchStmt\r
221 {\r
222   IdentifierExpr ident;\r
223   std::vector<SwitchCase> cases;\r
224 };\r
225 \r
226 // TODO: int i = 0 (var decl)\r
227 struct ForStmt\r
228 {\r
229   std::string varName;\r
230   std::shared_ptr<Expr> initValue;\r
231   std::shared_ptr<Expr> condition;\r
232   std::shared_ptr<Expr> action;\r
233   Body body;\r
234 };\r
235 \r
236 struct WhileStmt\r
237 {\r
238   Expr condition;\r
239   Body body;\r
240 };\r
241 \r
242 struct AssignStmt\r
243 {\r
244   std::string name;\r
245   Expr expr;\r
246 };\r
247 \r
248 struct ReturnStmt\r
249 {\r
250   Expr expr;\r
251 };\r
252 \r
253 struct Stmt\r
254 {\r
255   StmtType type;\r
256   \r
257   IfStmt      _if;\r
258   SwitchStmt  _switch;\r
259   ForStmt     _for;\r
260   WhileStmt   _while;\r
261   AssignStmt  _assign;\r
262   ReturnStmt  _return;\r
263   Expr        _expr;\r
264 };\r
265 \r
266 \r