Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move block statement to statement #53

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ struct NullStmtNode : public StmtNode {
};

struct IfStmtNode : public StmtNode {
IfStmtNode(std::unique_ptr<ExprNode> expr,
std::unique_ptr<BlockStmtNode> then,
std::unique_ptr<BlockStmtNode> or_else = {})
IfStmtNode(std::unique_ptr<ExprNode> expr, std::unique_ptr<StmtNode> then,
std::unique_ptr<StmtNode> or_else = {})
: predicate{std::move(expr)},
then{std::move(then)},
or_else{std::move(or_else)} {}
Expand All @@ -105,8 +104,8 @@ struct IfStmtNode : public StmtNode {
virtual void Accept(ModifyingVisitor&) override;

std::unique_ptr<ExprNode> predicate;
std::unique_ptr<BlockStmtNode> then;
std::unique_ptr<BlockStmtNode> or_else;
std::unique_ptr<StmtNode> then;
std::unique_ptr<StmtNode> or_else;
};

struct ReturnStmtNode : public StmtNode {
Expand Down
24 changes: 21 additions & 3 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ extern std::unique_ptr<AstNode> program;
%left '+' '-'
%left '*' '/' '%'

// Resolve the ambiguity in the "dangling-else" grammar.
// Example: IF '(' expr ')' IF '(' expr ')' stmt • ELSE stmt
leewei05 marked this conversation as resolved.
Show resolved Hide resolved
// Yacc has two options to make, either shift or reduce:
// Shift derivation
// stmt
// ↳ 13: IF '(' expr ')' stmt
// ↳ 14: IF '(' expr ')' stmt • ELSE stmt
// Reduce derivation
// stmt
// ↳ 14: IF '(' expr ')' stmt ELSE stmt
// ↳ 13: IF '(' expr ')' stmt •
//
// Our goal is to find the closest IF for ELSE, so we tell Yacc to shift.
// ELSE has a higher precendence than IF due to increasing precedence order.
%precedence IF
%precedence ELSE

%start entry

%%
Expand All @@ -74,12 +91,12 @@ entry: main_func {
}
;

/* TODO: mix declarations and statements in compound statement */
main_func: INT MAIN '(' ')' block {
$$ = $5;
}
;

/* TODO: mix declarations and statements in compound statement */
block: '{' decls stmts '}' {
$$ = std::make_unique<BlockStmtNode>($2, $3);
}
Expand Down Expand Up @@ -109,8 +126,9 @@ stmts: stmts stmt {
stmt: ';' { $$ = std::make_unique<NullStmtNode>(); }
| RETURN expr ';' { $$ = std::make_unique<ReturnStmtNode>($2); }
| expr ';' { $$ = std::make_unique<ExprStmtNode>($1); }
| IF '(' expr ')' block { $$ = std::make_unique<IfStmtNode>($3, $5); }
| IF '(' expr ')' block ELSE block { $$ = std::make_unique<IfStmtNode>($3, $5, $7); }
| block { $$ = $1; }
| IF '(' expr ')' stmt %prec IF { $$ = std::make_unique<IfStmtNode>($3, $5); }
| IF '(' expr ')' stmt ELSE stmt { $$ = std::make_unique<IfStmtNode>($3, $5, $7); }
;

expr: ID { $$ = std::make_unique<IdExprNode>($1); }
Expand Down
12 changes: 12 additions & 0 deletions test/codegen/if_else_nested_single_stmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
int main() {
int i = 5;
if (i < 10)
if (i < 5)
i = 2;
else
i = 7;
else
i = 15;

return i;
}
1 change: 1 addition & 0 deletions test/codegen/if_else_nested_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7
9 changes: 9 additions & 0 deletions test/codegen/if_else_single_stmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int main() {
int i = 2;
if (i > 0)
i = i - 1;
else
i = i + 1;

return i;
}
1 change: 1 addition & 0 deletions test/codegen/if_else_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
7 changes: 7 additions & 0 deletions test/codegen/if_single_stmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int main() {
int i = 2;
if (i > 0)
i = 0;

return i;
}
1 change: 1 addition & 0 deletions test/codegen/if_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
12 changes: 12 additions & 0 deletions test/typecheck/if_else_nested_single_stmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
int main() {
int i = 5;
if (i < 10)
if (i < 5)
i = 2;
else
i = 7;
else
i = 15;

return i;
}
34 changes: 34 additions & 0 deletions test/typecheck/if_else_nested_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(i: int =
5: int
)
(if
(<
i: int
10: int
): int
(if
(<
i: int
5: int
): int
(=
i: int
2: int
): int
)
(else
(=
i: int
7: int
): int
)
)
(else
(=
i: int
15: int
): int
)
(ret
i: int
)
9 changes: 9 additions & 0 deletions test/typecheck/if_else_single_stmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int main() {
int i = 2;
if (i > 0)
i = i - 1;
else
i = i + 1;

return i;
}
28 changes: 28 additions & 0 deletions test/typecheck/if_else_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(i: int =
2: int
)
(if
(>
i: int
0: int
): int
(=
i: int
(-
i: int
1: int
): int
): int
)
(else
(=
i: int
(+
i: int
1: int
): int
): int
)
(ret
i: int
)
7 changes: 7 additions & 0 deletions test/typecheck/if_single_stmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int main() {
int i = 2;
if (i > 0)
i = i + 1;

return 0;
}
19 changes: 19 additions & 0 deletions test/typecheck/if_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(i: int =
2: int
)
(if
(>
i: int
0: int
): int
(=
i: int
(+
i: int
1: int
): int
): int
)
(ret
0: int
)