Add switch case statement

This commit is contained in:
Krzosa Karol
2022-06-19 09:31:16 +02:00
parent fc0d4345ee
commit 94b820a071
7 changed files with 114 additions and 2 deletions

View File

@@ -192,7 +192,7 @@ gen_value(Value a){
}
function void
gen_stmt_scope(Ast_Scope *scope){
gen_stmt_scope(Ast_Scope *scope, B32 switch_case_gen_break = 0){
gen("{");
global_indent++;
For(scope->stmts) {
@@ -200,6 +200,7 @@ gen_stmt_scope(Ast_Scope *scope){
genln("");
gen_ast(it);
}
if(switch_case_gen_break == 1) genln("break;");
global_indent--;
genln("}");
}
@@ -570,6 +571,33 @@ gen_ast(Ast *ast){
BREAK();
}
CASE(SWITCH, Switch){
gen("switch(");
gen_expr(node->value);
gen("){");
global_indent++;
For(node->cases){
For_Named(it->labels, label){
genln("case ");
gen_expr(label);
gen(":");
}
gen_stmt_scope(it->scope, it->fallthrough ? false : true);
}
if(node->default_scope){
genln("default: ");
gen_stmt_scope(node->default_scope);
}
global_indent--;
genln("}");
BREAK();
}
CASE(VAR_UNPACK, Var_Unpack){
For(node->vars)
gen_ast(it);
@@ -771,6 +799,7 @@ typedef S64 SizeS;
typedef float F32;
typedef double F64;
typedef S32 Bool;
typedef S64 Type;
#define true 1
#define false 0