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