Skip to content

Commit 4e93302

Browse files
committed
支持类型推导
1 parent 8e6fad5 commit 4e93302

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

src/PHPVisitor.cpp

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,34 @@ antlrcpp::Any PHPVisitor::visitClassMember(PHPParser::ClassMemberContext *contex
7575
std::string word = token->getText();
7676

7777
debug() && printf("func:%s line:%d %s\n", __FUNCTION__, line, word.data());
78-
78+
7979
PHPParser::FieldDefinitionContext *field = context->fieldDefinition();
8080
if (field) {
81-
antlr4::Token *token9 = field->fieldName()->variablename()->getStart();
81+
antlr4::Token *token9 = field->fieldName()->variablename()->getStart();
8282

8383
std::string word9 = token9->getText();
8484
debug() && printf("\tfield:%s line:%zu %s\n", __FUNCTION__, token9->getLine(), word9.data());
8585

8686
classFields.push_back(word9);
87+
88+
std::string type = "unknow";
89+
if (field->Equals()) {
90+
if (field->atom()) {
91+
PHPParser::AtomContext *atom = field->atom();
92+
if (atom->number()) {
93+
type = "int";
94+
} else if (atom->string()) {
95+
type = "string";
96+
} else if (atom->Boolean()) {
97+
type = "bool";
98+
} else if (atom->arrayDeclaration()) {
99+
type = "array";
100+
} else if (atom->functionInvocation()) {
101+
type = "function";
102+
}
103+
}
104+
}
105+
types.push_back(type);
87106
}
88107

89108
// 修饰
@@ -162,10 +181,15 @@ std::string PHPVisitor::gerSetter()
162181
std::stringstream str;
163182
for (size_t i =0; i < classFields.size(); i ++) {
164183
std::string field = classFields[i];
184+
std::string type = types[i];
165185

166-
std::string setter = gerFieldSetter(field);
186+
std::string setter = gerFieldSetter(field, type);
167187
//printf("%s\n", setter.data());
168188

189+
if (setter.empty()) {
190+
continue;
191+
}
192+
169193
str << setter;
170194

171195
if (i < classFields.size() - 1) {
@@ -181,8 +205,12 @@ std::string PHPVisitor::gerGetter()
181205
std::stringstream str;
182206
for (size_t i =0; i < classFields.size(); i ++) {
183207
std::string field = classFields[i];
208+
std::string type = types[i];
184209

185-
std::string getter = gerFieldGetter(field);
210+
std::string getter = gerFieldGetter(field, type);
211+
if (getter.empty()) {
212+
continue;
213+
}
186214

187215
str << getter;
188216

@@ -199,12 +227,25 @@ std::string PHPVisitor::gerGetterSetter()
199227
std::stringstream str;
200228
for (size_t i =0; i < classFields.size(); i ++) {
201229
std::string field = classFields[i];
230+
std::string type = types[i];
202231

203-
std::string getter = gerFieldGetter(field);
204-
std::string setter = gerFieldSetter(field);
232+
std::string getter = gerFieldGetter(field, type);
233+
std::string setter = gerFieldSetter(field, type);
205234

206-
str << setter << "\n" << getter;
235+
debug() && printf("type : %s\n", type.data());
207236

237+
if (setter.empty() && getter.empty()) {
238+
continue;
239+
}
240+
if (!setter.empty()) {
241+
str << setter;
242+
}
243+
if (!setter.empty() && !getter.empty()) {
244+
str << "\n";
245+
}
246+
if (!getter.empty()) {
247+
str << getter;
248+
}
208249
if (i < classFields.size() - 1) {
209250
str << "\n";
210251
}
@@ -213,16 +254,20 @@ std::string PHPVisitor::gerGetterSetter()
213254
return str.str();
214255
}
215256

216-
std::string PHPVisitor::gerFieldGetter(std::string field)
257+
std::string PHPVisitor::gerFieldGetter(std::string field, std::string type)
217258
{
218259
std::string name = "get" + std::string(toUpper(field));
219260
if (std::find(methods.begin(), methods.end(), name) != methods.end()) {
220261
return "";
221262
}
263+
std::string fieldType = "mixed";
264+
if (!type.empty() && type != "function" && type != "unknow") {
265+
fieldType = type;
266+
}
222267
std::string tab = " ";// "\t";
223-
std::stringstream res;
268+
std::stringstream res;
224269
res << tab << "/**\n"
225-
<< tab << " * @return mixed\n" //TODO: 类型推导
270+
<< tab << " * @return " << fieldType << "\n" //TODO: 类型推导
226271
<< tab << " */\n"
227272
<< tab << "public function " << name <<"()\n"
228273
<< tab << "{\n"
@@ -231,20 +276,28 @@ std::string PHPVisitor::gerFieldGetter(std::string field)
231276
return res.str();
232277
}
233278

234-
std::string PHPVisitor::gerFieldSetter(std::string field)
279+
std::string PHPVisitor::gerFieldSetter(std::string field, std::string type)
235280
{
236281
std::string name = "set" + std::string(toUpper(field));
237282
if (std::find(methods.begin(), methods.end(), name) != methods.end()) {
238283
return "";
239284
}
285+
std::string fieldType = "mixed";
286+
if (!type.empty() && type != "function" && type != "unknow") {
287+
fieldType = type;
288+
}
289+
std::string paramType = "";
290+
if (fieldType != "mixed") {
291+
paramType = fieldType + " ";
292+
}
240293
std::string tab = " ";// "\t";
241294
std::stringstream res;
242295
res << tab << "/**\n"
243-
<< tab << " * @param mixed $" << field << "\n" //TODO:: 类型推导
296+
<< tab << " * @param " << fieldType << " $" << field << "\n" //TODO:: 类型推导
244297
<< tab << " *\n"
245298
<< tab << " * @return self\n"
246299
<< tab << " */\n"
247-
<< tab << "public function " << name <<"($" << field <<")\n"
300+
<< tab << "public function " << name <<"(" << paramType << "$" << field <<")\n"
248301
<< tab << "{\n"
249302
<< tab << "\t$this->" << field <<" = $" << field << ";\n"
250303
"\n" // 空行

src/PHPVisitor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class PHPVisitor : public PHPParserBaseVisitor {
99
public:
1010

1111
std::vector<std::string> classFields;
12+
std::vector<std::string> types;
1213
std::vector<std::string> methods;
1314
std::string gerSetter();
1415
std::string gerGetter();
@@ -25,8 +26,8 @@ class PHPVisitor : public PHPParserBaseVisitor {
2526

2627
private:
2728
bool isDebug = false;
28-
std::string gerFieldSetter(std::string field);
29-
std::string gerFieldGetter(std::string field);
29+
std::string gerFieldSetter(std::string field, std::string type = "");
30+
std::string gerFieldGetter(std::string field, std::string type = "");
3031
char* toUpper(std::string str);
3132
bool debug();
3233
};

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "PHPParserVisitor.h"
1212
#include "PHPVisitor.h"
1313

14-
#define PHPBOT_VERSION "Beta1.0.0"
14+
#define PHPBOT_VERSION "Beta1.0.2"
1515

1616
using namespace std;
1717
using namespace antlr4;

0 commit comments

Comments
 (0)