Cleanup iterators, remove logging, add message queue

This commit is contained in:
Krzosa Karol
2023-01-01 19:18:42 +01:00
parent 3de813212a
commit ec66f02e46
8 changed files with 214 additions and 217 deletions

View File

@@ -102,19 +102,6 @@ struct Array{
force_inline T *begin() { return data; } force_inline T *begin() { return data; }
force_inline T *end () { return data + len; } force_inline T *end () { return data + len; }
force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; } force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; }
struct Array_Iter{
Array<T> *array;
S64 i;
T *item;
force_inline void next(){ i+=1; item = &array->data[i]; }
force_inline B32 is_valid(){ return i < array->len; }
};
force_inline Array_Iter iter(){ return {this, 0, begin()};}
#define For_It_Named(array, it) for(auto it = (array).iter(); it.is_valid(); it.next())
#define For_It(array) For_It_Named(array, it)
}; };

View File

@@ -344,74 +344,3 @@ compile_file_to_string(Allocator *allocator, String filename) {
String result = compile_to_c_code(); String result = compile_to_c_code();
return result; return result;
} }
const U32 COMPILE_NULL = 0x0;
const U32 COMPILE_PRINT_STATS = 0x1;
const U32 COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY = 0x2;
const U32 COMPILE_AND_RUN = 0x4;
const U32 COMPILE_TESTING = 0x8;
CORE_Static void
compile_file(Allocator *allocator, String filename, U32 compile_flags = COMPILE_NULL) {
String result = compile_file_to_string(allocator, filename);
if (is_flag_set(compile_flags, COMPILE_AND_RUN)) {
log_info_no_nl("%Q - ", filename);
}
B32 r = os_write_file("program.c"_s, result);
assert(r);
F64 total_compiler_time = os_time() - pctx->total_time;
log_info_no_nl("%f - ", total_compiler_time);
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
F64 begin = os_time();
String_Builder builder = {scratch};
builder.addf("clang program.c -Wall -Wno-unused-function -Wno-parentheses-equality -g -o a" OS_EXE " ");
For(pctx->files_to_link) {
builder.addf("-l%Q ", it->intern_val);
}
String compiler_call = string_flatten(scratch, &builder);
log_trace("%Q", compiler_call);
system((const char *)compiler_call.str);
F64 end = os_time();
if (is_flag_set(compile_flags, COMPILE_PRINT_STATS)) {
log_info("total = %f", os_time() - pctx->total_time);
log_info("clang = %f", end - begin);
log_info("parsing = %f", pctx->parsing_time_end - pctx->parsing_time_begin);
log_info("resolving = %f", pctx->resolving_time_end - pctx->resolving_time_begin);
log_info("generatin = %f", pctx->generating_time_end - pctx->generating_time_begin);
}
if (is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)) {
// @! allocator stats
//Arena *p = (Arena *)pctx->perm;
//Arena *stage = (Arena *)&pctx->stage_arena;
//log_info("Pernament arena len: %llu commit: %llu", pct, p->memory.commit);
//log_info("Stage arena len: %llu commit: %llu", stage->len, stage->memory.commit);
//log_info("Scratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit);
//log_info("Scratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit);
}
if (is_flag_set(compile_flags, COMPILE_AND_RUN)) {
String testing = compile_flags & COMPILE_TESTING ? "testing"_s : ""_s;
#if OS_WINDOWS
String sys = string_fmt(scratch, "a.exe %Q", testing);
#else
String sys = string_fmt(scratch, "./a.out %Q", testing);
#endif
int result = system((char *)sys.str);
assert(result != -1);
if (result == 0) {
log_info_no_nl(PRINTF_GREEN "OK!" PRINTF_RESET);
}
if (result != 0) {
log_info_no_nl(PRINTF_RED "ERROR!" PRINTF_RESET);
}
log_info("");
}
}

View File

