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