From b3c40ba79bc7fef3e42b4a745e35634f328c6d81 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Mon, 10 Jul 2017 13:05:30 -0400 Subject: [PATCH] almost removed out context completely --- meta/4ed_metagen.cpp | 626 ++++++++++++++--------------- meta/4ed_out_context.cpp | 61 +-- site/4ed_sitegen.cpp | 27 +- string/4coder_string_build_num.txt | 2 +- string/4ed_string_builder.cpp | 458 +++++++++++---------- 5 files changed, 544 insertions(+), 630 deletions(-) diff --git a/meta/4ed_metagen.cpp b/meta/4ed_metagen.cpp index 7c714983..0aef8d8c 100644 --- a/meta/4ed_metagen.cpp +++ b/meta/4ed_metagen.cpp @@ -15,12 +15,12 @@ #define API_H "4coder_API/app_functions.h" #define OS_API_H "4ed_os_custom_api.h" +#include "../4ed_defines.h" #include "4ed_meta_defines.h" #include "../4coder_API/version.h" #define FSTRING_IMPLEMENTATION #include "../4coder_lib/4coder_string.h" -//#include "../4coder_lib/4coder_mem.h" #include "../4cpp/4cpp_lexer.h" @@ -75,67 +75,67 @@ char *keys_that_need_codes[] = { internal void generate_keycode_enum(){ + Temp temp = fm_begin_temp(); + char *filename_keycodes = KEYCODES_FILE; - uint16_t code = 0xD800; + u16 code = 0xD800; + String out = str_alloc(10 << 20); - String out = make_out_string(10 << 20); + i32 count = ArrayCount(keys_that_need_codes); - Out_Context context = {0}; - if (begin_file_out(&context, filename_keycodes, &out)){ - int32_t count = ArrayCount(keys_that_need_codes); - - append(&out, "enum{\n"); - for (int32_t i = 0; i < count;){ - append(&out, "key_"); - append(&out, keys_that_need_codes[i++]); - append(&out, " = "); - append_int_to_str(&out, code++); - append(&out, ",\n"); - } - append(&out, "};\n"); - - append(&out, - "static char*\n" - "global_key_name(uint32_t key_code, int32_t *size){\n" - "char *result = 0;\n" - "switch(key_code){\n"); - - for (int32_t i = 0; i < count; ++i){ - append(&out, "case key_"); - append(&out, keys_that_need_codes[i]); - append(&out, ": result = \""); - append(&out, keys_that_need_codes[i]); - append(&out, "\"; *size = sizeof(\""); - append(&out, keys_that_need_codes[i]); - append(&out, "\")-1; break;\n"); - } - - append(&out, - "}\n" - "return(result);\n" - "}\n"); - - end_file_out(context); + append(&out, "enum{\n"); + for (i32 i = 0; i < count;){ + append(&out, "key_"); + append(&out, keys_that_need_codes[i++]); + append(&out, " = "); + append_int_to_str(&out, code++); + append(&out, ",\n"); } + append(&out, "};\n"); + + append(&out, + "static char*\n" + "global_key_name(uint32_t key_code, int32_t *size){\n" + "char *result = 0;\n" + "switch(key_code){\n"); + + for (i32 i = 0; i < count; ++i){ + append(&out, "case key_"); + append(&out, keys_that_need_codes[i]); + append(&out, ": result = \""); + append(&out, keys_that_need_codes[i]); + append(&out, "\"; *size = sizeof(\""); + append(&out, keys_that_need_codes[i]); + append(&out, "\")-1; break;\n"); + } + + append(&out, + "}\n" + "return(result);\n" + "}\n"); + + end_file_out(filename_keycodes, &out); + + fm_end_temp(temp); } ////////////////////////////////////////////////////////////////////////////////////////////////// -static void +internal void struct_begin(String *str, char *name){ append(str, "struct "); append(str, name); append(str, "{\n"); //} } -static void +internal void enum_begin(String *str, char *name){ append(str, "enum "); append(str, name); append(str, "{\n"); //} } -static void +internal void struct_end(String *str){ //{ append(str, "};\n\n"); } @@ -180,142 +180,127 @@ static char* main_style_fields[] = { "next_undo", }; -static char* +internal char* make_style_tag(char *tag){ - char *str; - int32_t len; - - len = (int32_t)strlen(tag); - str = (char*)malloc(len + 1); - to_camel_cc(tag, str); + i32 len = (i32)strlen(tag); + char *str = fm_push_array(char, len + 1); + to_camel(tag, str); str[len] = 0; - return(str); } -static void +internal void generate_style(){ + Temp temp = fm_begin_temp(); + char filename_4coder[] = STYLE_FILE; char filename_4ed[] = "4ed_style.h"; - String out = make_out_string(10 << 20); - Out_Context context = {0}; + String out = str_alloc(10 << 20);; - if (begin_file_out(&context, filename_4coder, &out)){ - - enum_begin(&out, "Style_Tag"); - { - int32_t count = ArrayCount(bar_style_fields); - for (int32_t i = 0; i < count; ++i){ - char *tag = make_style_tag(bar_style_fields[i]); - append(&out, "Stag_"); - append(&out, tag); - append(&out, ",\n"); - free(tag); - } - - count = ArrayCount(main_style_fields); - for (int32_t i = 0; i < count; ++i){ - char *tag = make_style_tag(main_style_fields[i]); - append(&out, "Stag_"); - append(&out, tag); - append(&out, ",\n"); - free(tag); - } - - append(&out, "Stag_COUNT\n"); + enum_begin(&out, "Style_Tag"); + { + i32 count = ArrayCount(bar_style_fields); + for (i32 i = 0; i < count; ++i){ + char *tag = make_style_tag(bar_style_fields[i]); + append(&out, "Stag_"); + append(&out, tag); + append(&out, ",\n"); } - struct_end(&out); - append(&out, "static char *style_tag_names[] = {\n"); - { - int32_t count = ArrayCount(bar_style_fields); - for (int32_t i = 0; i < count; ++i){ - char *tag = make_style_tag(bar_style_fields[i]); - append(&out, "\""); - append(&out, tag); - append(&out, "\",\n"); - free(tag); - } - - count = ArrayCount(main_style_fields); - for (int32_t i = 0; i < count; ++i){ - char *tag = make_style_tag(main_style_fields[i]); - append(&out, "\""); - append(&out, tag); - append(&out, "\",\n"); - free(tag); - } + count = ArrayCount(main_style_fields); + for (i32 i = 0; i < count; ++i){ + char *tag = make_style_tag(main_style_fields[i]); + append(&out, "Stag_"); + append(&out, tag); + append(&out, ",\n"); } - append(&out, "};\n"); - end_file_out(context); + append(&out, "Stag_COUNT\n"); + } + struct_end(&out); + + append(&out, "static char *style_tag_names[] = {\n"); + { + i32 count = ArrayCount(bar_style_fields); + for (i32 i = 0; i < count; ++i){ + char *tag = make_style_tag(bar_style_fields[i]); + append(&out, "\""); + append(&out, tag); + append(&out, "\",\n"); + } + + count = ArrayCount(main_style_fields); + for (i32 i = 0; i < count; ++i){ + char *tag = make_style_tag(main_style_fields[i]); + append(&out, "\""); + append(&out, tag); + append(&out, "\",\n"); + } + } + append(&out, "};\n"); + + end_file_out(filename_4coder, &out); + + struct_begin(&out, "Interactive_Style"); + { + i32 count = ArrayCount(bar_style_fields); + for (i32 i = 0; i < count; ++i){ + append(&out, "u32 "); + append(&out, bar_style_fields[i]); + append(&out, "_color;\n"); + } + } + struct_end(&out); + + struct_begin(&out, "Style_Main_Data"); + { + i32 count = ArrayCount(main_style_fields); + for (i32 i = 0; i < count; ++i){ + append(&out, "u32 "); + append(&out, main_style_fields[i]); + append(&out, "_color;\n"); + } + append(&out, "Interactive_Style file_info_style;\n"); + } + struct_end(&out); + + { + append(&out, + "inline u32*\n" + "style_index_by_tag(Style_Main_Data *s, u32 tag){\n" + "u32 *result = 0;\n" + "switch (tag){\n"); + + i32 count = ArrayCount(bar_style_fields); + for (i32 i = 0; i < count; ++i){ + char *tag = make_style_tag(bar_style_fields[i]); + append(&out, "case Stag_"); + append(&out, tag); + append(&out, ": result = &s->file_info_style."); + append(&out, bar_style_fields[i]); + append(&out, "_color; break;\n"); + } + + count = ArrayCount(main_style_fields); + for (i32 i = 0; i < count; ++i){ + char *tag = make_style_tag(main_style_fields[i]); + append(&out, "case Stag_"); + append(&out, tag); + append(&out, ": result = &s->"); + append(&out, main_style_fields[i]); + append(&out, "_color; break;\n"); + } + + append(&out, + "}\n" + "return(result);\n" + "}\n\n"); } - if (begin_file_out(&context, filename_4ed, &out)){ - - struct_begin(&out, "Interactive_Style"); - { - int32_t count = ArrayCount(bar_style_fields); - for (int32_t i = 0; i < count; ++i){ - append(&out, "u32 "); - append(&out, bar_style_fields[i]); - append(&out, "_color;\n"); - } - } - struct_end(&out); - - struct_begin(&out, "Style_Main_Data"); - { - int32_t count = ArrayCount(main_style_fields); - for (int32_t i = 0; i < count; ++i){ - append(&out, "u32 "); - append(&out, main_style_fields[i]); - append(&out, "_color;\n"); - } - append(&out, "Interactive_Style file_info_style;\n"); - } - struct_end(&out); - - { - append(&out, - "inline u32*\n" - "style_index_by_tag(Style_Main_Data *s, u32 tag){\n" - "u32 *result = 0;\n" - "switch (tag){\n"); - - int32_t count = ArrayCount(bar_style_fields); - for (int32_t i = 0; i < count; ++i){ - char *tag = make_style_tag(bar_style_fields[i]); - append(&out, "case Stag_"); - append(&out, tag); - append(&out, ": result = &s->file_info_style."); - append(&out, bar_style_fields[i]); - append(&out, "_color; break;\n"); - free(tag); - } - - count = ArrayCount(main_style_fields); - for (int32_t i = 0; i < count; ++i){ - char *tag = make_style_tag(main_style_fields[i]); - append(&out, "case Stag_"); - append(&out, tag); - append(&out, ": result = &s->"); - append(&out, main_style_fields[i]); - append(&out, "_color; break;\n"); - free(tag); - } - - append(&out, - "}\n" - "return(result);\n" - "}\n\n"); - } - - end_file_out(context); - } + end_file_out(filename_4ed, &out); - free(out.str); + fm_end_temp(temp); } ////////////////////////////////////////////////////////////////////////////////////////////////// @@ -324,16 +309,16 @@ generate_style(){ // Meta Parse Rules // -static void -print_function_body_code(String *out, Parse_Context *context, int32_t start){ +internal void +print_function_body_code(String *out, Parse_Context *context, i32 start){ String pstr = {0}, lexeme = {0}; Cpp_Token *token = 0; - int32_t do_print = 0; - int32_t nest_level = 0; - int32_t finish = false; - int32_t do_whitespace_print = false; - int32_t is_first = true; + i32 do_print = 0; + i32 nest_level = 0; + i32 finish = false; + i32 do_whitespace_print = false; + i32 is_first = true; for (; (token = get_token(context)) != 0; get_next_token(context)){ if (do_whitespace_print){ @@ -378,30 +363,26 @@ print_function_body_code(String *out, Parse_Context *context, int32_t start){ } } -typedef struct App_API_Name{ +struct App_API_Name{ String macro; String public_name; -} App_API_Name; +}; -typedef struct App_API{ +struct App_API{ App_API_Name *names; -} App_API; +}; -static App_API -allocate_app_api(int32_t count){ +internal App_API +allocate_app_api(i32 count){ App_API app_api = {0}; app_api.names = fm_push_array(App_API_Name, count); memset(app_api.names, 0, sizeof(App_API_Name)*count); return(app_api); } -static void +internal void generate_custom_headers(){ - META_BEGIN(); - - int32_t size = (512 << 20); - void *mem = malloc(size); - memset(mem, 0, size); + Temp temp = fm_begin_temp(); // NOTE(allen): Parse the customization API files static char *functions_files[] = { "4ed_api_implementation.cpp", 0 }; @@ -413,7 +394,7 @@ generate_custom_headers(){ // NOTE(allen): Compute and store variations of the function names App_API func_4ed_names = allocate_app_api(unit_custom.set.count); - for (int32_t i = 0; i < unit_custom.set.count; ++i){ + for (i32 i = 0; i < unit_custom.set.count; ++i){ String name_string = unit_custom.set.items[i].name; String *macro = &func_4ed_names.names[i].macro; String *public_name = &func_4ed_names.names[i].public_name; @@ -430,164 +411,157 @@ generate_custom_headers(){ // NOTE(allen): Output String out = str_alloc(10 << 20); - Out_Context context = {0}; // NOTE(allen): Custom API headers - if (begin_file_out(&context, OS_API_H, &out)){ - int32_t main_api_count = unit_custom.parse[0].item_count; - int32_t os_api_count = unit_custom.parse[1].item_count; - append(&out, "struct Application_Links;\n"); - - for (int32_t i = main_api_count; i < os_api_count; ++i){ - append(&out, "#define "); - append(&out, func_4ed_names.names[i].macro); - append(&out, "(n) "); - append(&out, unit_custom.set.items[i].ret); - append(&out, " n"); - append(&out, unit_custom.set.items[i].args); - append_s_char(&out, '\n'); - } - - for (int32_t i = main_api_count; i < os_api_count; ++i){ - append(&out, "typedef "); - append(&out, func_4ed_names.names[i].macro); - append_s_char(&out, '('); - append(&out, unit_custom.set.items[i].name); - append(&out, "_Function);\n"); - } - - end_file_out(context); - } - else{ - // TODO(allen): warning + i32 main_api_count = unit_custom.parse[0].item_count; + i32 os_api_count = unit_custom.parse[1].item_count; + append(&out, "struct Application_Links;\n"); + + for (i32 i = main_api_count; i < os_api_count; ++i){ + append(&out, "#define "); + append(&out, func_4ed_names.names[i].macro); + append(&out, "(n) "); + append(&out, unit_custom.set.items[i].ret); + append(&out, " n"); + append(&out, unit_custom.set.items[i].args); + append_s_char(&out, '\n'); } - if (begin_file_out(&context, API_H, &out)){ - append(&out, "struct Application_Links;\n"); - - for (int32_t i = 0; i < unit_custom.set.count; ++i){ - append(&out, "#define "); - append(&out, func_4ed_names.names[i].macro); - append(&out, "(n) "); - append(&out, unit_custom.set.items[i].ret); - append(&out, " n"); - append(&out, unit_custom.set.items[i].args); - append_s_char(&out, '\n'); - } - - for (int32_t i = 0; i < unit_custom.set.count; ++i){ - append(&out, "typedef "); - append(&out, func_4ed_names.names[i].macro); - append_s_char(&out, '('); - append(&out, unit_custom.set.items[i].name); - append(&out, "_Function);\n"); - } - - append(&out, "struct Application_Links{\n"); - - - append(&out, "#if defined(ALLOW_DEP_4CODER)\n"); - for (int32_t i = 0; i < unit_custom.set.count; ++i){ - append(&out, unit_custom.set.items[i].name); - append(&out, "_Function *"); - append(&out, func_4ed_names.names[i].public_name); - append(&out, ";\n"); - } - - append(&out, "#else\n"); - - for (int32_t i = 0; i < unit_custom.set.count; ++i){ - append(&out, unit_custom.set.items[i].name); - append(&out, "_Function *"); - append(&out, func_4ed_names.names[i].public_name); - append(&out, "_;\n"); - } - append(&out, "#endif\n"); - - append(&out, - "void *memory;\n" - "int32_t memory_size;\n" - "void *cmd_context;\n" - "void *system_links;\n" - "void *current_coroutine;\n" - "int32_t type_coroutine;\n" - "};\n"); - - append(&out, "#define FillAppLinksAPI(app_links) do{"); - for (int32_t i = 0; i < unit_custom.set.count; ++i){ - append(&out, "\\\napp_links->"); - append(&out, func_4ed_names.names[i].public_name); - append(&out, "_ = "); - append(&out, unit_custom.set.items[i].name); - append_s_char(&out, ';'); - } - append(&out, "} while(false)\n"); - - append(&out, "#if defined(ALLOW_DEP_4CODER)\n"); - for (int32_t use_dep = 1; use_dep >= 0; --use_dep){ - for (int32_t i = 0; i < unit_custom.set.count; ++i){ - Argument_Breakdown breakdown = unit_custom.set.items[i].breakdown; - String ret = unit_custom.set.items[i].ret; - String public_name = func_4ed_names.names[i].public_name; - - append(&out, "static inline "); - append(&out, ret); - append(&out, " "); - append(&out, public_name); - - append(&out, "("); - for (int32_t j = 0; j < breakdown.count; ++j){ - append(&out, breakdown.args[j].param_string); - if (j+1 != breakdown.count){ - append(&out, ", "); - } - } - append(&out, "){"); - - if (match_ss(ret, make_lit_string("void"))){ - append(&out, "("); - } - else{ - append(&out, "return("); - } - - append(&out, "app->"); - append(&out, public_name); - if (!use_dep){ - append(&out, "_"); - } - - append(&out, "("); - for (int32_t j = 0; j < breakdown.count; ++j){ - append(&out, breakdown.args[j].param_name); - if (j+1 != breakdown.count){ - append(&out, ", "); - } - } - append(&out, ")"); - - append(&out, ");}\n"); - } - if (use_dep == 1){ - append(&out, "#else\n"); - } - } - append(&out, "#endif\n"); - - end_file_out(context); - } - else{ - // TODO(allen): warning + for (i32 i = main_api_count; i < os_api_count; ++i){ + append(&out, "typedef "); + append(&out, func_4ed_names.names[i].macro); + append_s_char(&out, '('); + append(&out, unit_custom.set.items[i].name); + append(&out, "_Function);\n"); } - META_FINISH(); + end_file_out(OS_API_H, &out); + + append(&out, "struct Application_Links;\n"); + + for (i32 i = 0; i < unit_custom.set.count; ++i){ + append(&out, "#define "); + append(&out, func_4ed_names.names[i].macro); + append(&out, "(n) "); + append(&out, unit_custom.set.items[i].ret); + append(&out, " n"); + append(&out, unit_custom.set.items[i].args); + append_s_char(&out, '\n'); + } + + for (i32 i = 0; i < unit_custom.set.count; ++i){ + append(&out, "typedef "); + append(&out, func_4ed_names.names[i].macro); + append_s_char(&out, '('); + append(&out, unit_custom.set.items[i].name); + append(&out, "_Function);\n"); + } + + append(&out, "struct Application_Links{\n"); + + + append(&out, "#if defined(ALLOW_DEP_4CODER)\n"); + for (i32 i = 0; i < unit_custom.set.count; ++i){ + append(&out, unit_custom.set.items[i].name); + append(&out, "_Function *"); + append(&out, func_4ed_names.names[i].public_name); + append(&out, ";\n"); + } + + append(&out, "#else\n"); + + for (i32 i = 0; i < unit_custom.set.count; ++i){ + append(&out, unit_custom.set.items[i].name); + append(&out, "_Function *"); + append(&out, func_4ed_names.names[i].public_name); + append(&out, "_;\n"); + } + append(&out, "#endif\n"); + + append(&out, + "void *memory;\n" + "int32_t memory_size;\n" + "void *cmd_context;\n" + "void *system_links;\n" + "void *current_coroutine;\n" + "int32_t type_coroutine;\n" + "};\n"); + + append(&out, "#define FillAppLinksAPI(app_links) do{"); + for (i32 i = 0; i < unit_custom.set.count; ++i){ + append(&out, "\\\napp_links->"); + append(&out, func_4ed_names.names[i].public_name); + append(&out, "_ = "); + append(&out, unit_custom.set.items[i].name); + append_s_char(&out, ';'); + } + append(&out, "} while(false)\n"); + + append(&out, "#if defined(ALLOW_DEP_4CODER)\n"); + for (i32 use_dep = 1; use_dep >= 0; --use_dep){ + for (i32 i = 0; i < unit_custom.set.count; ++i){ + Argument_Breakdown breakdown = unit_custom.set.items[i].breakdown; + String ret = unit_custom.set.items[i].ret; + String public_name = func_4ed_names.names[i].public_name; + + append(&out, "static inline "); + append(&out, ret); + append(&out, " "); + append(&out, public_name); + + append(&out, "("); + for (i32 j = 0; j < breakdown.count; ++j){ + append(&out, breakdown.args[j].param_string); + if (j+1 != breakdown.count){ + append(&out, ", "); + } + } + append(&out, "){"); + + if (match_ss(ret, make_lit_string("void"))){ + append(&out, "("); + } + else{ + append(&out, "return("); + } + + append(&out, "app->"); + append(&out, public_name); + if (!use_dep){ + append(&out, "_"); + } + + append(&out, "("); + for (i32 j = 0; j < breakdown.count; ++j){ + append(&out, breakdown.args[j].param_name); + if (j+1 != breakdown.count){ + append(&out, ", "); + } + } + append(&out, ")"); + + append(&out, ");}\n"); + } + if (use_dep == 1){ + append(&out, "#else\n"); + } + } + append(&out, "#endif\n"); + + end_file_out(API_H, &out); + + fm_end_temp(temp); } int main(int argc, char **argv){ + META_BEGIN(); + fm_init_system(); generate_keycode_enum(); generate_style(); generate_custom_headers(); + + META_FINISH(); } // BOTTOM diff --git a/meta/4ed_out_context.cpp b/meta/4ed_out_context.cpp index 7d0831b5..c3950294 100644 --- a/meta/4ed_out_context.cpp +++ b/meta/4ed_out_context.cpp @@ -12,60 +12,17 @@ #if !defined(OUT_CONTEXT_4CODER) #define OUT_CONTEXT_4CODER -typedef struct Out_Context{ - char out_directory_space[256]; - String out_directory; - FILE *file; - String *str; -} Out_Context; - -static void -set_context_directory(Out_Context *context, char *dst_directory){ - context->out_directory = make_fixed_width_string(context->out_directory_space); - copy_sc(&context->out_directory, dst_directory); -} - -static int32_t -begin_file_out(Out_Context *out_context, char *filename, String *out){ - char str_space[512]; - String name = make_fixed_width_string(str_space); - if (out_context->out_directory.size > 0){ - append_ss(&name, out_context->out_directory); - append_sc(&name, "/"); +internal void +end_file_out(char *out_file, String *out_data){ + FILE *file = fopen(out_file, "wb"); + if (file != 0){ + fwrite(out_data->str, 1, out_data->size, file); + fclose(file); } - append_sc(&name, filename); - terminate_with_null(&name); - - int32_t r = 0; - out_context->file = fopen(name.str, "wb"); - out_context->str = out; - out->size = 0; - if (out_context->file){ - r = 1; + else{ + fprintf(stdout, "Could not open output file %s\n", out_file); } - - return(r); -} - -static void -dump_file_out(Out_Context out_context){ - fwrite(out_context.str->str, 1, out_context.str->size, out_context.file); - out_context.str->size = 0; -} - -static void -end_file_out(Out_Context out_context){ - dump_file_out(out_context); - fclose(out_context.file); -} - -static String -make_out_string(int32_t x){ - String str; - str.size = 0; - str.memory_size = x; - str.str = (char*)malloc(x); - return(str); + out_data->size = 0; } #endif diff --git a/site/4ed_sitegen.cpp b/site/4ed_sitegen.cpp index c5073879..ee1ebaf0 100644 --- a/site/4ed_sitegen.cpp +++ b/site/4ed_sitegen.cpp @@ -91,28 +91,15 @@ print_function_body_code(String *out, Parse_Context *context, i32 start){ static void do_html_output(Document_System *doc_system, char *dst_directory, Abstract_Item *doc){ - // NOTE(allen): Output - i32 out_size = 10 << 20; - void *mem = malloc(out_size); - Assert(mem != 0); - String out = make_string_cap(mem, 0, out_size); + String out = make_string_cap(fm__push(10 << 20), 0, 10 << 20); + Assert(out.str != 0); - Out_Context context = {0}; - set_context_directory(&context, dst_directory); - - // Output Docs - char space[256]; - if (doc_get_link_string(doc, space, sizeof(space))){ - if (begin_file_out(&context, space, &out)){ - generate_document_html(&out, doc_system, doc); - end_file_out(context); - } - else{ - fprintf(stderr, "Failed to open %s\n", space); - } + char doc_link[256]; + if (doc_get_link_string(doc, doc_link, sizeof(doc_link))){ + generate_document_html(&out, doc_system, doc); + char *name = fm_str(dst_directory, "/", doc_link); + end_file_out(name, &out); } - - free(mem); } // TODO(allen): replace the documentation declaration system with a straight up enriched text system diff --git a/string/4coder_string_build_num.txt b/string/4coder_string_build_num.txt index 5e38390b..ddb1bfd4 100644 --- a/string/4coder_string_build_num.txt +++ b/string/4coder_string_build_num.txt @@ -1,5 +1,5 @@ 1 0 -99 +100 diff --git a/string/4ed_string_builder.cpp b/string/4ed_string_builder.cpp index 3544e457..7055bd72 100644 --- a/string/4ed_string_builder.cpp +++ b/string/4ed_string_builder.cpp @@ -192,252 +192,248 @@ int main(){ } } - // NOTE(allen): Output - String out = str_alloc(10 << 20); - Out_Context context = {0}; - // NOTE(allen): String Library - if (begin_file_out(&context, GENERATED_FILE, &out)){ - Cpp_Token *token = 0; - i32 start = 0; + String out = str_alloc(10 << 20); + + Cpp_Token *token = 0; + i32 start = 0; + + Parse parse = string_unit.parse[0]; + Parse_Context pcontext = setup_parse_context(parse); + + for (; (token = get_token(&pcontext)) != 0; get_next_token(&pcontext)){ + if (!(token->flags & CPP_TFLAG_PP_BODY) && + token->type == CPP_TOKEN_IDENTIFIER){ + String lexeme = get_lexeme(*token, pcontext.data); + if (match(lexeme, "FSTRING_BEGIN")){ + start = token->start + token->size; + break; + } + } + } + + append(&out, "/*\n"); + + append(&out, GENERATED_FILE " - Version "V_MAJ"."V_MIN"."); + append_int_to_str(&out, build_number); + append(&out, "\n"); + + append(&out, STANDARD_DISCLAIMER); + append(&out, + "To include implementation: #define FSTRING_IMPLEMENTATION\n" + "To use in C mode: #define FSTRING_C\n"); + + append(&out, "*/\n"); + + String pstr = {0}; + i32 do_whitespace_print = true; + + for(;(token = get_next_token(&pcontext)) != 0;){ + if (do_whitespace_print){ + pstr = str_start_end(pcontext.data, start, token->start); + append(&out, pstr); + } + else{ + do_whitespace_print = true; + } - Parse parse = string_unit.parse[0]; - Parse_Context pcontext = setup_parse_context(parse); + String lexeme = get_lexeme(*token, pcontext.data); - for (; (token = get_token(&pcontext)) != 0; get_next_token(&pcontext)){ - if (!(token->flags & CPP_TFLAG_PP_BODY) && - token->type == CPP_TOKEN_IDENTIFIER){ - String lexeme = get_lexeme(*token, pcontext.data); - if (match(lexeme, "FSTRING_BEGIN")){ - start = token->start + token->size; - break; + i32 do_print = true; + if (match(lexeme, "FSTRING_DECLS")){ + append(&out, "#if !defined(FCODER_STRING_H)\n#define FCODER_STRING_H\n\n"); + do_print = false; + + local_persist i32 RETURN_PADDING = 16; + local_persist i32 SIG_PADDING = 35; + + for (i32 j = 0; j < string_unit.set.count; ++j){ + char line_[2048]; + String line = make_fixed_width_string(line_); + Item_Node *item = string_unit.set.items + j; + + if (item->t == Item_Function){ + append (&line, item->ret); + append_padding (&line, ' ', SIG_PADDING); + append (&line, item->name); + append (&line, item->args); + append (&line, ";\n"); + } + else if (item->t == Item_Macro){ + append (&line, "#ifndef "); + append_padding (&line, ' ', 10); + append (&line, item->name); + append_s_char (&line, '\n'); + + append (&line, "# define "); + append_padding (&line, ' ', 10); + append (&line, item->name); + append (&line, item->args); + append_s_char (&line, ' '); + append (&line, item->body); + append_s_char (&line, '\n'); + + append (&line, "#endif"); + append_s_char (&line, '\n'); + } + else{ + InvalidCodePath; + } + + append(&out, line); + } + + append(&out, "\n#endif\n"); + + // NOTE(allen): C++ overload definitions + append(&out, "\n#if !defined(FSTRING_C) && !defined(FSTRING_GUARD)\n\n"); + + for (i32 j = 0; j < string_unit.set.count; ++j){ + char line_space[2048]; + String line = make_fixed_width_string(line_space); + + Item_Node *item = &string_unit.set.items[j]; + + if (item->t == Item_Function){ + String cpp_name = item->cpp_name; + if (cpp_name.str != 0){ + Argument_Breakdown breakdown = item->breakdown; + + append (&line, item->ret); + append_padding(&line, ' ', SIG_PADDING); + append (&line, cpp_name); + append (&line, item->args); + if (match(item->ret, "void")){ + append(&line, "{("); + } + else{ + append(&line, "{return("); + } + append (&line, item->name); + append_s_char(&line, '('); + + if (breakdown.count > 0){ + for (i32 i = 0; i < breakdown.count; ++i){ + if (i != 0){ + append_s_char(&line, ','); + } + append(&line, breakdown.args[i].param_name); + } + } + else{ + append(&line, "void"); + } + + append(&line, "));}\n"); + + append(&out, line); + } + } + } + + append(&out, "\n#endif\n"); + } + + else if (match(lexeme, "API_EXPORT_MACRO")){ + token = get_next_token(&pcontext); + if (token && token->type == CPP_TOKEN_COMMENT){ + token = get_next_token(&pcontext); + if (token && token->type == CPP_PP_DEFINE){ + for (;(token = get_next_token(&pcontext)) != 0;){ + if (!(token->flags & CPP_TFLAG_PP_BODY)){ + break; + } + } + if (token != 0){ + get_prev_token(&pcontext); + } + do_print = false; + do_whitespace_print = false; } } } - append(&out, "/*\n"); + else if (match(lexeme, "API_EXPORT") || match(lexeme, "API_EXPORT_INLINE")){ + if (!(token->flags & CPP_TFLAG_PP_BODY)){ + if (match(lexeme, "API_EXPORT_INLINE")){ + append(&out, "#if !defined(FSTRING_GUARD)\n"); + } + else{ + append(&out, "#if defined(FSTRING_IMPLEMENTATION)\n"); + } + print_function_body_code(&out, &pcontext, start); + append(&out, "\n#endif"); + do_print = false; + } + } - append(&out, GENERATED_FILE " - Version "V_MAJ"."V_MIN"."); - append_int_to_str(&out, build_number); - append(&out, "\n"); + else if (match(lexeme, "CPP_NAME")){ + Cpp_Token *token_start = token; + i32 has_cpp_name = false; + + token = get_next_token(&pcontext); + if (token && token->type == CPP_TOKEN_PARENTHESE_OPEN){ + token = get_next_token(&pcontext); + if (token && token->type == CPP_TOKEN_IDENTIFIER){ + token = get_next_token(&pcontext); + if (token && token->type == CPP_TOKEN_PARENTHESE_CLOSE){ + has_cpp_name = true; + do_print = false; + } + } + } + + if (!has_cpp_name){ + token = set_token(&pcontext, token_start); + } + } - append(&out, STANDARD_DISCLAIMER); - append(&out, - "To include implementation: #define FSTRING_IMPLEMENTATION\n" - "To use in C mode: #define FSTRING_C\n"); + else if (token->type == CPP_TOKEN_COMMENT){ + if (check_and_fix_docs(&lexeme)){ + do_print = false; + } + } - append(&out, "*/\n"); + else if (token->type == CPP_PP_INCLUDE){ + token = get_next_token(&pcontext); + if (token && token->type == CPP_PP_INCLUDE_FILE){ + lexeme = get_lexeme(*token, pcontext.data); + lexeme.size -= 2; + lexeme.str += 1; + + char space[512]; + String str = make_fixed_width_string(space); + append(&str, lexeme); + terminate_with_null(&str); + String dump = file_dump(str.str); + if (dump.str){ + append(&out, dump); + } + else{ + lexeme.size += 2; + lexeme.str -= 1; + append(&out, "#error Could not find "); + append(&out, lexeme); + append(&out, "\n"); + } + free(dump.str); + } + + do_print = false; + } - String pstr = {0}; - i32 do_whitespace_print = true; - - for(;(token = get_next_token(&pcontext)) != 0;){ - if (do_whitespace_print){ - pstr = str_start_end(pcontext.data, start, token->start); + if ((token = get_token(&pcontext)) != 0){ + if (do_print){ + pstr = get_lexeme(*token, pcontext.data); append(&out, pstr); } - else{ - do_whitespace_print = true; - } - - String lexeme = get_lexeme(*token, pcontext.data); - - i32 do_print = true; - if (match(lexeme, "FSTRING_DECLS")){ - append(&out, "#if !defined(FCODER_STRING_H)\n#define FCODER_STRING_H\n\n"); - do_print = false; - - local_persist i32 RETURN_PADDING = 16; - local_persist i32 SIG_PADDING = 35; - - for (i32 j = 0; j < string_unit.set.count; ++j){ - char line_[2048]; - String line = make_fixed_width_string(line_); - Item_Node *item = string_unit.set.items + j; - - if (item->t == Item_Function){ - append (&line, item->ret); - append_padding (&line, ' ', SIG_PADDING); - append (&line, item->name); - append (&line, item->args); - append (&line, ";\n"); - } - else if (item->t == Item_Macro){ - append (&line, "#ifndef "); - append_padding (&line, ' ', 10); - append (&line, item->name); - append_s_char (&line, '\n'); - - append (&line, "# define "); - append_padding (&line, ' ', 10); - append (&line, item->name); - append (&line, item->args); - append_s_char (&line, ' '); - append (&line, item->body); - append_s_char (&line, '\n'); - - append (&line, "#endif"); - append_s_char (&line, '\n'); - } - else{ - InvalidCodePath; - } - - append(&out, line); - } - - append(&out, "\n#endif\n"); - - // NOTE(allen): C++ overload definitions - append(&out, "\n#if !defined(FSTRING_C) && !defined(FSTRING_GUARD)\n\n"); - - for (i32 j = 0; j < string_unit.set.count; ++j){ - char line_space[2048]; - String line = make_fixed_width_string(line_space); - - Item_Node *item = &string_unit.set.items[j]; - - if (item->t == Item_Function){ - String cpp_name = item->cpp_name; - if (cpp_name.str != 0){ - Argument_Breakdown breakdown = item->breakdown; - - append (&line, item->ret); - append_padding(&line, ' ', SIG_PADDING); - append (&line, cpp_name); - append (&line, item->args); - if (match(item->ret, "void")){ - append(&line, "{("); - } - else{ - append(&line, "{return("); - } - append (&line, item->name); - append_s_char(&line, '('); - - if (breakdown.count > 0){ - for (i32 i = 0; i < breakdown.count; ++i){ - if (i != 0){ - append_s_char(&line, ','); - } - append(&line, breakdown.args[i].param_name); - } - } - else{ - append(&line, "void"); - } - - append(&line, "));}\n"); - - append(&out, line); - } - } - } - - append(&out, "\n#endif\n"); - } - - else if (match(lexeme, "API_EXPORT_MACRO")){ - token = get_next_token(&pcontext); - if (token && token->type == CPP_TOKEN_COMMENT){ - token = get_next_token(&pcontext); - if (token && token->type == CPP_PP_DEFINE){ - for (;(token = get_next_token(&pcontext)) != 0;){ - if (!(token->flags & CPP_TFLAG_PP_BODY)){ - break; - } - } - if (token != 0){ - get_prev_token(&pcontext); - } - do_print = false; - do_whitespace_print = false; - } - } - } - - else if (match(lexeme, "API_EXPORT") || match(lexeme, "API_EXPORT_INLINE")){ - if (!(token->flags & CPP_TFLAG_PP_BODY)){ - if (match(lexeme, "API_EXPORT_INLINE")){ - append(&out, "#if !defined(FSTRING_GUARD)\n"); - } - else{ - append(&out, "#if defined(FSTRING_IMPLEMENTATION)\n"); - } - print_function_body_code(&out, &pcontext, start); - append(&out, "\n#endif"); - do_print = false; - } - } - - else if (match(lexeme, "CPP_NAME")){ - Cpp_Token *token_start = token; - i32 has_cpp_name = false; - - token = get_next_token(&pcontext); - if (token && token->type == CPP_TOKEN_PARENTHESE_OPEN){ - token = get_next_token(&pcontext); - if (token && token->type == CPP_TOKEN_IDENTIFIER){ - token = get_next_token(&pcontext); - if (token && token->type == CPP_TOKEN_PARENTHESE_CLOSE){ - has_cpp_name = true; - do_print = false; - } - } - } - - if (!has_cpp_name){ - token = set_token(&pcontext, token_start); - } - } - - else if (token->type == CPP_TOKEN_COMMENT){ - if (check_and_fix_docs(&lexeme)){ - do_print = false; - } - } - - else if (token->type == CPP_PP_INCLUDE){ - token = get_next_token(&pcontext); - if (token && token->type == CPP_PP_INCLUDE_FILE){ - lexeme = get_lexeme(*token, pcontext.data); - lexeme.size -= 2; - lexeme.str += 1; - - char space[512]; - String str = make_fixed_width_string(space); - append(&str, lexeme); - terminate_with_null(&str); - String dump = file_dump(str.str); - if (dump.str){ - append(&out, dump); - } - else{ - lexeme.size += 2; - lexeme.str -= 1; - append(&out, "#error Could not find "); - append(&out, lexeme); - append(&out, "\n"); - } - free(dump.str); - } - - do_print = false; - } - - if ((token = get_token(&pcontext)) != 0){ - if (do_print){ - pstr = get_lexeme(*token, pcontext.data); - append(&out, pstr); - } - start = token->start + token->size; - } + start = token->start + token->size; } - pstr = str_start_end(pcontext.data, start, parse.code.size); - append(&out, pstr); - - end_file_out(context); } + pstr = str_start_end(pcontext.data, start, parse.code.size); + append(&out, pstr); + + end_file_out(GENERATED_FILE, &out); // NOTE(allen): Publish the new file. (Would like to be able to automatically test the result before publishing). {