From ff38dc4b7c115b9397bdd35623a555236d055cf8 Mon Sep 17 00:00:00 2001 From: lcw Date: Wed, 30 Aug 2023 15:16:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A0=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84=E7=BB=93=E6=9E=84=E4=BD=93=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20(#2)=20=E5=90=8C=E6=97=B6=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E4=BA=86=20admit=20=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- docs/BNF.txt | 15 +++++++++------ include/token.h | 2 ++ src/scan.cpp | 4 ++++ src/token.cpp | 2 ++ src/work.cpp | 24 +++++++++++++++++++++--- 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 6397af5..a1e2fe3 100644 --- a/.gitignore +++ b/.gitignore @@ -37,7 +37,7 @@ build/ CMakeList.txt acpa -.stackdump +*.stackdump # --> Editors # Vim diff --git a/docs/BNF.txt b/docs/BNF.txt index 48af84c..7421461 100644 --- a/docs/BNF.txt +++ b/docs/BNF.txt @@ -6,7 +6,7 @@ 模板参数列表 ::= "<" [模板参数列表内容] ">" -结构体定义 ::= "struct" <标识符> [模板参数列表] <函数参数列表> "{" 结构体内容 "}" ";" +结构体定义 ::= "struct" <标识符> [模板参数列表] ("(" "delete" ")" | <函数参数列表>) "{" 结构体内容 "}" ";" 匿名结构体定义 ::= "struct" "{" <结构体内容> "}" <标识符> ";" @@ -33,14 +33,17 @@ 值 ::= <基本值> | <基本值> "." <局部值> -基本值 ::= 标识符 - | 新实例 - | 新函数 - | 直接函数调用 +基本值 ::= <标识符> + | <新实例> + | <新函数> + | <新公理> + | <直接函数调用> 新实例 ::= <结构体类型> [类型列表] <值列表> -直接函数调用 ::= (<标识符> | <新函数> | <直接函数调用>) [类型列表] <值列表> +新公理 ::= "admit" "<" <类型> ">" "(" ")" + +直接函数调用 ::= (<标识符> | <新函数> | <新公理> | <直接函数调用>) [类型列表] <值列表> 局部值 ::= <局部基本值> | <局部基本值> "." <局部值> diff --git a/include/token.h b/include/token.h index 631b323..427d56a 100644 --- a/include/token.h +++ b/include/token.h @@ -23,6 +23,8 @@ enum class TokenType { RETURN, // return TYPEOF, // typeof PRIVATE, // private + ADMIT, // admit + DELETE, // delete ID, // identifier EXCEED }; diff --git a/src/scan.cpp b/src/scan.cpp index 891f95a..31085ac 100644 --- a/src/scan.cpp +++ b/src/scan.cpp @@ -65,6 +65,10 @@ vector 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; } diff --git a/src/token.cpp b/src/token.cpp index c29fb90..98ad3cc 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -19,4 +19,6 @@ std::string token_mp[] = {",", "return", "typeof", "private", + "admit", + "delete", "ID"}; diff --git a/src/work.cpp b/src/work.cpp index d9b2c0e..542beb4 100644 --- a/src/work.cpp +++ b/src/work.cpp @@ -179,6 +179,13 @@ shared_ptr createVal() { jump(TokenType::RB); t = static_pointer_cast(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> createStruct() { vector>> pars; auto t = make_shared(); + 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(make_shared()); + t->c2.push_back(null_type); } jump(TokenType::LB);