abouttreesummaryrefslogcommitdiff
path: root/src/repr.h
blob: 959e74dfe891873eecfcdbf2671d003497f8bb95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#pragma once

#include <vector>
#include <string>
#include <memory>

#include "TocParser.h"

using namespace std;

struct Type;
struct Variable;
struct Body;
struct Function;
struct Struct;
struct Namespace;
struct Program;

struct FuncExpr;
struct MethodExpr;
struct LitExpr;
struct ParenExpr;
struct DotExpr;
struct PrefixOperatorExpr;
struct PostfixOperatorExpr;
struct BinaryOperatorExpr;
struct TernaryOperatorExpr;
struct BracketsExpr;
struct IdentifierExpr;
struct Expr;

struct IfStmt;
struct SwitchStmt;
struct ForStmt;
struct WhileStmt;
struct AssignStmt;
struct ReturnStmt;
struct Stmt;

struct Context
{
  std::optional<std::string> name;
  std::shared_ptr<Context> parent;
  std::vector<Variable> variables;
  std::vector<Function> functions;
  std::vector<Struct> structs;
  std::vector<Namespace> namespaces;
};

enum class TypeModifierType
{
  Pointer, Array
};

struct TypeModifier
{
  TypeModifierType type;

  bool _staticArray;
  int _arraySize;
};

struct Type
{
  std::vector<std::string> namespacePrefixes;
  std::string name;
  std::vector<TypeModifier> modifiers;
  std::vector<Type> genericInstantiation;
  
  bool operator!=(const Type & that)
  {
    if (this->name != that.name)
      return true;

    for (int i = 0; i < this->modifiers.size(); i++)
      if (this->modifiers[i].type != that.modifiers[i].type)
        return true;

    for (int i = 0; i < this->namespacePrefixes.size(); i++)
      if (this->namespacePrefixes[i] != that.namespacePrefixes[i])
        return true;
    return false;
  }
};

struct Variable
{
  std::string name;
  Type type;
};

struct Body
{
  std::shared_ptr<Context> ctx;
  std::vector<Stmt> statements;
};

struct Function
{
  std::string name;
  Type returnType;
  std::vector<Variable> parameters;
  bool defined;

  std::vector<std::string> genericTypeNames;
  std::vector<std::vector<Type>> genericInstantiations;

  Body body;
};

template<typename T>
struct StructMember
{
  T t;
  bool isPublic;
  operator T() { return t; }
};

struct Struct
{
  std::string name;
  std::vector<std::string> genericTypeNames;
  std::vector<std::vector<Type>> genericInstantiations;
  std::vector<StructMember<Variable>> members;
  std::vector<StructMember<Function>> methods;
};

struct Namespace
{
  std::string name;
  std::shared_ptr<Context> ctx;
};

struct Program
{
  std::shared_ptr<Context> ctx;
};

enum class ExprType
{
  Func, Method, Lit, Paren, Dot, PrefixOp, PostfixOp, BinaryOp, TernaryOp, Bracket, Identifier
};

struct FuncExpr
{
  std::vector<std::string> namespacePrefixes;
  std::string functionName;
  std::vector<Expr> arguments;
  std::vector<Type> genericInstantiation;
};

struct MethodExpr
{
  std::shared_ptr<Expr> expr;
  std::string methodName;
  std::vector<Expr> arguments;
  std::vector<Type> genericInstantiation;
};

enum class LitType
{
  Int, Decimal, String, Bool
};

struct LitExpr
{
  LitType type;

  int _int;
  double _decimal;
  std::string _string;
  bool _bool;
};

struct ParenExpr
{
  std::shared_ptr<Expr> expr;
};

struct DotExpr
{
  bool isPointer;
  std::shared_ptr<Expr> expr;
  std::string identifier;
};

enum class PrefixOperatorType
{
  Plus, Minus, Increment, Decrement,
  LogicalNot, BitwiseNot, Dereference, AddressOf,
  COUNT
};
static std::string PrefixOperatorTypeStrings[] =
{
  "+", "-", "++", "--", "!", "~", "*", "&" };

struct PrefixOperatorExpr
{
  PrefixOperatorType type;
  std::shared_ptr<Expr> expr;
};

enum class PostfixOperatorType
{
  Increment, Decrement,
  COUNT
};
static std::string PostfixOperatorTypeStrings[] =
{
  "++", "--" };

struct PostfixOperatorExpr
{
  PostfixOperatorType type;
  std::shared_ptr<Expr> expr;
};

enum class BinaryOperatorType
{
  Plus, Minus, Multiply, Divide, Modulo, BitwiseAnd, BitwiseOr, BitwiseXor, LessThan, GreaterThan,
  LeftShift, RightShift, LogicalAnd, LogicalOr, Equals, NotEquals, LessThanEquals, GreaterThanEquals, BitwiseAndEquals, BitwiseOrEquals, BitwiseXorEquals,
  PlusEquals, MinusEquals, MultiplyEquals, DivideEquals, ModuloEquals,
  LeftShiftEquals, RightShiftEquals,
  COUNT
};
static std::string BinaryOperatorTypeStrings[] =
{
  "+", "-", "*", "/", "%", "&", "|", "^", "<", ">",
  "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",
  "+=","-=","*=","/=","%=",
  "<<=",">>=" };

struct BinaryOperatorExpr
{
  BinaryOperatorType type;
  std::shared_ptr<Expr> lexpr;
  std::shared_ptr<Expr> rexpr;
};

struct TernaryOperatorExpr
{
  std::shared_ptr<Expr> lexpr;
  std::shared_ptr<Expr> rexprTrue;
  std::shared_ptr<Expr> rexprFalse;
};

struct BracketsExpr
{
  std::shared_ptr<Expr> lexpr;
  std::shared_ptr<Expr> rexpr;
};

struct IdentifierExpr
{
  std::vector<std::string> namespacePrefixes;
  std::string identifier;
};

struct Expr
{
  ExprType type;

  FuncExpr            _func;
  MethodExpr          _method;
  LitExpr             _lit;
  ParenExpr           _paren;
  DotExpr             _dot;
  PrefixOperatorExpr  _prefixOp;
  PostfixOperatorExpr _postfixOp;
  BinaryOperatorExpr  _binaryOp;
  TernaryOperatorExpr _ternaryOp;
  BracketsExpr        _brackets;
  IdentifierExpr      _identifier;
};

enum class StmtType
{
  If, Switch, For, While, Assign, Return, Expr
};

struct ElseStmt
{
  bool _if;
  std::shared_ptr<Expr> expr;
  Body body;
};
struct IfStmt
{
  Expr condition;
  Body body;
  std::vector<ElseStmt> elses;
};

struct SwitchCase
{
  std::shared_ptr<Expr> expr;
  Body body;
};

struct SwitchStmt
{
  std::shared_ptr<Expr> ident;
  std::vector<SwitchCase> cases;
};

// TODO: int i = 0 (var decl)
struct ForStmt
{
  std::shared_ptr<AssignStmt> init;
  std::shared_ptr<Expr> condition;
  std::shared_ptr<Expr> action;
  Body body;
};

struct WhileStmt
{
  Expr condition;
  Body body;
};

struct AssignStmt
{
  Expr lexpr, rexpr;
};

struct ReturnStmt
{
  Expr expr;
};

struct Stmt
{
  StmtType type;
  
  IfStmt      _if;
  SwitchStmt  _switch;
  ForStmt     _for;
  WhileStmt   _while;
  AssignStmt  _assign;
  ReturnStmt  _return;
  Expr        _expr;
};