成员改为默认公有,并为构造成员添加访问修饰符 (#1)

This commit is contained in:
lcw 2023-08-30 10:33:57 +08:00
parent d49a1ae9d2
commit 366603675e
4 changed files with 17 additions and 13 deletions

View File

@ -22,7 +22,7 @@ enum class TokenType {
FN, // Fn
RETURN, // return
TYPEOF, // typeof
PUBLIC, // public
PRIVATE, // private
ID, // identifier
EXCEED
};

View File

@ -63,8 +63,8 @@ vector<Token> scan(string s) {
type = TokenType::RETURN, t.clear();
} else if (t == "typeof") {
type = TokenType::TYPEOF, t.clear();
} else if (t == "public") {
type = TokenType::PUBLIC, t.clear();
} else if (t == "private") {
type = TokenType::PRIVATE, t.clear();
} else {
type = TokenType::ID;
}

View File

@ -18,5 +18,5 @@ std::string token_mp[] = {",",
"Fn",
"return",
"typeof",
"public",
"private",
"ID"};

View File

@ -82,7 +82,7 @@ shared_ptr<ValType> _createType(shared_ptr<Struct>);
shared_ptr<ValType> createType();
vector<shared_ptr<ValType>> createTypes();
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<ValType>> createVar();
@ -384,11 +384,15 @@ vector<pair<string, shared_ptr<TemplateType>>> createTems() {
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;
jump(TokenType::LP);
if (preview(TokenType::ID)) {
if (!preview(TokenType::RP)) {
auto single = [&]() {
bool pub = 1;
if (preview(TokenType::PRIVATE)) {
pub = 0, pt++;
}
auto s = jump(TokenType::ID);
if (ndefs.find(s) != ndefs.end()) {
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;
ndefs[s] = static_pointer_cast<Def>(d);
pars.push_back({s, t});
if (vars != nullptr && pub) (*vars)[s]=t;
};
for (single(); preview(TokenType::COMMA); pt++, single()) {}
}
@ -411,30 +416,29 @@ pair<string, shared_ptr<Struct>> createStruct() {
string s;
vector<pair<string, shared_ptr<TemplateType>>> tems;
vector<pair<string, shared_ptr<ValType>>> pars;
auto t = make_shared<Struct>();
if (preview(TokenType::ID)) {
s = tokens[pt++].s;
if (preview(TokenType::LT)) {
tems = createTems();
}
pars = createPars();
pars = createPars(&t->vars);
}
auto t = make_shared<Struct>();
for (const auto& pr : tems) {
t->c1.push_back(pr.second);
}
for (const auto& pr : pars) {
t->c2.push_back(pr.second);
t->vars[pr.first] = pr.second;
}
jump(TokenType::LB);
vector<string> defs;
vector<shared_ptr<Struct>> subs;
while (!preview(TokenType::RB)) {
bool pub = 0;
if (preview(TokenType::PUBLIC)) {
pub = 1, pt++;
bool pub = 1;
if (preview(TokenType::PRIVATE)) {
pub = 0, pt++;
}
if (preview({TokenType::STRUCT, TokenType::ID})) {
auto tt = createStruct();