~lbnz/xr0

1e3c83d774674e0c00246b5a64a8c683f017937d — Claude Betz a month ago 2062971
fix: normalise `if' ast representation for stack based model

issue: https://github.com/xr0-org/xr0/issues/45
4 files changed, 29 insertions(+), 3 deletions(-)

M include/ast.h
M src/ast/gram.y
M src/ast/stmt/stmt.c
A tests/1-branches/0103-body-abstract.x
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;
}