abouttreesummaryrefslogcommitdiff
path: root/src/repr.h
blob: c745ba17b3c3ae65d4f68a08b0a2a3cfe4f3f2f4 (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
#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 Program;
struct FuncExpr;
struct LitExpr;
struct IdentifierExpr;
struct BracketsExpr;
struct UnaryOperatorExpr;
struct BinaryOperatorExpr;
struct TernaryOperatorExpr;
struct DotExpr;
struct Expr;
struct IfStmt;
struct SwitchStmt;
struct ForStmt;
struct WhileStmt;
struct AssignStmt;
struct ReturnStmt;
struct Stmt;

enum class TypeModifierType {
  Pointer, Array
};

struct TypeModifier {
  TypeModifierType type;

  int _arraySize;
};

struct Type {
  std::string name;
  std::vector<TypeModifier> modifiers;
};

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

struct Body {
  std::vector<Variable> variables;
  std::vector<Stmt> statements;
};

struct Function {
  Type returnType;
  std::string name;
  std::vector<Variable> parameters;
  Body body;
};

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

struct Program {
  std::vector<Variable> variables;
  std::vector<Struct> structs;
  std::vector<Function> functions;
};

enum class ExprType {
  Func, Lit, Identifier, Brackets, UnaryOperator, BinaryOperator, TernaryOperator, Dot
};

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

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

struct LitExpr {
  LitType type;

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

struct IdentifierExpr {
  std::string name;
};

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

enum class UnaryOperatorType {
  Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost, LogicalNot, BitwiseNot, Dereference, AddressOf
};

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
};
static std::string UnaryOperatorTypeStrings[] = {
  "+", "-", "++", "--", "++", "--", "!", "~", "*", "&" };

static std::string BinaryOperatorTypeStrings[] = {
  "+", "-", "*", "/", "%", "&", "|", "^", "<", ">",
  "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",
  "+=","-=","*=","/=","%=",
  "<<=",">>=" };

struct UnaryOperatorExpr {
  UnaryOperatorType type;
  std::shared_ptr<Expr> expr;
};

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 DotExpr {
  std::shared_ptr<Expr> lexpr;
  IdentifierExpr ident;
};

struct Expr {
  ExprType type;

  FuncExpr            _func;
  LitExpr             _lit;
  IdentifierExpr      _identifier;
  BracketsExpr        _brackets;
  UnaryOperatorExpr   _unaryOperator;
  BinaryOperatorExpr  _binaryOperator;
  TernaryOperatorExpr _ternaryOperator;
  DotExpr             _dot;
};

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 {
  IdentifierExpr ident;
  std::vector<SwitchCase> cases;
};

struct ForStmt {
  AssignStmt assign;
  std::shared_ptr<Expr> condition;
  std::shared_ptr<Expr> action;
  Body body;
};

struct WhileStmt {
  Expr condition;
  Body body;
};

struct AssignStmt {
  Expr lexpr;
  Expr rexpr;
};

struct ReturnStmt {
  Expr expr;
};

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