Core: Add defer
This commit is contained in:
@@ -17,6 +17,7 @@ global S32 is_inside_struct;
|
||||
|
||||
CORE_Static void gen_ast(Ast *ast);
|
||||
CORE_Static bool gen_expr(Ast_Expr *ast);
|
||||
CORE_Static void gen_stmt_scope(Ast_Scope *scope, B32 switch_case_gen_break = 0);
|
||||
|
||||
CORE_Static void
|
||||
gen_indent() {
|
||||
@@ -266,7 +267,25 @@ gen_value(Token *pos, Value a) {
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
gen_stmt_scope(Ast_Scope *scope, B32 switch_case_gen_break = 0) {
|
||||
gen_defers_for_scope(Ast_Scope *scope) {
|
||||
int save_line = last_line;
|
||||
For(scope->defers.reverse()) {
|
||||
gen("/* defer */");
|
||||
gen_stmt_scope(it->scope);
|
||||
}
|
||||
last_line = save_line;
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
gen_defers_for_scope_and_parents(Ast_Scope *scope) {
|
||||
for (; scope;) {
|
||||
gen_defers_for_scope(scope);
|
||||
scope = scope->parent_scope;
|
||||
}
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
gen_stmt_scope(Ast_Scope *scope, B32 switch_case_gen_break) {
|
||||
gen("{");
|
||||
global_indent++;
|
||||
For(scope->stmts) {
|
||||
@@ -274,6 +293,7 @@ gen_stmt_scope(Ast_Scope *scope, B32 switch_case_gen_break = 0) {
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
}
|
||||
gen_defers_for_scope(scope);
|
||||
if (switch_case_gen_break == 1) genln("break;");
|
||||
global_indent--;
|
||||
gen_last_line();
|
||||
@@ -581,6 +601,7 @@ gen_ast(Ast *ast) {
|
||||
}
|
||||
|
||||
CASE(RETURN, Return) {
|
||||
gen_defers_for_scope_and_parents(node->parent_scope);
|
||||
gen("return ");
|
||||
if (node->expr) gen_expr(node->expr);
|
||||
gen(";");
|
||||
@@ -634,13 +655,19 @@ gen_ast(Ast *ast) {
|
||||
} break;
|
||||
|
||||
case AST_CONTINUE: {
|
||||
gen_defers_for_scope(ast->parent_scope);
|
||||
gen("continue;");
|
||||
} break;
|
||||
|
||||
case AST_BREAK: {
|
||||
gen_defers_for_scope(ast->parent_scope);
|
||||
gen("break;");
|
||||
} break;
|
||||
|
||||
case AST_DEFER: {
|
||||
gen("// defer");
|
||||
} break;
|
||||
|
||||
case AST_COMPILER_BREAKPOINT_STMT: {
|
||||
__debugbreak();
|
||||
} break;
|
||||
@@ -686,6 +713,7 @@ gen_ast(Ast *ast) {
|
||||
}
|
||||
global_indent--;
|
||||
gen_last_line();
|
||||
gen_defers_for_scope(ast->parent_scope);
|
||||
genln("}");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user