Repository navigation
Expand file tree
/
Copy pathParseExpression.cpp
More file actions
204 lines (156 loc) · 5.41 KB
/
Copy pathParseExpression.cpp
File metadata and controls
204 lines (156 loc) · 5.41 KB
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
#include "TreeSitterParser.hpp"
#include <cstring>
#include <string>
unique_ptr<VarUseExpr> Parser::ParseIdentifier () {
std::string name = cursor.getSourceStr();
return make_unique<VarUseExpr>(name);
}
unique_ptr<Expr> Parser::ParseExpression () {
auto node = cursor.goToChild();
auto type = cursor.getType();
unique_ptr<Expr> result;
if (type == "parathensisExpr") result = ParseParanthesis();
else if (type == "binary_expression") result = ParseBinaryOp();
else if (type == "number") result = ParseNumber();
else if (type == "string") result = ParseString();
else if (type == "identifier") result = ParseIdentifier();
else if (type == "ternaryStatement") result = ParseTernary();
else if (type == "func_call") result = ParseFuncCall();
else if (type == "bool") result = ParseBool();
else if (type == "pointer") result = ParsePointer();
else if (type == "accessor") result = ParseAccessor();
else if (type == "char") result = ParseChar();
else {
throw invalid_argument((std::string("Parsing expression invalid type: ") + type).c_str());
}
cursor.goToParent();
return result;
}
unique_ptr<Expr> Parser::ParseNumber() {
auto src = cursor.getSourceStr();
bool isFloat = src[src.size()-1] == 'f';
// detect if it is a float or not
istringstream is(src);
if (isFloat) {
float x;
is >> x;
return make_unique<FloatExpr>(x);
}
if (strchr(src.c_str(), '.') != NULL) {
double x;
is >> x;
return make_unique<DoubleExpr>(x);
}
else {
int x;
is >> x;
return make_unique<IntExpr>(x);
}
}
unique_ptr<Expr> Parser::ParseString() {
auto src = cursor.getSourceStr();
src.erase(src.begin()); // remove the supporting ""
src.pop_back();
return make_unique<StringExpr>(src);
}
unique_ptr<Expr> Parser::ParseBinaryOp() {
auto src = cursor.currentNode();
// get first expression
cursor.goToChild();
if (cursor.getType() == "parathensisExpr") {
auto result = ParseParanthesis();
cursor.goToParent();
return move(result);
}
auto firstExpression = ParseExpression();
// get op type
cursor.goToSibling(true);
std::string op = cursor.getType();
// go to second expression
cursor.goToSibling();
unique_ptr<Expr> secondExpression = nullptr;
string asTypeOp = "";
if (op != "as") secondExpression = ParseExpression();
else asTypeOp = cursor.getSourceStr();
cursor.goToParent(); // redirect back to the binary expression
return make_unique<BinaryOpExpr>(op, move(firstExpression), move(secondExpression), asTypeOp);
}
unique_ptr<Expr> Parser::ParseAssigment() {
int numChilds = cursor.getNumChilds();
cursor.goToChild();
vector<unique_ptr<Expr>> p = {};
unique_ptr<Expr> expr = nullptr;
std::string var_name = "";
std::string type = cursor.getType();
// new
var_name = cursor.getSourceStr();
// test if there [
cursor.goToSibling(true);
if (cursor.getSourceStr() == "[") {
while (true) {
cursor.goToSibling(true);
auto srcSource = cursor.getSourceStr();
if (srcSource == "]") break;
if (srcSource == ",") continue;
p.push_back(ParseExpression());
}
cursor.goToSibling(true);
}
if (cursor.getSourceStr() == "=") {
cursor.goToSibling();
expr = ParseExpression();
}
else {
auto baseChild = make_unique<VarUseExpr>(var_name);
auto increment = make_unique<IntExpr>(1);
if (cursor.getSourceStr() == "++")
expr = make_unique<BinaryOpExpr>("+", move(baseChild), move(increment));
else if (cursor.getSourceStr() == "--")
expr = make_unique<BinaryOpExpr>("-", move(baseChild), move(increment));
}
cursor.goToSibling();
if (var_name == "") throw invalid_argument("Var name cannot be empty!");
cursor.goToParent();
return make_unique<AssignExpr>(var_name, move(expr), move(p));
}
unique_ptr<Expr> Parser::ParseReturn () {
optional<unique_ptr<Expr>> val = nullopt;
cursor.goToChild();
if (cursor.getType() == "expression") {
val = ParseExpression();
}
cursor.goToParent();
return make_unique<ReturnExpr>(move(val));
}
unique_ptr<Expr> Parser::ParseBool () {
unique_ptr<Expr> ret;
cursor.goToChild();
if (cursor.getType() == "true") ret = make_unique<IntExpr>(1, 1);
else if (cursor.getType() == "false") ret = make_unique<IntExpr>(0, 1);
else throw invalid_argument("this should never happen");
cursor.goToParent();
return move(ret);
}
unique_ptr<PointerExpr> Parser::ParsePointer () {
cursor.goToChild();
auto expr = ParseIdentifier();
cursor.goToParent();
return make_unique<PointerExpr>(expr->var);
}
unique_ptr<AccessorExpr> Parser::ParseAccessor () {
int numChilds = cursor.getNumChilds();
cursor.goToChild();
auto ex = ParseIdentifier();
vector<unique_ptr<Expr>> arr;
cursor.goToSibling();
for (int i = 0; i < numChilds-1; i++) {
arr.push_back(ParseExpression());
cursor.goToSibling();
}
cursor.goToParent();
return make_unique<AccessorExpr>(move(ex), move(arr));
}
unique_ptr<CharExpr> Parser::ParseChar () {
string x = cursor.getSourceStr();
return make_unique<CharExpr>(x[1]); // skip the ' at the beginning
}