Repository navigation
Expand file tree
/
Copy pathVariableDecl.cpp
More file actions
65 lines (48 loc) · 1.93 KB
/
Copy pathVariableDecl.cpp
File metadata and controls
65 lines (48 loc) · 1.93 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
#include "TreeSitterParser.hpp"
unique_ptr<VarDeclareExpr> Parser::ParseVarDecl () {
int numChilds = cursor.getNumChilds();
std::optional<std::string> primitiveType = std::nullopt;
optional<unique_ptr<Expr>> expr = std::nullopt;
string identifier;
if (cursor.getNumChilds() == 1) {
throw invalid_argument("cannot declare auto variable with empty value");
}
vector<unique_ptr<Expr>> exprs;
// get primitive type
cursor.goToChild();
if (cursor.getType() == "primitive_type") {
primitiveType = cursor.getSourceStr();
cursor.goToSibling();
// get identifier
assert(cursor.getType() == "identifier");
identifier = cursor.getSourceStr();
if (numChilds > 2) {
// get next char
cursor.goToSibling(true);
if (cursor.getSourceStr() == "=") { // normal var decl
cursor.goToSibling(true);
assert(cursor.getType() == "expression");
expr = ParseExpression();
} else { // ohh is a class decl
cursor.goToSibling(true);
while (true) {
auto ex = ParseExpression();
cursor.goToSibling(true);
exprs.push_back(move(ex));
if (cursor.getSourceStr() == ",") cursor.goToSibling(true);
else break;
}
}
}
}
else {
assert(cursor.getType() == "identifier");
identifier = cursor.getSourceStr();
cursor.goToSibling();
assert(cursor.getType() == "expression");
expr = ParseExpression();
}
// go back to the var decl expr
cursor.goToParent();
return make_unique<VarDeclareExpr>(VarMutability::VAR_MUTABILITY_VAR, identifier, move(expr), primitiveType, move(exprs));
}