Get rid of clib in big int, introduce message api, add stringify message

This commit is contained in:
Krzosa Karol
2023-01-01 19:51:35 +01:00
parent ec66f02e46
commit aa994c999a
7 changed files with 82 additions and 92 deletions

View File

@@ -1,16 +1,17 @@
CORE_Static Ast_Decl *parse_decl(B32 is_global);
static void core_add_message(Core_Message_Kind kind, String string, Token *pos1, Token *pos2 = 0, int line = -1, const char *file = 0) {
static Core_Message *core_add_message(Core_Message_Kind kind, String string, Token *pos1, Token *pos2 = 0, int line = -1, const char *file = 0) {
if (kind == CORE_ERROR) pctx->errors_occured += 1;
if (kind == CORE_WARNING) pctx->warnings_occured += 1;
Core_Message *message = allocate_struct(pctx->perm, Core_Message);
message->kind = kind;
message->string = string;
message->pos1 = pos1;
message->pos2 = pos2;
message->tokens[0] = pos1;
message->tokens[1] = pos2;
message->trace_line = line;
message->trace_file = (char *)file;
SLL_QUEUE_ADD(pctx->first_message, pctx->last_message, message);
return message;
}
#define log_trace(...) core_log_trace(__LINE__, __FILE__,##__VA_ARGS__)
@@ -22,80 +23,81 @@ static void core_log_trace(int line, const char *file, const char *str, ...){
#define PRINTF_GREEN "\033[32m"
#define PRINTF_RED "\033[31m"
#define PRINTF_RESET "\033[0m"
//
//CORE_Static void
//print_token_line(Token *token){
// if(!token) return;
//
// // Print from line begin to token
// int i1 = token->str - token->line_begin;
// log_info_no_nl("%.*s", i1, token->line_begin);
//
// // Print token part
// if(pctx->color_codes_enabled){
// log_info_no_nl( PRINTF_RED "%.*s" PRINTF_RESET, (int)token->len, token->str);
// } else {
// log_info_no_nl("%.*s", (int)token->len, token->str);
// }
//
//
// // Print to end of line from token
// int iend = 0;
// U8 *pointer = token->str + token->len;
// while(pointer[iend]!='\n' && pointer[iend]!=0) iend++;
// log_info_no_nl("%.*s", iend, pointer);
// log_info_no_nl("\n");
//}
//
//CORE_Static void
//print_token_context(Token *token){
// if(!token) return;
// log_info_no_nl("\n");
// print_token_line(token);
//}
String core_stringify_message(Core_Ctx *pctx, Allocator *allocator, Core_Message *msg, int color_codes_enabled = false) {
String_Builder &b = pctx->helper_builder;
if (msg->kind == CORE_ERROR) b.addf("Error! ");
else if (msg->kind == CORE_WARNING) b.addf("Warning! ");
else if (msg->kind == CORE_TRACE) b.addf("Trace: ");
else invalid_codepath;
for (int i = 0; i < buff_cap(msg->tokens); i += 1) {
Token *it = msg->tokens[i];
if (it) {
if (it->kind == TK_Error) {
b.addf("%Q | ", it->error_val);
}
}
}
b.addf("%Q", msg->string);
for (int i = 0; i < buff_cap(msg->tokens); i += 1) {
Token *token = msg->tokens[i];
if (token) {
b.addf("\n");
// Print from line begin to token
int i1 = token->str - token->line_begin;
b.addf("%.*s", i1, token->line_begin);
// Print token part
if(color_codes_enabled){
b.addf( PRINTF_RED "%.*s" PRINTF_RESET, (int)token->len, token->str);
} else {
b.addf("%.*s", (int)token->len, token->str);
}
// Print to end of line from token
int iend = 0;
U8 *pointer = token->str + token->len;
while(pointer[iend]!='\n' && pointer[iend]!=0) iend++;
b.addf("%.*s", iend, pointer);
}
}
for (int i = 0; i < buff_cap(msg->tokens); i += 1) {
Token *it = msg->tokens[i];
if (it) {
b.addf("\n%s:%d", it->file.str, (int)it->line + 1);
}
}
String result = string_flatten(allocator, &b);
return result;
}
static void compiler_error(Token *token1, Token *token2, const char *str, ...) {
STRING_FMT(pctx->perm, str, string);
core_add_message(CORE_ERROR, string, token1, token2);
// log_info_no_nl("\n%s", string.str);
// if(token1){
// if(token1->kind == TK_Error){
// log_info_no_nl("\nToken Error: %.*s", (int)token1->error_val.len, token1->error_val.str);
// }
// print_token_context(token1);
// }
//
// if(token2){
// if(token2->kind == TK_Error){
// log_info_no_nl("\nToken Error: %.*s", (int)token2->error_val.len, token2->error_val.str);
// }
// print_token_context(token2);
// }
//
// if(token1) log_info_no_nl("\n%s:%d", token1->file.str, (S32)token1->line + 1);
// if(token2) log_info_no_nl("\n%s:%d", token2->file.str, (S32)token2->line + 1);
//
// fflush(stdout);
Breakpoint;
Core_Message *msg = core_add_message(CORE_ERROR, string, token1, token2);
if (pctx->debugger_break_on_compiler_error) {
String str = core_stringify_message(pctx, pctx->perm, msg, pctx->color_codes_enabled);
printf("%s", str.str); // @! How to get rid of printf ?
fflush(stdout);
Breakpoint;
}
}
CORE_Static void
compiler_error(Token *token, const char *str, ...){
STRING_FMT(pctx->perm, str, string);
core_add_message(CORE_ERROR, string, token);
// 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);
// if(token){
// if(token->kind == TK_Error){
// log_info_no_nl("\nToken Error: %.*s", (int)token->error_val.len, token->error_val.str);
// }
//
// print_token_context(token);
// }
//fflush(stdout);
Breakpoint;
Core_Message *msg = core_add_message(CORE_ERROR, string, token);
if (pctx->debugger_break_on_compiler_error) {
String str = core_stringify_message(pctx, pctx->perm, msg, pctx->color_codes_enabled);
printf("%s", str.str); // @! How to get rid of printf ?
fflush(stdout);
Breakpoint;
}
}
CORE_Static Token *