成员改为默认公有,并为构造成员添加访问修饰符 (#1)
This commit is contained in:
parent
d49a1ae9d2
commit
366603675e
@ -22,7 +22,7 @@ enum class TokenType {
|
|||||||
FN, // Fn
|
FN, // Fn
|
||||||
RETURN, // return
|
RETURN, // return
|
||||||
TYPEOF, // typeof
|
TYPEOF, // typeof
|
||||||
PUBLIC, // public
|
PRIVATE, // private
|
||||||
ID, // identifier
|
ID, // identifier
|
||||||
EXCEED
|
EXCEED
|
||||||
};
|
};
|
||||||
|
@ -63,8 +63,8 @@ vector<Token> scan(string s) {
|
|||||||
type = TokenType::RETURN, t.clear();
|
type = TokenType::RETURN, t.clear();
|
||||||
} else if (t == "typeof") {
|
} else if (t == "typeof") {
|
||||||
type = TokenType::TYPEOF, t.clear();
|
type = TokenType::TYPEOF, t.clear();
|
||||||
} else if (t == "public") {
|
} else if (t == "private") {
|
||||||
type = TokenType::PUBLIC, t.clear();
|
type = TokenType::PRIVATE, t.clear();
|
||||||
} else {
|
} else {
|
||||||
type = TokenType::ID;
|
type = TokenType::ID;
|
||||||
}
|
}
|
||||||
|
@ -18,5 +18,5 @@ std::string token_mp[] = {",",
|
|||||||
"Fn",
|
"Fn",
|
||||||
"return",
|
"return",
|
||||||
"typeof",
|
"typeof",
|
||||||
"public",
|
"private",
|
||||||
"ID"};
|
"ID"};
|
||||||
|
22
src/work.cpp
22
src/work.cpp
@ -82,7 +82,7 @@ shared_ptr<ValType> _createType(shared_ptr<Struct>);
|
|||||||
shared_ptr<ValType> createType();
|
shared_ptr<ValType> createType();
|
||||||
vector<shared_ptr<ValType>> createTypes();
|
vector<shared_ptr<ValType>> createTypes();
|
||||||
vector<pair<string, shared_ptr<TemplateType>>> createTems();
|
vector<pair<string, shared_ptr<TemplateType>>> createTems();
|
||||||
vector<pair<string, shared_ptr<ValType>>> createPars();
|
vector<pair<string, shared_ptr<ValType>>> createPars(map<string, shared_ptr<ValType>>* = nullptr);
|
||||||
pair<string, shared_ptr<Struct>> createStruct();
|
pair<string, shared_ptr<Struct>> createStruct();
|
||||||
pair<string, shared_ptr<ValType>> createVar();
|
pair<string, shared_ptr<ValType>> createVar();
|
||||||
|
|
||||||
@ -384,11 +384,15 @@ vector<pair<string, shared_ptr<TemplateType>>> createTems() {
|
|||||||
return tems;
|
return tems;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<pair<string, shared_ptr<ValType>>> createPars() {
|
vector<pair<string, shared_ptr<ValType>>> createPars(map<string, shared_ptr<ValType>>* vars) {
|
||||||
vector<pair<string, shared_ptr<ValType>>> pars;
|
vector<pair<string, shared_ptr<ValType>>> pars;
|
||||||
jump(TokenType::LP);
|
jump(TokenType::LP);
|
||||||
if (preview(TokenType::ID)) {
|
if (!preview(TokenType::RP)) {
|
||||||
auto single = [&]() {
|
auto single = [&]() {
|
||||||
|
bool pub = 1;
|
||||||
|
if (preview(TokenType::PRIVATE)) {
|
||||||
|
pub = 0, pt++;
|
||||||
|
}
|
||||||
auto s = jump(TokenType::ID);
|
auto s = jump(TokenType::ID);
|
||||||
if (ndefs.find(s) != ndefs.end()) {
|
if (ndefs.find(s) != ndefs.end()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
@ -399,6 +403,7 @@ vector<pair<string, shared_ptr<ValType>>> createPars() {
|
|||||||
d->def_var = t;
|
d->def_var = t;
|
||||||
ndefs[s] = static_pointer_cast<Def>(d);
|
ndefs[s] = static_pointer_cast<Def>(d);
|
||||||
pars.push_back({s, t});
|
pars.push_back({s, t});
|
||||||
|
if (vars != nullptr && pub) (*vars)[s]=t;
|
||||||
};
|
};
|
||||||
for (single(); preview(TokenType::COMMA); pt++, single()) {}
|
for (single(); preview(TokenType::COMMA); pt++, single()) {}
|
||||||
}
|
}
|
||||||
@ -411,30 +416,29 @@ pair<string, shared_ptr<Struct>> createStruct() {
|
|||||||
string s;
|
string s;
|
||||||
vector<pair<string, shared_ptr<TemplateType>>> tems;
|
vector<pair<string, shared_ptr<TemplateType>>> tems;
|
||||||
vector<pair<string, shared_ptr<ValType>>> pars;
|
vector<pair<string, shared_ptr<ValType>>> pars;
|
||||||
|
auto t = make_shared<Struct>();
|
||||||
|
|
||||||
if (preview(TokenType::ID)) {
|
if (preview(TokenType::ID)) {
|
||||||
s = tokens[pt++].s;
|
s = tokens[pt++].s;
|
||||||
if (preview(TokenType::LT)) {
|
if (preview(TokenType::LT)) {
|
||||||
tems = createTems();
|
tems = createTems();
|
||||||
}
|
}
|
||||||
pars = createPars();
|
pars = createPars(&t->vars);
|
||||||
}
|
}
|
||||||
auto t = make_shared<Struct>();
|
|
||||||
for (const auto& pr : tems) {
|
for (const auto& pr : tems) {
|
||||||
t->c1.push_back(pr.second);
|
t->c1.push_back(pr.second);
|
||||||
}
|
}
|
||||||
for (const auto& pr : pars) {
|
for (const auto& pr : pars) {
|
||||||
t->c2.push_back(pr.second);
|
t->c2.push_back(pr.second);
|
||||||
t->vars[pr.first] = pr.second;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jump(TokenType::LB);
|
jump(TokenType::LB);
|
||||||
vector<string> defs;
|
vector<string> defs;
|
||||||
vector<shared_ptr<Struct>> subs;
|
vector<shared_ptr<Struct>> subs;
|
||||||
while (!preview(TokenType::RB)) {
|
while (!preview(TokenType::RB)) {
|
||||||
bool pub = 0;
|
bool pub = 1;
|
||||||
if (preview(TokenType::PUBLIC)) {
|
if (preview(TokenType::PRIVATE)) {
|
||||||
pub = 1, pt++;
|
pub = 0, pt++;
|
||||||
}
|
}
|
||||||
if (preview({TokenType::STRUCT, TokenType::ID})) {
|
if (preview({TokenType::STRUCT, TokenType::ID})) {
|
||||||
auto tt = createStruct();
|
auto tt = createStruct();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user