Get rid of clib in big int, introduce message api, add stringify message
This commit is contained in:
2
base.cpp
2
base.cpp
@@ -204,7 +204,7 @@ align_down(size_t size, size_t align){
|
||||
}
|
||||
|
||||
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 *s = (U8*)src;
|
||||
for(size_t i = 0; i < size; i++){
|
||||
|
||||
@@ -209,26 +209,16 @@ string_builder_make(Allocator *a, S64 first_block_size = 4096){
|
||||
return sb;
|
||||
}
|
||||
|
||||
enum String_Builder_Flag{
|
||||
String_Builder_Flag_None = 0,
|
||||
String_Builder_Flag_AddSize = 0,
|
||||
};
|
||||
|
||||
CORE_Static String
|
||||
string_flatten(Allocator *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){
|
||||
// @! Make string_flatten a method
|
||||
static String string_flatten(Allocator *a, String_Builder *b){
|
||||
// @Note(Krzosa): Compute size to allocate
|
||||
S64 size = 1;
|
||||
if(is_flag_set(flags, String_Builder_Flag_AddSize)) size += sizeof(size_t);
|
||||
For_Linked_List(b->first){
|
||||
size += it->len;
|
||||
}
|
||||
|
||||
String result = {};
|
||||
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
|
||||
For_Linked_List(b->first){
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright (c) 2019 Christoffer Lerno. All rights reserved.
|
||||
// Use of this source code is governed by the GNU LGPLv3.0 license
|
||||
// a copy of which can be found in the LICENSE file.
|
||||
#include <string.h>
|
||||
|
||||
#define Set_BigInt_Arena(x) BigInt_Arena bigint_allocator(x)
|
||||
struct BigInt_Arena{
|
||||
@@ -55,7 +54,7 @@ bigint_copy(Allocator *allocator, BigInt *src){
|
||||
dest.digit_count = src->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;
|
||||
}
|
||||
|
||||
@@ -186,7 +185,7 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src)
|
||||
dest->is_negative = src->is_negative;
|
||||
dest->digit_count = src->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)
|
||||
@@ -329,7 +328,7 @@ void bigint_init_data(BigInt *dest, const uint64_t *digits, unsigned int digit_c
|
||||
dest->digit_count = digit_count;
|
||||
dest->is_negative = is_negative;
|
||||
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);
|
||||
}
|
||||
@@ -344,7 +343,7 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
|
||||
if (dest->is_negative) {
|
||||
f128M_sub(&zero, &op->value, &abs_val);
|
||||
} else {
|
||||
memcpy(&abs_val, &op->value, sizeof(float128_t));
|
||||
memory_copy(&abs_val, &op->value, sizeof(float128_t));
|
||||
}
|
||||
|
||||
float128_t max_u64;
|
||||
|
||||
@@ -6,6 +6,7 @@ core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
|
||||
|
||||
ctx->emit_type_info = true;
|
||||
ctx->emit_line_directives = true;
|
||||
ctx->debugger_break_on_compiler_error = true;
|
||||
ctx->color_codes_enabled = true;
|
||||
ctx->same_scope_token = { SAME_SCOPE };
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ struct Core_Ctx{
|
||||
|
||||
Scratch_Arena *scratch;
|
||||
Scratch_Arena *stage_arena;
|
||||
String_Builder helper_builder;
|
||||
|
||||
int errors_occured;
|
||||
int warnings_occured;
|
||||
@@ -78,8 +79,6 @@ struct Core_Ctx{
|
||||
String working_folder;
|
||||
List<Token *> files_to_link;
|
||||
|
||||
String_Builder helper_builder;
|
||||
|
||||
F64 generating_time_begin;
|
||||
F64 generating_time_end;
|
||||
F64 resolving_time_begin;
|
||||
@@ -91,6 +90,7 @@ struct Core_Ctx{
|
||||
F64 parsing_time_end;
|
||||
|
||||
bool color_codes_enabled;
|
||||
bool debugger_break_on_compiler_error;
|
||||
|
||||
// Codegen stage mostly
|
||||
S64 indent;
|
||||
|
||||
@@ -635,14 +635,12 @@ struct Core_Message {
|
||||
Core_Message *next;
|
||||
Core_Message_Kind kind;
|
||||
String string;
|
||||
Token *pos1;
|
||||
Token *pos2;
|
||||
Token *tokens[2];
|
||||
|
||||
int trace_line;
|
||||
char *trace_file;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
Array<Token> core_lex_file(Allocator *allocator, char *filename);
|
||||
Array<Token> core_lex_string(Allocator *allocator, char *string, size_t size);
|
||||
|
||||
140
core_parsing.cpp
140
core_parsing.cpp
@@ -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 *
|
||||
|
||||
Reference in New Issue
Block a user