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