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

26
ast.cpp
View File

@@ -26,6 +26,8 @@ enum Ast_Kind: U32{
AST_LENGTH_OF,
AST_ALIGN_OF,
AST_SWITCH,
AST_SWITCH_CASE,
AST_VAR_UNPACK,
AST_BREAK,
AST_COMPOUND,
@@ -174,6 +176,18 @@ struct Ast_Array: Ast_Expr{
Ast_Expr *expr;
};
struct Ast_Switch_Case: Ast{
Array<Ast_Expr *> labels;
Ast_Scope *scope;
B32 fallthrough;
};
struct Ast_Switch: Ast{
Ast_Expr *value;
Array<Ast_Switch_Case *> cases;
Ast_Scope *default_scope;
};
/*
How does current declaration order resolver works:
* First we put all the global declarations into the global scope (when parsing) all unresolved
@@ -251,6 +265,18 @@ struct Ast_Decl: Ast{
result->pos = ipos; \
result->di = ++pctx->unique_ids
#define MAKE_AST(T,kind,pos,flags) (T *)ast_new(sizeof(T), kind, pos, flags)
function Ast *
ast_new(SizeU size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0){
Ast *result = (Ast *)exp_alloc(pctx->perm, size, AF_ZeroMemory);
result->flags = flags;
result->kind = kind;
result->parent_scope = pctx->currently_parsed_scope;
result->pos = pos;
result->di = ++pctx->unique_ids;
return result;
}
function Ast_Atom *
ast_str(Token *pos, Intern_String string){
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);