添加无构造函数的结构体支持 (#2)

同时加入了 admit 语法
This commit is contained in:
lcw 2023-08-30 15:16:59 +08:00
parent 366603675e
commit ff38dc4b7c
6 changed files with 39 additions and 10 deletions

2
.gitignore vendored
View File

@ -37,7 +37,7 @@
build/
CMakeList.txt
acpa
.stackdump
*.stackdump
# --> Editors
# Vim

View File

@ -6,7 +6,7 @@
模板参数列表 ::= "<" [模板参数列表内容] ">"
结构体定义 ::= "struct" <标识符> [模板参数列表] <函数参数列表> "{" 结构体内容 "}" ";"
结构体定义 ::= "struct" <标识符> [模板参数列表] ("(" "delete" ")" | <函数参数列表>) "{" 结构体内容 "}" ";"
匿名结构体定义 ::= "struct" "{" <结构体内容> "}" <标识符> ";"
@ -33,14 +33,17 @@
值 ::= <基本值>
| <基本值> "." <局部值>
基本值 ::= 标识符
| 新实例
| 新函数
| 直接函数调用
基本值 ::= <标识符>
| <新实例>
| <新函数>
| <新公理>
| <直接函数调用>
新实例 ::= <结构体类型> [类型列表] <值列表>
直接函数调用 ::= (<标识符> | <新函数> | <直接函数调用>) [类型列表] <值列表>
新公理 ::= "admit" "<" <类型> ">" "(" ")"
直接函数调用 ::= (<标识符> | <新函数> | <新公理> | <直接函数调用>) [类型列表] <值列表>
局部值 ::= <局部基本值>
| <局部基本值> "." <局部值>

View File

@ -23,6 +23,8 @@ enum class TokenType {
RETURN, // return
TYPEOF, // typeof
PRIVATE, // private
ADMIT, // admit
DELETE, // delete
ID, // identifier
EXCEED
};

View File

@ -65,6 +65,10 @@ vector<Token> scan(string s) {
type = TokenType::TYPEOF, t.clear();
} else if (t == "private") {
type = TokenType::PRIVATE, t.clear();
} else if (t == "admit") {
type = TokenType::ADMIT, t.clear();
} else if (t == "delete") {
type = TokenType::DELETE, t.clear();
} else {
type = TokenType::ID;
}

View File

@ -19,4 +19,6 @@ std::string token_mp[] = {",",
"return",
"typeof",
"private",
"admit",
"delete",
"ID"};

View File

@ -179,6 +179,13 @@ shared_ptr<ValType> createVal() {
jump(TokenType::RB);
t = static_pointer_cast<ValType>(tt);
} else if (preview(TokenType::ADMIT)) {
pt++;
jump(TokenType::LT);
t = createType();
jump(TokenType::RT);
jump(TokenType::LP);
jump(TokenType::RP);
} else {
bool flag = 0; // 新实例
if (preview(TokenType::TYPEOF)) {
@ -418,18 +425,29 @@ pair<string, shared_ptr<Struct>> createStruct() {
vector<pair<string, shared_ptr<ValType>>> pars;
auto t = make_shared<Struct>();
bool constructor = 1;
if (preview(TokenType::ID)) {
s = tokens[pt++].s;
if (preview(TokenType::LT)) {
tems = createTems();
}
pars = createPars(&t->vars);
if(preview({TokenType::LP,TokenType::DELETE})) {
constructor = 0;
pt += 2, jump(TokenType::RP);
} else {
pars = createPars(&t->vars);
}
}
for (const auto& pr : tems) {
t->c1.push_back(pr.second);
}
for (const auto& pr : pars) {
t->c2.push_back(pr.second);
if(constructor) {
for (const auto& pr : pars) {
t->c2.push_back(pr.second);
}
} else {
static auto null_type = static_pointer_cast<ValType>(make_shared<TemplateType>());
t->c2.push_back(null_type);
}
jump(TokenType::LB);