From 9ca9d46c1a43d9c1bfa3ba756646e2f5ef48a61e Mon Sep 17 00:00:00 2001 From: Lai-YT <381xvmvbib@gmail.com> Date: Sat, 8 Jun 2024 18:07:36 +0800 Subject: [PATCH] Inline type alias & rename data member Since an item of a compound statement is no longer an `std::variant`, we no longer use a type alias to simplify the type. Additionally, the data member has been renamed to remove the word `item`, which is a technical term used during parsing. --- include/ast.hpp | 7 +++---- parser.y | 6 +++--- src/ast_dumper.cpp | 4 ++-- src/qbe_ir_generator.cpp | 4 ++-- src/type_checker.cpp | 4 ++-- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/include/ast.hpp b/include/ast.hpp index 13b68599..d8f8534f 100644 --- a/include/ast.hpp +++ b/include/ast.hpp @@ -211,14 +211,13 @@ struct LoopInitNode : public AstNode { }; struct CompoundStmtNode : public StmtNode { - using Item = std::unique_ptr; - CompoundStmtNode(Location loc, std::vector items) - : StmtNode{loc}, items{std::move(items)} {} + CompoundStmtNode(Location loc, std::vector> stmts) + : StmtNode{loc}, stmts{std::move(stmts)} {} void Accept(NonModifyingVisitor&) const override; void Accept(ModifyingVisitor&) override; - std::vector items; + std::vector> stmts; }; /// @brief Root of the entire program. diff --git a/parser.y b/parser.y index 2101a41b..a4f12610 100644 --- a/parser.y +++ b/parser.y @@ -131,7 +131,7 @@ std::unique_ptr ResolveType(std::unique_ptr resolved_type, %nterm > loop_init %nterm > stmt jump_stmt selection_stmt labeled_stmt block_item %nterm > compound_stmt -%nterm > block_item_list block_item_list_opt +%nterm >> block_item_list block_item_list_opt // Resolve the ambiguity in the "dangling-else" grammar. // Example: IF LEFT_PAREN expr RIGHT_PAREN IF LEFT_PAREN expr RIGHT_PAREN stmt • ELSE stmt @@ -198,12 +198,12 @@ compound_stmt: LEFT_CURLY block_item_list_opt RIGHT_CURLY { block_item_list_opt: block_item_list { $$ = $1; } | epsilon { - $$ = std::vector{}; + $$ = std::vector>{}; } ; block_item_list: block_item { - $$ = std::vector{}; + $$ = std::vector>{}; $$.push_back($1); } | block_item_list block_item { diff --git a/src/ast_dumper.cpp b/src/ast_dumper.cpp index 055a42bf..58d644fd 100644 --- a/src/ast_dumper.cpp +++ b/src/ast_dumper.cpp @@ -178,8 +178,8 @@ void AstDumper::Visit(const CompoundStmtNode& compound_stmt) { std::cout << indenter_.Indent() << "CompoundStmtNode <" << compound_stmt.loc << ">\n"; indenter_.IncreaseLevel(); - for (const auto& item : compound_stmt.items) { - item->Accept(*this); + for (const auto& stmt : compound_stmt.stmts) { + stmt->Accept(*this); } indenter_.DecreaseLevel(); } diff --git a/src/qbe_ir_generator.cpp b/src/qbe_ir_generator.cpp index f92119ba..bb13e431 100644 --- a/src/qbe_ir_generator.cpp +++ b/src/qbe_ir_generator.cpp @@ -273,8 +273,8 @@ void QbeIrGenerator::Visit(const CompoundStmtNode& compound_stmt) { // because it doesn't know whether it is a if statement body or a function. // Thus, by moving label creation to an upper level, each block can have its // correct starting label. - for (const auto& item : compound_stmt.items) { - item->Accept(*this); + for (const auto& stmt : compound_stmt.stmts) { + stmt->Accept(*this); } } diff --git a/src/type_checker.cpp b/src/type_checker.cpp index b00970fb..04c40d5e 100644 --- a/src/type_checker.cpp +++ b/src/type_checker.cpp @@ -200,8 +200,8 @@ void TypeChecker::Visit(LoopInitNode& loop_init) { void TypeChecker::Visit(CompoundStmtNode& compound_stmt) { env_.PushScope(ScopeKind::kBlock); - for (auto& item : compound_stmt.items) { - item->Accept(*this); + for (auto& stmt : compound_stmt.stmts) { + stmt->Accept(*this); } env_.PopScope(); }