M include/ast.h => include/ast.h +3 -0
@@ 393,6 393,9 @@ ast_stmt_create_iter_e(struct ast_stmt *iter_stmt);
struct ast_stmt *
ast_stmt_create_compound(struct lexememarker *, struct ast_block *);
+struct ast_stmt *
+ast_stmt_as_compound(struct ast_stmt *);
+
struct ast_block *
ast_stmt_as_block(struct ast_stmt *);
M src/ast/gram.y => src/ast/gram.y +4 -2
@@ 743,9 743,11 @@ expression_statement
selection_statement
: IF '(' expression ')' statement
- { $$ = ast_stmt_create_sel(lexloc(), false, $3, $5, NULL); }
+ { $$ = ast_stmt_create_sel(lexloc(), false, $3, ast_stmt_as_compound($5), NULL); }
| IF '(' expression ')' statement ELSE statement
- { $$ = ast_stmt_create_sel(lexloc(), false, $3, $5, $7); }
+ { $$ = ast_stmt_create_sel(
+ lexloc(), false, $3, ast_stmt_as_compound($5), ast_stmt_as_compound($7)
+ ); }
| SWITCH '(' expression ')' statement
{ assert(false); }
;
M src/ast/stmt/stmt.c => src/ast/stmt/stmt.c +12 -1
@@ 120,7 120,18 @@ ast_stmt_to_block(struct ast_stmt *stmt)
return b;
}
-
+struct ast_stmt *
+ast_stmt_as_compound(struct ast_stmt *stmt)
+{
+ if (stmt->kind == STMT_COMPOUND) {
+ return stmt;
+ }
+ struct ast_block *b = ast_block_create(NULL, 0, NULL, 0);
+ ast_block_append_stmt(b, stmt);
+ return ast_stmt_create_compound(
+ lexememarker_copy(ast_stmt_lexememarker(stmt)), b
+ );
+}
bool
ast_stmt_ispre(struct ast_stmt *stmt)
A tests/1-branches/0103-body-abstract.x => tests/1-branches/0103-body-abstract.x +10 -0
@@ 0,0 1,10 @@
+#include <stdlib.h>
+
+void
+f(int cond) ~ [ if (cond) return malloc(1); ]
+{
+ if (cond) {
+ return malloc(1);
+ }
+ return NULL;
+}