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

@@ -204,7 +204,7 @@ align_down(size_t size, size_t align){
} }
CORE_Static void CORE_Static void
memory_copy(void *dst, void *src, size_t size){ memory_copy(void *dst, const void *src, size_t size){
U8 *d = (U8*)dst; U8 *d = (U8*)dst;
U8 *s = (U8*)src; U8 *s = (U8*)src;
for(size_t i = 0; i < size; i++){ for(size_t i = 0; i < size; i++){

View File

@@ -209,26 +209,16 @@ string_builder_make(Allocator *a, S64 first_block_size = 4096){
return sb; return sb;
} }
enum String_Builder_Flag{ // @! Make string_flatten a method
String_Builder_Flag_None = 0, static String string_flatten(Allocator *a, String_Builder *b){
String_Builder_Flag_AddSize = 0,
};
CORE_Static String
string_flatten(Allocator *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){
// @Note(Krzosa): Compute size to allocate // @Note(Krzosa): Compute size to allocate
S64 size = 1; S64 size = 1;
if(is_flag_set(flags, String_Builder_Flag_AddSize)) size += sizeof(size_t);
For_Linked_List(b->first){ For_Linked_List(b->first){
size += it->len; size += it->len;
} }
String result = {}; String result = {};
result.str = (U8 *)allocate_size(a, size, false); result.str = (U8 *)allocate_size(a, size, false);
if(is_flag_set(flags, String_Builder_Flag_AddSize)) {
memory_copy(result.str + result.len, &size, sizeof(S64));
result.len += sizeof(S64);
}
// @Note(Krzosa): Copy the content of each block into the string // @Note(Krzosa): Copy the content of each block into the string
For_Linked_List(b->first){ For_Linked_List(b->first){

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2019 Christoffer Lerno. All rights reserved. // Copyright (c) 2019 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by the GNU LGPLv3.0 license // Use of this source code is governed by the GNU LGPLv3.0 license
// a copy of which can be found in the LICENSE file. // a copy of which can be found in the LICENSE file.
#include <string.h>
#define Set_BigInt_Arena(x) BigInt_Arena bigint_allocator(x) #define Set_BigInt_Arena(x) BigInt_Arena bigint_allocator(x)
struct BigInt_Arena{ struct BigInt_Arena{
@@ -55,7 +54,7 @@ bigint_copy(Allocator *allocator, BigInt *src){
dest.digit_count = src->digit_count; dest.digit_count = src->digit_count;
dest.digits = allocate_array(allocator, uint64_t, dest.digit_count); dest.digits = allocate_array(allocator, uint64_t, dest.digit_count);
memcpy(dest.digits, src->digits, sizeof(uint64_t) * dest.digit_count); memory_copy(dest.digits, src->digits, sizeof(uint64_t) * dest.digit_count);
return dest; return dest;
} }
@@ -186,7 +185,7 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src)
dest->is_negative = src->is_negative; dest->is_negative = src->is_negative;
dest->digit_count = src->digit_count; dest->digit_count = src->digit_count;
dest->digits = ALLOC_DIGITS(dest->digit_count); dest->digits = ALLOC_DIGITS(dest->digit_count);
memcpy(dest->digits, src->digits, sizeof(uint64_t) * dest->digit_count); memory_copy(dest->digits, src->digits, sizeof(uint64_t) * dest->digit_count);
} }
void bigint_negate(BigInt *dest, const BigInt *source) void bigint_negate(BigInt *dest, const BigInt *source)
@@ -329,7 +328,7 @@ void bigint_init_data(BigInt *dest, const uint64_t *digits, unsigned int digit_c
dest->digit_count = digit_count; dest->digit_count = digit_count;
dest->is_negative = is_negative; dest->is_negative = is_negative;
dest->digits = ALLOC_DIGITS(digit_count); dest->digits = ALLOC_DIGITS(digit_count);
memcpy(dest->digits, digits, sizeof(uint64_t) * digit_count); memory_copy(dest->digits, digits, sizeof(uint64_t) * digit_count);
normalize(dest); normalize(dest);
} }
@@ -344,7 +343,7 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
if (dest->is_negative) { if (dest->is_negative) {
f128M_sub(&zero, &op->value, &abs_val); f128M_sub(&zero, &op->value, &abs_val);
} else { } else {
memcpy(&abs_val, &op->value, sizeof(float128_t)); memory_copy(&abs_val, &op->value, sizeof(float128_t));
} }
float128_t max_u64; float128_t max_u64;

View File

@@ -6,6 +6,7 @@ core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
ctx->emit_type_info = true; ctx->emit_type_info = true;
ctx->emit_line_directives = true; ctx->emit_line_directives = true;
ctx->debugger_break_on_compiler_error = true;
ctx->color_codes_enabled = true; ctx->color_codes_enabled = true;
ctx->same_scope_token = { SAME_SCOPE }; ctx->same_scope_token = { SAME_SCOPE };

View File

@@ -40,6 +40,7 @@ struct Core_Ctx{
Scratch_Arena *scratch; Scratch_Arena *scratch;
Scratch_Arena *stage_arena; Scratch_Arena *stage_arena;
String_Builder helper_builder;
int errors_occured; int errors_occured;
int warnings_occured; int warnings_occured;
@@ -78,8 +79,6 @@ struct Core_Ctx{
String working_folder; String working_folder;
List<Token *> files_to_link; List<Token *> files_to_link;
String_Builder helper_builder;
F64 generating_time_begin; F64 generating_time_begin;
F64 generating_time_end; F64 generating_time_end;
F64 resolving_time_begin; F64 resolving_time_begin;
@@ -91,6 +90,7 @@ struct Core_Ctx{
F64 parsing_time_end; F64 parsing_time_end;
bool color_codes_enabled; bool color_codes_enabled;
bool debugger_break_on_compiler_error;
// Codegen stage mostly // Codegen stage mostly
S64 indent; S64 indent;

View File

@@ -635,14 +635,12 @@ struct Core_Message {
Core_Message *next; Core_Message *next;
Core_Message_Kind kind; Core_Message_Kind kind;
String string; String string;
Token *pos1; Token *tokens[2];
Token *pos2;
int trace_line; int trace_line;
char *trace_file; char *trace_file;
}; };
/* /*
Array<Token> core_lex_file(Allocator *allocator, char *filename); Array<Token> core_lex_file(Allocator *allocator, char *filename);
Array<Token> core_lex_string(Allocator *allocator, char *string, size_t size); Array<Token> core_lex_string(Allocator *allocator, char *string, size_t size);

View File

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