Skip to content

Commit

Permalink
Inline type alias & rename data member
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lai-YT authored and leewei05 committed Jun 13, 2024
1 parent 74a79a7 commit 9ca9d46
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
7 changes: 3 additions & 4 deletions include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,13 @@ struct LoopInitNode : public AstNode {
};

struct CompoundStmtNode : public StmtNode {
using Item = std::unique_ptr<StmtNode>;
CompoundStmtNode(Location loc, std::vector<Item> items)
: StmtNode{loc}, items{std::move(items)} {}
CompoundStmtNode(Location loc, std::vector<std::unique_ptr<StmtNode>> stmts)
: StmtNode{loc}, stmts{std::move(stmts)} {}

void Accept(NonModifyingVisitor&) const override;
void Accept(ModifyingVisitor&) override;

std::vector<Item> items;
std::vector<std::unique_ptr<StmtNode>> stmts;
};

/// @brief Root of the entire program.
Expand Down
6 changes: 3 additions & 3 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ std::unique_ptr<Type> ResolveType(std::unique_ptr<Type> resolved_type,
%nterm <std::unique_ptr<LoopInitNode>> loop_init
%nterm <std::unique_ptr<StmtNode>> stmt jump_stmt selection_stmt labeled_stmt block_item
%nterm <std::unique_ptr<CompoundStmtNode>> compound_stmt
%nterm <std::vector<CompoundStmtNode::Item>> block_item_list block_item_list_opt
%nterm <std::vector<std::unique_ptr<StmtNode>>> 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
Expand Down Expand Up @@ -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<CompoundStmtNode::Item>{};
$$ = std::vector<std::unique_ptr<StmtNode>>{};
}
;

block_item_list: block_item {
$$ = std::vector<CompoundStmtNode::Item>{};
$$ = std::vector<std::unique_ptr<StmtNode>>{};
$$.push_back($1);
}
| block_item_list block_item {
Expand Down
4 changes: 2 additions & 2 deletions src/ast_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 9ca9d46

Please sign in to comment.