diff --git a/base_data_structures.cpp b/base_data_structures.cpp index 2060c2a..d4adf5c 100644 --- a/base_data_structures.cpp +++ b/base_data_structures.cpp @@ -102,19 +102,6 @@ struct Array{ force_inline T *begin() { return data; } force_inline T *end () { return data + len; } force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; } - - struct Array_Iter{ - Array *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) }; diff --git a/core_compiler.cpp b/core_compiler.cpp index 4e13b65..894c258 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -344,74 +344,3 @@ compile_file_to_string(Allocator *allocator, String filename) { String result = compile_to_c_code(); 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(""); - } - -} diff --git a/core_compiler.h b/core_compiler.h index 269e388..817e66a 100644 --- a/core_compiler.h +++ b/core_compiler.h @@ -41,6 +41,11 @@ struct Core_Ctx{ Scratch_Arena *scratch; Scratch_Arena *stage_arena; + int errors_occured; + int warnings_occured; + Core_Message *first_message; + Core_Message *last_message; + // Lexer stuff Lex_Stream stream; Array tokens; diff --git a/core_compiler_includes.cpp b/core_compiler_includes.cpp new file mode 100644 index 0000000..4a4bca0 --- /dev/null +++ b/core_compiler_includes.cpp @@ -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" \ No newline at end of file diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index 551ecdc..69f444c 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -26,14 +26,7 @@ struct String{ uint8_t *str; int64_t len; }; - -union Intern_String{ // Basically just String - String s; - struct{ - uint8_t *str; - int64_t len; - }; -}; +typedef String Intern_String; template struct Array { @@ -612,9 +605,9 @@ struct Ast_Decl: Ast{ Ast_Lambda *lambda; }; - /*#import meta - meta.inline_value_fields() - */ +/*#import meta +meta.inline_value_fields() +*/ union { Value value; struct { @@ -632,10 +625,30 @@ struct Ast_Decl: Ast{ /*END*/ }; -/* - Array lex_file(Allocator *allocator, char *filename); - Array lex_string(Allocator *allocator, char *string, size_t size); +enum Core_Message_Kind { + CORE_ERROR, + 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 core_lex_file(Allocator *allocator, char *filename); + Array core_lex_string(Allocator *allocator, char *string, size_t size); + + Core_Context *core_create_context(Allocator *allocator); + Array core_lex_string(Core_Context *ctx, char *string, size_t size); */ \ No newline at end of file diff --git a/core_main.cpp b/core_main.cpp index 5c4d36a..a15aa73 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -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} */ -#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 "core_compiler_includes.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 -#include "os_windows.cpp" -#elif OS_LINUX -#include "os_linux.cpp" + String sys = string_fmt(scratch, "a.exe %Q", testing); #else -#error Couldnt figure out OS using macros + String sys = string_fmt(scratch, "./a.out %Q", testing); #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" - + int result = system((char *)sys.str); + assert(result != -1); + if (result == 0) { + printf(PRINTF_GREEN "OK!" PRINTF_RESET); + } + if (result != 0) { + printf(PRINTF_RED "ERROR!" PRINTF_RESET); + } + printf("\n"); + } +} int main(int argument_count, char **arguments){ Arena arena = {}; @@ -303,7 +333,7 @@ int main(int argument_count, char **arguments){ } if(!args.len){ - log_info("Please specify a file to compile!"); + printf("Please specify a file to compile!"); return 0; } diff --git a/core_parsing.cpp b/core_parsing.cpp index e5ea6a0..b642c40 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -1,103 +1,100 @@ 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"); - } +static void 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->trace_line = line; + message->trace_file = (char *)file; + SLL_QUEUE_ADD(pctx->first_message, pctx->last_message, message); } -CORE_Static void -print_token_line(Token *token){ - if(!token) return; +#define log_trace(...) core_log_trace(__LINE__, __FILE__,##__VA_ARGS__) +static void core_log_trace(int line, const char *file, const char *str, ...){ + STRING_FMT(pctx->perm, str, string); + core_add_message(CORE_TRACE, string, 0, 0, line, file); +} #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); +//} - // 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); -} - -CORE_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); - - 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); + 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_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); - } +// 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); - print_token_context(token); - } - - - - fflush(stdout); Breakpoint; } diff --git a/core_typechecking.cpp b/core_typechecking.cpp index f374d04..16dc870 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -734,18 +734,20 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ Scratch_Scope _scope(scratch); Array types = {scratch}; - For_It(node->expr){ + int i = 0; + For(node->expr){ Operand op; if(is_tuple(ret)){ - Ast_Type *sub_type = ret->agg.members[it.i].type; - op = resolve_expr(*it.item, AST_CAN_BE_NULL, sub_type, 0); + Ast_Type *sub_type = ret->agg.members[i].type; + op = resolve_expr(it, AST_CAN_BE_NULL, sub_type, 0); } 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); } types.add(op.type); + i += 1; } 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)); node->resolved_type = type; - For_It(node->expr){ + i = 0; + For(node->expr){ 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();