diff --git a/4ed_app_target.cpp b/4ed_app_target.cpp index a918aa8d..33e3fc28 100644 --- a/4ed_app_target.cpp +++ b/4ed_app_target.cpp @@ -9,9 +9,7 @@ // TOP -// TODO(allen): get away from assert.h // TODO(allen): get away from string.h -#include #include #include "4ed_defines.h" diff --git a/4ed_buffer.cpp b/4ed_buffer.cpp index 8355b75e..95409c6e 100644 --- a/4ed_buffer.cpp +++ b/4ed_buffer.cpp @@ -67,13 +67,13 @@ buffer_quick_unsort_cursors(Cursor_With_Index *positions, i32 start, i32 pivot){ inline void buffer_sort_cursors(Cursor_With_Index *positions, i32 count){ - assert(count > 0); + Assert(count > 0); buffer_quick_sort_cursors(positions, 0, count-1); } inline void buffer_unsort_cursors(Cursor_With_Index *positions, i32 count){ - assert(count > 0); + Assert(count > 0); buffer_quick_unsort_cursors(positions, 0, count-1); } @@ -460,9 +460,9 @@ buffer_replace_range(Gap_Buffer *buffer, i32 start, i32 end, char *str, i32 len, i32 result = 0; i32 move_size = 0; - assert(0 <= start); - assert(start <= end); - assert(end <= size); + Assert(0 <= start); + Assert(start <= end); + Assert(end <= size); *shift_amount = (len - (end - start)); if (*shift_amount + size <= buffer->max){ @@ -484,8 +484,8 @@ buffer_replace_range(Gap_Buffer *buffer, i32 start, i32 end, char *str, i32 len, buffer->size1 = start + len; buffer->gap_size -= *shift_amount; - assert(buffer->size1 + buffer->size2 == size + *shift_amount); - assert(buffer->size1 + buffer->gap_size + buffer->size2 == buffer->max); + Assert(buffer->size1 + buffer->size2 == size + *shift_amount); + Assert(buffer->size1 + buffer->gap_size + buffer->size2 == buffer->max); } else{ *request_amount = l_round_up_i32(2*(*shift_amount + size), 4 << 10); @@ -531,7 +531,7 @@ buffer_edit_provide_memory(Gap_Buffer *buffer, void *new_data, i32 new_max){ i32 size = buffer_size(buffer); i32 new_gap_size = new_max - size; - assert(new_max >= size); + Assert(new_max >= size); memcpy(new_data, buffer->data, buffer->size1); memcpy((char*)new_data + buffer->size1 + new_gap_size, buffer->data + buffer->size1 + buffer->gap_size, buffer->size2); @@ -569,7 +569,7 @@ buffer_convert_out(Gap_Buffer *buffer, char *dest, i32 max){ Gap_Buffer_Stream stream = {0}; i32 i = 0; i32 size = buffer_size(buffer); - assert(size + buffer->line_count <= max); + Assert(size + buffer->line_count <= max); i32 pos = 0; if (buffer_stringify_loop(&stream, buffer, 0, size)){ @@ -578,7 +578,7 @@ buffer_convert_out(Gap_Buffer *buffer, char *dest, i32 max){ i32 chunk_size = stream.end - i; i32 out_size = 0; i32 result = eol_convert_out(dest + pos, max - pos, stream.data + i, chunk_size, &out_size); - assert(result); + Assert(result); i = stream.end; pos += out_size; still_looping = buffer_stringify_next(&stream); @@ -594,9 +594,9 @@ buffer_count_newlines(Gap_Buffer *buffer, i32 start, i32 end){ i32 i = start; i32 count = 0; - assert(0 <= start); - assert(start <= end); - assert(end <= buffer_size(buffer)); + Assert(0 <= start); + Assert(start <= end); + Assert(end <= buffer_size(buffer)); if (buffer_stringify_loop(&stream, buffer, i, end)){ b32 still_looping = 0; @@ -647,7 +647,7 @@ buffer_measure_starts(Buffer_Measure_Starts *state, Gap_Buffer *buffer){ }while(still_looping); } - assert(i == size); + Assert(i == size); if (start_ptr == start_end){ goto buffer_measure_starts_widths_end; @@ -671,7 +671,7 @@ internal void buffer_measure_character_starts(System_Functions *system, Render_Font *font, Gap_Buffer *buffer, i32 *character_starts, i32 mode, i32 virtual_white){ PRFL_FUNC_GROUP(); - assert(mode == 0); + Assert(mode == 0); Gap_Buffer_Stream stream = {0}; i32 i = 0; @@ -726,7 +726,7 @@ buffer_measure_character_starts(System_Functions *system, Render_Font *font, Gap }while(still_looping); } - assert(line_index-1 == buffer->line_count); + Assert(line_index-1 == buffer->line_count); } enum{ @@ -902,7 +902,7 @@ buffer_measure_wrap_y(Buffer_Measure_Wrap_State *S_ptr, Buffer_Measure_Wrap_Para ++S.current_wrap_index; params.wrap_line_index[S.line_index++] = S.current_wrap_index; - assert(S.line_index-1 == params.buffer->line_count); + Assert(S.line_index-1 == params.buffer->line_count); S_stop.status = BLStatus_Finished; DrReturn(S_stop); @@ -917,10 +917,10 @@ buffer_remeasure_starts(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li i32 *starts = buffer->line_starts; i32 line_count = buffer->line_count; - assert(0 <= line_start); - assert(line_start <= line_end); - assert(line_end < line_count); - assert(line_count + line_shift <= buffer->line_max); + Assert(0 <= line_start); + Assert(line_start <= line_end); + Assert(line_end < line_count); + Assert(line_count + line_shift <= buffer->line_max); ++line_end; @@ -981,19 +981,19 @@ buffer_remeasure_starts(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li } buffer_remeasure_starts_end:; - assert(line_count >= 1); + Assert(line_count >= 1); buffer->line_count = new_line_count; } internal void buffer_remeasure_character_starts(System_Functions *system, Render_Font *font, Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 line_shift, i32 *character_starts, i32 mode, i32 virtual_whitespace){ - assert(mode == 0); + Assert(mode == 0); i32 new_line_count = buffer->line_count; - assert(0 <= line_start); - assert(line_start <= line_end); - assert(line_end < new_line_count - line_shift); + Assert(0 <= line_start); + Assert(line_start <= line_end); + Assert(line_end < new_line_count - line_shift); ++line_end; @@ -1066,7 +1066,7 @@ buffer_remeasure_character_starts(System_Functions *system, Render_Font *font, G }while(still_looping); } - assert(line_i >= new_line_end); + Assert(line_i >= new_line_end); buffer_remeasure_character_starts_end:; @@ -1088,9 +1088,9 @@ buffer_remeasure_wrap_y(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li f32 *wraps, f32 font_height, f32 *adv, f32 max_width){ i32 new_line_count = buffer->line_count; - assert(0 <= line_start); - assert(line_start <= line_end); - assert(line_end < new_line_count - line_shift); + Assert(0 <= line_start); + Assert(line_start <= line_end); + Assert(line_end < new_line_count - line_shift); ++line_end; @@ -1185,7 +1185,7 @@ binary_search(i32 *array, i32 value, i32 l_bound, i32 u_bound){ else{ break; } - assert(start < end); + Assert(start < end); if (start == end - 1){ i = start; break; @@ -1197,11 +1197,11 @@ binary_search(i32 *array, i32 value, i32 l_bound, i32 u_bound){ inline i32 buffer_get_line_index_range(Gap_Buffer *buffer, i32 pos, i32 l_bound, i32 u_bound){ - assert(0 <= l_bound); - assert(l_bound <= u_bound); - assert(u_bound <= buffer->line_count); + Assert(0 <= l_bound); + Assert(l_bound <= u_bound); + Assert(u_bound <= buffer->line_count); - assert(buffer->line_starts != 0); + Assert(buffer->line_starts != 0); i32 i = binary_search(buffer->line_starts, pos, l_bound, u_bound); return(i); @@ -1666,8 +1666,8 @@ buffer_invert_edit_shift(Gap_Buffer *buffer, Buffer_Edit edit, Buffer_Edit *inve i32 *str_pos, i32 max, i32 shift_amount){ i32 pos = *str_pos; i32 len = edit.end - edit.start; - assert(pos >= 0); - assert(pos + len <= max); + Assert(pos >= 0); + Assert(pos + len <= max); *str_pos = pos + len; inverse->str_start = pos; @@ -2019,7 +2019,7 @@ buffer_render_data(Buffer_Render_State *S_ptr, Buffer_Render_Params params, f32 } *params.count = (i32)(S.write.item - params.items); - assert(*params.count <= params.max); + Assert(*params.count <= params.max); S_stop.status = BLStatus_Finished; DrReturn(S_stop); diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index 8f850eb8..65b1059d 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -435,7 +435,7 @@ view_compute_cursor(System_Functions *system, View *view, Buffer_Seek seek, b32 first_wrap_determination = 0; } else{ - assert(stop.pos == wrap_unit_end); + Assert(stop.pos == wrap_unit_end); do_wrap = 1; ++wrap_array_index; wrap_unit_end = file->state.wrap_positions[wrap_array_index]; @@ -6005,7 +6005,7 @@ draw_file_loaded(System_Functions *system, View *view, i32_Rect rect, b32 is_act first_wrap_determination = 0; } else{ - assert(stop.pos == wrap_unit_end); + Assert(stop.pos == wrap_unit_end); do_wrap = 1; ++wrap_array_index; wrap_unit_end = file->state.wrap_positions[wrap_array_index]; diff --git a/meta/4ed_build.cpp b/meta/4ed_build.cpp index 6dc29981..b602f454 100644 --- a/meta/4ed_build.cpp +++ b/meta/4ed_build.cpp @@ -591,7 +591,7 @@ package(char *cdir){ char *tier = tiers[tier_index]; u32 flags = base_flags | tier_flags[tier_index]; - String_Temp temp = fm_begin_temp(); + Temp temp = fm_begin_temp(); char *tier_package_root = fm_str(base_package_root, "_", tier); for (u32 arch = 0; arch < Arch_COUNT; ++arch){ char *par_dir = fm_str(tier_package_root, "_", arch_names[arch]); @@ -645,7 +645,7 @@ package(char *cdir){ // NOTE(allen): power { - String_Temp temp = fm_begin_temp(); + Temp temp = fm_begin_temp(); char *pack_power_par_dir = fm_str("../current_dist_power"); char *pack_power_dir = fm_str(pack_power_par_dir, "/power"); diff --git a/meta/4ed_file_moving.h b/meta/4ed_file_moving.h index 7733e1a4..3965122d 100644 --- a/meta/4ed_file_moving.h +++ b/meta/4ed_file_moving.h @@ -75,10 +75,15 @@ internal char **fm_prepare_list_internal(char **l1, ...); internal char **fm_list_one_item(char *item); +internal void *fm__push(umem size); +internal void fm_align(); + +#define fm_push_array(T,c) (T*)fm__push(sizeof(T)*c) + // File System Navigation -typedef umem String_Temp; -internal String_Temp fm_begin_temp(); -internal void fm_end_temp(String_Temp temp); +typedef umem Temp; +internal Temp fm_begin_temp(); +internal void fm_end_temp(Temp temp); internal i32 fm_get_current_directory(char *buffer, i32 max); @@ -182,17 +187,18 @@ umem fm_arena_max = 0; internal void fm__init_memory(){ + Assert(fm_arena_memory == 0); fm_arena_max = MB(16); fm_arena_memory = (char*)malloc(fm_arena_max); } -internal String_Temp +internal Temp fm_begin_temp(){ return(fm_arena_pos); } internal void -fm_end_temp(String_Temp temp){ +fm_end_temp(Temp temp){ fm_arena_pos = temp; } @@ -208,6 +214,11 @@ fm__push(umem size){ return(result); } +internal void +fm_align(){ + fm_arena_pos = (fm_arena_pos+7)&(~7); +} + #if defined(IS_WINDOWS) typedef uint32_t DWORD; diff --git a/meta/4ed_meta_keywords.h b/meta/4ed_meta_keywords.h new file mode 100644 index 00000000..b6f54ba8 --- /dev/null +++ b/meta/4ed_meta_keywords.h @@ -0,0 +1,29 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 10.07.2017 + * + * Standard meta-parser configuration for 4coder code. + * + */ + +// TOP + +#if !defined(FRED_META_KEYWORDS_H) +#define FRED_META_KEYWORDS_H + +internal Meta_Keywords meta_keywords[] = { + {make_lit_string("API_EXPORT") , Item_Function } , + {make_lit_string("API_EXPORT_INLINE") , Item_Function } , + {make_lit_string("API_EXPORT_MACRO") , Item_Macro } , + {make_lit_string("CPP_NAME") , Item_CppName } , + {make_lit_string("TYPEDEF") , Item_Typedef } , + {make_lit_string("STRUCT") , Item_Struct } , + {make_lit_string("UNION") , Item_Union } , + {make_lit_string("ENUM") , Item_Enum } , +}; + +#endif + +// BOTTOM + diff --git a/meta/4ed_meta_parser.cpp b/meta/4ed_meta_parser.cpp index 0c0f618d..25b5527a 100644 --- a/meta/4ed_meta_parser.cpp +++ b/meta/4ed_meta_parser.cpp @@ -9,37 +9,37 @@ // TOP -#if !defined(META_PARSER_CPP) -#define META_PARSER_CPP +#if !defined(FRED_META_PARSER_CPP) +#define FRED_META_PARSER_CPP -typedef struct Parse_Context{ +struct Parse_Context{ Cpp_Token *token_s; Cpp_Token *token_e; Cpp_Token *token; char *data; -} Parse_Context; +}; -typedef struct Argument{ +struct Argument{ String param_string; String param_name; -} Argument; +}; -typedef struct Argument_Breakdown{ - int32_t count; +struct Argument_Breakdown{ + i32 count; Argument *args; -} Argument_Breakdown; +}; -typedef struct Documentation{ - int32_t param_count; +struct Documentation{ + i32 param_count; String *param_name; String *param_docs; String return_doc; String main_doc; - int32_t see_also_count; + i32 see_also_count; String *see_also; -} Documentation; +}; -typedef enum Item_Type{ +enum Item_Type{ Item_Null, Item_Function, Item_CppName, @@ -50,10 +50,10 @@ typedef enum Item_Type{ Item_Enum, Item_Type_Count, #define Item_Type_User0 Item_Type_Count -} Item_Type; +}; -typedef struct Item_Node{ - int32_t t; +struct Item_Node{ + i32 t; String cpp_name; String name; @@ -72,50 +72,49 @@ typedef struct Item_Node{ Item_Node *first_child; Item_Node *next_sibling; -} Item_Node; +}; -typedef struct Item_Set{ +struct Item_Set{ Item_Node *items; - int32_t count; -} Item_Set; + i32 count; +}; -typedef struct Parse{ +struct Parse{ String code; Cpp_Token_Array tokens; - int32_t item_count; -} Parse; + i32 item_count; +}; -typedef struct Meta_Unit{ +struct Meta_Unit{ Item_Set set; - Parse *parse; - int32_t count; -} Meta_Unit; + i32 count; +}; -typedef struct Meta_Keywords{ +struct Meta_Keywords{ String key; Item_Type type; -} Meta_Keywords; +}; -typedef struct Used_Links{ +struct Used_Links{ String *strs; - int32_t count, max; -} Used_Links; + i32 count, max; +}; -static Item_Node null_item_node = {0}; +internal Item_Node null_item_node = {0}; -static String -str_start_end(char *data, int32_t start, int32_t end){ +internal String +str_start_end(char *data, i32 start, i32 end){ return(make_string(data + start, end - start)); } -static String +internal String get_lexeme(Cpp_Token token, char *code){ String str = make_string(code + token.start, token.size); return(str); } -static Parse_Context +internal Parse_Context setup_parse_context(char *data, Cpp_Token_Array array){ Parse_Context context; context.token_s = array.tokens; @@ -125,7 +124,7 @@ setup_parse_context(char *data, Cpp_Token_Array array){ return(context); } -static Parse_Context +internal Parse_Context setup_parse_context(Parse parse){ Parse_Context context; context.token_s = parse.tokens.tokens; @@ -135,7 +134,7 @@ setup_parse_context(Parse parse){ return(context); } -static Cpp_Token* +internal Cpp_Token* get_token(Parse_Context *context){ Cpp_Token *result = context->token; if (result >= context->token_e){ @@ -144,7 +143,7 @@ get_token(Parse_Context *context){ return(result); } -static Cpp_Token* +internal Cpp_Token* get_next_token(Parse_Context *context){ Cpp_Token *result = context->token+1; context->token = result; @@ -155,7 +154,7 @@ get_next_token(Parse_Context *context){ return(result); } -static Cpp_Token* +internal Cpp_Token* get_prev_token(Parse_Context *context){ Cpp_Token *result = context->token-1; if (result < context->token_s){ @@ -167,7 +166,7 @@ get_prev_token(Parse_Context *context){ return(result); } -static Cpp_Token* +internal Cpp_Token* can_back_step(Parse_Context *context){ Cpp_Token *result = context->token-1; if (result < context->token_s){ @@ -176,7 +175,7 @@ can_back_step(Parse_Context *context){ return(result); } -static Cpp_Token* +internal Cpp_Token* set_token(Parse_Context *context, Cpp_Token *token){ Cpp_Token *result = 0; if (token >= context->token_s && token < context->token_e){ @@ -186,23 +185,23 @@ set_token(Parse_Context *context, Cpp_Token *token){ return(result); } -static String -str_alloc(Partition *part, int32_t cap){ - return(make_string_cap(push_array(part, char, cap), 0, cap)); +internal String +str_alloc(i32 cap){ + return(make_string_cap(fm_push_array(char, cap), 0, cap)); } -static Item_Set -allocate_item_set(Partition *part, int32_t count){ +internal Item_Set +allocate_item_set(i32 count){ Item_Set item_set = {0}; if (count > 0){ - item_set.items = push_array(part, Item_Node, count); + item_set.items = fm_push_array(Item_Node, count); item_set.count = count; memset(item_set.items, 0, sizeof(Item_Node)*count); } return(item_set); } -static String +internal String file_dump(char *filename){ String result = {0}; FILE *file = fopen(filename, "rb"); @@ -224,7 +223,7 @@ file_dump(char *filename){ return(result); } -static Parse +internal Parse meta_lex(char *filename){ Parse result = {0}; result.code = file_dump(filename); @@ -235,19 +234,19 @@ meta_lex(char *filename){ return(result); } -static String +internal String get_first_line(String source){ String line = {0}; - int32_t pos = find_s_char(source, 0, '\n'); + i32 pos = find_s_char(source, 0, '\n'); line = substr(source, 0, pos); return(line); } -static String +internal String get_next_line(String source, String line){ String next = {0}; - int32_t pos = (int32_t)(line.str - source.str) + line.size; - int32_t start = 0; + i32 pos = (i32)(line.str - source.str) + line.size; + i32 start = 0; if (pos < source.size){ Assert(source.str[pos] == '\n'); @@ -262,9 +261,9 @@ get_next_line(String source, String line){ return(next); } -static int32_t +internal i32 is_comment(String str){ - int32_t result = 0; + i32 result = 0; if (str.size >= 2){ if (str.str[0] == '/' && str.str[1] == '/'){ @@ -283,7 +282,7 @@ typedef enum Doc_Note_Type{ HIDE_MEMBERS, } Doc_Note_Type; -static String +internal String defined_doc_notes[] = { make_lit_string("DOC_PARAM"), make_lit_string("DOC_RETURN"), @@ -293,9 +292,9 @@ defined_doc_notes[] = { make_lit_string("HIDE_MEMBERS"), }; -static int32_t +internal i32 check_and_fix_docs(String *doc_string){ - int32_t result = false; + i32 result = false; if (doc_string->size > 4){ if (doc_string->str[0] == '/'){ @@ -314,9 +313,9 @@ check_and_fix_docs(String *doc_string){ return(result); } -static int32_t +internal i32 get_doc_string_from_prev(Parse_Context *context, String *doc_string){ - int32_t result = false; + i32 result = false; if (can_back_step(context)){ Cpp_Token *prev_token = get_token(context) - 1; @@ -334,12 +333,12 @@ get_doc_string_from_prev(Parse_Context *context, String *doc_string){ return(result); } -static String -doc_parse_note(String source, int32_t *pos){ +internal String +doc_parse_note(String source, i32 *pos){ String result = {0}; - int32_t p = *pos; - int32_t start = p; + i32 p = *pos; + i32 start = p; for (; p < source.size; ++p){ if (source.str[p] == '('){ break; @@ -354,16 +353,16 @@ doc_parse_note(String source, int32_t *pos){ return(result); } -static String -doc_parse_note_string(String source, int32_t *pos){ +internal String +doc_parse_note_string(String source, i32 *pos){ String result = {0}; Assert(source.str[*pos] == '('); - int32_t p = *pos + 1; - int32_t start = p; + i32 p = *pos + 1; + i32 start = p; - int32_t nest_level = 0; + i32 nest_level = 0; for (; p < source.size; ++p){ if (source.str[p] == ')'){ @@ -388,12 +387,12 @@ doc_parse_note_string(String source, int32_t *pos){ return(result); } -static String -doc_parse_parameter(String source, int32_t *pos){ +internal String +doc_parse_parameter(String source, i32 *pos){ String result = {0}; - int32_t p = *pos; - int32_t start = p; + i32 p = *pos; + i32 start = p; for (; p < source.size; ++p){ if (source.str[p] == ','){ @@ -410,12 +409,12 @@ doc_parse_parameter(String source, int32_t *pos){ return(result); } -static String -doc_parse_last_parameter(String source, int32_t *pos){ +internal String +doc_parse_last_parameter(String source, i32 *pos){ String result = {0}; - int32_t p = *pos; - int32_t start = p; + i32 p = *pos; + i32 start = p; for (; p < source.size; ++p){ if (source.str[p] == ')'){ @@ -431,13 +430,13 @@ doc_parse_last_parameter(String source, int32_t *pos){ return(result); } -static void -perform_doc_parse(Partition *part, String doc_string, Documentation *doc){ - int32_t keep_parsing = true; - int32_t pos = 0; +internal void +perform_doc_parse(String doc_string, Documentation *doc){ + i32 keep_parsing = true; + i32 pos = 0; - int32_t param_count = 0; - int32_t see_count = 0; + i32 param_count = 0; + i32 see_count = 0; do{ String doc_note = doc_parse_note(doc_string, &pos); @@ -445,7 +444,7 @@ perform_doc_parse(Partition *part, String doc_string, Documentation *doc){ keep_parsing = false; } else{ - int32_t doc_note_type; + i32 doc_note_type; if (string_set_match(defined_doc_notes, ArrayCount(defined_doc_notes), doc_note, &doc_note_type)){ doc_parse_note_string(doc_string, &pos); @@ -459,8 +458,8 @@ perform_doc_parse(Partition *part, String doc_string, Documentation *doc){ }while(keep_parsing); if (param_count + see_count > 0){ - int32_t memory_size = sizeof(String)*(2*param_count + see_count); - doc->param_name = push_array(part, String, memory_size); + i32 memory_size = sizeof(String)*(2*param_count + see_count); + doc->param_name = fm_push_array(String, memory_size); doc->param_docs = doc->param_name + param_count; doc->see_also = doc->param_docs + param_count; @@ -468,8 +467,8 @@ perform_doc_parse(Partition *part, String doc_string, Documentation *doc){ doc->see_also_count = see_count; } - int32_t param_index = 0; - int32_t see_index = 0; + i32 param_index = 0; + i32 see_index = 0; keep_parsing = true; pos = 0; @@ -479,7 +478,7 @@ perform_doc_parse(Partition *part, String doc_string, Documentation *doc){ keep_parsing = false; } else{ - int32_t doc_note_type; + i32 doc_note_type; if (string_set_match(defined_doc_notes, ArrayCount(defined_doc_notes), doc_note, &doc_note_type)){ String doc_note_string = doc_parse_note_string(doc_string, &pos); @@ -488,7 +487,7 @@ perform_doc_parse(Partition *part, String doc_string, Documentation *doc){ case DOC_PARAM: { Assert(param_index < param_count); - int32_t param_pos = 0; + i32 param_pos = 0; String param_name = doc_parse_parameter(doc_note_string, ¶m_pos); String param_docs = doc_parse_last_parameter(doc_note_string, ¶m_pos); doc->param_name[param_index] = param_name; @@ -520,13 +519,12 @@ perform_doc_parse(Partition *part, String doc_string, Documentation *doc){ }while(keep_parsing); } -static int32_t -struct_parse(Partition *part, int32_t is_struct, - Parse_Context *context, Item_Node *top_member); +internal i32 +struct_parse(i32 is_struct, Parse_Context *context, Item_Node *top_member); -static int32_t -struct_parse_member(Partition *part, Parse_Context *context, Item_Node *member){ - int32_t result = false; +internal i32 +struct_parse_member(Parse_Context *context, Item_Node *member){ + i32 result = false; Cpp_Token *token = get_token(context); @@ -544,7 +542,7 @@ struct_parse_member(Partition *part, Parse_Context *context, Item_Node *member){ if (token){ String name = {0}; Cpp_Token *token_j = 0; - int32_t nest_level = 0; + i32 nest_level = 0; for (; (token_j = get_token(context)) > start_token; get_prev_token(context)){ if (token_j->type == CPP_TOKEN_BRACKET_CLOSE){ @@ -584,8 +582,8 @@ struct_parse_member(Partition *part, Parse_Context *context, Item_Node *member){ return(result); } -static Item_Node* -struct_parse_next_member(Partition *part, Parse_Context *context){ +internal Item_Node* +struct_parse_next_member(Parse_Context *context){ Item_Node *result = 0; Cpp_Token *token = 0; @@ -595,9 +593,9 @@ struct_parse_next_member(Partition *part, Parse_Context *context){ (token->flags & CPP_TFLAG_IS_KEYWORD)){ String lexeme = get_lexeme(*token, context->data); - if (match_ss(lexeme, make_lit_string("STRUCT"))){ - Item_Node *member = push_struct(part, Item_Node); - if (struct_parse(part, true, context, member)){ + if (match(lexeme, "STRUCT")){ + Item_Node *member = fm_push_array(Item_Node, 1); + if (struct_parse(true, context, member)){ result = member; break; } @@ -605,9 +603,9 @@ struct_parse_next_member(Partition *part, Parse_Context *context){ Assert(!"unhandled error"); } } - else if (match_ss(lexeme, make_lit_string("UNION"))){ - Item_Node *member = push_struct(part, Item_Node); - if (struct_parse(part, false, context, member)){ + else if (match(lexeme, "UNION")){ + Item_Node *member = fm_push_array(Item_Node, 1); + if (struct_parse(false, context, member)){ result = member; break; } @@ -616,8 +614,8 @@ struct_parse_next_member(Partition *part, Parse_Context *context){ } } else{ - Item_Node *member = push_struct(part, Item_Node); - if (struct_parse_member(part, context, member)){ + Item_Node *member = fm_push_array(Item_Node, 1); + if (struct_parse_member(context, member)){ result = member; break; } @@ -634,9 +632,9 @@ struct_parse_next_member(Partition *part, Parse_Context *context){ return(result); } -static int32_t -struct_parse(Partition *part, int32_t is_struct, Parse_Context *context, Item_Node *top_member){ - int32_t result = false; +internal i32 +struct_parse(i32 is_struct, Parse_Context *context, Item_Node *top_member){ + i32 result = false; Cpp_Token *start_token = get_token(context); Cpp_Token *token = 0; @@ -673,14 +671,14 @@ struct_parse(Partition *part, int32_t is_struct, Parse_Context *context, Item_No } set_token(context, token+1); - Item_Node *new_member = struct_parse_next_member(part, context); + Item_Node *new_member = struct_parse_next_member(context); if (new_member){ top_member->first_child = new_member; Item_Node *head_member = new_member; for(;;){ - new_member = struct_parse_next_member(part, context); + new_member = struct_parse_next_member(context); if (new_member){ head_member->next_sibling = new_member; head_member = new_member; @@ -715,9 +713,9 @@ struct_parse(Partition *part, int32_t is_struct, Parse_Context *context, Item_No return(result); } -static int32_t +internal i32 typedef_parse(Parse_Context *context, Item_Node *item){ - int32_t result = false; + i32 result = false; Cpp_Token *token = get_token(context); String doc_string = {0}; @@ -756,9 +754,9 @@ typedef_parse(Parse_Context *context, Item_Node *item){ return(result); } -static int32_t -enum_parse(Partition *part, Parse_Context *context, Item_Node *item){ - int32_t result = false; +internal i32 +enum_parse(Parse_Context *context, Item_Node *item){ + i32 result = false; String parent_doc_string = {0}; get_doc_string_from_prev(context, &parent_doc_string); @@ -829,7 +827,7 @@ enum_parse(Partition *part, Parse_Context *context, Item_Node *item){ } } - Item_Node *new_member = push_struct(part, Item_Node); + Item_Node *new_member = fm_push_array(Item_Node, 1); if (first_member == 0){ first_member = new_member; } @@ -866,12 +864,12 @@ enum_parse(Partition *part, Parse_Context *context, Item_Node *item){ return(result); } -static Argument_Breakdown -allocate_argument_breakdown(Partition *part, int32_t count){ +internal Argument_Breakdown +allocate_argument_breakdown(i32 count){ Argument_Breakdown breakdown = {0}; if (count > 0){ breakdown.count = count; - breakdown.args = push_array(part, Argument, count); + breakdown.args = fm_push_array(Argument, count); memset(breakdown.args, 0, sizeof(Argument)*count); } return(breakdown); @@ -882,13 +880,13 @@ Parse arguments by giving pointers to the tokens: foo(a, ... , z) ^ ^ */ -static Argument_Breakdown -parameter_parse(Partition *part, char *data, Cpp_Token *args_start_token, Cpp_Token *args_end_token){ - int32_t arg_index = 0; +internal Argument_Breakdown +parameter_parse(char *data, Cpp_Token *args_start_token, Cpp_Token *args_end_token){ + i32 arg_index = 0; Cpp_Token *arg_token = args_start_token + 1; - int32_t param_string_start = arg_token->start; + i32 param_string_start = arg_token->start; - int32_t arg_count = 1; + i32 arg_count = 1; arg_token = args_start_token; for (; arg_token < args_end_token; ++arg_token){ if (arg_token->type == CPP_TOKEN_COMMA){ @@ -896,14 +894,14 @@ parameter_parse(Partition *part, char *data, Cpp_Token *args_start_token, Cpp_To } } - Argument_Breakdown breakdown = allocate_argument_breakdown(part, arg_count); + Argument_Breakdown breakdown = allocate_argument_breakdown(arg_count); arg_token = args_start_token + 1; for (; arg_token <= args_end_token; ++arg_token){ if (arg_token->type == CPP_TOKEN_COMMA || arg_token->type == CPP_TOKEN_PARENTHESE_CLOSE){ - int32_t size = arg_token->start - param_string_start; + i32 size = arg_token->start - param_string_start; String param_string = make_string(data + param_string_start, size); param_string = chop_whitespace(param_string); breakdown.args[arg_index].param_string = param_string; @@ -912,8 +910,8 @@ parameter_parse(Partition *part, char *data, Cpp_Token *args_start_token, Cpp_To param_name_token->start > param_string_start; --param_name_token){ if (param_name_token->type == CPP_TOKEN_IDENTIFIER){ - int32_t name_start = param_name_token->start; - int32_t name_size = param_name_token->size; + i32 name_start = param_name_token->start; + i32 name_size = param_name_token->size; breakdown.args[arg_index].param_name = make_string(data + name_start, name_size); break; } @@ -935,9 +933,9 @@ Moves the context in the following way: ~~~~~~~ name( ~~~~~~~ ^ -> ^ */ -static int32_t +internal i32 function_parse_goto_name(Parse_Context *context){ - int32_t result = false; + i32 result = false; Cpp_Token *token = 0; @@ -967,9 +965,9 @@ Moves the context in the following way: ~~~~~~~ name( ~~~~~~~ /* XXX // ^ ---------------> ^ */ -static int32_t +internal i32 function_get_doc(Parse_Context *context, char *data, String *doc_string){ - int32_t result = false; + i32 result = false; Cpp_Token *token = get_token(context); String lexeme = {0}; @@ -995,9 +993,9 @@ function_get_doc(Parse_Context *context, char *data, String *doc_string){ return(result); } -static int32_t +internal i32 cpp_name_parse(Parse_Context *context, String *name){ - int32_t result = false; + i32 result = false; Cpp_Token *token = 0; Cpp_Token *token_start = get_token(context); @@ -1026,9 +1024,9 @@ Moves the context in the following way: RETTY~ name( ~~~~~~~ ) ^ ---------------> ^ */ -static int32_t -function_sig_parse(Partition *part, Parse_Context *context, Item_Node *item, String cpp_name){ - int32_t result = false; +internal i32 +function_sig_parse(Parse_Context *context, Item_Node *item, String cpp_name){ + i32 result = false; Cpp_Token *token = 0; Cpp_Token *args_start_token = 0; @@ -1051,7 +1049,7 @@ function_sig_parse(Partition *part, Parse_Context *context, Item_Node *item, Str item->args = str_start_end(context->data, args_start_token->start, token->start + token->size); item->t = Item_Function; item->cpp_name = cpp_name; - item->breakdown = parameter_parse(part, context->data, args_start_token, token); + item->breakdown = parameter_parse(context->data, args_start_token, token); Assert(get_token(context)->type == CPP_TOKEN_PARENTHESE_CLOSE); result = true; @@ -1066,9 +1064,9 @@ Moves the context in the following way: MARKER ~~~ name( ~~~~~~~ ) ^ -------------------> ^ */ -static int32_t -function_parse(Partition *part, Parse_Context *context, Item_Node *item, String cpp_name){ - int32_t result = false; +internal i32 +function_parse(Parse_Context *context, Item_Node *item, String cpp_name){ + i32 result = false; String doc_string = {0}; Cpp_Token *token = get_token(context); @@ -1081,7 +1079,7 @@ function_parse(Partition *part, Parse_Context *context, Item_Node *item, String set_token(context, token); if (get_next_token(context)){ - if (function_sig_parse(part, context, item, cpp_name)){ + if (function_sig_parse(context, item, cpp_name)){ Assert(get_token(context)->type == CPP_TOKEN_PARENTHESE_CLOSE); result = true; } @@ -1095,9 +1093,9 @@ Moves the context in the following way: /* ~~~ // #define ^ ----> ^ */ -static int32_t +internal i32 macro_parse_check(Parse_Context *context){ - int32_t result = false; + i32 result = false; Cpp_Token *token = 0; @@ -1119,9 +1117,9 @@ Moves the context in the following way: /* ~~~ // #define ~~~~~~~~~~~~~~~~~ NOT_IN_MACRO_BODY ^ ----------------------------> ^ */ -static int32_t -macro_parse(Partition *part, Parse_Context *context, Item_Node *item){ - int32_t result = false; +internal i32 +macro_parse(Parse_Context *context, Item_Node *item){ + i32 result = false; Cpp_Token *token = 0; Cpp_Token *doc_token = 0; @@ -1159,7 +1157,7 @@ macro_parse(Partition *part, Parse_Context *context, Item_Node *item){ if (token){ item->args = str_start_end(context->data, args_start_token->start, token->start + token->size); - item->breakdown = parameter_parse(part, context->data, args_start_token, token); + item->breakdown = parameter_parse(context->data, args_start_token, token); if ((token = get_next_token(context)) != 0){ Cpp_Token *body_start = token; @@ -1189,18 +1187,18 @@ macro_parse(Partition *part, Parse_Context *context, Item_Node *item){ return(result); } -static Meta_Unit -compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keywords *meta_keywords, int32_t key_count){ +internal Meta_Unit +compile_meta_unit(char *code_directory, char **files, Meta_Keywords *meta_keywords, i32 key_count){ Meta_Unit unit = {0}; - int32_t file_count = 0; + i32 file_count = 0; for (char **file_ptr = files; *file_ptr; ++file_ptr, ++file_count); unit.count = file_count; - unit.parse = push_array(part, Parse, file_count); + unit.parse = fm_push_array(Parse, file_count); b32 all_files_lexed = true; - int32_t i = 0; + i32 i = 0; for (char **file_ptr = files; *file_ptr; ++file_ptr, ++i){ char str_space[512]; String name = make_fixed_width_string(str_space); @@ -1222,7 +1220,7 @@ compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keyw if (all_files_lexed){ // TODO(allen): This stage counts nested structs and unions which is not correct. Luckily it only means we over allocate by a few items, but fixing it to be exactly correct would be nice. - for (int32_t J = 0; J < unit.count; ++J){ + for (i32 J = 0; J < unit.count; ++J){ Cpp_Token *token = 0; Parse_Context context_ = setup_parse_context(unit.parse[J]); Parse_Context *context = &context_; @@ -1231,7 +1229,7 @@ compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keyw if (!(token->flags & CPP_TFLAG_PP_BODY)){ String lexeme = get_lexeme(*token, context->data); - int32_t match_index = 0; + i32 match_index = 0; if (string_set_match_table(meta_keywords, sizeof(*meta_keywords), key_count, lexeme, &match_index)){ Item_Type type = meta_keywords[match_index].type; @@ -1247,31 +1245,31 @@ compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keyw } if (unit.set.count > 0){ - unit.set = allocate_item_set(part, unit.set.count); + unit.set = allocate_item_set(unit.set.count); } - int32_t index = 0; + i32 index = 0; - for (int32_t J = 0; J < unit.count; ++J){ + for (i32 J = 0; J < unit.count; ++J){ Cpp_Token *token = 0; Parse_Context context_ = setup_parse_context(unit.parse[J]); Parse_Context *context = &context_; String cpp_name = {0}; - int32_t has_cpp_name = 0; + i32 has_cpp_name = 0; for (; (token = get_token(context)) != 0; get_next_token(context)){ if (!(token->flags & CPP_TFLAG_PP_BODY)){ String lexeme = get_lexeme(*token, context->data); - int32_t match_index = 0; + i32 match_index = 0; if (string_set_match_table(meta_keywords, sizeof(*meta_keywords), key_count, lexeme, &match_index)){ Item_Type type = meta_keywords[match_index].type; switch (type){ case Item_Function: { - if (function_parse(part, context, unit.set.items + index, cpp_name)){ + if (function_parse(context, unit.set.items + index, cpp_name)){ Assert(unit.set.items[index].t == Item_Function); ++index; } @@ -1292,7 +1290,7 @@ compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keyw case Item_Macro: { - if (macro_parse(part, context, unit.set.items + index)){ + if (macro_parse(context, unit.set.items + index)){ Assert(unit.set.items[index].t == Item_Macro); ++index; } @@ -1314,9 +1312,8 @@ compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keyw case Item_Struct: case Item_Union: //struct/union { - if (struct_parse(part, (type == Item_Struct), context, unit.set.items + index)){ - Assert(unit.set.items[index].t == Item_Struct || - unit.set.items[index].t == Item_Union); + if (struct_parse((type == Item_Struct), context, unit.set.items + index)){ + Assert(unit.set.items[index].t == Item_Struct ||unit.set.items[index].t == Item_Union); ++index; } else{ @@ -1326,7 +1323,7 @@ compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keyw case Item_Enum: //ENUM { - if (enum_parse(part, context, unit.set.items + index)){ + if (enum_parse(context, unit.set.items + index)){ Assert(unit.set.items[index].t == Item_Enum); ++index; } @@ -1363,10 +1360,10 @@ compile_meta_unit(Partition *part, char *code_directory, char **files, Meta_Keyw return(unit); } -static Meta_Unit -compile_meta_unit(Partition *part, char *code_directory, char *file, Meta_Keywords *meta_keywords, int32_t key_count){ +internal Meta_Unit +compile_meta_unit(char *code_directory, char *file, Meta_Keywords *meta_keywords, i32 key_count){ char *file_array[2] = {file, 0}; - Meta_Unit unit = compile_meta_unit(part, code_directory, file_array, meta_keywords, key_count); + Meta_Unit unit = compile_meta_unit(code_directory, file_array, meta_keywords, key_count); return(unit); } diff --git a/meta/4ed_out_context.cpp b/meta/4ed_out_context.cpp index c9d802af..7d0831b5 100644 --- a/meta/4ed_out_context.cpp +++ b/meta/4ed_out_context.cpp @@ -68,44 +68,6 @@ make_out_string(int32_t x){ return(str); } -static void -do_file_copy(Partition *part, char *src_file, char *dst_dir, char *dst_file){ - char dst[256]; - String str = {0}; - int32_t success = 0; - - str = make_fixed_width_string(dst); - append_sc(&str, dst_dir); - append_sc(&str, "/"); - append_sc(&str, dst_file); - terminate_with_null(&str); - - Temp_Memory temp = begin_temp_memory(part); - int32_t mem_size = partition_remaining(part); - void *mem = push_block(part, mem_size); - FILE *in = fopen(src_file, "rb"); - if (in){ - fseek(in, 0, SEEK_END); - int32_t file_size = ftell(in); - if (mem_size >= file_size){ - fseek(in, 0, SEEK_SET); - fread(mem, 1, file_size, in); - FILE *out = fopen(dst, "wb"); - if (out){ - fwrite(mem, 1, file_size, out); - fclose(out); - success = 1; - } - } - fclose(in); - } - end_temp_memory(temp); - - if (!success){ - fprintf(stderr, "Could not copy from %s to %s\n", src_file, dst); - } -} - #endif // BOTTOM diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index 38206fa9..318c0988 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -12,7 +12,6 @@ #define IS_PLAT_LAYER -#include #include #include "4ed_defines.h" #include "4coder_API/version.h" diff --git a/platform_win32/win32_4ed.cpp b/platform_win32/win32_4ed.cpp index d7658b74..d3922e03 100644 --- a/platform_win32/win32_4ed.cpp +++ b/platform_win32/win32_4ed.cpp @@ -40,7 +40,6 @@ #define WINDOW_NAME L"4coder: " L_VERSION -#include #include #include "4coder_lib/4coder_utf8.h" diff --git a/power/4coder_experiments.cpp b/power/4coder_experiments.cpp index a9ad6f83..1bf61895 100644 --- a/power/4coder_experiments.cpp +++ b/power/4coder_experiments.cpp @@ -19,72 +19,71 @@ TYPE: 'build-target' # define BIND_4CODER_TESTS(context) ((void)context) #endif -#include #include static float get_line_y(Application_Links *app, View_Summary *view, int32_t line){ - Full_Cursor cursor = {0}; - view_compute_cursor(app, view, seek_line_char(line, 1), &cursor); - float y = cursor.wrapped_y; - if (view->unwrapped_lines){ - y = cursor.unwrapped_y; - } - return(y); + Full_Cursor cursor = {0}; + view_compute_cursor(app, view, seek_line_char(line, 1), &cursor); + float y = cursor.wrapped_y; + if (view->unwrapped_lines){ + y = cursor.unwrapped_y; + } + return(y); } CUSTOM_COMMAND_SIG(kill_rect){ - View_Summary view = get_active_view(app, AccessOpen); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessOpen); - - i32_Rect rect = get_line_x_rect(&view); - - bool32 unwrapped = view.unwrapped_lines; - - for (int32_t line = rect.y1; line >= rect.y0; --line){ - int32_t start = 0; - int32_t end = 0; - - bool32 success = 1; - Full_Cursor cursor = {0}; - - float y = get_line_y(app, &view, line); - - if (success){ - success = view_compute_cursor(app, &view, seek_xy((float)rect.x0, y, 0, unwrapped), &cursor); + View_Summary view = get_active_view(app, AccessOpen); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessOpen); + + i32_Rect rect = get_line_x_rect(&view); + + bool32 unwrapped = view.unwrapped_lines; + + for (int32_t line = rect.y1; line >= rect.y0; --line){ + int32_t start = 0; + int32_t end = 0; + + bool32 success = 1; + Full_Cursor cursor = {0}; + + float y = get_line_y(app, &view, line); + + if (success){ + success = view_compute_cursor(app, &view, seek_xy((float)rect.x0, y, 0, unwrapped), &cursor); + } + start = cursor.pos; + + if (success){ + success = view_compute_cursor(app, &view, seek_xy((float)rect.x1, y, 0, unwrapped), &cursor); + } + end = cursor.pos; + + if (success){ + buffer_replace_range(app, &buffer, start, end, 0, 0); + } } - start = cursor.pos; - - if (success){ - success = view_compute_cursor(app, &view, seek_xy((float)rect.x1, y, 0, unwrapped), &cursor); - } - end = cursor.pos; - - if (success){ - buffer_replace_range(app, &buffer, start, end, 0, 0); - } - } } static void pad_buffer_line(Application_Links *app, Partition *part, Buffer_Summary *buffer, int32_t line, char padchar, int32_t target){ - Partial_Cursor start = {0}; - Partial_Cursor end = {0}; - - if (buffer_compute_cursor(app, buffer, seek_line_char(line, 1), &start)){ - if (buffer_compute_cursor(app, buffer, seek_line_char(line, 65536), &end)){ - if (start.line == line){ - if (end.character-1 < target){ - Temp_Memory temp = begin_temp_memory(part); - int32_t size = target - (end.character-1); - char *str = push_array(part, char, size); - memset(str, ' ', size); - buffer_replace_range(app, buffer, end.pos, end.pos, str, size); - end_temp_memory(temp); + Partial_Cursor start = {0}; + Partial_Cursor end = {0}; + + if (buffer_compute_cursor(app, buffer, seek_line_char(line, 1), &start)){ + if (buffer_compute_cursor(app, buffer, seek_line_char(line, 65536), &end)){ + if (start.line == line){ + if (end.character-1 < target){ + Temp_Memory temp = begin_temp_memory(part); + int32_t size = target - (end.character-1); + char *str = push_array(part, char, size); + memset(str, ' ', size); + buffer_replace_range(app, buffer, end.pos, end.pos, str, size); + end_temp_memory(temp); + } + } } - } } - } } /* @@ -123,86 +122,86 @@ doing multi-cursor for now. */ CUSTOM_COMMAND_SIG(multi_line_edit){ - Partition *part = &global_part; - - View_Summary view = get_active_view(app, AccessOpen); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessOpen); - - Buffer_Rect rect = get_rect(&view); - - int32_t start_line = view.cursor.line; - int32_t pos = view.cursor.character-1; - - for (int32_t i = rect.line0; i <= rect.line1; ++i){ - pad_buffer_line(app, &global_part, &buffer, i, ' ', pos); - } - - int32_t line_count = rect.line1 - rect.line0 + 1; - - for (;;){ - User_Input in = get_user_input(app, EventOnAnyKey, EventOnEsc | EventOnButton); - if (in.abort) break; - - if (in.key.character && key_is_unmodified(&in.key)){ - char str = (char)in.key.character; - - Temp_Memory temp = begin_temp_memory(part); - Buffer_Edit *edit = push_array(part, Buffer_Edit, line_count); - Buffer_Edit *edits = edit; - - for (int32_t i = rect.line0; i <= rect.line1; ++i){ - Partial_Cursor cursor = {0}; - - if (buffer_compute_cursor(app, &buffer, seek_line_char(i, pos+1), &cursor)){ - edit->str_start = 0; - edit->len = 1; - edit->start = cursor.pos; - edit->end = cursor.pos; - ++edit; + Partition *part = &global_part; + + View_Summary view = get_active_view(app, AccessOpen); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessOpen); + + Buffer_Rect rect = get_rect(&view); + + int32_t start_line = view.cursor.line; + int32_t pos = view.cursor.character-1; + + for (int32_t i = rect.line0; i <= rect.line1; ++i){ + pad_buffer_line(app, &global_part, &buffer, i, ' ', pos); + } + + int32_t line_count = rect.line1 - rect.line0 + 1; + + for (;;){ + User_Input in = get_user_input(app, EventOnAnyKey, EventOnEsc | EventOnButton); + if (in.abort) break; + + if (in.key.character && key_is_unmodified(&in.key)){ + char str = (char)in.key.character; + + Temp_Memory temp = begin_temp_memory(part); + Buffer_Edit *edit = push_array(part, Buffer_Edit, line_count); + Buffer_Edit *edits = edit; + + for (int32_t i = rect.line0; i <= rect.line1; ++i){ + Partial_Cursor cursor = {0}; + + if (buffer_compute_cursor(app, &buffer, seek_line_char(i, pos+1), &cursor)){ + edit->str_start = 0; + edit->len = 1; + edit->start = cursor.pos; + edit->end = cursor.pos; + ++edit; + } + } + + int32_t edit_count = (int)(edit - edits); + buffer_batch_edit(app, &buffer, &str, 1, edits, edit_count, BatchEdit_Normal); + + end_temp_memory(temp); + + ++pos; + + view_set_cursor(app, &view, seek_line_char(start_line, pos+1), true); } - } - - int32_t edit_count = (int)(edit - edits); - buffer_batch_edit(app, &buffer, &str, 1, edits, edit_count, BatchEdit_Normal); - - end_temp_memory(temp); - - ++pos; - - view_set_cursor(app, &view, seek_line_char(start_line, pos+1), true); - } - else if (in.key.keycode == key_back){ - if (pos > 0){ - - Temp_Memory temp = begin_temp_memory(part); - Buffer_Edit *edit = push_array(part, Buffer_Edit, line_count); - Buffer_Edit *edits = edit; - - for (int32_t i = rect.line0; i <= rect.line1; ++i){ - Partial_Cursor cursor = {0}; - - if (buffer_compute_cursor(app, &buffer, seek_line_char(i, pos+1), &cursor)){ - edit->str_start = 0; - edit->len = 0; - edit->start = cursor.pos-1; - edit->end = cursor.pos; - ++edit; - } + else if (in.key.keycode == key_back){ + if (pos > 0){ + + Temp_Memory temp = begin_temp_memory(part); + Buffer_Edit *edit = push_array(part, Buffer_Edit, line_count); + Buffer_Edit *edits = edit; + + for (int32_t i = rect.line0; i <= rect.line1; ++i){ + Partial_Cursor cursor = {0}; + + if (buffer_compute_cursor(app, &buffer, seek_line_char(i, pos+1), &cursor)){ + edit->str_start = 0; + edit->len = 0; + edit->start = cursor.pos-1; + edit->end = cursor.pos; + ++edit; + } + } + + int32_t edit_count = (int)(edit - edits); + buffer_batch_edit(app, &buffer, 0, 0, edits, edit_count, BatchEdit_Normal); + + end_temp_memory(temp); + + --pos; + } + + } + else{ + break; } - - int32_t edit_count = (int)(edit - edits); - buffer_batch_edit(app, &buffer, 0, 0, edits, edit_count, BatchEdit_Normal); - - end_temp_memory(temp); - - --pos; - } - } - else{ - break; - } - } } // @@ -210,1222 +209,1222 @@ CUSTOM_COMMAND_SIG(multi_line_edit){ // enum{ - FindScope_Parent = 0x1, - FindScope_NextSibling = 0x1, - FindScope_EndOfToken = 0x2, + FindScope_Parent = 0x1, + FindScope_NextSibling = 0x1, + FindScope_EndOfToken = 0x2, }; static bool32 find_scope_top(Application_Links *app, Buffer_Summary *buffer, int32_t start_pos, uint32_t flags, int32_t *end_pos_out){ - Cpp_Get_Token_Result get_result = {0}; - - bool32 success = 0; - int32_t position = 0; - - if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ - int32_t token_index = get_result.token_index; - if (flags & FindScope_Parent){ - --token_index; - if (get_result.in_whitespace){ - ++token_index; - } - } - - if (token_index >= 0){ - static const int32_t chunk_cap = 512; - Cpp_Token chunk[chunk_cap]; - Stream_Tokens stream = {0}; - - if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){int32_t nest_level = 0; - bool32 still_looping = 0; - do{ - for (; token_index >= stream.start; --token_index){ - Cpp_Token *token = &stream.tokens[token_index]; - - switch (token->type){ - case CPP_TOKEN_BRACE_OPEN: - { - if (nest_level == 0){ - success = 1; - position = token->start; - if (flags & FindScope_EndOfToken){ - position += token->size; - } - goto finished; - } - else{ - --nest_level; - } - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - ++nest_level; - }break; + Cpp_Get_Token_Result get_result = {0}; + + bool32 success = 0; + int32_t position = 0; + + if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ + int32_t token_index = get_result.token_index; + if (flags & FindScope_Parent){ + --token_index; + if (get_result.in_whitespace){ + ++token_index; } - } - still_looping = backward_stream_tokens(&stream); - }while(still_looping); - } + } + + if (token_index >= 0){ + static const int32_t chunk_cap = 512; + Cpp_Token chunk[chunk_cap]; + Stream_Tokens stream = {0}; + + if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){int32_t nest_level = 0; + bool32 still_looping = 0; + do{ + for (; token_index >= stream.start; --token_index){ + Cpp_Token *token = &stream.tokens[token_index]; + + switch (token->type){ + case CPP_TOKEN_BRACE_OPEN: + { + if (nest_level == 0){ + success = 1; + position = token->start; + if (flags & FindScope_EndOfToken){ + position += token->size; + } + goto finished; + } + else{ + --nest_level; + } + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + ++nest_level; + }break; + } + } + still_looping = backward_stream_tokens(&stream); + }while(still_looping); + } + } } - } - - finished:; - *end_pos_out = position; - return(success); + + finished:; + *end_pos_out = position; + return(success); } static bool32 find_scope_bottom(Application_Links *app, Buffer_Summary *buffer, int32_t start_pos, uint32_t flags, int32_t *end_pos_out){ - Cpp_Get_Token_Result get_result = {0}; - - bool32 success = 0; - int32_t position = 0; - - if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ - int32_t token_index = get_result.token_index+1; - if (flags & FindScope_Parent){ - --token_index; - if (get_result.in_whitespace){ - ++token_index; - } - } - - if (token_index >= 0){ - static const int32_t chunk_cap = 512; - Cpp_Token chunk[chunk_cap]; - Stream_Tokens stream = {0}; - - if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){ - int32_t nest_level = 0; - bool32 still_looping = 0; - do{ - for (; token_index < stream.end; ++token_index){ - Cpp_Token *token = &stream.tokens[token_index]; - - switch (token->type){ - case CPP_TOKEN_BRACE_OPEN: - { - ++nest_level; - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - if (nest_level == 0){ - success = 1; - position = token->start; - if (flags & FindScope_EndOfToken){ - position += token->size; - } - goto finished; - } - else{ - --nest_level; - } - }break; + Cpp_Get_Token_Result get_result = {0}; + + bool32 success = 0; + int32_t position = 0; + + if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ + int32_t token_index = get_result.token_index+1; + if (flags & FindScope_Parent){ + --token_index; + if (get_result.in_whitespace){ + ++token_index; } - } - still_looping = forward_stream_tokens(&stream); - }while(still_looping); - } + } + + if (token_index >= 0){ + static const int32_t chunk_cap = 512; + Cpp_Token chunk[chunk_cap]; + Stream_Tokens stream = {0}; + + if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){ + int32_t nest_level = 0; + bool32 still_looping = 0; + do{ + for (; token_index < stream.end; ++token_index){ + Cpp_Token *token = &stream.tokens[token_index]; + + switch (token->type){ + case CPP_TOKEN_BRACE_OPEN: + { + ++nest_level; + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + if (nest_level == 0){ + success = 1; + position = token->start; + if (flags & FindScope_EndOfToken){ + position += token->size; + } + goto finished; + } + else{ + --nest_level; + } + }break; + } + } + still_looping = forward_stream_tokens(&stream); + }while(still_looping); + } + } } - } - - finished:; - *end_pos_out = position; - return(success); + + finished:; + *end_pos_out = position; + return(success); } static bool32 find_next_scope(Application_Links *app, Buffer_Summary *buffer, int32_t start_pos, uint32_t flags, int32_t *end_pos_out){ - Cpp_Get_Token_Result get_result = {0}; - - bool32 success = 0; - int32_t position = 0; - - if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ - int32_t token_index = get_result.token_index+1; - - if (token_index >= 0){ - static const int32_t chunk_cap = 512; - Cpp_Token chunk[chunk_cap]; - Stream_Tokens stream = {0}; - - if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){ - if (flags & FindScope_NextSibling){ - int32_t nest_level = 1; - - bool32 still_looping = 0; - do{ - for (; token_index < stream.end; ++token_index){ - Cpp_Token *token = &stream.tokens[token_index]; - - switch (token->type){ - case CPP_TOKEN_BRACE_OPEN: - { - if (nest_level == 0){ - success = 1; - position = token->start; - if (flags & FindScope_EndOfToken){ - position += token->size; - } - goto finished; - } - else{ - ++nest_level; - } - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - --nest_level; - if (nest_level == -1){ - position = start_pos; - goto finished; - } - }break; - } - } - still_looping = forward_stream_tokens(&stream); - }while(still_looping); - } - else{ - bool32 still_looping = 0; - do{ - for (; token_index < stream.end; ++token_index){ - Cpp_Token *token = &stream.tokens[token_index]; - - if (token->type == CPP_TOKEN_BRACE_OPEN){ - success = 1; - position = token->start; - if (flags & FindScope_EndOfToken){ - position += token->size; + Cpp_Get_Token_Result get_result = {0}; + + bool32 success = 0; + int32_t position = 0; + + if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ + int32_t token_index = get_result.token_index+1; + + if (token_index >= 0){ + static const int32_t chunk_cap = 512; + Cpp_Token chunk[chunk_cap]; + Stream_Tokens stream = {0}; + + if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){ + if (flags & FindScope_NextSibling){ + int32_t nest_level = 1; + + bool32 still_looping = 0; + do{ + for (; token_index < stream.end; ++token_index){ + Cpp_Token *token = &stream.tokens[token_index]; + + switch (token->type){ + case CPP_TOKEN_BRACE_OPEN: + { + if (nest_level == 0){ + success = 1; + position = token->start; + if (flags & FindScope_EndOfToken){ + position += token->size; + } + goto finished; + } + else{ + ++nest_level; + } + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + --nest_level; + if (nest_level == -1){ + position = start_pos; + goto finished; + } + }break; + } + } + still_looping = forward_stream_tokens(&stream); + }while(still_looping); + } + else{ + bool32 still_looping = 0; + do{ + for (; token_index < stream.end; ++token_index){ + Cpp_Token *token = &stream.tokens[token_index]; + + if (token->type == CPP_TOKEN_BRACE_OPEN){ + success = 1; + position = token->start; + if (flags & FindScope_EndOfToken){ + position += token->size; + } + goto finished; + } + } + still_looping = forward_stream_tokens(&stream); + }while(still_looping); } - goto finished; - } } - still_looping = forward_stream_tokens(&stream); - }while(still_looping); } - } } - } - - finished:; - *end_pos_out = position; - return(success); + + finished:; + *end_pos_out = position; + return(success); } static bool32 find_prev_scope(Application_Links *app, Buffer_Summary *buffer, int32_t start_pos, uint32_t flags, int32_t *end_pos_out){ - Cpp_Get_Token_Result get_result = {0}; - - bool32 success = 0; - int32_t position = 0; - - if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ - int32_t token_index = get_result.token_index-1; - - if (token_index >= 0){ - static const int32_t chunk_cap = 512; - Cpp_Token chunk[chunk_cap]; - Stream_Tokens stream = {0}; - - if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){ - if (flags & FindScope_NextSibling){ - int32_t nest_level = -1; - bool32 still_looping = 0; - do{ - for (; token_index >= stream.start; --token_index){ - Cpp_Token *token = &stream.tokens[token_index]; - - switch (token->type){ - case CPP_TOKEN_BRACE_OPEN: - { - if (nest_level == -1){ - position = start_pos; - goto finished; - } - else if (nest_level == 0){ - success = 1; - position = token->start; - if (flags & FindScope_EndOfToken){ - position += token->size; - } - goto finished; - } - else{ - --nest_level; - } - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - ++nest_level; - }break; - } - } - still_looping = backward_stream_tokens(&stream); - }while(still_looping); - } - else{ - bool32 still_looping = 0; - do{ - for (; token_index >= stream.start; --token_index){ - Cpp_Token *token = &stream.tokens[token_index]; - - if (token->type == CPP_TOKEN_BRACE_OPEN){ - success = 1; - position = token->start; - if (flags & FindScope_EndOfToken){ - position += token->size; + Cpp_Get_Token_Result get_result = {0}; + + bool32 success = 0; + int32_t position = 0; + + if (buffer_get_token_index(app, buffer, start_pos, &get_result)){ + int32_t token_index = get_result.token_index-1; + + if (token_index >= 0){ + static const int32_t chunk_cap = 512; + Cpp_Token chunk[chunk_cap]; + Stream_Tokens stream = {0}; + + if (init_stream_tokens(&stream, app, buffer, token_index, chunk, chunk_cap)){ + if (flags & FindScope_NextSibling){ + int32_t nest_level = -1; + bool32 still_looping = 0; + do{ + for (; token_index >= stream.start; --token_index){ + Cpp_Token *token = &stream.tokens[token_index]; + + switch (token->type){ + case CPP_TOKEN_BRACE_OPEN: + { + if (nest_level == -1){ + position = start_pos; + goto finished; + } + else if (nest_level == 0){ + success = 1; + position = token->start; + if (flags & FindScope_EndOfToken){ + position += token->size; + } + goto finished; + } + else{ + --nest_level; + } + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + ++nest_level; + }break; + } + } + still_looping = backward_stream_tokens(&stream); + }while(still_looping); + } + else{ + bool32 still_looping = 0; + do{ + for (; token_index >= stream.start; --token_index){ + Cpp_Token *token = &stream.tokens[token_index]; + + if (token->type == CPP_TOKEN_BRACE_OPEN){ + success = 1; + position = token->start; + if (flags & FindScope_EndOfToken){ + position += token->size; + } + goto finished; + } + } + still_looping = backward_stream_tokens(&stream); + }while(still_looping); } - goto finished; - } } - still_looping = backward_stream_tokens(&stream); - }while(still_looping); } - } } - } - - finished:; - *end_pos_out = position; - return(success); + + finished:; + *end_pos_out = position; + return(success); } static void view_set_to_region(Application_Links *app, View_Summary *view, int32_t major_pos, int32_t minor_pos, float normalized_threshold){ - Range range = make_range(major_pos, minor_pos); - bool32 bottom_major = false; - if (major_pos == range.max){ - bottom_major = true; - } - - Full_Cursor top, bottom; - if (view_compute_cursor(app, view, seek_pos(range.min), &top)){ - if (view_compute_cursor(app, view, seek_pos(range.max), &bottom)){ - float top_y = top.wrapped_y; - float bottom_y = bottom.wrapped_y; - if (view->unwrapped_lines){ - top_y = top.unwrapped_y; - bottom_y = bottom.unwrapped_y; - } - - GUI_Scroll_Vars scroll = view->scroll_vars; - float half_view_height = .5f*(float)(view->file_region.y1 - view->file_region.y0); - float threshold = normalized_threshold * half_view_height; - float current_center_y = ((float)scroll.target_y) + half_view_height; - - if (top_y < current_center_y - threshold || bottom_y > current_center_y + threshold){ - float center_target_y = .5f*(top_y + bottom_y); - - if (bottom_major){ - if (center_target_y < bottom_y - half_view_height * .9f){ - center_target_y = bottom_y - half_view_height * .9f; - } - } - else{ - if (center_target_y > top_y + half_view_height * .9f){ - center_target_y = top_y + half_view_height * .9f; - } - } - - float target_y = center_target_y - half_view_height; - if (target_y < 0){ - target_y = 0; - } - - scroll.target_y = (int32_t)(target_y); - view_set_scroll(app, view, scroll); - } + Range range = make_range(major_pos, minor_pos); + bool32 bottom_major = false; + if (major_pos == range.max){ + bottom_major = true; + } + + Full_Cursor top, bottom; + if (view_compute_cursor(app, view, seek_pos(range.min), &top)){ + if (view_compute_cursor(app, view, seek_pos(range.max), &bottom)){ + float top_y = top.wrapped_y; + float bottom_y = bottom.wrapped_y; + if (view->unwrapped_lines){ + top_y = top.unwrapped_y; + bottom_y = bottom.unwrapped_y; + } + + GUI_Scroll_Vars scroll = view->scroll_vars; + float half_view_height = .5f*(float)(view->file_region.y1 - view->file_region.y0); + float threshold = normalized_threshold * half_view_height; + float current_center_y = ((float)scroll.target_y) + half_view_height; + + if (top_y < current_center_y - threshold || bottom_y > current_center_y + threshold){ + float center_target_y = .5f*(top_y + bottom_y); + + if (bottom_major){ + if (center_target_y < bottom_y - half_view_height * .9f){ + center_target_y = bottom_y - half_view_height * .9f; + } + } + else{ + if (center_target_y > top_y + half_view_height * .9f){ + center_target_y = top_y + half_view_height * .9f; + } + } + + float target_y = center_target_y - half_view_height; + if (target_y < 0){ + target_y = 0; + } + + scroll.target_y = (int32_t)(target_y); + view_set_scroll(app, view, scroll); + } + } } - } } static float scope_center_threshold = 0.75f; CUSTOM_COMMAND_SIG(highlight_surrounding_scope){ - uint32_t access = AccessProtected; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t start_pos = view.cursor.pos; - int32_t top = 0, bottom = 0; - if (find_scope_top(app, &buffer, start_pos, FindScope_Parent, &top)){ - view_set_cursor(app, &view, seek_pos(top), true); - if (find_scope_bottom(app, &buffer, start_pos, FindScope_Parent | FindScope_EndOfToken, &bottom)){ - view_set_mark(app, &view, seek_pos(bottom)); - view_set_to_region(app, &view, top, bottom, scope_center_threshold); + uint32_t access = AccessProtected; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t start_pos = view.cursor.pos; + int32_t top = 0, bottom = 0; + if (find_scope_top(app, &buffer, start_pos, FindScope_Parent, &top)){ + view_set_cursor(app, &view, seek_pos(top), true); + if (find_scope_bottom(app, &buffer, start_pos, FindScope_Parent | FindScope_EndOfToken, &bottom)){ + view_set_mark(app, &view, seek_pos(bottom)); + view_set_to_region(app, &view, top, bottom, scope_center_threshold); + } + else{ + view_set_to_region(app, &view, top, top, scope_center_threshold); + } } - else{ - view_set_to_region(app, &view, top, top, scope_center_threshold); - } - } } // TODO(allen): These aren't super top notch yet (the ones about children and siblings). I am not sure I want them around anyway, because it's not that fluid feeling even when it works. CUSTOM_COMMAND_SIG(highlight_first_child_scope){ - uint32_t access = AccessProtected; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t start_pos = view.cursor.pos; - int32_t top = 0, bottom = 0; - if (find_next_scope(app, &buffer, start_pos, 0, &top)){ - if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ - view_set_cursor(app, &view, seek_pos(top), true); - view_set_mark(app, &view, seek_pos(bottom)); - view_set_to_region(app, &view, top, bottom, scope_center_threshold); + uint32_t access = AccessProtected; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t start_pos = view.cursor.pos; + int32_t top = 0, bottom = 0; + if (find_next_scope(app, &buffer, start_pos, 0, &top)){ + if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ + view_set_cursor(app, &view, seek_pos(top), true); + view_set_mark(app, &view, seek_pos(bottom)); + view_set_to_region(app, &view, top, bottom, scope_center_threshold); + } } - } } CUSTOM_COMMAND_SIG(highlight_next_sibling_scope){ - uint32_t access = AccessProtected; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t start_pos = view.cursor.pos; - int32_t top = 0, bottom = 0; - if (find_next_scope(app, &buffer, start_pos, FindScope_NextSibling, &top)){ - if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ - view_set_cursor(app, &view, seek_pos(top), true); - view_set_mark(app, &view, seek_pos(bottom)); - view_set_to_region(app, &view, top, bottom, scope_center_threshold); + uint32_t access = AccessProtected; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t start_pos = view.cursor.pos; + int32_t top = 0, bottom = 0; + if (find_next_scope(app, &buffer, start_pos, FindScope_NextSibling, &top)){ + if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ + view_set_cursor(app, &view, seek_pos(top), true); + view_set_mark(app, &view, seek_pos(bottom)); + view_set_to_region(app, &view, top, bottom, scope_center_threshold); + } } - } } CUSTOM_COMMAND_SIG(highlight_prev_sibling_scope){ - uint32_t access = AccessProtected; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t start_pos = view.cursor.pos; - int32_t top = 0, bottom = 0; - if (find_prev_scope(app, &buffer, start_pos, FindScope_NextSibling, &top)){ - if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ - view_set_cursor(app, &view, seek_pos(top), true); - view_set_mark(app, &view, seek_pos(bottom)); - view_set_to_region(app, &view, top, bottom, scope_center_threshold); + uint32_t access = AccessProtected; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t start_pos = view.cursor.pos; + int32_t top = 0, bottom = 0; + if (find_prev_scope(app, &buffer, start_pos, FindScope_NextSibling, &top)){ + if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ + view_set_cursor(app, &view, seek_pos(top), true); + view_set_mark(app, &view, seek_pos(bottom)); + view_set_to_region(app, &view, top, bottom, scope_center_threshold); + } } - } } CUSTOM_COMMAND_SIG(highlight_next_scope_absolute){ - uint32_t access = AccessProtected; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t start_pos = view.cursor.pos; - int32_t top = 0, bottom = 0; - if (find_next_scope(app, &buffer, start_pos, 0, &top)){ - if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ - view_set_cursor(app, &view, seek_pos(top), true); - view_set_mark(app, &view, seek_pos(bottom)); - view_set_to_region(app, &view, top, bottom, scope_center_threshold); + uint32_t access = AccessProtected; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t start_pos = view.cursor.pos; + int32_t top = 0, bottom = 0; + if (find_next_scope(app, &buffer, start_pos, 0, &top)){ + if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ + view_set_cursor(app, &view, seek_pos(top), true); + view_set_mark(app, &view, seek_pos(bottom)); + view_set_to_region(app, &view, top, bottom, scope_center_threshold); + } } - } } CUSTOM_COMMAND_SIG(highlight_prev_scope_absolute){ - uint32_t access = AccessProtected; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t start_pos = view.cursor.pos; - int32_t top = 0, bottom = 0; - if (find_prev_scope(app, &buffer, start_pos, 0, &top)){ - if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ - view_set_cursor(app, &view, seek_pos(top), true); - view_set_mark(app, &view, seek_pos(bottom)); - view_set_to_region(app, &view, top, bottom, scope_center_threshold); + uint32_t access = AccessProtected; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t start_pos = view.cursor.pos; + int32_t top = 0, bottom = 0; + if (find_prev_scope(app, &buffer, start_pos, 0, &top)){ + if (find_scope_bottom(app, &buffer, top, FindScope_EndOfToken, &bottom)){ + view_set_cursor(app, &view, seek_pos(top), true); + view_set_mark(app, &view, seek_pos(bottom)); + view_set_to_region(app, &view, top, bottom, scope_center_threshold); + } } - } } CUSTOM_COMMAND_SIG(place_in_scope){ - uint32_t access = AccessOpen; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - Range lines; - Range range = get_range(&view); - lines.min = buffer_get_line_index(app, &buffer, range.min); - range.min = buffer_get_line_start(app, &buffer, lines.min); - - lines.max = buffer_get_line_index(app, &buffer, range.max); - range.max = buffer_get_line_end(app, &buffer, lines.max); - - bool32 do_full = false; - - if (lines.min < lines.max){ - do_full = true; - } - else if (!buffer_line_is_blank(app, &buffer, lines.min)){ - do_full = true; - } - - if (do_full){ - Buffer_Edit edits[2]; - char str[5] = "{\n\n}"; - - int32_t min_adjustment = 0; - int32_t max_adjustment = 4; - - if (buffer_line_is_blank(app, &buffer, lines.min)){ - str[0] = '\n'; - str[1] = '{'; - ++min_adjustment; + uint32_t access = AccessOpen; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + Range lines; + Range range = get_range(&view); + lines.min = buffer_get_line_index(app, &buffer, range.min); + range.min = buffer_get_line_start(app, &buffer, lines.min); + + lines.max = buffer_get_line_index(app, &buffer, range.max); + range.max = buffer_get_line_end(app, &buffer, lines.max); + + bool32 do_full = false; + + if (lines.min < lines.max){ + do_full = true; } - - if (buffer_line_is_blank(app, &buffer, lines.max)){ - str[2] = '}'; - str[3] = '\n'; - --max_adjustment; + else if (!buffer_line_is_blank(app, &buffer, lines.min)){ + do_full = true; } - - int32_t min_pos = range.min + min_adjustment; - int32_t max_pos = range.max + max_adjustment; - - int32_t cursor_pos = min_pos; - int32_t mark_pos = max_pos; - - if (view.cursor.pos > view.mark.pos){ - cursor_pos = max_pos; - mark_pos = min_pos; + + if (do_full){ + Buffer_Edit edits[2]; + char str[5] = "{\n\n}"; + + int32_t min_adjustment = 0; + int32_t max_adjustment = 4; + + if (buffer_line_is_blank(app, &buffer, lines.min)){ + str[0] = '\n'; + str[1] = '{'; + ++min_adjustment; + } + + if (buffer_line_is_blank(app, &buffer, lines.max)){ + str[2] = '}'; + str[3] = '\n'; + --max_adjustment; + } + + int32_t min_pos = range.min + min_adjustment; + int32_t max_pos = range.max + max_adjustment; + + int32_t cursor_pos = min_pos; + int32_t mark_pos = max_pos; + + if (view.cursor.pos > view.mark.pos){ + cursor_pos = max_pos; + mark_pos = min_pos; + } + + edits[0].str_start = 0; + edits[0].len = 2; + edits[0].start = range.min; + edits[0].end = range.min; + + edits[1].str_start = 2; + edits[1].len = 2; + edits[1].start = range.max; + edits[1].end = range.max; + + buffer_batch_edit(app, &buffer, str, 4, edits, 2, BatchEdit_Normal); + + view_set_cursor(app, &view, seek_pos(cursor_pos), true); + view_set_mark(app, &view, seek_pos(mark_pos)); + } + else{ + buffer_replace_range(app, &buffer, range.min, range.max, "{\n\n}", 4); + view_set_cursor(app, &view, seek_pos(range.min + 2), true); + view_set_mark(app, &view, seek_pos(range.min + 2)); } - - edits[0].str_start = 0; - edits[0].len = 2; - edits[0].start = range.min; - edits[0].end = range.min; - - edits[1].str_start = 2; - edits[1].len = 2; - edits[1].start = range.max; - edits[1].end = range.max; - - buffer_batch_edit(app, &buffer, str, 4, edits, 2, BatchEdit_Normal); - - view_set_cursor(app, &view, seek_pos(cursor_pos), true); - view_set_mark(app, &view, seek_pos(mark_pos)); - } - else{ - buffer_replace_range(app, &buffer, range.min, range.max, "{\n\n}", 4); - view_set_cursor(app, &view, seek_pos(range.min + 2), true); - view_set_mark(app, &view, seek_pos(range.min + 2)); - } } CUSTOM_COMMAND_SIG(delete_current_scope){ - uint32_t access = AccessOpen; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t top = view.cursor.pos; - int32_t bottom = view.mark.pos; - - if (top > bottom){ - int32_t x = top; - top = bottom; - bottom = x; - } - - if (buffer_get_char(app, &buffer, top) == '{' && buffer_get_char(app, &buffer, bottom-1) == '}'){ - int32_t top_len = 1; - int32_t bottom_len = 1; - if (buffer_get_char(app, &buffer, top-1) == '\n'){ - top_len = 2; + uint32_t access = AccessOpen; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t top = view.cursor.pos; + int32_t bottom = view.mark.pos; + + if (top > bottom){ + int32_t x = top; + top = bottom; + bottom = x; } - if (buffer_get_char(app, &buffer, bottom+1) == '\n'){ - bottom_len = 2; + + if (buffer_get_char(app, &buffer, top) == '{' && buffer_get_char(app, &buffer, bottom-1) == '}'){ + int32_t top_len = 1; + int32_t bottom_len = 1; + if (buffer_get_char(app, &buffer, top-1) == '\n'){ + top_len = 2; + } + if (buffer_get_char(app, &buffer, bottom+1) == '\n'){ + bottom_len = 2; + } + + Buffer_Edit edits[2]; + edits[0].str_start = 0; + edits[0].len = 0; + edits[0].start = top+1 - top_len; + edits[0].end = top+1; + + edits[1].str_start = 0; + edits[1].len = 0; + edits[1].start = bottom-1; + edits[1].end = bottom-1 + bottom_len; + + buffer_batch_edit(app, &buffer, 0, 0, edits, 2, BatchEdit_Normal); } - - Buffer_Edit edits[2]; - edits[0].str_start = 0; - edits[0].len = 0; - edits[0].start = top+1 - top_len; - edits[0].end = top+1; - - edits[1].str_start = 0; - edits[1].len = 0; - edits[1].start = bottom-1; - edits[1].end = bottom-1 + bottom_len; - - buffer_batch_edit(app, &buffer, 0, 0, edits, 2, BatchEdit_Normal); - } } struct Statement_Parser{ - Stream_Tokens stream; - int32_t token_index; - Buffer_Summary *buffer; + Stream_Tokens stream; + int32_t token_index; + Buffer_Summary *buffer; }; static Cpp_Token* parser_next_token(Statement_Parser *parser){ - Cpp_Token *result = 0; - bool32 still_looping = true; - while (parser->token_index >= parser->stream.end && still_looping){ - still_looping = forward_stream_tokens(&parser->stream); - } - if (parser->token_index < parser->stream.end){ - result = &parser->stream.tokens[parser->token_index]; - ++parser->token_index; - } - return(result); + Cpp_Token *result = 0; + bool32 still_looping = true; + while (parser->token_index >= parser->stream.end && still_looping){ + still_looping = forward_stream_tokens(&parser->stream); + } + if (parser->token_index < parser->stream.end){ + result = &parser->stream.tokens[parser->token_index]; + ++parser->token_index; + } + return(result); } static bool32 parse_statement_down(Application_Links *app, Statement_Parser *parser, Cpp_Token *token_out); static bool32 parse_for_down(Application_Links *app, Statement_Parser *parser, Cpp_Token *token_out){ - bool32 success = false; - Cpp_Token *token = parser_next_token(parser); - - int32_t paren_level = 0; - while (token != 0){ - if (!(token->flags & CPP_TFLAG_PP_BODY)){ - switch (token->type){ - case CPP_TOKEN_PARENTHESE_OPEN: - { - ++paren_level; - }break; - - case CPP_TOKEN_PARENTHESE_CLOSE: - { - --paren_level; - if (paren_level == 0){ - success = parse_statement_down(app, parser, token_out); - goto finished; - } - else if (paren_level < 0){ - success = false; - goto finished; - } - }break; - } + bool32 success = false; + Cpp_Token *token = parser_next_token(parser); + + int32_t paren_level = 0; + while (token != 0){ + if (!(token->flags & CPP_TFLAG_PP_BODY)){ + switch (token->type){ + case CPP_TOKEN_PARENTHESE_OPEN: + { + ++paren_level; + }break; + + case CPP_TOKEN_PARENTHESE_CLOSE: + { + --paren_level; + if (paren_level == 0){ + success = parse_statement_down(app, parser, token_out); + goto finished; + } + else if (paren_level < 0){ + success = false; + goto finished; + } + }break; + } + } + + token = parser_next_token(parser); } - - token = parser_next_token(parser); - } - - finished:; - return(success); + + finished:; + return(success); } static bool32 parse_if_down(Application_Links *app, Statement_Parser *parser, Cpp_Token *token_out){ - bool32 success = false; - Cpp_Token *token = parser_next_token(parser); - - if (token != 0){ - success = parse_statement_down(app, parser, token_out); - if (success){ - token = parser_next_token(parser); - if (token != 0 && token->type == CPP_TOKEN_KEY_CONTROL_FLOW){ - char lexeme[32]; - if (sizeof(lexeme)-1 >= token->size){ - if (buffer_read_range(app, parser->buffer, token->start, token->start + token->size, lexeme)){ - lexeme[token->size] = 0; - if (match(lexeme, "else")){ - success = parse_statement_down(app, parser, token_out); + bool32 success = false; + Cpp_Token *token = parser_next_token(parser); + + if (token != 0){ + success = parse_statement_down(app, parser, token_out); + if (success){ + token = parser_next_token(parser); + if (token != 0 && token->type == CPP_TOKEN_KEY_CONTROL_FLOW){ + char lexeme[32]; + if (sizeof(lexeme)-1 >= token->size){ + if (buffer_read_range(app, parser->buffer, token->start, token->start + token->size, lexeme)){ + lexeme[token->size] = 0; + if (match(lexeme, "else")){ + success = parse_statement_down(app, parser, token_out); + } + } + } } - } } - } } - } - - return(success); + + return(success); } static bool32 parse_block_down(Application_Links *app, Statement_Parser *parser, Cpp_Token *token_out){ - bool32 success = false; - Cpp_Token *token = parser_next_token(parser); - - int32_t nest_level = 0; - while (token != 0){ - switch (token->type){ - case CPP_TOKEN_BRACE_OPEN: - { - ++nest_level; - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - if (nest_level == 0){ - *token_out = *token; - success = true; - goto finished; + bool32 success = false; + Cpp_Token *token = parser_next_token(parser); + + int32_t nest_level = 0; + while (token != 0){ + switch (token->type){ + case CPP_TOKEN_BRACE_OPEN: + { + ++nest_level; + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + if (nest_level == 0){ + *token_out = *token; + success = true; + goto finished; + } + --nest_level; + }break; } - --nest_level; - }break; + token = parser_next_token(parser); } - token = parser_next_token(parser); - } - - finished:; - return(success); + + finished:; + return(success); } static bool32 parse_statement_down(Application_Links *app, Statement_Parser *parser, Cpp_Token *token_out){ - bool32 success = false; - Cpp_Token *token = parser_next_token(parser); - - if (token != 0){ - bool32 not_getting_block = false; - - do{ - switch (token->type){ - case CPP_TOKEN_BRACE_CLOSE: - { - goto finished; - }break; - - case CPP_TOKEN_KEY_CONTROL_FLOW: - { - char lexeme[32]; - if (sizeof(lexeme)-1 >= token->size){ - if (buffer_read_range(app, parser->buffer, token->start, token->start + token->size, lexeme)){ - lexeme[token->size] = 0; - if (match(lexeme, "for")){ - success = parse_for_down(app, parser, token_out); - goto finished; - } - else if (match(lexeme, "if")){ - success = parse_if_down(app, parser, token_out); - goto finished; - } - else if (match(lexeme, "else")){ - success = false; - goto finished; - } + bool32 success = false; + Cpp_Token *token = parser_next_token(parser); + + if (token != 0){ + bool32 not_getting_block = false; + + do{ + switch (token->type){ + case CPP_TOKEN_BRACE_CLOSE: + { + goto finished; + }break; + + case CPP_TOKEN_KEY_CONTROL_FLOW: + { + char lexeme[32]; + if (sizeof(lexeme)-1 >= token->size){ + if (buffer_read_range(app, parser->buffer, token->start, token->start + token->size, lexeme)){ + lexeme[token->size] = 0; + if (match(lexeme, "for")){ + success = parse_for_down(app, parser, token_out); + goto finished; + } + else if (match(lexeme, "if")){ + success = parse_if_down(app, parser, token_out); + goto finished; + } + else if (match(lexeme, "else")){ + success = false; + goto finished; + } + } + } + }break; + + case CPP_TOKEN_BRACE_OPEN: + { + if (!not_getting_block){ + success = parse_block_down(app, parser, token_out); + goto finished; + } + }break; + + case CPP_TOKEN_SEMICOLON: + { + success = true; + *token_out = *token; + goto finished; + }break; + + case CPP_TOKEN_EQ: + { + not_getting_block = true; + }break; } - } - }break; - - case CPP_TOKEN_BRACE_OPEN: - { - if (!not_getting_block){ - success = parse_block_down(app, parser, token_out); - goto finished; - } - }break; - - case CPP_TOKEN_SEMICOLON: - { - success = true; - *token_out = *token; - goto finished; - }break; - - case CPP_TOKEN_EQ: - { - not_getting_block = true; - }break; - } - - token = parser_next_token(parser); - }while(token != 0); - } - - finished:; - return(success); + + token = parser_next_token(parser); + }while(token != 0); + } + + finished:; + return(success); } static bool32 find_whole_statement_down(Application_Links *app, Buffer_Summary *buffer, int32_t pos, int32_t *start_out, int32_t *end_out){ - bool32 result = false; - int32_t start = pos; - int32_t end = start; - - Cpp_Get_Token_Result get_result = {0}; - - if (buffer_get_token_index(app, buffer, pos, &get_result)){ - Statement_Parser parser = {0}; - parser.token_index = get_result.token_index; - - if (parser.token_index < 0){ - parser.token_index = 0; + bool32 result = false; + int32_t start = pos; + int32_t end = start; + + Cpp_Get_Token_Result get_result = {0}; + + if (buffer_get_token_index(app, buffer, pos, &get_result)){ + Statement_Parser parser = {0}; + parser.token_index = get_result.token_index; + + if (parser.token_index < 0){ + parser.token_index = 0; + } + if (get_result.in_whitespace){ + parser.token_index += 1; + } + + static const int32_t chunk_cap = 512; + Cpp_Token chunk[chunk_cap]; + + if (init_stream_tokens(&parser.stream, app, buffer, parser.token_index, chunk, chunk_cap)){ + parser.buffer = buffer; + + Cpp_Token end_token = {0}; + if (parse_statement_down(app, &parser, &end_token)){ + end = end_token.start + end_token.size; + result = true; + } + } } - if (get_result.in_whitespace){ - parser.token_index += 1; - } - - static const int32_t chunk_cap = 512; - Cpp_Token chunk[chunk_cap]; - - if (init_stream_tokens(&parser.stream, app, buffer, parser.token_index, chunk, chunk_cap)){ - parser.buffer = buffer; - - Cpp_Token end_token = {0}; - if (parse_statement_down(app, &parser, &end_token)){ - end = end_token.start + end_token.size; - result = true; - } - } - } - - *start_out = start; - *end_out = end; - return(result); + + *start_out = start; + *end_out = end; + return(result); } CUSTOM_COMMAND_SIG(scope_absorb_down){ - uint32_t access = AccessOpen; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - int32_t top = view.cursor.pos; - int32_t bottom = view.mark.pos; - - if (top > bottom){ - int32_t x = top; - top = bottom; - bottom = x; - } - - Partition *part = &global_part; - - Temp_Memory temp = begin_temp_memory(part); - if (buffer_get_char(app, &buffer, top) == '{' && buffer_get_char(app, &buffer, bottom-1) == '}'){ - Range range; - if (find_whole_statement_down(app, &buffer, bottom, &range.start, &range.end)){ - char *string_space = push_array(part, char, range.end - range.start); - buffer_read_range(app, &buffer, range.start, range.end, string_space); - - String string = make_string(string_space, range.end - range.start); - string = skip_chop_whitespace(string); - - int32_t newline_count = 0; - for (char *ptr = string_space; ptr < string.str; ++ptr){ - if (*ptr == '\n'){ - ++newline_count; - } - } - - bool32 extra_newline = false; - if (newline_count >= 2){ - extra_newline = true; - } - - int32_t edit_len = string.size + 1; - if (extra_newline){ - edit_len += 1; - } - - char *edit_str = push_array(part, char, edit_len); - if (extra_newline){ - edit_str[0] = '\n'; - copy_fast_unsafe(edit_str+1, string); - edit_str[edit_len-1] = '\n'; - } - else{ - copy_fast_unsafe(edit_str, string); - edit_str[edit_len-1] = '\n'; - } - - Buffer_Edit edits[2]; - edits[0].str_start = 0; - edits[0].len = edit_len; - edits[0].start = bottom-1; - edits[0].end = bottom-1; - - edits[1].str_start = 0; - edits[1].len = 0; - edits[1].start = range.start; - edits[1].end = range.end; - - buffer_batch_edit(app, &buffer, edit_str, edit_len, edits, 2, BatchEdit_Normal); + uint32_t access = AccessOpen; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + int32_t top = view.cursor.pos; + int32_t bottom = view.mark.pos; + + if (top > bottom){ + int32_t x = top; + top = bottom; + bottom = x; } - } - end_temp_memory(temp); + + Partition *part = &global_part; + + Temp_Memory temp = begin_temp_memory(part); + if (buffer_get_char(app, &buffer, top) == '{' && buffer_get_char(app, &buffer, bottom-1) == '}'){ + Range range; + if (find_whole_statement_down(app, &buffer, bottom, &range.start, &range.end)){ + char *string_space = push_array(part, char, range.end - range.start); + buffer_read_range(app, &buffer, range.start, range.end, string_space); + + String string = make_string(string_space, range.end - range.start); + string = skip_chop_whitespace(string); + + int32_t newline_count = 0; + for (char *ptr = string_space; ptr < string.str; ++ptr){ + if (*ptr == '\n'){ + ++newline_count; + } + } + + bool32 extra_newline = false; + if (newline_count >= 2){ + extra_newline = true; + } + + int32_t edit_len = string.size + 1; + if (extra_newline){ + edit_len += 1; + } + + char *edit_str = push_array(part, char, edit_len); + if (extra_newline){ + edit_str[0] = '\n'; + copy_fast_unsafe(edit_str+1, string); + edit_str[edit_len-1] = '\n'; + } + else{ + copy_fast_unsafe(edit_str, string); + edit_str[edit_len-1] = '\n'; + } + + Buffer_Edit edits[2]; + edits[0].str_start = 0; + edits[0].len = edit_len; + edits[0].start = bottom-1; + edits[0].end = bottom-1; + + edits[1].str_start = 0; + edits[1].len = 0; + edits[1].start = range.start; + edits[1].end = range.end; + + buffer_batch_edit(app, &buffer, edit_str, edit_len, edits, 2, BatchEdit_Normal); + } + } + end_temp_memory(temp); } // NOTE(allen): Some basic code manipulation ideas. CUSTOM_COMMAND_SIG(rename_parameter){ - uint32_t access = AccessOpen; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - Partition *part = &global_part; - - Temp_Memory temp = begin_temp_memory(part); - - Cpp_Get_Token_Result result; - if (buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){ - if (!result.in_whitespace){ - static const int32_t stream_space_size = 512; - Cpp_Token stream_space[stream_space_size]; - Stream_Tokens stream = {0}; - - if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, stream_space_size)){ - int32_t token_index = result.token_index; - Cpp_Token token = stream.tokens[token_index]; - - if (token.type == CPP_TOKEN_IDENTIFIER){ - - char old_lexeme_base[128]; - String old_lexeme = make_fixed_width_string(old_lexeme_base); - - if (token.size < sizeof(old_lexeme_base)){ - Cpp_Token original_token = token; - old_lexeme.size = token.size; - buffer_read_range(app, &buffer, token.start, token.start+token.size, old_lexeme.str); - - int32_t proc_body_found = 0; - bool32 still_looping = 0; - - ++token_index; - do{ - for (; token_index < stream.end; ++token_index){ - Cpp_Token *token_ptr = stream.tokens + token_index; - switch (token_ptr->type){ - case CPP_TOKEN_BRACE_OPEN: - { - proc_body_found = 1; - goto doublebreak; - }break; - - case CPP_TOKEN_BRACE_CLOSE: - case CPP_TOKEN_PARENTHESE_OPEN: - { - goto doublebreak; - }break; - } - } - still_looping = forward_stream_tokens(&stream); - }while(still_looping); - doublebreak:; - - if (proc_body_found){ - - Query_Bar with; - char with_space[1024]; - with.prompt = make_lit_string("New Name: "); - with.string = make_fixed_width_string(with_space); - if (!query_user_string(app, &with)) return; - - String replace_string = with.string; - - Buffer_Edit *edits = (Buffer_Edit*)partition_current(part); - int32_t edit_max = (partition_remaining(part))/sizeof(Buffer_Edit); - int32_t edit_count = 0; - - if (edit_max >= 1){ - Buffer_Edit edit; - edit.str_start = 0; - edit.len = replace_string.size; - edit.start = original_token.start; - edit.end = original_token.start + original_token.size; - - edits[edit_count] = edit; - ++edit_count; - } - - int32_t nesting_level = 0; - int32_t closed_correctly = 0; - ++token_index; - still_looping = 0; - do{ - for (; token_index < stream.end; ++token_index){ - Cpp_Token *token_ptr = stream.tokens + token_index; - switch (token_ptr->type){ - case CPP_TOKEN_IDENTIFIER: - { - if (token_ptr->size == old_lexeme.size){ - char other_lexeme_base[128]; - String other_lexeme = make_fixed_width_string(other_lexeme_base); - other_lexeme.size = old_lexeme.size; - buffer_read_range(app, &buffer, token_ptr->start, - token_ptr->start+token_ptr->size, - other_lexeme.str); - - if (match(old_lexeme, other_lexeme)){ - Buffer_Edit edit; - edit.str_start = 0; - edit.len = replace_string.size; - edit.start = token_ptr->start; - edit.end = token_ptr->start + token_ptr->size; - - if (edit_count < edit_max){ - edits[edit_count] = edit; - ++edit_count; - } - else{ - goto doublebreak2; - } + uint32_t access = AccessOpen; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + Partition *part = &global_part; + + Temp_Memory temp = begin_temp_memory(part); + + Cpp_Get_Token_Result result; + if (buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){ + if (!result.in_whitespace){ + static const int32_t stream_space_size = 512; + Cpp_Token stream_space[stream_space_size]; + Stream_Tokens stream = {0}; + + if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, stream_space_size)){ + int32_t token_index = result.token_index; + Cpp_Token token = stream.tokens[token_index]; + + if (token.type == CPP_TOKEN_IDENTIFIER){ + + char old_lexeme_base[128]; + String old_lexeme = make_fixed_width_string(old_lexeme_base); + + if (token.size < sizeof(old_lexeme_base)){ + Cpp_Token original_token = token; + old_lexeme.size = token.size; + buffer_read_range(app, &buffer, token.start, token.start+token.size, old_lexeme.str); + + int32_t proc_body_found = 0; + bool32 still_looping = 0; + + ++token_index; + do{ + for (; token_index < stream.end; ++token_index){ + Cpp_Token *token_ptr = stream.tokens + token_index; + switch (token_ptr->type){ + case CPP_TOKEN_BRACE_OPEN: + { + proc_body_found = 1; + goto doublebreak; + }break; + + case CPP_TOKEN_BRACE_CLOSE: + case CPP_TOKEN_PARENTHESE_OPEN: + { + goto doublebreak; + }break; + } + } + still_looping = forward_stream_tokens(&stream); + }while(still_looping); + doublebreak:; + + if (proc_body_found){ + + Query_Bar with; + char with_space[1024]; + with.prompt = make_lit_string("New Name: "); + with.string = make_fixed_width_string(with_space); + if (!query_user_string(app, &with)) return; + + String replace_string = with.string; + + Buffer_Edit *edits = (Buffer_Edit*)partition_current(part); + int32_t edit_max = (partition_remaining(part))/sizeof(Buffer_Edit); + int32_t edit_count = 0; + + if (edit_max >= 1){ + Buffer_Edit edit; + edit.str_start = 0; + edit.len = replace_string.size; + edit.start = original_token.start; + edit.end = original_token.start + original_token.size; + + edits[edit_count] = edit; + ++edit_count; + } + + int32_t nesting_level = 0; + int32_t closed_correctly = 0; + ++token_index; + still_looping = 0; + do{ + for (; token_index < stream.end; ++token_index){ + Cpp_Token *token_ptr = stream.tokens + token_index; + switch (token_ptr->type){ + case CPP_TOKEN_IDENTIFIER: + { + if (token_ptr->size == old_lexeme.size){ + char other_lexeme_base[128]; + String other_lexeme = make_fixed_width_string(other_lexeme_base); + other_lexeme.size = old_lexeme.size; + buffer_read_range(app, &buffer, token_ptr->start, + token_ptr->start+token_ptr->size, + other_lexeme.str); + + if (match(old_lexeme, other_lexeme)){ + Buffer_Edit edit; + edit.str_start = 0; + edit.len = replace_string.size; + edit.start = token_ptr->start; + edit.end = token_ptr->start + token_ptr->size; + + if (edit_count < edit_max){ + edits[edit_count] = edit; + ++edit_count; + } + else{ + goto doublebreak2; + } + } + } + }break; + + case CPP_TOKEN_BRACE_OPEN: + { + ++nesting_level; + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + if (nesting_level == 0){ + closed_correctly = 1; + goto doublebreak2; + } + else{ + --nesting_level; + } + }break; + } + } + still_looping = forward_stream_tokens(&stream); + }while(still_looping); + doublebreak2:; + + if (closed_correctly){ + buffer_batch_edit(app, &buffer, replace_string.str, replace_string.size, + edits, edit_count, BatchEdit_Normal); + } } - } - }break; - - case CPP_TOKEN_BRACE_OPEN: - { - ++nesting_level; - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - if (nesting_level == 0){ - closed_correctly = 1; - goto doublebreak2; - } - else{ - --nesting_level; - } - }break; - } + } } - still_looping = forward_stream_tokens(&stream); - }while(still_looping); - doublebreak2:; - - if (closed_correctly){ - buffer_batch_edit(app, &buffer, replace_string.str, replace_string.size, - edits, edit_count, BatchEdit_Normal); - } } - } } - } } - } - - end_temp_memory(temp); + + end_temp_memory(temp); } CUSTOM_COMMAND_SIG(write_explicit_enum_values){ - uint32_t access = AccessOpen; - View_Summary view = get_active_view(app, access); - Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); - - Partition *part = &global_part; - - Temp_Memory temp = begin_temp_memory(part); - - Cpp_Get_Token_Result result; - if (buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){ - if (!result.in_whitespace){ - Cpp_Token stream_space[32]; - Stream_Tokens stream = {0}; - - if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){ - int32_t token_index = result.token_index; - Cpp_Token token = stream.tokens[token_index]; - - if (token.type == CPP_TOKEN_BRACE_OPEN){ - - ++token_index; - - int32_t seeker_index = token_index; - Stream_Tokens seek_stream = begin_temp_stream_token(&stream); - - bool32 closed_correctly = false; - bool32 still_looping = false; - do{ - for (; seeker_index < stream.end; ++seeker_index){ - Cpp_Token *token_seeker = stream.tokens + seeker_index; - switch (token_seeker->type){ - case CPP_TOKEN_BRACE_CLOSE: - closed_correctly = true; - goto finished_seek; - - case CPP_TOKEN_BRACE_OPEN: - goto finished_seek; - } - } - still_looping = forward_stream_tokens(&stream); - }while(still_looping); - finished_seek:; - end_temp_stream_token(&stream, seek_stream); - - if (closed_correctly){ - int32_t count_estimate = 1 + (seeker_index - token_index)/2; - - int32_t edit_count = 0; - Buffer_Edit *edits = push_array(part, Buffer_Edit, count_estimate); - - char *string_base = (char*)partition_current(part); - String string = make_string(string_base, 0, partition_remaining(part)); - - closed_correctly = false; - still_looping = false; - int32_t value = 0; - do{ - for (;token_index < stream.end; ++token_index){ - Cpp_Token *token_ptr = stream.tokens + token_index; - switch (token_ptr->type){ - case CPP_TOKEN_IDENTIFIER: - { - int32_t edit_start = token_ptr->start + token_ptr->size; - int32_t edit_stop = edit_start; - - int32_t edit_is_good = 0; + uint32_t access = AccessOpen; + View_Summary view = get_active_view(app, access); + Buffer_Summary buffer = get_buffer(app, view.buffer_id, access); + + Partition *part = &global_part; + + Temp_Memory temp = begin_temp_memory(part); + + Cpp_Get_Token_Result result; + if (buffer_get_token_index(app, &buffer, view.cursor.pos, &result)){ + if (!result.in_whitespace){ + Cpp_Token stream_space[32]; + Stream_Tokens stream = {0}; + + if (init_stream_tokens(&stream, app, &buffer, result.token_index, stream_space, 32)){ + int32_t token_index = result.token_index; + Cpp_Token token = stream.tokens[token_index]; + + if (token.type == CPP_TOKEN_BRACE_OPEN){ + ++token_index; + + int32_t seeker_index = token_index; + Stream_Tokens seek_stream = begin_temp_stream_token(&stream); + + bool32 closed_correctly = false; + bool32 still_looping = false; do{ - for (; token_index < stream.end; ++token_index){ - token_ptr = stream.tokens + token_index; - switch (token_ptr->type){ - case CPP_TOKEN_COMMA: - { - edit_stop = token_ptr->start; - edit_is_good = 1; - goto good_edit; - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - edit_stop = token_ptr->start; - closed_correctly = 1; - edit_is_good = 1; - goto good_edit; - }break; + for (; seeker_index < stream.end; ++seeker_index){ + Cpp_Token *token_seeker = stream.tokens + seeker_index; + switch (token_seeker->type){ + case CPP_TOKEN_BRACE_CLOSE: + closed_correctly = true; + goto finished_seek; + + case CPP_TOKEN_BRACE_OPEN: + goto finished_seek; + } } - } - still_looping = forward_stream_tokens(&stream); + still_looping = forward_stream_tokens(&stream); }while(still_looping); - - good_edit:; - if (edit_is_good){ - int32_t str_pos = string.size; - - append(&string, " = "); - append_int_to_str(&string, value); - if (closed_correctly){ - append(&string, "\n"); - } - ++value; - - int32_t str_size = string.size - str_pos; - - Buffer_Edit edit; - edit.str_start = str_pos; - edit.len = str_size; - edit.start = edit_start; - edit.end = edit_stop; - - assert(edit_count < count_estimate); - edits[edit_count] = edit; - ++edit_count; + finished_seek:; + end_temp_stream_token(&stream, seek_stream); + + if (closed_correctly){ + int32_t count_estimate = 1 + (seeker_index - token_index)/2; + + int32_t edit_count = 0; + Buffer_Edit *edits = push_array(part, Buffer_Edit, count_estimate); + + char *string_base = (char*)partition_current(part); + String string = make_string(string_base, 0, partition_remaining(part)); + + closed_correctly = false; + still_looping = false; + int32_t value = 0; + do{ + for (;token_index < stream.end; ++token_index){ + Cpp_Token *token_ptr = stream.tokens + token_index; + switch (token_ptr->type){ + case CPP_TOKEN_IDENTIFIER: + { + int32_t edit_start = token_ptr->start + token_ptr->size; + int32_t edit_stop = edit_start; + + int32_t edit_is_good = 0; + ++token_index; + do{ + for (; token_index < stream.end; ++token_index){ + token_ptr = stream.tokens + token_index; + switch (token_ptr->type){ + case CPP_TOKEN_COMMA: + { + edit_stop = token_ptr->start; + edit_is_good = 1; + goto good_edit; + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + edit_stop = token_ptr->start; + closed_correctly = 1; + edit_is_good = 1; + goto good_edit; + }break; + } + } + still_looping = forward_stream_tokens(&stream); + }while(still_looping); + + good_edit:; + if (edit_is_good){ + int32_t str_pos = string.size; + + append(&string, " = "); + append_int_to_str(&string, value); + if (closed_correctly){ + append(&string, "\n"); + } + ++value; + + int32_t str_size = string.size - str_pos; + + Buffer_Edit edit; + edit.str_start = str_pos; + edit.len = str_size; + edit.start = edit_start; + edit.end = edit_stop; + + Assert(edit_count < count_estimate); + edits[edit_count] = edit; + ++edit_count; + } + if (!edit_is_good || closed_correctly){ + goto finished; + } + }break; + + case CPP_TOKEN_BRACE_CLOSE: + { + closed_correctly = 1; + goto finished; + }break; + } + } + + still_looping = forward_stream_tokens(&stream); + }while(still_looping); + + finished:; + if (closed_correctly){ + buffer_batch_edit(app, &buffer, string_base, string.size, + edits, edit_count, BatchEdit_Normal); + } } - if (!edit_is_good || closed_correctly){ - goto finished; - } - }break; - - case CPP_TOKEN_BRACE_CLOSE: - { - closed_correctly = 1; - goto finished; - }break; } - } - - still_looping = forward_stream_tokens(&stream); - }while(still_looping); - - finished:; - if (closed_correctly){ - buffer_batch_edit(app, &buffer, string_base, string.size, - edits, edit_count, BatchEdit_Normal); } - } } - } } - } - - end_temp_memory(temp); + + end_temp_memory(temp); } CUSTOM_COMMAND_SIG(punishment){ - Theme_Color colors[4]; - colors[0].tag = Stag_Back; - colors[1].tag = Stag_Margin; - colors[2].tag = Stag_Margin_Hover; - colors[3].tag = Stag_Margin_Active; - get_theme_colors(app, colors, 4); - - for (uint32_t i = 0; i < 4; ++i){ - int_color color = colors[i].color; - uint8_t *c = (uint8_t*)(&color); - c[0] = 0xFF - c[0]; - c[1] = 0xFF - c[1]; - c[2] = 0xFF - c[2]; - c[3] = 0xFF - c[3]; - colors[i].color = color; - } - - set_theme_colors(app, colors, 4); + Theme_Color colors[4]; + colors[0].tag = Stag_Back; + colors[1].tag = Stag_Margin; + colors[2].tag = Stag_Margin_Hover; + colors[3].tag = Stag_Margin_Active; + get_theme_colors(app, colors, 4); + + for (uint32_t i = 0; i < 4; ++i){ + int_color color = colors[i].color; + uint8_t *c = (uint8_t*)(&color); + c[0] = 0xFF - c[0]; + c[1] = 0xFF - c[1]; + c[2] = 0xFF - c[2]; + c[3] = 0xFF - c[3]; + colors[i].color = color; + } + + set_theme_colors(app, colors, 4); } extern "C" int32_t get_bindings(void *data, int32_t size){ - Bind_Helper context_ = begin_bind_helper(data, size); - Bind_Helper *context = &context_; - - set_hook(context, hook_view_size_change, default_view_adjust); - - set_start_hook(context, default_start); - set_open_file_hook(context, default_file_settings); - set_new_file_hook(context, default_new_file); - set_save_file_hook(context, default_file_save); - set_end_file_hook(context, end_file_close_jump_list); - - set_input_filter(context, default_suppress_mouse_filter); - set_command_caller(context, default_command_caller); - - set_scroll_rule(context, smooth_scroll_rule); - - default_keys(context); - - // NOTE(allen|a4.0.6): Command maps can be opened more than - // once so that you can extend existing maps very easily. - // You can also use the helper "restart_map" instead of - // begin_map to clear everything that was in the map and - // bind new things instead. - begin_map(context, mapid_global); - end_map(context); - - begin_map(context, mapid_file); - bind(context, 's', MDFR_CTRL, punishment); - bind(context, 's', MDFR_ALT, save); - - bind(context, 'k', MDFR_ALT, kill_rect); - bind(context, ' ', MDFR_ALT | MDFR_CTRL, multi_line_edit); - - bind(context, key_page_up, MDFR_ALT, miblo_increment_time_stamp); - bind(context, key_page_down, MDFR_ALT, miblo_decrement_time_stamp); - - bind(context, key_home, MDFR_ALT, miblo_increment_time_stamp_minute); - bind(context, key_end, MDFR_ALT, miblo_decrement_time_stamp_minute); - - end_map(context); - - begin_map(context, default_code_map); - bind(context, '[', MDFR_ALT, highlight_surrounding_scope); - bind(context, ']', MDFR_ALT, highlight_prev_scope_absolute); - bind(context, '\'', MDFR_ALT, highlight_next_scope_absolute); - - bind(context, '/', MDFR_ALT, place_in_scope); - bind(context, '-', MDFR_ALT, delete_current_scope); - bind(context, 'j', MDFR_ALT, scope_absorb_down); - - bind(context, key_insert, MDFR_CTRL, write_explicit_enum_values); - bind(context, 'p', MDFR_ALT, rename_parameter); - end_map(context); - - BIND_4CODER_TESTS(context); - - int32_t result = end_bind_helper(context); - return(result); + Bind_Helper context_ = begin_bind_helper(data, size); + Bind_Helper *context = &context_; + + set_hook(context, hook_view_size_change, default_view_adjust); + + set_start_hook(context, default_start); + set_open_file_hook(context, default_file_settings); + set_new_file_hook(context, default_new_file); + set_save_file_hook(context, default_file_save); + set_end_file_hook(context, end_file_close_jump_list); + + set_input_filter(context, default_suppress_mouse_filter); + set_command_caller(context, default_command_caller); + + set_scroll_rule(context, smooth_scroll_rule); + + default_keys(context); + + // NOTE(allen|a4.0.6): Command maps can be opened more than + // once so that you can extend existing maps very easily. + // You can also use the helper "restart_map" instead of + // begin_map to clear everything that was in the map and + // bind new things instead. + begin_map(context, mapid_global); + end_map(context); + + begin_map(context, mapid_file); + bind(context, 's', MDFR_CTRL, punishment); + bind(context, 's', MDFR_ALT, save); + + bind(context, 'k', MDFR_ALT, kill_rect); + bind(context, ' ', MDFR_ALT | MDFR_CTRL, multi_line_edit); + + bind(context, key_page_up, MDFR_ALT, miblo_increment_time_stamp); + bind(context, key_page_down, MDFR_ALT, miblo_decrement_time_stamp); + + bind(context, key_home, MDFR_ALT, miblo_increment_time_stamp_minute); + bind(context, key_end, MDFR_ALT, miblo_decrement_time_stamp_minute); + + end_map(context); + + begin_map(context, default_code_map); + bind(context, '[', MDFR_ALT, highlight_surrounding_scope); + bind(context, ']', MDFR_ALT, highlight_prev_scope_absolute); + bind(context, '\'', MDFR_ALT, highlight_next_scope_absolute); + + bind(context, '/', MDFR_ALT, place_in_scope); + bind(context, '-', MDFR_ALT, delete_current_scope); + bind(context, 'j', MDFR_ALT, scope_absorb_down); + + bind(context, key_insert, MDFR_CTRL, write_explicit_enum_values); + bind(context, 'p', MDFR_ALT, rename_parameter); + end_map(context); + + BIND_4CODER_TESTS(context); + + int32_t result = end_bind_helper(context); + return(result); } #endif diff --git a/site/4ed_abstract_document.cpp b/site/4ed_abstract_document.cpp index d080c8cf..0fad1c9f 100644 --- a/site/4ed_abstract_document.cpp +++ b/site/4ed_abstract_document.cpp @@ -15,8 +15,8 @@ struct Enriched_Text{ String source; }; -static Enriched_Text -load_enriched_text(Partition *part, char *directory, char *filename){ +internal Enriched_Text +load_enriched_text(char *directory, char *filename){ Enriched_Text result = {0}; char space[256]; @@ -81,7 +81,7 @@ struct Document_Item{ } text; }; }; -static Document_Item null_document_item = {0}; +global Document_Item null_document_item = {0}; enum{ ItemType_Document, @@ -111,7 +111,6 @@ struct Abstract_Item{ // TODO(allen): make these external // Document building members - Partition *part; Document_Item *section_stack[16]; i32 section_top; @@ -122,7 +121,7 @@ struct Abstract_Item{ float h_w_ratio; Basic_List img_instantiations; }; -static Abstract_Item null_abstract_item = {0}; +global Abstract_Item null_abstract_item = {0}; struct Image_Instantiation{ i32 w, h; @@ -132,21 +131,19 @@ struct Document_System{ Basic_List doc_list; Basic_List img_list; Basic_List file_list; - Partition *part; }; -static Document_System -create_document_system(Partition *part){ +internal Document_System +create_document_system(){ Document_System system = {0}; - system.part = part; return(system); } -static void* -push_item_on_list(Partition *part, Basic_List *list, i32 item_size){ +internal void* +push_item_on_list(Basic_List *list, i32 item_size){ i32 mem_size = item_size + sizeof(Basic_Node); - void *mem = push_block(part, mem_size); - assert(mem != 0); + void *mem = fm__push(mem_size); + Assert(mem != 0); memset(mem, 0, mem_size); Basic_Node *node = (Basic_Node*)mem; @@ -163,7 +160,7 @@ push_item_on_list(Partition *part, Basic_List *list, i32 item_size){ return(result); } -static Abstract_Item* +internal Abstract_Item* get_item_by_name(Basic_List list, String name){ Abstract_Item *result = 0; @@ -180,7 +177,7 @@ get_item_by_name(Basic_List list, String name){ return(result); } -static Abstract_Item* +internal Abstract_Item* get_item_by_name(Basic_List list, char *name){ Abstract_Item *result = 0; @@ -197,7 +194,7 @@ get_item_by_name(Basic_List list, char *name){ return(result); } -static Image_Instantiation* +internal Image_Instantiation* get_image_instantiation(Basic_List list, i32 w, i32 h){ Image_Instantiation *result = 0; @@ -214,59 +211,58 @@ get_image_instantiation(Basic_List list, i32 w, i32 h){ return(result); } -static Abstract_Item* -create_item(Partition *part, Basic_List *list, char *name){ +internal Abstract_Item* +create_item(Basic_List *list, char *name){ Abstract_Item *lookup = get_item_by_name(*list, name); Abstract_Item *result = 0; if (lookup == 0){ - result = (Abstract_Item*)push_item_on_list(part, list, sizeof(Abstract_Item)); + result = (Abstract_Item*)push_item_on_list(list, sizeof(Abstract_Item)); } return(result); } -static void -add_image_instantiation(Partition *part, Basic_List *list, i32 w, i32 h){ - Image_Instantiation *instantiation = (Image_Instantiation*)push_item_on_list(part, list, sizeof(Image_Instantiation)); +internal void +add_image_instantiation(Basic_List *list, i32 w, i32 h){ + Image_Instantiation *instantiation = (Image_Instantiation*)push_item_on_list(list, sizeof(Image_Instantiation)); instantiation->w = w; instantiation->h = h; } -static void -set_section_name(Partition *part, Document_Item *item, char *name, i32 show_title){ +internal void +set_section_name(Document_Item *item, char *name, i32 show_title){ i32 name_len = str_size(name); - item->section.name = make_string_cap(push_array(part, char, name_len+1), 0, name_len+1); - partition_align(part, 8); + item->section.name = make_string_cap(fm_push_array(char, name_len+1), 0, name_len+1); + fm_align(); append_sc(&item->section.name, name); item->section.show_title = show_title; } -static void -set_section_id(Partition *part, Document_Item *item, char *id){ +internal void +set_section_id(Document_Item *item, char *id){ i32 id_len = str_size(id); - item->section.id = make_string_cap(push_array(part, char, id_len+1), 0, id_len+1); - partition_align(part, 8); + item->section.id = make_string_cap(fm_push_array(char, id_len+1), 0, id_len+1); + fm_align(); append_sc(&item->section.id, id); } -static void -begin_document_description(Abstract_Item *doc, Partition *part, char *title, i32 show_title){ +internal void +begin_document_description(Abstract_Item *doc, char *title, i32 show_title){ *doc = null_abstract_item; doc->item_type = ItemType_Document; - doc->part = part; - doc->root_item = push_struct(doc->part, Document_Item); + doc->root_item = fm_push_array(Document_Item, 1); *doc->root_item = null_document_item; doc->section_stack[doc->section_top] = doc->root_item; doc->root_item->type = Doc_Root; - set_section_name(doc->part, doc->root_item, title, show_title); + set_section_name(doc->root_item, title, show_title); } -static Abstract_Item* +internal Abstract_Item* add_generic_file(Document_System *system, char *source_file, char *extension, char *name){ - Abstract_Item *item = create_item(system->part, &system->file_list, name); + Abstract_Item *item = create_item(&system->file_list, name); if (item){ item->item_type = ItemType_GenericFile; item->extension = extension; @@ -276,10 +272,10 @@ add_generic_file(Document_System *system, char *source_file, char *extension, ch return(item); } -static Abstract_Item* +internal Abstract_Item* add_image_description(Document_System *system, char *source_file, char *extension, char *name){ - Abstract_Item *item = create_item(system->part, &system->img_list, name); - if (item){ + Abstract_Item *item = create_item(&system->img_list, name); + if (item != 0){ item->item_type = ItemType_Image; item->extension = extension; item->source_file = source_file; @@ -300,22 +296,22 @@ add_image_description(Document_System *system, char *source_file, char *extensio return(item); } -static Abstract_Item* +internal Abstract_Item* begin_document_description(Document_System *system, char *title, char *name, i32 show_title){ - Abstract_Item *item = create_item(system->part, &system->doc_list, name); + Abstract_Item *item = create_item(&system->doc_list, name); if (item){ - begin_document_description(item, system->part, title, show_title); + begin_document_description(item, title, show_title); item->name = name; } return(item); } -static void +internal void end_document_description(Abstract_Item *item){ Assert(item->section_top == 0); } -static void +internal void append_child(Document_Item *parent, Document_Item *item){ Assert(parent->type == Doc_Root || parent->type == Doc_Section); if (parent->section.last_child == 0){ @@ -328,45 +324,45 @@ append_child(Document_Item *parent, Document_Item *item){ item->parent = parent; } -static void +internal void begin_section(Abstract_Item *item, char *title, char *id){ Assert(item->section_top+1 < ArrayCount(item->section_stack)); Document_Item *parent = item->section_stack[item->section_top]; - Document_Item *section = push_struct(item->part, Document_Item); + Document_Item *section = fm_push_array(Document_Item, 1); *section = null_document_item; item->section_stack[++item->section_top] = section; section->type = Doc_Section; - set_section_name(item->part, section, title, 1); - if (id){ - set_section_id(item->part, section, id); + set_section_name(section, title, 1); + if (id != 0){ + set_section_id(section, id); } append_child(parent, section); } -static void +internal void end_section(Abstract_Item *doc){ Assert(doc->section_top > 0); --doc->section_top; } -static void +internal void add_todo(Abstract_Item *doc){ Document_Item *parent = doc->section_stack[doc->section_top]; - Document_Item *item = push_struct(doc->part, Document_Item); + Document_Item *item = fm_push_array(Document_Item, 1); *item = null_document_item; item->type = Doc_Todo; append_child(parent, item); } -static void +internal void add_element_list(Abstract_Item *doc, Meta_Unit *unit){ Document_Item *parent = doc->section_stack[doc->section_top]; - Document_Item *item = push_struct(doc->part, Document_Item); + Document_Item *item = fm_push_array(Document_Item, 1); *item = null_document_item; item->type = Doc_Element_List; item->unit_elements.unit = unit; @@ -374,10 +370,10 @@ add_element_list(Abstract_Item *doc, Meta_Unit *unit){ append_child(parent, item); } -static void +internal void add_element_list(Abstract_Item *doc, Meta_Unit *unit, Alternate_Names_Array *alt_names, i32 alt_name_type){ Document_Item *parent = doc->section_stack[doc->section_top]; - Document_Item *item = push_struct(doc->part, Document_Item); + Document_Item *item = fm_push_array(Document_Item, 1); *item = null_document_item; item->type = Doc_Element_List; item->unit_elements.unit = unit; @@ -387,10 +383,10 @@ add_element_list(Abstract_Item *doc, Meta_Unit *unit, Alternate_Names_Array *alt append_child(parent, item); } -static void +internal void add_full_elements(Abstract_Item *doc, Meta_Unit *unit){ Document_Item *parent = doc->section_stack[doc->section_top]; - Document_Item *item = push_struct(doc->part, Document_Item); + Document_Item *item = fm_push_array(Document_Item, 1); *item = null_document_item; item->type = Doc_Full_Elements; item->unit_elements.unit = unit; @@ -398,10 +394,10 @@ add_full_elements(Abstract_Item *doc, Meta_Unit *unit){ append_child(parent, item); } -static void +internal void add_full_elements(Abstract_Item *doc, Meta_Unit *unit, Alternate_Names_Array *alt_names, i32 alt_name_type){ Document_Item *parent = doc->section_stack[doc->section_top]; - Document_Item *item = push_struct(doc->part, Document_Item); + Document_Item *item = fm_push_array(Document_Item, 1); *item = null_document_item; item->type = Doc_Full_Elements; item->unit_elements.unit = unit; @@ -411,22 +407,22 @@ add_full_elements(Abstract_Item *doc, Meta_Unit *unit, Alternate_Names_Array *al append_child(parent, item); } -static void +internal void add_table_of_contents(Abstract_Item *doc){ Document_Item *parent = doc->section_stack[doc->section_top]; - Document_Item *item = push_struct(doc->part, Document_Item); + Document_Item *item = fm_push_array(Document_Item, 1); *item = null_document_item; item->type = Doc_Table_Of_Contents; append_child(parent, item); } -static void +internal void add_enriched_text(Abstract_Item *doc, Enriched_Text *text){ Assert(doc->section_top+1 < ArrayCount(doc->section_stack)); Document_Item *parent = doc->section_stack[doc->section_top]; - Document_Item *item = push_struct(doc->part, Document_Item); + Document_Item *item = fm_push_array(Document_Item, 1); *item = null_document_item; item->type = Doc_Enriched_Text; item->text.text = text; @@ -478,7 +474,7 @@ struct Section_Counter{ i32 nest_level; }; -static i32 +internal i32 doc_get_link_string(Abstract_Item *doc, char *space, i32 capacity){ String str = make_string_cap(space, 0, capacity); append_sc(&str, doc->name); @@ -487,7 +483,7 @@ doc_get_link_string(Abstract_Item *doc, char *space, i32 capacity){ return(result); } -static i32 +internal i32 img_get_link_string(Abstract_Item *img, char *space, i32 capacity, i32 w, i32 h){ String str = make_string_cap(space, 0, capacity); append_sc(&str, img->name); @@ -503,7 +499,7 @@ img_get_link_string(Abstract_Item *img, char *space, i32 capacity, i32 w, i32 h) return(result); } -static void +internal void append_section_number_reduced(String *out, Section_Counter *section_counter, i32 reduce){ i32 level = section_counter->nest_level-reduce; for (i32 i = 1; i <= level; ++i){ @@ -514,12 +510,12 @@ append_section_number_reduced(String *out, Section_Counter *section_counter, i32 } } -static void +internal void append_section_number(String *out, Section_Counter *section_counter){ append_section_number_reduced(out, section_counter, 0); } -static i32 +internal i32 extract_command_body(String *out, String l, i32 *i_in_out, i32 *body_start_out, i32 *body_end_out, String command_name, i32 require_body){ i32 result = 0; @@ -569,7 +565,7 @@ extract_command_body(String *out, String l, i32 *i_in_out, i32 *body_start_out, return(result); } -static void +internal void html_render_section_header(String *out, String section_name, String section_id, Section_Counter *section_counter){ if (section_counter->nest_level <= 1){ if (section_id.size > 0){ @@ -603,8 +599,8 @@ html_render_section_header(String *out, String section_name, String section_id, #define HTML_WIDTH 800 -static void -write_enriched_text_html(String *out, Partition *part, Enriched_Text *text, Document_System *doc_system, Section_Counter *section_counter){ +internal void +write_enriched_text_html(String *out, Enriched_Text *text, Document_System *doc_system, Section_Counter *section_counter){ String source = text->source; append_sc(out, "
"); @@ -675,7 +671,7 @@ write_enriched_text_html(String *out, Partition *part, Enriched_Text *text, Docu Cmd_COUNT, }; - static String enriched_commands[Cmd_COUNT]; + local_persist String enriched_commands[Cmd_COUNT]; enriched_commands[Cmd_BackSlash] = make_lit_string("\\"); enriched_commands[Cmd_Version] = make_lit_string("VERSION"); @@ -854,7 +850,7 @@ write_enriched_text_html(String *out, Partition *part, Enriched_Text *text, Docu char space[256]; if (img_get_link_string(img_lookup, space, sizeof(space), pixel_width, pixel_height)){ append_sc(out, space); - add_image_instantiation(part, &img_lookup->img_instantiations, pixel_width, pixel_height); + add_image_instantiation(&img_lookup->img_instantiations, pixel_width, pixel_height); } else{ NotImplemented; @@ -918,7 +914,7 @@ write_enriched_text_html(String *out, Partition *part, Enriched_Text *text, Docu append_sc(out, "
"); } -static void +internal void print_item_in_list(String *out, String name, char *id_postfix){ append_sc(out, "
  • "HTML_DOC_ITEM_OPEN); - print_doc_description(out, part, doc.main_doc); + print_doc_description(out, doc.main_doc); append_sc(out, HTML_DOC_ITEM_CLOSE""); append_sc(out, ""); @@ -1220,7 +1216,7 @@ print_struct_docs(String *out, Partition *part, Item_Node *member){ } } -static void +internal void print_see_also(String *out, Documentation *doc){ i32 doc_see_count = doc->see_also_count; if (doc_see_count > 0){ @@ -1237,18 +1233,18 @@ print_see_also(String *out, Documentation *doc){ } } -static void -print_function_docs(String *out, Partition *part, String name, String doc_string){ +internal void +print_function_docs(String *out, String name, String doc_string){ if (doc_string.size == 0){ append_sc(out, "No documentation generated for this function."); fprintf(stderr, "warning: no documentation string for %.*s\n", name.size, name.str); } - Temp_Memory temp = begin_temp_memory(part); + Temp temp = fm_begin_temp(); Documentation doc = {0}; - perform_doc_parse(part, doc_string, &doc); + perform_doc_parse(doc_string, &doc); i32 doc_param_count = doc.param_count; if (doc_param_count > 0){ @@ -1279,18 +1275,18 @@ print_function_docs(String *out, Partition *part, String name, String doc_string String main_doc = doc.main_doc; if (main_doc.size != 0){ append_sc(out, HTML_DOC_HEAD_OPEN"Description"HTML_DOC_HEAD_CLOSE HTML_DOC_ITEM_OPEN); - print_doc_description(out, part, main_doc); + print_doc_description(out, main_doc); append_sc(out, HTML_DOC_ITEM_CLOSE); } print_see_also(out, &doc); - end_temp_memory(temp); + fm_end_temp(temp); } -static void -print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, char *id_postfix, char *section, i32 I, Alternate_Name *alt_name, i32 alt_name_type){ - Temp_Memory temp = begin_temp_memory(part); +internal void +print_item_html(String *out, Used_Links *used, Item_Node *item, char *id_postfix, char *section, i32 I, Alternate_Name *alt_name, i32 alt_name_type){ + Temp temp = fm_begin_temp(); String name = item->name; @@ -1347,7 +1343,7 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, append_sc(out, ""); // NOTE(allen): Descriptive section - print_function_docs(out, part, name, item->doc_string); + print_function_docs(out, name, item->doc_string); }break; case Item_Macro: @@ -1359,7 +1355,7 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, append_sc(out, ""); // NOTE(allen): Descriptive section - print_function_docs(out, part, name, item->doc_string); + print_function_docs(out, name, item->doc_string); }break; case Item_Typedef: @@ -1379,14 +1375,14 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, // NOTE(allen): Descriptive section String doc_string = item->doc_string; Documentation doc = {0}; - perform_doc_parse(part, doc_string, &doc); + perform_doc_parse(doc_string, &doc); String main_doc = doc.main_doc; if (main_doc.size != 0){ append_sc(out, HTML_DOC_HEAD_OPEN"Description"HTML_DOC_HEAD_CLOSE); append_sc(out, HTML_DOC_ITEM_OPEN); - print_doc_description(out, part, main_doc); + print_doc_description(out, main_doc); append_sc(out, HTML_DOC_ITEM_CLOSE); } else{ @@ -1410,14 +1406,14 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, // NOTE(allen): Descriptive section String doc_string = item->doc_string; Documentation doc = {0}; - perform_doc_parse(part, doc_string, &doc); + perform_doc_parse(doc_string, &doc); String main_doc = doc.main_doc; if (main_doc.size != 0){ append_sc(out, HTML_DOC_HEAD_OPEN"Description"HTML_DOC_HEAD_CLOSE); append_sc(out, HTML_DOC_ITEM_OPEN); - print_doc_description(out, part, main_doc); + print_doc_description(out, main_doc); append_sc(out, HTML_DOC_ITEM_CLOSE); } else{ @@ -1431,7 +1427,7 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, member; member = member->next_sibling){ Documentation doc = {0}; - perform_doc_parse(part, member->doc_string, &doc); + perform_doc_parse(member->doc_string, &doc); append_sc(out, "
    "); @@ -1448,7 +1444,7 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, append_sc(out, "
    "); append_sc(out, "
    "HTML_DOC_ITEM_OPEN); - print_doc_description(out, part, doc.main_doc); + print_doc_description(out, doc.main_doc); append_sc(out, HTML_DOC_ITEM_CLOSE"
    "); append_sc(out, ""); @@ -1488,14 +1484,14 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, // NOTE(allen): Descriptive section { Documentation doc = {0}; - perform_doc_parse(part, doc_string, &doc); + perform_doc_parse(doc_string, &doc); String main_doc = doc.main_doc; if (main_doc.size != 0){ append_sc(out, HTML_DOC_HEAD_OPEN"Description"HTML_DOC_HEAD_CLOSE); append_sc(out, HTML_DOC_ITEM_OPEN); - print_doc_description(out, part, main_doc); + print_doc_description(out, main_doc); append_sc(out, HTML_DOC_ITEM_CLOSE); } else{ @@ -1505,7 +1501,7 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, if (!hide_members){ if (item->first_child){ append_sc(out, HTML_DOC_HEAD_OPEN"Fields"HTML_DOC_HEAD_CLOSE); - print_struct_docs(out, part, item); + print_struct_docs(out, item); } } @@ -1521,11 +1517,11 @@ print_item_html(String *out, Partition *part, Used_Links *used, Item_Node *item, // NOTE(allen): Close the item box append_sc(out, "
    "); - end_temp_memory(temp); + fm_end_temp(temp); } -static void -doc_item_head_html(String *out, Partition *part, Document_System *doc_system, Used_Links *used_links, Document_Item *item, Section_Counter *section_counter){ +internal void +doc_item_head_html(String *out, Document_System *doc_system, Used_Links *used_links, Document_Item *item, Section_Counter *section_counter){ switch (item->type){ case Doc_Root: { @@ -1610,7 +1606,7 @@ doc_item_head_html(String *out, Partition *part, Document_System *doc_system, Us case Doc_Enriched_Text: { - write_enriched_text_html(out, part, item->text.text, doc_system, section_counter); + write_enriched_text_html(out, item->text.text, doc_system, section_counter); }break; case Doc_Element_List: @@ -1661,13 +1657,13 @@ doc_item_head_html(String *out, Partition *part, Document_System *doc_system, Us if (alt_names){ i32 I = 1; for (i32 i = 0; i < count; ++i, ++I){ - print_item_html(out, part, used_links, &unit->set.items[i], "_doc", section_str.str, I, &alt_names->names[i], item->unit_elements.alt_name_type); + print_item_html(out, used_links, &unit->set.items[i], "_doc", section_str.str, I, &alt_names->names[i], item->unit_elements.alt_name_type); } } else{ i32 I = 1; for (i32 i = 0; i < count; ++i, ++I){ - print_item_html(out, part, used_links, &unit->set.items[i], "_doc", section_str.str, I, 0, 0); + print_item_html(out, used_links, &unit->set.items[i], "_doc", section_str.str, I, 0, 0); } } }break; @@ -1702,8 +1698,8 @@ doc_item_head_html(String *out, Partition *part, Document_System *doc_system, Us } } -static void -doc_item_foot_html(String *out, Partition *part, Document_System *doc_system, Used_Links *used_links, Document_Item *item, Section_Counter *section_counter){ +internal void +doc_item_foot_html(String *out, Document_System *doc_system, Used_Links *used_links, Document_Item *item, Section_Counter *section_counter){ switch (item->type){ case Doc_Root: { @@ -1716,9 +1712,9 @@ doc_item_foot_html(String *out, Partition *part, Document_System *doc_system, Us } } -static void -generate_item_html(String *out, Partition *part, Document_System *doc_system, Used_Links *used_links, Document_Item *item, Section_Counter *section_counter){ - doc_item_head_html(out, part, doc_system, used_links, item, section_counter); +internal void +generate_item_html(String *out, Document_System *doc_system, Used_Links *used_links, Document_Item *item, Section_Counter *section_counter){ + doc_item_head_html(out, doc_system, used_links, item, section_counter); if (item->type == Doc_Root || item->type == Doc_Section){ i32 level = ++section_counter->nest_level; @@ -1726,25 +1722,25 @@ generate_item_html(String *out, Partition *part, Document_System *doc_system, Us for (Document_Item *m = item->section.first_child; m != 0; m = m->next){ - generate_item_html(out, part, doc_system, used_links, m, section_counter); + generate_item_html(out, doc_system, used_links, m, section_counter); } --section_counter->nest_level; ++section_counter->counter[section_counter->nest_level]; } - doc_item_foot_html(out, part, doc_system, used_links, item, section_counter); + doc_item_foot_html(out, doc_system, used_links, item, section_counter); } -static void -generate_document_html(String *out, Partition *part, Document_System *doc_system, Abstract_Item *doc){ - assert(doc->root_item != 0); +internal void +generate_document_html(String *out, Document_System *doc_system, Abstract_Item *doc){ + Assert(doc->root_item != 0); Used_Links used_links = {0}; - init_used_links(part, &used_links, 4000); + init_used_links(&used_links, 4000); Section_Counter section_counter = {0}; section_counter.counter[section_counter.nest_level] = 1; - generate_item_html(out, part, doc_system, &used_links, doc->root_item, §ion_counter); + generate_item_html(out, doc_system, &used_links, doc->root_item, §ion_counter); } // BOTTOM diff --git a/site/4ed_sitegen.cpp b/site/4ed_sitegen.cpp index 69a3f57a..538505e0 100644 --- a/site/4ed_sitegen.cpp +++ b/site/4ed_sitegen.cpp @@ -23,14 +23,15 @@ #include "../4ed_defines.h" #include "../meta/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" +#define FTECH_FILE_MOVING_IMPLEMENTATION +#include "../meta/4ed_file_moving.h" #include "../meta/4ed_meta_parser.cpp" +#include "../meta/4ed_meta_keywords.h" #include "../meta/4ed_out_context.cpp" #include "4ed_abstract_document.cpp" @@ -89,13 +90,13 @@ print_function_body_code(String *out, Parse_Context *context, i32 start){ } static void -do_html_output(Document_System *doc_system, Partition *part, char *dst_directory, Abstract_Item *doc){ +do_html_output(Document_System *doc_system, char *dst_directory, Abstract_Item *doc){ // NOTE(allen): Output i32 out_size = 10 << 20; - Tail_Temp_Partition temp = begin_tail_part(part, out_size); + void *mem = malloc(out_size); + Assert(mem != 0); + String out = make_string_cap(mem, 0, out_size); - String out = str_alloc(&temp.part, out_size); - assert(out.str); Out_Context context = {0}; set_context_directory(&context, dst_directory); @@ -103,7 +104,7 @@ do_html_output(Document_System *doc_system, Partition *part, char *dst_directory char space[256]; if (doc_get_link_string(doc, space, sizeof(space))){ if (begin_file_out(&context, space, &out)){ - generate_document_html(&out, part, doc_system, doc); + generate_document_html(&out, doc_system, doc); end_file_out(context); } else{ @@ -111,13 +112,13 @@ do_html_output(Document_System *doc_system, Partition *part, char *dst_directory } } - end_tail_part(temp); + free(mem); } static Abstract_Item* -generate_homepage(Document_System *doc_system, Partition *part, char *src_directory){ - Enriched_Text *home = push_struct(part, Enriched_Text); - *home = load_enriched_text(part, src_directory, "home.txt"); +generate_homepage(Document_System *doc_system, char *src_directory){ + Enriched_Text *home = fm_push_array(Enriched_Text, 1); + *home = load_enriched_text(src_directory, "home.txt"); Abstract_Item *doc = begin_document_description(doc_system, "4coder Home", "home", 0); add_enriched_text(doc, home); @@ -128,49 +129,38 @@ generate_homepage(Document_System *doc_system, Partition *part, char *src_direct // TODO(allen): replace the documentation declaration system with a straight up enriched text system static Abstract_Item* -generate_4coder_docs(Document_System *doc_system, Partition *part, char *code_directory, char *src_directory){ - static Meta_Keywords meta_keywords[] = { - {make_lit_string("API_EXPORT") , Item_Function } , - {make_lit_string("API_EXPORT_INLINE") , Item_Function } , - {make_lit_string("API_EXPORT_MACRO") , Item_Macro } , - {make_lit_string("CPP_NAME") , Item_CppName } , - {make_lit_string("TYPEDEF") , Item_Typedef } , - {make_lit_string("STRUCT") , Item_Struct } , - {make_lit_string("UNION") , Item_Union } , - {make_lit_string("ENUM") , Item_Enum } , - }; - +generate_4coder_docs(Document_System *doc_system, char *code_directory, char *src_directory){ #define ExpandArray(a) (a), (ArrayCount(a)) - Meta_Unit *custom_types_unit = push_struct(part, Meta_Unit); - Meta_Unit *lexer_funcs_unit = push_struct(part, Meta_Unit); - Meta_Unit *lexer_types_unit = push_struct(part, Meta_Unit); - Meta_Unit *string_unit = push_struct(part, Meta_Unit); - Meta_Unit *custom_funcs_unit = push_struct(part, Meta_Unit); + Meta_Unit *custom_types_unit = fm_push_array(Meta_Unit, 1); + Meta_Unit *lexer_funcs_unit = fm_push_array(Meta_Unit, 1); + Meta_Unit *lexer_types_unit = fm_push_array(Meta_Unit, 1); + Meta_Unit *string_unit = fm_push_array(Meta_Unit, 1); + Meta_Unit *custom_funcs_unit = fm_push_array(Meta_Unit, 1); - Enriched_Text *introduction = push_struct(part, Enriched_Text); - Enriched_Text *lexer_introduction = push_struct(part, Enriched_Text); + Enriched_Text *introduction = fm_push_array(Enriched_Text, 1); + Enriched_Text *lexer_introduction = fm_push_array(Enriched_Text, 1); // NOTE(allen): Parse the code. - *custom_types_unit = compile_meta_unit(part, code_directory, "4coder_API/types.h", ExpandArray(meta_keywords)); + *custom_types_unit = compile_meta_unit(code_directory, "4coder_API/types.h", ExpandArray(meta_keywords)); Assert(custom_types_unit->count != 0); - *lexer_funcs_unit = compile_meta_unit(part, code_directory, "4cpp/4cpp_lexer.h", ExpandArray(meta_keywords)); + *lexer_funcs_unit = compile_meta_unit(code_directory, "4cpp/4cpp_lexer.h", ExpandArray(meta_keywords)); Assert(lexer_funcs_unit->count != 0); - *lexer_types_unit = compile_meta_unit(part, code_directory, "4cpp/4cpp_lexer_types.h", ExpandArray(meta_keywords)); + *lexer_types_unit = compile_meta_unit(code_directory, "4cpp/4cpp_lexer_types.h", ExpandArray(meta_keywords)); Assert(lexer_types_unit->count != 0); - *string_unit = compile_meta_unit(part, code_directory, "string/internal_4coder_string.cpp", ExpandArray(meta_keywords)); + *string_unit = compile_meta_unit(code_directory, "string/internal_4coder_string.cpp", ExpandArray(meta_keywords)); Assert(string_unit->count != 0); - *custom_funcs_unit = compile_meta_unit(part, code_directory, "4ed_api_implementation.cpp", ExpandArray(meta_keywords)); + *custom_funcs_unit = compile_meta_unit(code_directory, "4ed_api_implementation.cpp", ExpandArray(meta_keywords)); Assert(custom_funcs_unit->count != 0); // NOTE(allen): Compute and store variations of the custom function names - Alternate_Names_Array *custom_func_names = push_struct(part, Alternate_Names_Array); + Alternate_Names_Array *custom_func_names = fm_push_array(Alternate_Names_Array, 1); i32 name_count = custom_funcs_unit->set.count; - custom_func_names->names = push_array(part, Alternate_Name, name_count); + custom_func_names->names = fm_push_array(Alternate_Name, name_count); memset(custom_func_names->names, 0, sizeof(*custom_func_names->names)*name_count); for (i32 i = 0; i < custom_funcs_unit->set.count; ++i){ @@ -178,19 +168,19 @@ generate_4coder_docs(Document_System *doc_system, Partition *part, char *code_di String *macro = &custom_func_names->names[i].macro; String *public_name = &custom_func_names->names[i].public_name; - *macro = str_alloc(part, name_string.size+4); + *macro = str_alloc(name_string.size+4); to_upper_ss(macro, name_string); append_ss(macro, make_lit_string("_SIG")); - *public_name = str_alloc(part, name_string.size); + *public_name = str_alloc(name_string.size); to_lower_ss(public_name, name_string); - partition_align(part, 4); + fm_align(); } // NOTE(allen): Load enriched text materials - *introduction = load_enriched_text(part, src_directory, "introduction.txt"); - *lexer_introduction = load_enriched_text(part, src_directory, "lexer_introduction.txt"); + *introduction = load_enriched_text(src_directory, "introduction.txt"); + *lexer_introduction = load_enriched_text(src_directory, "lexer_introduction.txt"); // NOTE(allen): Put together the abstract document Abstract_Item *doc = begin_document_description(doc_system, "4coder API Docs", "custom_docs", 1); @@ -262,9 +252,9 @@ generate_4coder_docs(Document_System *doc_system, Partition *part, char *code_di } static Abstract_Item* -generate_feature_list(Document_System *doc_system, Partition *part, char *src_directory){ - Enriched_Text *feature_list = push_struct(part, Enriched_Text); - *feature_list = load_enriched_text(part, src_directory, "feature_list.txt"); +generate_feature_list(Document_System *doc_system, char *src_directory){ + Enriched_Text *feature_list = fm_push_array(Enriched_Text, 1); + *feature_list = load_enriched_text(src_directory, "feature_list.txt"); Abstract_Item *doc = begin_document_description(doc_system, "4coder Feature List", "features", 0); add_enriched_text(doc, feature_list); @@ -274,9 +264,9 @@ generate_feature_list(Document_System *doc_system, Partition *part, char *src_di } static Abstract_Item* -generate_binding_list(Document_System *doc_system, Partition *part, char *src_directory){ - Enriched_Text *binding_list = push_struct(part, Enriched_Text); - *binding_list = load_enriched_text(part, src_directory, "binding_list.txt"); +generate_binding_list(Document_System *doc_system, char *src_directory){ + Enriched_Text *binding_list = fm_push_array(Enriched_Text, 1); + *binding_list = load_enriched_text(src_directory, "binding_list.txt"); Abstract_Item *doc = begin_document_description(doc_system, "4coder Binding List", "bindings", 0); add_enriched_text(doc, binding_list); @@ -286,9 +276,9 @@ generate_binding_list(Document_System *doc_system, Partition *part, char *src_di } static Abstract_Item* -generate_roadmap(Document_System *doc_system, Partition *part, char *src_directory){ - Enriched_Text *roadmap = push_struct(part, Enriched_Text); - *roadmap = load_enriched_text(part, src_directory, "roadmap.txt"); +generate_roadmap(Document_System *doc_system, char *src_directory){ + Enriched_Text *roadmap = fm_push_array(Enriched_Text, 1); + *roadmap = load_enriched_text(src_directory, "roadmap.txt"); Abstract_Item *doc = begin_document_description(doc_system, "4coder Roadmap", "roadmap", 0); add_enriched_text(doc, roadmap); @@ -298,9 +288,9 @@ generate_roadmap(Document_System *doc_system, Partition *part, char *src_directo } static Abstract_Item* -generate_tutorials(Document_System *doc_system, Partition *part, char *src_directory){ - Enriched_Text *roadmap = push_struct(part, Enriched_Text); - *roadmap = load_enriched_text(part, src_directory, "tutorials.txt"); +generate_tutorials(Document_System *doc_system, char *src_directory){ + Enriched_Text *roadmap = fm_push_array(Enriched_Text, 1); + *roadmap = load_enriched_text(src_directory, "tutorials.txt"); Abstract_Item *doc = begin_document_description(doc_system, "4coder Tutorials", "tutorials", 0); add_enriched_text(doc, roadmap); @@ -310,16 +300,16 @@ generate_tutorials(Document_System *doc_system, Partition *part, char *src_direc } static String -push_string(Partition *part, i32 size){ +push_string(i32 size){ String str = {0}; str.memory_size = size; - str.str = push_array(part, char, size); - partition_align(part, 4); + str.str = fm_push_array(char, size); + fm_align(); return(str); } static void -do_image_resize(Partition *part, char *src_file, char *dst_file, char *extension, i32 w, i32 h){ +do_image_resize(char *src_file, char *dst_file, char *extension, i32 w, i32 h){ i32 x = 0, y = 0, channels = 0; stbi_uc *image = stbi_load(src_file, &x, &y, &channels, 0); @@ -336,15 +326,9 @@ do_image_resize(Partition *part, char *src_file, char *dst_file, char *extension static void generate_site(char *code_directory, char *asset_directory, char *src_directory, char *dst_directory){ - i32 size = (512 << 20); - void *mem = malloc(size); - memset(mem, 0, size); - - Partition part_ = make_part(mem, size); - Partition *part = &part_; String str; - Document_System doc_system = create_document_system(part); + Document_System doc_system = create_document_system(); // TODO(allen): code compression here struct Site_Asset{ @@ -370,7 +354,7 @@ generate_site(char *code_directory, char *asset_directory, char *src_directory, for (u32 i = 0; i < ArrayCount(asset_list); ++i){ Site_Asset *asset = &asset_list[i]; - str = push_string(part, 256); + str = push_string(256); append_sc(&str, asset_directory); append_sc(&str, "/"); append_sc(&str, asset->filename); @@ -391,42 +375,36 @@ generate_site(char *code_directory, char *asset_directory, char *src_directory, } } - generate_homepage(&doc_system, part, src_directory); - generate_4coder_docs(&doc_system, part, code_directory, src_directory); - generate_feature_list(&doc_system, part, src_directory); - generate_binding_list(&doc_system, part, src_directory); - generate_roadmap(&doc_system, part, src_directory); - generate_tutorials(&doc_system, part, src_directory); + generate_homepage(&doc_system, src_directory); + generate_4coder_docs(&doc_system, code_directory, src_directory); + generate_feature_list(&doc_system, src_directory); + generate_binding_list(&doc_system, src_directory); + generate_roadmap(&doc_system, src_directory); + generate_tutorials(&doc_system, src_directory); for (Basic_Node *node = doc_system.doc_list.head; node != 0; node = node->next){ Abstract_Item *doc = NodeGetData(node, Abstract_Item); - assert(doc->item_type == ItemType_Document); - do_html_output(&doc_system, part, dst_directory, doc); + Assert(doc->item_type == ItemType_Document); + do_html_output(&doc_system, dst_directory, doc); } for (Basic_Node *node = doc_system.file_list.head; node != 0; node = node->next){ Abstract_Item *file = NodeGetData(node, Abstract_Item); - assert(file->item_type == ItemType_GenericFile); + Assert(file->item_type == ItemType_GenericFile); - char space[256]; - String str = make_fixed_width_string(space); - append_sc(&str, file->name); - append_sc(&str, "."); - append_sc(&str, file->extension); - terminate_with_null(&str); - - do_file_copy(part, file->source_file, dst_directory, space); + char *file_name = fm_str(file->name, ".", file->extension); + fm_copy_file(fm_str(file_name), fm_str(dst_directory, "/", file_name)); } for (Basic_Node *node = doc_system.img_list.head; node != 0; node = node->next){ Abstract_Item *img = NodeGetData(node, Abstract_Item); - assert(img->item_type == ItemType_Image); + Assert(img->item_type == ItemType_Image); for (Basic_Node *node = img->img_instantiations.head; node != 0; @@ -443,7 +421,7 @@ generate_site(char *code_directory, char *asset_directory, char *src_directory, append_sc(&str, space); terminate_with_null(&str); - do_image_resize(part, img->source_file, space2, img->extension, inst->w, inst->h); + do_image_resize(img->source_file, space2, img->extension, inst->w, inst->h); } } } @@ -451,6 +429,7 @@ generate_site(char *code_directory, char *asset_directory, char *src_directory, int main(int argc, char **argv){ META_BEGIN(); + fm_init_system(); if (argc == 5){ generate_site(argv[1], argv[2], argv[3], argv[4]); diff --git a/string/4coder_string_build_num.txt b/string/4coder_string_build_num.txt index 0809364e..7a6a8e7d 100644 --- a/string/4coder_string_build_num.txt +++ b/string/4coder_string_build_num.txt @@ -1,5 +1,5 @@ 1 0 -93 +95 diff --git a/string/4ed_string_builder.cpp b/string/4ed_string_builder.cpp index 7b1521fc..17ea3adc 100644 --- a/string/4ed_string_builder.cpp +++ b/string/4ed_string_builder.cpp @@ -25,11 +25,12 @@ #include "../4ed_defines.h" #include "../meta/4ed_meta_defines.h" -#define FTECH_FILE_MOVING_IMPLEMENTATION -#include "../meta/4ed_file_moving.h" #include "../4coder_lib/4coder_mem.h" +#define FTECH_FILE_MOVING_IMPLEMENTATION +#include "../meta/4ed_file_moving.h" #include "../meta/4ed_meta_parser.cpp" +#include "../meta/4ed_meta_keywords.h" #include "../meta/4ed_out_context.cpp" #include @@ -42,44 +43,40 @@ #define V_MAJ STR_(V_MAJ_NUM) #define V_MIN STR_(V_MIN_NUM) -static b32 +internal char* +parse_next_line(char *str, char *str_end){ + char *ptr = str; + for (; ptr < str_end && *ptr != '\n'; ++ptr); + ++ptr; + return(ptr); +} + +internal b32 parse_build_number(char *file_name, i32 *major_out, i32 *minor_out, i32 *build_out){ b32 result = false; String file = file_dump(file_name); if (file.str != 0){ - char *end_str = file.str + file.size; - char *major_str = file.str; - char *minor_str = major_str; - for (; minor_str < end_str && *minor_str != '\n'; ++minor_str); - ++minor_str; - *major_out = 0; *minor_out = 0; *build_out = 0; - if (major_str < end_str){ - char *build_str = minor_str; - for (; build_str < end_str && *build_str != '\n'; ++build_str); - ++build_str; + char *end_str = file.str + file.size; + char *major_str = file.str; + char *minor_str = parse_next_line(major_str, end_str); + char *build_str = parse_next_line(minor_str, end_str); + char *ender = parse_next_line(build_str, end_str); + + if (major_str < end_str && build_str < end_str && ender < end_str){ + minor_str[-1] = 0; + build_str[-1] = 0; + ender[-1] = 0; - if (build_str < end_str){ - char *ender = build_str; - for (; ender < end_str && *ender != '\n'; ++ender); - - if (ender < end_str){ - *ender = 0; - } - - minor_str[-1] = 0; - build_str[-1] = 0; - - *major_out = str_to_int_c(major_str); - *minor_out = str_to_int_c(minor_str); - *build_out = str_to_int_c(build_str); - - result = true; - } + *major_out = str_to_int_c(major_str); + *minor_out = str_to_int_c(minor_str); + *build_out = str_to_int_c(build_str); + + result = true; } free(file.str); @@ -88,12 +85,10 @@ parse_build_number(char *file_name, i32 *major_out, i32 *minor_out, i32 *build_o return(result); } -static void +internal void save_build_number(char *file_name, i32 major, i32 minor, i32 build){ FILE *out = fopen(file_name, "wb"); - fprintf(out, "%d\n", major); - fprintf(out, "%d\n", minor); - fprintf(out, "%d\n\n\n", build); + fprintf(out, "%d\n%d\n%d\n\n\n", major, minor, build); fclose(out); } @@ -103,21 +98,21 @@ save_build_number(char *file_name, i32 major, i32 minor, i32 build){ // 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){ pstr = str_start_end(context->data, start, token->start); - append_ss(out, pstr); + append(out, pstr); } else{ do_whitespace_print = true; @@ -146,7 +141,7 @@ print_function_body_code(String *out, Parse_Context *context, int32_t start){ if (do_print){ pstr = get_lexeme(*token, context->data); - append_ss(out, pstr); + append(out, pstr); } start = token->start + token->size; @@ -166,25 +161,12 @@ int main(){ META_BEGIN(); fm_init_system(); - fm_init_system(); - i32 size = (512 << 20); void *mem = malloc(size); memset(mem, 0, size); Partition part_ = make_part(mem, size); - Partition *part = &part_; - - static Meta_Keywords meta_keywords[] = { - {make_lit_string("API_EXPORT") , Item_Function } , - {make_lit_string("API_EXPORT_INLINE") , Item_Function } , - {make_lit_string("API_EXPORT_MACRO") , Item_Macro } , - {make_lit_string("CPP_NAME") , Item_CppName } , - {make_lit_string("TYPEDEF") , Item_Typedef } , - {make_lit_string("STRUCT") , Item_Struct } , - {make_lit_string("UNION") , Item_Union } , - {make_lit_string("ENUM") , Item_Enum } , - }; + //Partition *part = &part_; // NOTE(allen): Parse the internal string file. char *string_files[] = { INTERNAL_STRING, 0 }; @@ -226,7 +208,7 @@ int main(){ // NOTE(allen): String Library if (begin_file_out(&context, GENERATED_FILE, &out)){ Cpp_Token *token = 0; - int32_t start = 0; + i32 start = 0; Parse parse = string_unit.parse[0]; Parse_Context pcontext = setup_parse_context(parse); @@ -235,33 +217,33 @@ int main(){ if (!(token->flags & CPP_TFLAG_PP_BODY) && token->type == CPP_TOKEN_IDENTIFIER){ String lexeme = get_lexeme(*token, pcontext.data); - if (match_ss(lexeme, make_lit_string("FSTRING_BEGIN"))){ + if (match(lexeme, "FSTRING_BEGIN")){ start = token->start + token->size; break; } } } - append_sc(&out, "/*\n"); + append(&out, "/*\n"); - append_sc(&out, GENERATED_FILE " - Version "V_MAJ"."V_MIN"."); + append(&out, GENERATED_FILE " - Version "V_MAJ"."V_MIN"."); append_int_to_str(&out, build_number); - append_sc(&out, "\n"); + append(&out, "\n"); - append_sc(&out, STANDARD_DISCLAIMER); - append_sc(&out, - "To include implementation: #define FSTRING_IMPLEMENTATION\n" - "To use in C mode: #define FSTRING_C\n"); + append(&out, STANDARD_DISCLAIMER); + append(&out, + "To include implementation: #define FSTRING_IMPLEMENTATION\n" + "To use in C mode: #define FSTRING_C\n"); - append_sc(&out, "*/\n"); + append(&out, "*/\n"); String pstr = {0}; - int32_t do_whitespace_print = true; + 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_ss(&out, pstr); + append(&out, pstr); } else{ do_whitespace_print = true; @@ -269,56 +251,56 @@ int main(){ String lexeme = get_lexeme(*token, pcontext.data); - int32_t do_print = true; - if (match_ss(lexeme, make_lit_string("FSTRING_DECLS"))){ - append_sc(&out, "#if !defined(FCODER_STRING_H)\n#define FCODER_STRING_H\n\n"); + 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; - static int32_t RETURN_PADDING = 16; - static int32_t SIG_PADDING = 35; + local_persist i32 RETURN_PADDING = 16; + local_persist i32 SIG_PADDING = 35; - for (int32_t j = 0; j < string_unit.set.count; ++j){ + 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_ss (&line, item->ret); + append (&line, item->ret); append_padding (&line, ' ', SIG_PADDING); - append_ss (&line, item->name); - append_ss (&line, item->args); - append_sc (&line, ";\n"); + append (&line, item->name); + append (&line, item->args); + append (&line, ";\n"); } else if (item->t == Item_Macro){ - append_ss (&line, make_lit_string("#ifndef ")); + append (&line, "#ifndef "); append_padding (&line, ' ', 10); - append_ss (&line, item->name); + append (&line, item->name); append_s_char (&line, '\n'); - append_ss (&line, make_lit_string("# define ")); + append (&line, "# define "); append_padding (&line, ' ', 10); - append_ss (&line, item->name); - append_ss (&line, item->args); + append (&line, item->name); + append (&line, item->args); append_s_char (&line, ' '); - append_ss (&line, item->body); + append (&line, item->body); append_s_char (&line, '\n'); - append_ss (&line, make_lit_string("#endif")); + append (&line, "#endif"); append_s_char (&line, '\n'); } else{ InvalidCodePath; } - append_ss(&out, line); + append(&out, line); } - append_sc(&out, "\n#endif\n"); + append(&out, "\n#endif\n"); // NOTE(allen): C++ overload definitions - append_sc(&out, "\n#if !defined(FSTRING_C) && !defined(FSTRING_GUARD)\n\n"); + append(&out, "\n#if !defined(FSTRING_C) && !defined(FSTRING_GUARD)\n\n"); - for (int32_t j = 0; j < string_unit.set.count; ++j){ + for (i32 j = 0; j < string_unit.set.count; ++j){ char line_space[2048]; String line = make_fixed_width_string(line_space); @@ -329,42 +311,42 @@ int main(){ if (cpp_name.str != 0){ Argument_Breakdown breakdown = item->breakdown; - append_ss (&line, item->ret); + append (&line, item->ret); append_padding(&line, ' ', SIG_PADDING); - append_ss (&line, cpp_name); - append_ss (&line, item->args); - if (match_ss(item->ret, make_lit_string("void"))){ - append_ss(&line, make_lit_string("{(")); + append (&line, cpp_name); + append (&line, item->args); + if (match(item->ret, "void")){ + append(&line, "{("); } else{ - append_ss(&line, make_lit_string("{return(")); + append(&line, "{return("); } - append_ss (&line, item->name); + append (&line, item->name); append_s_char(&line, '('); if (breakdown.count > 0){ - for (int32_t i = 0; i < breakdown.count; ++i){ + for (i32 i = 0; i < breakdown.count; ++i){ if (i != 0){ append_s_char(&line, ','); } - append_ss(&line, breakdown.args[i].param_name); + append(&line, breakdown.args[i].param_name); } } else{ - append_ss(&line, make_lit_string("void")); + append(&line, "void"); } - append_ss(&line, make_lit_string("));}\n")); + append(&line, "));}\n"); - append_ss(&out, line); + append(&out, line); } } } - append_sc(&out, "\n#endif\n"); + append(&out, "\n#endif\n"); } - else if (match_ss(lexeme, make_lit_string("API_EXPORT_MACRO"))){ + else if (match(lexeme, "API_EXPORT_MACRO")){ token = get_next_token(&pcontext); if (token && token->type == CPP_TOKEN_COMMENT){ token = get_next_token(&pcontext); @@ -383,24 +365,23 @@ int main(){ } } - else if (match_ss(lexeme, make_lit_string("API_EXPORT")) || - match_ss(lexeme, make_lit_string("API_EXPORT_INLINE"))){ + else if (match(lexeme, "API_EXPORT") || match(lexeme, "API_EXPORT_INLINE")){ if (!(token->flags & CPP_TFLAG_PP_BODY)){ - if (match_ss(lexeme, make_lit_string("API_EXPORT_INLINE"))){ - append_sc(&out, "#if !defined(FSTRING_GUARD)\n"); + if (match(lexeme, "API_EXPORT_INLINE")){ + append(&out, "#if !defined(FSTRING_GUARD)\n"); } else{ - append_sc(&out, "#if defined(FSTRING_IMPLEMENTATION)\n"); + append(&out, "#if defined(FSTRING_IMPLEMENTATION)\n"); } print_function_body_code(&out, &pcontext, start); - append_sc(&out, "\n#endif"); + append(&out, "\n#endif"); do_print = false; } } - else if (match_ss(lexeme, make_lit_string("CPP_NAME"))){ + else if (match(lexeme, "CPP_NAME")){ Cpp_Token *token_start = token; - int32_t has_cpp_name = false; + i32 has_cpp_name = false; token = get_next_token(&pcontext); if (token && token->type == CPP_TOKEN_PARENTHESE_OPEN){ @@ -434,18 +415,18 @@ int main(){ char space[512]; String str = make_fixed_width_string(space); - append_ss(&str, lexeme); + append(&str, lexeme); terminate_with_null(&str); String dump = file_dump(str.str); if (dump.str){ - append_ss(&out, dump); + append(&out, dump); } else{ lexeme.size += 2; lexeme.str -= 1; - append_sc(&out, "#error Could not find "); - append_ss(&out, lexeme); - append_sc(&out, "\n"); + append(&out, "#error Could not find "); + append(&out, lexeme); + append(&out, "\n"); } free(dump.str); } @@ -456,13 +437,13 @@ int main(){ if ((token = get_token(&pcontext)) != 0){ if (do_print){ pstr = get_lexeme(*token, pcontext.data); - append_ss(&out, pstr); + append(&out, pstr); } start = token->start + token->size; } } pstr = str_start_end(pcontext.data, start, parse.code.size); - append_ss(&out, pstr); + append(&out, pstr); end_file_out(context); }