Skip to content

Commit

Permalink
Remove expression list in parser
Browse files Browse the repository at this point in the history
We don't need exprs non-terminal after implement statements.
  • Loading branch information
leewei05 committed Aug 12, 2023
1 parent 9d15727 commit a08864e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
31 changes: 10 additions & 21 deletions ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,36 @@ class NullStmtNode : public StmtNode {

class ReturnStmtNode : public StmtNode {
public:
ReturnStmtNode(std::vector<std::unique_ptr<ExprNode>>&& exprs)
: exprs_{std::move(exprs)} {}
ReturnStmtNode(std::unique_ptr<ExprNode> expr) : expr_{std::move(expr)} {}

void CodeGen() const override {
output << " ret ";
for (const auto& expr : exprs_) {
expr->CodeGen();
}
expr_->CodeGen();
}

void Dump(int pad) const override {
for (const auto& expr : exprs_) {
expr->Dump(pad);
}
std::cout << Pad(pad) << "(ret" << std::endl;
expr_->Dump(pad + 2);
std::cout << Pad(pad) << ')' << std::endl;
}

protected:
std::vector<std::unique_ptr<ExprNode>> exprs_;
std::unique_ptr<ExprNode> expr_;
};

/// @note Any expression can be turned into a statement by adding a semicolon
/// to the end of the expression.
class ExprStmtNode : public StmtNode {
public:
ExprStmtNode(std::vector<std::unique_ptr<ExprNode>>&& exprs)
: exprs_{std::move(exprs)} {}

void CodeGen() const override {
for (const auto& expr : exprs_) {
expr->CodeGen();
}
}
ExprStmtNode(std::unique_ptr<ExprNode> expr) : expr_{std::move(expr)} {}

void CodeGen() const override {}
void Dump(int pad) const override {
for (const auto& expr : exprs_) {
expr->Dump(pad);
}
expr_->Dump(pad);
}

protected:
std::vector<std::unique_ptr<ExprNode>> exprs_;
std::unique_ptr<ExprNode> expr_;
};

class IdExprNode : public ExprNode {
Expand Down
13 changes: 2 additions & 11 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ std::ofstream output;
;

%nterm <std::unique_ptr<ExprNode>> expr
%nterm <std::vector<std::unique_ptr<ExprNode>>> exprs
%nterm <std::unique_ptr<StmtNode>> stmt
%nterm <std::vector<std::unique_ptr<StmtNode>>> stmts
%nterm <std::vector<std::unique_ptr<StmtNode>>> main_func
Expand Down Expand Up @@ -69,18 +68,10 @@ stmts: stmts stmt {
;

stmt: ';' { $$ = std::make_unique<NullStmtNode>(); }
| RETURN exprs ';' { $$ = std::make_unique<ReturnStmtNode>($2); }
| exprs ';' { $$ = std::make_unique<ExprStmtNode>($1); }
| RETURN expr ';' { $$ = std::make_unique<ReturnStmtNode>($2); }
| expr ';' { $$ = std::make_unique<ExprStmtNode>($1); }
;

exprs: exprs expr {
auto exprs = $1;
exprs.push_back($2);
$$ = std::move(exprs);
}
| epsilon { $$ = std::vector<std::unique_ptr<ExprNode>>{}; }
;

expr: ID { $$ = std::make_unique<IdExprNode>($1); }
| NUM { $$ = std::make_unique<IntConstExprNode>($1); }
| expr '+' expr { $$ = std::make_unique<PlusExprNode>($1, $3); }
Expand Down
2 changes: 1 addition & 1 deletion test/arithmetic.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int main () {
int main() {
1 + 2;
1 - 2;
1 * 2;
Expand Down

0 comments on commit a08864e

Please sign in to comment.