@@ -41,6 +41,11 @@ struct Core_Ctx{
Scratch_Arena *scratch; Scratch_Arena *scratch;
Scratch_Arena *stage_arena; Scratch_Arena *stage_arena;
int errors_occured;
int warnings_occured;
Core_Message *first_message;
Core_Message *last_message;
// Lexer stuff // Lexer stuff
Lex_Stream stream; Lex_Stream stream;
Array<Token> tokens; Array<Token> tokens;

View File

@@ -0,0 +1,32 @@
#include "base.cpp"
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
#include "base_unicode.cpp"
#include "base_arena.cpp"
#include "base_data_structures.cpp"
#include "base_string.cpp"
#include "os.h"
#if OS_WINDOWS
#include "os_windows.cpp"
#elif OS_LINUX
#include "os_linux.cpp"
#else
#error Couldnt figure out OS using macros
#endif
#include "core_compiler_interface.hpp"
#include "c3_big_int.h"
#include "core_compiler.h"
#include "core_types.h"
#include "core_globals.cpp"
#include "core_generated.cpp"
#include "c3_big_int.cpp"
#include "core_lexing.cpp"
#include "core_ast.cpp"
#include "core_parsing.cpp"
#include "core_typechecking.h"
#include "core_types.cpp"
#include "core_typechecking.cpp"
#include "core_compiler.cpp"
#include "core_codegen_c_language.cpp"

View File

@@ -26,14 +26,7 @@ struct String{
uint8_t *str; uint8_t *str;
int64_t len; int64_t len;
}; };
typedef String Intern_String;
union Intern_String{ // Basically just String
String s;
struct{
uint8_t *str;
int64_t len;
};
};
template<class T> template<class T>
struct Array { struct Array {
@@ -632,10 +625,30 @@ struct Ast_Decl: Ast{
/*END*/ /*END*/
}; };
/* enum Core_Message_Kind {
Array<Token> lex_file(Allocator *allocator, char *filename); CORE_ERROR,
Array<Token> lex_string(Allocator *allocator, char *string, size_t size); CORE_WARNING,
CORE_TRACE,
};
struct Core_Message {
Core_Message *next;
Core_Message_Kind kind;
String string;
Token *pos1;
Token *pos2;
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);
Core_Context *core_create_context(Allocator *allocator);
Array<Token> core_lex_string(Core_Context *ctx, char *string, size_t size);
*/ */

View File

@@ -257,39 +257,69 @@ For modules it's a bit different cause they should be distributed as valid.
- [x] Think about compound expressions, unify with calls - maybe Thing(a=1) instead of Thing{a=1} - [x] Think about compound expressions, unify with calls - maybe Thing(a=1) instead of Thing{a=1}
*/ */
#include "base.cpp" #include "core_compiler_includes.cpp"
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
#include "base_unicode.cpp"
#include "base_arena.cpp"
#include "base_data_structures.cpp"
#include "base_string.cpp"
#include "os.h" const U32 COMPILE_NULL = 0x0;
const U32 COMPILE_PRINT_STATS = 0x1;
const U32 COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY = 0x2;
const U32 COMPILE_AND_RUN = 0x4;
const U32 COMPILE_TESTING = 0x8;
static void compile_file(Allocator *allocator, String filename, U32 compile_flags = COMPILE_NULL) {
if (is_flag_set(compile_flags, COMPILE_AND_RUN)) {
printf("%s - ", filename.str);
}
String result = compile_file_to_string(allocator, filename);
B32 r = os_write_file("program.c"_s, result);
assert(r);
F64 total_compiler_time = os_time() - pctx->total_time;
printf("%f - ", total_compiler_time);
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
F64 begin = os_time();
String_Builder builder = {scratch};
builder.addf("clang program.c -Wall -Wno-unused-function -Wno-parentheses-equality -g -o a" OS_EXE " ");
For(pctx->files_to_link) {
builder.addf("-l%Q ", it->intern_val);
}
String compiler_call = string_flatten(scratch, &builder);
system((const char *)compiler_call.str);
F64 end = os_time();
if (is_flag_set(compile_flags, COMPILE_PRINT_STATS)) {
printf("total = %f\n", os_time() - pctx->total_time);
printf("clang = %f\n", end - begin);
printf("parsing = %f\n", pctx->parsing_time_end - pctx->parsing_time_begin);
printf("resolving = %f\n", pctx->resolving_time_end - pctx->resolving_time_begin);
printf("generatin = %f\n", pctx->generating_time_end - pctx->generating_time_begin);
}
if (is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)) {
// @! Allocator stats
}
if (is_flag_set(compile_flags, COMPILE_AND_RUN)) {
String testing = compile_flags & COMPILE_TESTING ? "testing"_s : ""_s;
#if OS_WINDOWS #if OS_WINDOWS
#include "os_windows.cpp" String sys = string_fmt(scratch, "a.exe %Q", testing);
#elif OS_LINUX
#include "os_linux.cpp"
#else #else
#error Couldnt figure out OS using macros String sys = string_fmt(scratch, "./a.out %Q", testing);
#endif #endif
int result = system((char *)sys.str);
#include "core_compiler_interface.hpp" assert(result != -1);
#include "c3_big_int.h" if (result == 0) {
#include "core_compiler.h" printf(PRINTF_GREEN "OK!" PRINTF_RESET);
#include "core_types.h" }
#include "core_globals.cpp" if (result != 0) {
#include "core_generated.cpp" printf(PRINTF_RED "ERROR!" PRINTF_RESET);
#include "c3_big_int.cpp" }
#include "core_lexing.cpp" printf("\n");
#include "core_ast.cpp" }
#include "core_parsing.cpp" }
#include "core_typechecking.h"
#include "core_types.cpp"
#include "core_typechecking.cpp"
#include "core_compiler.cpp"
#include "core_codegen_c_language.cpp"
int main(int argument_count, char **arguments){ int main(int argument_count, char **arguments){
Arena arena = {}; Arena arena = {};
@@ -303,7 +333,7 @@ int main(int argument_count, char **arguments){
} }
if(!args.len){ if(!args.len){
log_info("Please specify a file to compile!"); printf("Please specify a file to compile!");
return 0; return 0;
} }

View File

@@ -1,103 +1,100 @@
CORE_Static Ast_Decl *parse_decl(B32 is_global); 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}; static void core_add_message(Core_Message_Kind kind, String string, Token *pos1, Token *pos2 = 0, int line = -1, const char *file = 0) {
typedef void Log_Proc(Log_Kind kind, String string, char *file, int line); if (kind == CORE_ERROR) pctx->errors_occured += 1;
if (kind == CORE_WARNING) pctx->warnings_occured += 1;
#define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__) Core_Message *message = allocate_struct(pctx->perm, Core_Message);
#define log_info_no_nl(...) handle_log_message(Log_Kind_Normal_No_NewLine, __LINE__, __FILE__,##__VA_ARGS__) message->kind = kind;
#define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__) message->string = string;
#define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__) message->pos1 = pos1;
CORE_Static void message->pos2 = pos2;
handle_log_message(Log_Kind kind, int line, const char *file, const char *str, ...){ message->trace_line = line;
if(kind == Log_Kind_Trace) return; message->trace_file = (char *)file;
SLL_QUEUE_ADD(pctx->first_message, pctx->last_message, message);
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 #define log_trace(...) core_log_trace(__LINE__, __FILE__,##__VA_ARGS__)
print_token_line(Token *token){ static void core_log_trace(int line, const char *file, const char *str, ...){
if(!token) return; STRING_FMT(pctx->perm, str, string);
core_add_message(CORE_TRACE, string, 0, 0, line, file);
}
#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
//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);
//}
// Print from line begin to token static void compiler_error(Token *token1, Token *token2, const char *str, ...) {
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);
}
CORE_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);
log_info_no_nl("\n%s", string.str); // log_info_no_nl("\n%s", string.str);
if(token1){ // if(token1){
if(token1->kind == TK_Error){ // if(token1->kind == TK_Error){
log_info_no_nl("\nToken Error: %.*s", (int)token1->error_val.len, token1->error_val.str); // log_info_no_nl("\nToken Error: %.*s", (int)token1->error_val.len, token1->error_val.str);
} // }
print_token_context(token1); // print_token_context(token1);
} // }
//
if(token2){ // if(token2){
if(token2->kind == TK_Error){ // if(token2->kind == TK_Error){
log_info_no_nl("\nToken Error: %.*s", (int)token2->error_val.len, token2->error_val.str); // log_info_no_nl("\nToken Error: %.*s", (int)token2->error_val.len, token2->error_val.str);
} // }
print_token_context(token2); // print_token_context(token2);
} // }
//
if(token1) log_info_no_nl("\n%s:%d", token1->file.str, (S32)token1->line + 1); // 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); // if(token2) log_info_no_nl("\n%s:%d", token2->file.str, (S32)token2->line + 1);
//
fflush(stdout); // fflush(stdout);
Breakpoint; 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);
if(token) log_info_no_nl("\n%s:%d %Q", token->file.str, (S32)token->line + 1, 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); // else log_info_no_nl("\n%s", string.str);
if(token){ // if(token){
if(token->kind == TK_Error){ // if(token->kind == TK_Error){
log_info_no_nl("\nToken Error: %.*s", (int)token->error_val.len, token->error_val.str); // log_info_no_nl("\nToken Error: %.*s", (int)token->error_val.len, token->error_val.str);
} // }
//
// print_token_context(token);
// }
//fflush(stdout);
print_token_context(token);
}
fflush(stdout);
Breakpoint; Breakpoint;
} }

