]> gitweb.ps.run Git - toc/blob - src/repr.h
complete 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 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 struct IdentifierExpr {\r
101   std::string name;\r
102 };\r
103 \r
104 struct BracketsExpr {\r
105   std::shared_ptr<Expr> lexpr;\r
106   std::shared_ptr<Expr> rexpr;\r
107 };\r
108 \r
109 enum class UnaryOperatorType {\r
110   Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost, LogicalNot, BitwiseNot, Dereference, AddressOf\r
111 };\r
112 \r
113 enum class BinaryOperatorType {\r
114   Plus, Minus, Multiply, Divide, Modulo, BitwiseAnd, BitwiseOr, BitwiseXor, LessThan, GreaterThan,\r
115   LeftShift, RightShift, LogicalAnd, LogicalOr, Equals, NotEquals, LessThanEquals, GreaterThanEquals, BitwiseAndEquals, BitwiseOrEquals, BitwiseXorEquals,\r
116   PlusEquals, MinusEquals, MultiplyEquals, DivideEquals, ModuloEquals,\r
117   LeftShiftEquals, RightShiftEquals\r
118 };\r
119 static std::string UnaryOperatorTypeStrings[] = {\r
120   "+", "-", "++", "--", "++", "--", "!", "~", "*", "&" };\r
121 \r
122 static std::string BinaryOperatorTypeStrings[] = {\r
123   "+", "-", "*", "/", "%", "&", "|", "^", "<", ">",\r
124   "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",\r
125   "+=","-=","*=","/=","%=",\r
126   "<<=",">>=" };\r
127 \r
128 struct UnaryOperatorExpr {\r
129   UnaryOperatorType type;\r
130   std::shared_ptr<Expr> expr;\r
131 };\r
132 \r
133 struct BinaryOperatorExpr {\r
134   BinaryOperatorType type;\r
135   std::shared_ptr<Expr> lexpr;\r
136   std::shared_ptr<Expr> rexpr;\r
137 };\r
138 \r
139 struct TernaryOperatorExpr {\r
140   std::shared_ptr<Expr> lexpr;\r
141   std::shared_ptr<Expr> rexprTrue;\r
142   std::shared_ptr<Expr> rexprFalse;\r
143 };\r
144 \r
145 struct DotExpr {\r
146   std::shared_ptr<Expr> lexpr;\r
147   IdentifierExpr ident;\r
148 };\r
149 \r
150 struct Expr {\r
151   ExprType type;\r
152 \r
153   FuncExpr            _func;\r
154   LitExpr             _lit;\r
155   IdentifierExpr      _identifier;\r
156   BracketsExpr        _brackets;\r
157   UnaryOperatorExpr   _unaryOperator;\r
158   BinaryOperatorExpr  _binaryOperator;\r
159   TernaryOperatorExpr _ternaryOperator;\r
160   DotExpr             _dot;\r
161 };\r
162 \r
163 enum class StmtType {\r
164   If, Switch, For, While, Assign, Return, Expr\r
165 };\r
166 \r
167 struct ElseStmt {\r
168   bool _if;\r
169   std::shared_ptr<Expr> expr;\r
170   Body body;\r
171 };\r
172 struct IfStmt {\r
173   Expr condition;\r
174   Body body;\r
175   std::vector<ElseStmt> elses;\r
176 };\r
177 \r
178 struct SwitchCase {\r
179   std::shared_ptr<Expr> expr;\r
180   Body body;\r
181 };\r
182 \r
183 struct SwitchStmt {\r
184   IdentifierExpr ident;\r
185   std::vector<SwitchCase> cases;\r
186 };\r
187 \r
188 struct ForStmt {\r
189   AssignStmt assign;\r
190   std::shared_ptr<Expr> condition;\r
191   std::shared_ptr<Expr> action;\r
192   Body body;\r
193 };\r
194 \r
195 struct WhileStmt {\r
196   Expr condition;\r
197   Body body;\r
198 };\r
199 \r
200 struct AssignStmt {\r
201   Expr lexpr;\r
202   Expr rexpr;\r
203 };\r
204 \r
205 struct ReturnStmt {\r
206   Expr expr;\r
207 };\r
208 \r
209 struct Stmt {\r
210   StmtType type;\r
211   \r
212   IfStmt      _if;\r
213   SwitchStmt  _switch;\r
214   ForStmt     _for;\r
215   WhileStmt   _while;\r
216   AssignStmt  _assign;\r
217   ReturnStmt  _return;\r
218   Expr        _expr;\r
219 };\r
220 \r
221 \r