Repository navigation
Expand file tree
/
Copy pathParseClass.cpp
More file actions
128 lines (94 loc) · 3.38 KB
/
Copy pathParseClass.cpp
File metadata and controls
128 lines (94 loc) · 3.38 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
#include <TreeSitterParser.hpp>
unique_ptr<Expr> Parser::ParseClass () {
bool isPublic = true;
vector<VarTemplate> variables;
vector<FuncTemplate> functions;
string className;
vector<ConstructTemplate> constructors;
map<string, int> variableDefined;
int numChilds = cursor.getNumChilds();
cursor.goToChild();
// get name
className = cursor.getSourceStr();
cursor.goToSibling();
for (int i = 1; i < numChilds; i++) {
string ty = cursor.getType();
if (ty == "publicPrivate") {
if (cursor.getSourceStr() == "public:") isPublic = true;
else if (cursor.getSourceStr() == "private:") isPublic = false;
cursor.goToSibling();
}
else if (ty == "var_declaration") {
auto exp = ParseVarDecl();
if (!exp->typeArg) throw invalid_argument("Cannot declare auto in class decl");
variableDefined[exp->name] = 1;
variables.push_back({isPublic, exp.release()});
cursor.goToSibling();
}
else if (ty == "function_declare") {
auto exp = ParseFunctionDeclare();
functions.push_back({
move(exp),
isPublic
});
cursor.goToSibling();
}
else if (ty == "constructor") {
vector<tuple<string, string>> params;
cursor.goToChild();
// parse parameter list
assert(cursor.getType() == "parameter_list");
int numParameters = cursor.getNumChilds();
cursor.goToChild();
for (int i = 0; i < numParameters; i++) {
cursor.goToChild();
std::string primitiveType = cursor.getSourceStr();
cursor.goToSibling();
std::string identifier = cursor.getSourceStr();
cursor.goToParent();
cursor.goToSibling();
params.push_back({primitiveType, identifier});
}
cursor.goToParent();
cursor.goToSibling();
// parse block
assert(cursor.getType() == "block");
auto bl = ParseBlock();
cursor.goToParent();
cursor.goToSibling();
// add to constructors
constructors.push_back({params, move(bl)});
}
}
cursor.goToParent();
return make_unique<ClassExpr>(className, move(variables), move(functions), move(constructors));
}
unique_ptr<ClassVarDecl> Parser::ParseClassVarDeclr () {
int numChild = cursor.getNumChilds();
cursor.goToChild();
string className = cursor.getSourceStr();
cursor.goToSibling();
string varName = cursor.getSourceStr();
vector<unique_ptr<Expr>> params;
for (int i = numChild; i > 2; i--) {
// parse
cursor.goToSibling();
params.push_back(ParseExpression());
}
cursor.goToParent();
return make_unique<ClassVarDecl>(className, varName, move(params));
}
unique_ptr<ClassVarAssign> Parser::ParseClassAssign () {
cursor.goToChild();
string className = cursor.getSourceStr();
cursor.goToSibling();
string varName = cursor.getSourceStr();
cursor.goToSibling();
auto expr = ParseExpression();
cursor.goToParent();
return make_unique<ClassVarAssign>(
className,
varName,
move(expr)
);
}