View File

@@ -734,18 +734,20 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
Scratch_Scope _scope(scratch); Scratch_Scope _scope(scratch);
Array<Ast_Type *> types = {scratch}; Array<Ast_Type *> types = {scratch};
For_It(node->expr){ int i = 0;
For(node->expr){
Operand op; Operand op;
if(is_tuple(ret)){ if(is_tuple(ret)){
Ast_Type *sub_type = ret->agg.members[it.i].type; Ast_Type *sub_type = ret->agg.members[i].type;
op = resolve_expr(*it.item, AST_CAN_BE_NULL, sub_type, 0); op = resolve_expr(it, AST_CAN_BE_NULL, sub_type, 0);
} }
else{ else{
op = resolve_expr(*it.item, AST_CAN_BE_NULL, ret, 0); op = resolve_expr(it, AST_CAN_BE_NULL, ret, 0);
convert_untyped_to_typed(node->pos, &op.value, ret); convert_untyped_to_typed(node->pos, &op.value, ret);
} }
types.add(op.type); types.add(op.type);
i += 1;
} }
Ast_Type *type = type_try_tupling(types, node); Ast_Type *type = type_try_tupling(types, node);
@@ -753,11 +755,13 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
compiler_error(node->pos, "Return statement has different type then returned value, expecting: %Q got instead %Q", typestring(ret), typestring(type)); compiler_error(node->pos, "Return statement has different type then returned value, expecting: %Q got instead %Q", typestring(ret), typestring(type));
node->resolved_type = type; node->resolved_type = type;
For_It(node->expr){ i = 0;
For(node->expr){
Ast_Type *sub_type = type; Ast_Type *sub_type = type;
if(is_tuple(type)) sub_type = type->agg.members[it.i].type; if(is_tuple(type)) sub_type = type->agg.members[i].type;
try_propagating_resolved_type_to_untyped_literals(*it.item, sub_type); try_propagating_resolved_type_to_untyped_literals(it, sub_type);
i += 1;
} }
BREAK(); BREAK();