Working on simplifying configurable allocation scheme

This commit is contained in:
Krzosa Karol
2023-01-01 12:40:58 +01:00
parent 8c0a8bf72b
commit c5539276ae
18 changed files with 169 additions and 347 deletions

View File

@@ -1,5 +1,25 @@
CORE_Static Ast_Decl *parse_decl(B32 is_global);
enum Log_Kind{Log_Kind_Normal_No_NewLine, Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace};
typedef void Log_Proc(Log_Kind kind, String string, char *file, int line);
#define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__)
#define log_info_no_nl(...) handle_log_message(Log_Kind_Normal_No_NewLine, __LINE__, __FILE__,##__VA_ARGS__)
#define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__)
#define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__)
CORE_Static void
handle_log_message(Log_Kind kind, int line, const char *file, const char *str, ...){
if(kind == Log_Kind_Trace) return;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
STRING_FMT(scratch, str, message);
printf("%s", message.str);
if(kind != Log_Kind_Normal_No_NewLine){
printf("\n");
}
}
CORE_Static void
print_token_line(Token *token){
if(!token) return;
@@ -37,8 +57,7 @@ print_token_context(Token *token){
CORE_Static void
compiler_error(Token *token1, Token *token2, const char *str, ...){
Scratch scratch;
STRING_FMT(scratch, str, string);
STRING_FMT(pctx->perm, str, string);
log_info_no_nl("\n%s", string.str);
if(token1){
@@ -64,8 +83,7 @@ compiler_error(Token *token1, Token *token2, const char *str, ...){
CORE_Static void
compiler_error(Token *token, const char *str, ...){
Scratch scratch;
STRING_FMT(scratch, str, string);
STRING_FMT(pctx->perm, str, string);
if(token) log_info_no_nl("\n%s:%d %Q", token->file.str, (S32)token->line + 1, string);
else log_info_no_nl("\n%s", string.str);
@@ -205,7 +223,8 @@ parse_init_stmt(Ast_Expr *expr){
CORE_Static Ast_Call *
parse_expr_call(Ast_Expr *left, Token_Kind close_kind){
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
Token *pos = token_get();
Array<Ast_Call_Item *> exprs = {scratch};
@@ -257,7 +276,8 @@ 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();
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
if(!scope_defined_outside) scope = begin_stmt_scope(scratch, token_block);
do{
Token *token = token_get();
@@ -441,7 +461,8 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
CORE_Static Ast_Lambda *
parse_lambda(Token *token){
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
Array<Ast_Decl *> params = {scratch};
if(!token_is(TK_CloseParen)){
@@ -668,7 +689,8 @@ parse_assign_expr(){
CORE_Static Ast_Decl *
parse_struct(Token *pos){
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
token_match(OPEN_SCOPE);
Ast_Scope *scope = begin_decl_scope(scratch, token_get());
@@ -692,7 +714,8 @@ parse_struct(Token *pos){
CORE_Static Ast_Decl *
parse_enum(Token *pos){
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
Ast_Expr *typespec = parse_optional_type();
Token *flag = token_match_pound(intern_flag);
@@ -772,12 +795,12 @@ register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32
CORE_Static Intern_String
preprocess_filename(Token *token_filename){
Scratch scratch;
Scratch_Scope _scope(pctx->scratch);
String filename = token_filename->intern_val.s;
Array<String_Replace> replace = {scratch};
Array<String_Replace> replace = {pctx->scratch};
replace.add({"$OS"_s, OS_NAME});
replace.add({"$os"_s, OS_NAME_LOWER});
String result0 = string_replace(scratch, filename, replace);
String result0 = string_replace(pctx->scratch, pctx->scratch, filename, replace);
Intern_String result = pctx->intern(result0);
return result;
}
@@ -918,7 +941,8 @@ CORE_Static void
parse_file(Ast_File *file){
assert(file);
Scratch scratch;
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
file->filecontent = os_read_file(pctx->perm, file->absolute_file_path);
if(file->filecontent.len == 0){
compiler_error(file->pos, "Failed to open file \"%Q\"", file->absolute_file_path);