diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index 1b6c8d7..20de468 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -342,6 +342,7 @@ enum Ast_Kind : uint32_t { AST_ENUM, AST_ENUM_MEMBER, AST_STRUCT, + AST_UNION, }; typedef uint32_t Ast_Flag; diff --git a/core_parsing.cpp b/core_parsing.cpp index d97b87a..8dbaffb 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -225,10 +225,9 @@ parse_init_stmt(Ast_Expr *expr) { CORE_Static Ast_Call * parse_expr_call(Ast_Expr *left, Token_Kind close_kind) { - Arena *scratch = pctx->scratch; - Scratch_Scope __scope(scratch); + Scratch_Scope scratch(pctx->scratch); Token *pos = token_get(); - Array exprs = {scratch}; + Array exprs = {scratch.arena}; while (!token_is(close_kind)) { Ast_Call_Item *item_comp = ast_new(Ast_Call_Item, AST_CALL_ITEM, token_get(), AST_EXPR); @@ -278,14 +277,13 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0) { if (token_expect(OPEN_SCOPE)) { // @todo: Fix error message here, it doesn't show proper token context Token *token_block = token_get(); - Arena *scratch = pctx->scratch; - Scratch_Scope __scope(scratch); - if (!scope_defined_outside) scope = begin_stmt_scope(scratch, token_block); + Scratch_Scope scratch(pctx->scratch); + if (!scope_defined_outside) scope = begin_stmt_scope(scratch.arena, token_block); do { Token *token = token_get(); if (token_match_keyword(pctx->keyword_return)) { - Array expr = {scratch}; + Array expr = {scratch.arena}; if (!token_is_scope()) { do { Ast_Expr *subexpr = parse_expr(); @@ -306,7 +304,7 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0) { else if (token_match_keyword(pctx->keyword_switch)) { Ast_Switch *result = ast_new(Ast_Switch, AST_SWITCH, token, AST_STMT); result->value = parse_expr(); - result->cases = {scratch}; + result->cases = {scratch.arena}; token_expect(OPEN_SCOPE); do { @@ -319,7 +317,7 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0) { if (token_match_pound(pctx->intern("fallthrough"_s))) switch_case->fallthrough = true; - switch_case->labels = {scratch}; + switch_case->labels = {scratch.arena}; do { switch_case->labels.add(parse_expr()); } while (token_match(TK_Comma)); @@ -359,7 +357,7 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0) { } else if (token_match_keyword(pctx->keyword_for)) { - Ast_Scope *for_scope = begin_stmt_scope(scratch, token_get()); + Ast_Scope *for_scope = begin_stmt_scope(scratch.arena, token_get()); Ast_Expr *init = 0; Ast_Expr *cond = 0; Ast_Expr *iter = 0; @@ -385,7 +383,7 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0) { } else if (token_match_keyword(pctx->keyword_if)) { - Array if_nodes = {scratch}; + Array if_nodes = {scratch.arena}; Ast_Expr *expr = parse_expr(); Ast_Expr *init_val = parse_init_stmt(expr); if (init_val != expr) { @@ -421,7 +419,7 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0) { scope->stmts.add(result_if); } else if (token_is(TK_Identifier) && token_is(TK_Comma, 1)) { - Array decls = {scratch}; + Array decls = {scratch.arena}; do { Token *name = token_match(TK_Identifier); Ast_Decl *decl = ast_var(name, 0, name->intern_val, 0); @@ -461,10 +459,9 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0) { CORE_Static Ast_Lambda * parse_lambda(Token *token) { - Arena *scratch = pctx->scratch; - Scratch_Scope __scope(scratch); + Scratch_Scope scratch(pctx->scratch); - Array params = {scratch}; + Array params = {scratch.arena}; if (!token_is(TK_CloseParen)) { for (;;) { Token *name = token_get(); @@ -487,7 +484,7 @@ parse_lambda(Token *token) { } token_expect(TK_CloseParen); - Array ret = {scratch}; + Array ret = {scratch.arena}; if (token_match(TK_Colon)) { do { Ast_Expr *typespec = parse_expr(); @@ -696,11 +693,10 @@ parse_assign_expr() { CORE_Static Ast_Decl * parse_struct(Token *pos) { - Arena *scratch = pctx->scratch; - Scratch_Scope __scope(scratch); + Scratch_Scope scratch(pctx->scratch); token_match(OPEN_SCOPE); - Ast_Scope *scope = begin_decl_scope(scratch, token_get()); + Ast_Scope *scope = begin_decl_scope(scratch.arena, token_get()); do { Token *token = token_expect(TK_Identifier); token_expect(TK_Colon); @@ -721,13 +717,12 @@ parse_struct(Token *pos) { CORE_Static Ast_Decl * parse_enum(Token *pos) { - Arena *scratch = pctx->scratch; - Scratch_Scope __scope(scratch); + Scratch_Scope scratch(pctx->scratch); Ast_Expr *typespec = parse_optional_type(); Token *flag = token_match_pound(pctx->intern_flag); token_match(OPEN_SCOPE); - Ast_Scope *scope = begin_decl_scope(scratch, token_get()); + Ast_Scope *scope = begin_decl_scope(scratch.arena, token_get()); do { Token *name = token_expect(TK_Identifier); Ast_Expr *value = 0; @@ -947,8 +942,7 @@ parse_decl(B32 is_global) { CORE_Static void parse_file(Ast_File *file) { assert(file); - Arena *scratch = pctx->scratch; - Scratch_Scope __scope(scratch); + Scratch_Scope scratch(pctx->scratch); if (!file->filecontent.len) { file->filecontent = os_read_file(pctx->perm, file->absolute_file_path);