started word complete

This commit is contained in:
Allen Webster 2016-02-11 12:17:52 -05:00
parent 5abea3ce3c
commit 5619527d37
14 changed files with 3909 additions and 4361 deletions

View File

@ -21,6 +21,9 @@ enum My_Maps{
}; };
HOOK_SIG(my_start){ HOOK_SIG(my_start){
exec_command(cmd_context, cmdid_open_panel_hsplit);
exec_command(cmd_context, cmdid_change_active_panel);
exec_command(cmd_context, cmdid_open_panel_vsplit); exec_command(cmd_context, cmdid_open_panel_vsplit);
exec_command(cmd_context, cmdid_change_active_panel); exec_command(cmd_context, cmdid_change_active_panel);
} }

View File

@ -226,11 +226,21 @@ enum Cpp_Token_Type{
CPP_TOKEN_EOF CPP_TOKEN_EOF
}; };
// TODO(allen): This is a dumb redundant type... probably just
// move towards using String for this everywhere eventually.
struct Cpp_File{ struct Cpp_File{
char *data; char *data;
int size; int size;
}; };
Cpp_File
data_as_cpp_file(Data data){
Cpp_File result;
result.data = (char*)data.data;
result.size = data.size;
return(result);
}
struct Cpp_Token{ struct Cpp_Token{
Cpp_Token_Type type; Cpp_Token_Type type;
fcpp_i32 start, size; fcpp_i32 start, size;

View File

@ -11,850 +11,7 @@
#define byte unsigned char #define byte unsigned char
struct Cpp_PP_Step{
int error_code;
int finished;
int early_out;
int mem_size;
int mem_type;
int emit;
int file_index;
int token_index;
};
enum Cpp_PP_Expansion_Type{
PPExp_Normal,
// never below this
PPExp_Count
};
struct Cpp_PP_Expansion_Header{
int prev_frame;
int type;
};
struct Cpp_PP_Expansion_Normal{
int file_offset;
int current_token;
int end_token;
int file_level;
};
struct Cpp_PP_State{
byte *base;
int top, frame, max;
int substep;
struct{
union{
int name_index;
int param_count;
int macro_offset;
} define;
} vars;
Debug(int did_advance);
};
internal int
cpp__push_expansion_normal(Cpp_PP_State *state, int file, int start, int end, int file_level){
Cpp_PP_Expansion_Header *header;
Cpp_PP_Expansion_Normal *expansion;
int added_size;
int prev_frame;
int result;
added_size = sizeof(Cpp_PP_Expansion_Header) + sizeof(Cpp_PP_Expansion_Normal);
if (state->top + added_size <= state->max){
result = 1;
prev_frame = state->frame;
state->frame = state->top;
state->top += added_size;
header = (Cpp_PP_Expansion_Header*)(state->base + state->frame);
expansion = (Cpp_PP_Expansion_Normal*)(header + 1);
header->prev_frame = prev_frame;
header->type = PPExp_Normal;
expansion->file_offset = file;
expansion->current_token = start;
expansion->end_token = end;
expansion->file_level = file_level;
}
else{
result = 0;
}
return(result);
}
internal int
cpp__pop_expansion(Cpp_PP_State *state){
Cpp_PP_Expansion_Header *header;
int result;
if (state->top >= sizeof(Cpp_PP_Expansion_Header)){
Assert(state->top - state->frame >= sizeof(Cpp_PP_Expansion_Header));
result = 1;
header = (Cpp_PP_Expansion_Header*)(state->base + state->frame);
state->top = state->frame;
state->frame = header->prev_frame;
}
else{
result = 0;
}
return(result);
}
enum Cpp_PP_Memory_Type{
PPMem_None,
PPMem_Definition_Items,
PPMem_Definition_Table,
PPMem_Spare_File_String,
PPMem_Spare_File_Tokens,
// never below this
PPMem_Count
};
internal void
cpp__pp_memory_request(Cpp_PP_Step *step, int amount, int type){
Assert(!step->early_out);
step->early_out = 1;
step->mem_size = amount;
step->mem_type = type;
}
enum Cpp_PP_Table_Entry_Type{
PPItem_Unset,
PPItem_File,
PPItem_Macro,
// never below this
PPItem_Count
};
struct Cpp_PP_Table_Entry{
int name_start, name_size;
int type;
unsigned int hash;
int item_offset;
};
struct Cpp_PP_Table{
Cpp_PP_Table_Entry *entries;
unsigned int count, max;
};
struct Cpp_PP_File{
Cpp_File file;
Cpp_Token_Stack tokens;
};
struct Cpp_PP_Macro{
int self_start;
int name_start;
int name_size;
int param_count;
int variadic;
int body_start;
int body_end;
};
struct Cpp_PP_Definitions{
byte *items;
int pos, restore_pos, max;
Cpp_PP_File *spare_file;
int spare_pos;
Cpp_PP_Table table;
};
internal unsigned int
cpp__hash(char *name, int size, int type){
unsigned int x;
int i;
x = 5381;
x = ((x << 5) + x) ^ ('A' + type);
for (i = 0; i < size; ++i, ++name){
x = ((x << 5) + x) ^ *name;
}
return(x);
}
internal unsigned int
cpp__hash(Cpp_PP_Definitions *definitions, int name_start, int name_size, int type){
unsigned int x;
char *s;
s = definitions->spare_file->file.data + name_start;
x = cpp__hash(s, name_size, type);
return(x);
}
internal int
cpp__table_can_add(Cpp_PP_Table *table){
int result;
result = 0;
if (table->count*8 < table->max*7) result = 1;
return(result);
}
internal void
cpp__table_add_direct(Cpp_PP_Table *table, Cpp_PP_Table_Entry entry){
unsigned int i, j;
i = entry.hash % table->max;
j = i - 1;
if (i <= 1) j += table->max;
for (; i != j; ++i){
if (i == table->max) i = 0;
if (table->entries[i].type == PPItem_Unset){
table->entries[i] = entry;
break;
}
}
Assert(i != j);
}
internal void
cpp__table_add(Cpp_PP_Definitions *definitions, Cpp_PP_Table *table,
int name_start, int name_size, int type, int offset){
Cpp_PP_Table_Entry entry;
unsigned int hash;
Assert(cpp__table_can_add(table));
hash = cpp__hash(definitions, name_start, name_size, type);
entry.name_start = name_start;
entry.name_size = name_size;
entry.type = type;
entry.hash = hash;
entry.item_offset = offset;
cpp__table_add_direct(table, entry);
}
internal int
cpp__str_match(char *a, char *b, int size){
char *e;
int result;
result = 0;
for(e = a + size; a < e && *a == *b; ++a, ++b);
if (a == e) result = 1;
return(result);
}
internal int
cpp__table_find_hash_pos(Cpp_PP_Definitions *definitions, Cpp_PP_Table *table,
char *name, int name_size, int typ e, unsigned int *hash_index){
Cpp_PP_Table_Entry *entry;
int result;
unsigned int hash;
unsigned int i, j;
result = 0;
hash = cpp__hash(name, name_size, type);
i = hash % table->max;
j = i - 1;
if (i <= 1) j += table->max;
for (; i != j; ++i){
if (i == table->max) i = 0;
entry = table->entries + i;
if (entry->type == PPItem_Unset) break;
if (entry->hash == hash && entry->type == type && entry->name_size == name_size &&
cpp__str_match(name, definitions->spare_file->file.data + entry->name_start, name_size)){
result = 1;
*hash_index = i;
break;
}
}
return(result);
}
internal int
cpp__table_find(Cpp_PP_Definitions *definitions, Cpp_PP_Table *table,
char *name, int name_size, int type, int *offset){
int result;
unsigned int hash_index;
result = 0;
if (cpp__table_find_hash_pos(definitions, table, name, name_size, type, &hash_index)){
result = 1;
*offset = table->entries[hash_index].item_offset;
}
return(result);
}
internal void
cpp__table_cpy(Cpp_PP_Table *dest, Cpp_PP_Table *src){
Cpp_PP_Table_Entry *entry;
unsigned int i;
Assert(dest->max >= src->max);
for (i = 0, entry = src->entries; i < src->max; ++i, ++entry){
if (entry->type != PPItem_Unset){
cpp__table_add_direct(dest, *entry);
}
}
}
internal void
cpp__def_begin_uncertain(Cpp_PP_Definitions *definitions){
definitions->restore_pos = definitions->pos;
}
internal void
cpp__def_success_uncertain(Cpp_PP_Definitions *definitions){
definitions->restore_pos = 0;
}
internal void
cpp__def_fail_uncertain(Cpp_PP_Definitions *definitions){
if (definitions->restore_pos != 0){
definitions->pos = definitions->restore_pos;
}
}
internal int
cpp__def_can_save_strings(Cpp_PP_Definitions *definitions, int total_size){
int result;
Cpp_PP_File *file;
file = definitions->spare_file;
if (definitions->spare_pos + total_size <= file->file.size) result = 1;
else result = 0;
return(result);
}
internal int
cpp__def_can_save_item(Cpp_PP_Definitions *definitions, int size){
int result;
if (definitions->pos + size <= definitions->max) result = 1;
else result = 0;
return(result);
}
internal int
cpp__def_save_string(Cpp_PP_Step *step, Cpp_PP_Definitions *definitions,
char *str, int size){
int result;
Cpp_PP_File *file;
file = definitions->spare_file;
Assert(definitions->spare_pos + size <= file->file.size);
memcpy(file->file.data + definitions->spare_pos, str, size);
result = definitions->spare_pos;
definitions->spare_pos += size;
return(result);
}
internal int
cpp__mem_up(int x){
int xx;
xx = x / 1024;
if (xx*1024 < x){
xx += 1;
}
xx = xx << 10;
return(xx);
}
internal int
cpp__def_begin_macro(Cpp_PP_Step *step, Cpp_PP_Definitions *definitions,
char *str, int size){
int result, str_pos;
Cpp_PP_Macro *macro_item;
result = 0;
if (cpp__def_can_save_strings(definitions, size)){
if (cpp__def_can_save_item(definitions, sizeof(Cpp_PP_Macro))){
str_pos = cpp__def_save_string(step, definitions, str, size);
Assert(definitions->pos + sizeof(Cpp_PP_Macro) <= definitions->max);
result = definitions->pos;
definitions->pos += sizeof(Cpp_PP_Macro);
macro_item = (Cpp_PP_Macro*)(definitions->items + result);
*macro_item = {};
macro_item->self_start = result;
macro_item->name_start = str_pos;
macro_item->name_size = size;
}
else{
cpp__pp_memory_request(step, cpp__mem_up(definitions->max + 1), PPMem_Definition_Items);
}
}
else{
cpp__pp_memory_request(step, cpp__mem_up(definitions->spare_file->file.size + size),
PPMem_Spare_File_String);
}
return(result);
}
internal void
cpp__def_set_macro_params(Cpp_PP_Definitions *definitions, int macro_offset, int param_count){
Cpp_PP_Macro *macro_item;
macro_item = (Cpp_PP_Macro*)(definitions->items + macro_offset);
macro_item->param_count = param_count;
}
internal void
cpp__def_set_macro_variadic(Cpp_PP_Definitions *definitions, int macro_offset, int variadic){
Cpp_PP_Macro *macro_item;
macro_item = (Cpp_PP_Macro*)(definitions->items + macro_offset);
macro_item->variadic = variadic;
}
internal void
cpp__def_set_macro_body_start(Cpp_PP_Definitions *definitions, int macro_offset, int body_start){
Cpp_PP_Macro *macro_item;
macro_item = (Cpp_PP_Macro*)(definitions->items + macro_offset);
macro_item->body_start = body_start;
}
internal void
cpp__def_set_macro_body_end(Cpp_PP_Definitions *definitions, int macro_offset, int body_end){
Cpp_PP_Macro *macro_item;
macro_item = (Cpp_PP_Macro*)(definitions->items + macro_offset);
macro_item->body_end = body_end;
}
internal void
cpp__def_end_macro(Cpp_PP_Step *step, Cpp_PP_Definitions *definitions){
if (cpp__table_can_add(&definitions->table)){
Cpp_PP_Macro *macro_item;
macro_item = (Cpp_PP_Macro*)(definitions->items + definitions->restore_pos);
cpp__table_add(definitions, &definitions->table,
macro_item->name_start, macro_item->name_size,
PPItem_Macro, macro_item->self_start);
}
else{
cpp__pp_memory_request(step,
cpp__mem_up(definitions->table.max*sizeof(Cpp_PP_Table_Entry)+1),
PPMem_Definition_Table);
}
}
internal Cpp_PP_File*
cpp_preproc_get_pp_file(Cpp_PP_Definitions *definitions, int file_offset){
Cpp_PP_File *result;
result = (Cpp_PP_File*)(definitions->items+file_offset);
return(result);
}
internal Cpp_File*
cpp_preproc_get_file(Cpp_PP_Definitions *definitions, int file_offset){
Cpp_PP_File *pp_file;
Cpp_File *result;
pp_file = cpp_preproc_get_pp_file(definitions, file_offset);
result = &pp_file->file;
return(result);
}
internal Cpp_Token*
cpp_preproc_get_token(Cpp_PP_Definitions *definitions, int file_offset, int token_index){
Cpp_PP_File *pp_file;
Cpp_Token *result;
pp_file = cpp_preproc_get_pp_file(definitions, file_offset);
result = &pp_file->tokens.tokens[token_index];
return(result);
}
internal int
cpp__define_file(Cpp_PP_Definitions *definitions, Cpp_File file, Cpp_Token_Stack tokens){
int result;
Cpp_PP_File *file_item;
Assert(definitions->pos + sizeof(Cpp_PP_File) <= definitions->max);
result = definitions->pos;
definitions->pos += sizeof(Cpp_PP_File);
file_item = (Cpp_PP_File*)(definitions->items + result);
file_item->file = file;
file_item->tokens = tokens;
return(result);
}
internal void
cpp_preproc_set_spare_space(Cpp_PP_Definitions *definitions,
int str_size, void *str_mem,
int token_size, void *token_mem){
Cpp_PP_File pp_file;
pp_file.file.size = str_size;
pp_file.file.data = (char*)str_mem;
pp_file.tokens.max_count = token_size;
pp_file.tokens.count = 0;
pp_file.tokens.tokens = (Cpp_Token*)token_mem;
int pos = cpp__define_file(definitions, pp_file.file, pp_file.tokens);
Cpp_PP_File *spare_file = cpp_preproc_get_pp_file(definitions, pos);
definitions->spare_file = spare_file;
definitions->spare_pos = 0;
}
internal void
cpp_preproc_target(Cpp_PP_State *state, Cpp_PP_Definitions *definitions,
Cpp_File file, Cpp_Token_Stack tokens){
int file_index;
Assert(state->top == 0);
file_index = cpp__define_file(definitions, file, tokens);
cpp__push_expansion_normal(state, file_index, 0, tokens.count, 1);
}
internal void*
cpp_preproc_provide_memory(Cpp_PP_State *state, Cpp_PP_Definitions *definitions,
int type, int size, void *memory){
Cpp_PP_Table new_table;
Cpp_PP_File *spare;
void *result;
result = 0;
switch (type){
case PPMem_None: Assert(0); break;
case PPMem_Definition_Items:
Assert(size > definitions->max);
memcpy(memory, definitions->items, definitions->pos);
result = definitions->items;
definitions->items = (byte*)memory;
break;
case PPMem_Definition_Table:
Assert(size > definitions->table.max * sizeof(*new_table.entries));
new_table.entries = (Cpp_PP_Table_Entry*)memory;
new_table.max = size/sizeof(*new_table.entries);
new_table.count = 0;
cpp__table_cpy(&new_table, &definitions->table);
result = definitions->table.entries;
definitions->table = new_table;
break;
case PPMem_Spare_File_String:
spare = definitions->spare_file;
Assert(size > spare->file.size);
memcpy(memory, spare->file.data, definitions->spare_pos);
result = spare->file.data;
spare->file.data = (char*)memory;
break;
case PPMem_Spare_File_Tokens:
spare = definitions->spare_file;
Assert(size > spare->tokens.count*sizeof(Cpp_Token));
memcpy(memory, spare->tokens.tokens, spare->tokens.count*sizeof(Cpp_Token));
result = spare->tokens.tokens;
spare->tokens.tokens = (Cpp_Token*)memory;
break;
default: Assert(0); break;
}
return(result);
}
enum Cpp_PP_Error{
PPErr_None,
PPErr_Define_Identifier_Missing,
PPErr_Parameter_Identifier_Missing,
PPErr_Preprocessor_Directive_In_Expansion,
PPErr_Expected_Comma_Or_Parenthese,
PPErr_Unfinished_Parameters,
// never below this
PPErr_Count
};
internal char*
cpp_preproc_error_str(int error_code){
char *result = 0;
switch (error_code){
case PPErr_None:
result = "no error"; break;
case PPErr_Define_Identifier_Missing:
result = "define identifier missing"; break;
case PPErr_Parameter_Identifier_Missing:
result = "parameter identifier missing"; break;
case PPErr_Preprocessor_Directive_In_Expansion:
result = "preprocessor directive in expansion"; break;
case PPErr_Expected_Comma_Or_Parenthese:
result = "expected comma or parenthese"; break;
case PPErr_Unfinished_Parameters:
result = "unfinished parameter list"; break;
default:
result = "unknown error code";
}
return(result);
}
internal int
cpp_preproc_recommend_termination(int error_code){
int result = 0;
return(result);
}
struct Cpp__PP_Visit{
int file_offset;
int token_index;
};
internal void
cpp__pp_next_token(Cpp_PP_State *state, Cpp_PP_Expansion_Normal *expansion){
++expansion->current_token;
Debug(state->did_advance = 1);
}
enum Cpp_PP_State_Step{
PPState_None,
PPState_Define,
PPState_Name,
PPState_Define_Body,
PPState_Define_Parameter,
PPState_Define_Parameter_Comma,
PPState_Error_Recovery,
// never below this
PPState_Count
};
internal int
cpp__pp_require_identifier(Cpp_PP_State *state, Cpp_Token token){
int result;
result = PPErr_None;
if ((token.flags & CPP_TFLAG_PP_BODY) == 0){
result = PPErr_Define_Identifier_Missing;
state->substep = PPState_None;
Debug(state->did_advance = 1);
}
else if (token.type != CPP_TOKEN_IDENTIFIER &&
(token.flags & CPP_TFLAG_IS_KEYWORD) == 0){
result = PPErr_Define_Identifier_Missing;
state->substep = PPState_Error_Recovery;
Debug(state->did_advance = 1);
}
return(result);
}
internal Cpp_PP_Step
cpp__pp_step_normal(Cpp_PP_State *state, Cpp_PP_Definitions *definitions,
Cpp_PP_Expansion_Normal *expansion){
Cpp__PP_Visit visit;
Cpp_PP_Step result;
Cpp_Token token;
Cpp_PP_File pp_file;
int void_step;
visit = {};
result = {};
void_step = 0;
if (expansion->current_token < expansion->end_token){
visit.file_offset = expansion->file_offset;
visit.token_index = expansion->current_token;
}
else{
void_step = 1;
cpp__pop_expansion(state);
}
if (!void_step){
pp_file = *cpp_preproc_get_pp_file(definitions, visit.file_offset);
Assert(visit.token_index >= 0 && visit.token_index < pp_file.tokens.count);
token = pp_file.tokens.tokens[visit.token_index];
Debug(state->did_advance = 0);
if (token.type == CPP_TOKEN_COMMENT || token.type == CPP_TOKEN_JUNK){
cpp__pp_next_token(state, expansion);
}
else{
switch (state->substep){
case PPState_None:
{
if (expansion->file_level == 0 && (token.flags & CPP_TFLAG_PP_DIRECTIVE)){
result.error_code = PPErr_Preprocessor_Directive_In_Expansion;
cpp__pp_next_token(state, expansion);
}
else{
switch (token.type){
case CPP_PP_DEFINE:
{
state->substep = PPState_Define;
state->vars = {};
cpp__pp_next_token(state, expansion);
}break;
default:
{
result.emit = 1;
result.file_index = visit.file_offset;
result.token_index = visit.token_index;
cpp__pp_next_token(state, expansion);
}break;
}
}
}break;
case PPState_Define:
{
result.error_code = cpp__pp_require_identifier(state, token);
if (result.error_code == PPErr_None){
cpp__def_begin_uncertain(definitions);
state->vars.define.macro_offset =
cpp__def_begin_macro(&result, definitions,
pp_file.file.data + token.start,
token.size);
if (result.early_out) goto cpp__pp_step_early_out;
state->substep = PPState_Name;
state->vars.define.name_index = visit.token_index;
cpp__pp_next_token(state, expansion);
}
}break;
case PPState_Name:
{
if ((token.flags & CPP_TFLAG_PP_BODY) == 0){
cpp__def_end_macro(&result, definitions);
if (result.early_out) goto cpp__pp_step_early_out;
cpp__def_success_uncertain(definitions);
state->substep = PPState_None;
Debug(state->did_advance = 1);
}
else if (token.type == CPP_TOKEN_PARENTHESE_OPEN){
state->substep = PPState_Define_Parameter;
cpp__pp_next_token(state, expansion);
}
else{
state->substep = PPState_Define_Body;
}
}break;
case PPState_Define_Parameter:
{
if (token.type == CPP_TOKEN_PARENTHESE_CLOSE &&
(token.flags & CPP_TFLAG_PP_BODY) &&
state->vars.define.param_count == 0){
state->substep = PPState_Define_Body;
cpp__pp_next_token(state, expansion);
}
else{
result.error_code = cpp__pp_require_identifier(state, token);
if (result.error_code == PPErr_None){
state->substep = PPState_Define_Parameter_Comma;
cpp__pp_next_token(state, expansion);
++state->vars.define.param_count;
}
else{
result.error_code = PPErr_Parameter_Identifier_Missing;
// NOTE(allen): Here I do not do the work I normally do when
// setting an error, because that work was done earlier, this is
// just translating the error code.
}
}
}break;
case PPState_Define_Parameter_Comma:
{
if ((token.flags & CPP_TFLAG_PP_BODY) == 0){
result.error_code = PPErr_Unfinished_Parameters;
state->substep = PPState_None;
Debug(state->did_advance = 1);
}
else{
if (token.type == CPP_TOKEN_COMMA){
state->substep = PPState_Define_Parameter;
cpp__pp_next_token(state, expansion);
}
else if (token.type == CPP_TOKEN_PARENTHESE_CLOSE){
state->substep = PPState_Define_Body;
cpp__pp_next_token(state, expansion);
}
else{
state->substep = PPState_Error_Recovery;
result.error_code = PPErr_Expected_Comma_Or_Parenthese;
Debug(state->did_advance = 1);
}
}
}break;
case PPState_Define_Body:
{
if ((token.flags & CPP_TFLAG_PP_BODY) == 0){
// TODO(allen): define macro
state->substep = PPState_None;
Debug(state->did_advance = 1);
}
else{
cpp__pp_next_token(state, expansion);
}
}break;
case PPState_Error_Recovery:
{
if ((token.flags & CPP_TFLAG_PP_BODY) == 0){
state->substep = PPState_None;
Debug(state->did_advance = 1);
}
else{
cpp__pp_next_token(state, expansion);
}
}break;
}
}
Assert(state->did_advance == 1);
}
cpp__pp_step_early_out:
return(result);
}
internal Cpp_PP_Step
cpp_preproc_step_nonalloc(Cpp_PP_State *state, Cpp_PP_Definitions *definitions){
Cpp_PP_Step result;
Cpp_PP_Expansion_Header *header;
Assert(state->top >= sizeof(Cpp_PP_Expansion_Header));
result = {};
header = (Cpp_PP_Expansion_Header*)(state->base + state->frame);
switch (header->type){
case PPExp_Normal:
result = cpp__pp_step_normal(state, definitions,
(Cpp_PP_Expansion_Normal*)(header + 1));
break;
default: Assert(0);
}
if (state->top == 0) result.finished = 1;
return(result);
}
#undef byte #undef byte

116
4ed.cpp
View File

@ -236,7 +236,7 @@ COMMAND_DECL(write_character){
i32 pos; i32 pos;
pos = view->cursor.pos; pos = view->cursor.pos;
i32 next_cursor_pos = view->cursor.pos + string.size; i32 next_cursor_pos = view->cursor.pos + string.size;
view_replace_range(system, mem, view, layout, pos, pos, (u8*)string.str, string.size, next_cursor_pos); view_replace_range(system, mem, view, layout, pos, pos, string.str, string.size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos); view_cursor_move(view, next_cursor_pos);
file->state.cursor_pos = view->cursor.pos; file->state.cursor_pos = view->cursor.pos;
} }
@ -421,7 +421,7 @@ COMMAND_DECL(search){
view_set_widget(view, FWIDG_SEARCH); view_set_widget(view, FWIDG_SEARCH);
view->isearch.str = vars->mini_str; view->isearch.str = vars->mini_str;
view->isearch.reverse = 0; view->isearch.reverse = 0;
view->isearch.pos = view->cursor.pos; view->isearch.pos = view->cursor.pos - 1;
} }
COMMAND_DECL(rsearch){ COMMAND_DECL(rsearch){
@ -433,7 +433,80 @@ COMMAND_DECL(rsearch){
view_set_widget(view, FWIDG_SEARCH); view_set_widget(view, FWIDG_SEARCH);
view->isearch.str = vars->mini_str; view->isearch.str = vars->mini_str;
view->isearch.reverse = 1; view->isearch.reverse = 1;
view->isearch.pos = view->cursor.pos; view->isearch.pos = view->cursor.pos + 1;
}
COMMAND_DECL(word_complete){
ProfileMomentFunction();
REQ_FILE_VIEW(view);
REQ_FILE(file, view);
USE_LAYOUT(layout);
USE_MEM(mem);
Partition *part = &mem->part;
Temp_Memory temp1, temp2;
Buffer_Type *buffer;
Buffer_Backify_Type loop;
char *data;
i32 end;
i32 cursor_pos, word_start, word_end;
char c;
char *str, *spare;
i32 size;
i32 match_start, match_end, match_size;
buffer = &file->state.buffer;
word_end = view->cursor.pos;
word_start = word_end;
cursor_pos = word_end - 1;
// TODO(allen): macros for these buffer loops and some method of breaking out of them.
for (loop = buffer_backify_loop(buffer, cursor_pos, 0);
buffer_backify_good(&loop);
buffer_backify_next(&loop)){
end = loop.absolute_pos;
data = loop.data - loop.absolute_pos;
for (; cursor_pos >= end; --cursor_pos){
c = data[cursor_pos];
if (char_is_alpha(c)){
word_start = cursor_pos;
}
else if (!char_is_numeric(c)){
goto double_break;
}
}
}
// TODO(allen): figure out how labels are scoped.
double_break:;
size = word_end - word_start;
if (size > 0){
temp1 = begin_temp_memory(part);
str = (char*)push_array(part, char, size);
buffer_stringify(buffer, word_start, word_end, str);
temp2 = begin_temp_memory(part);
spare = (char*)push_array(part, char, size);
end_temp_memory(temp2);
// TODO(allen): find string needs explicit end position
match_start = buffer_find_string(buffer, word_end, str, size, spare);
match_end = buffer_seek_word_right_assume_on_word(buffer, match_start);
match_size = match_end - match_start;
spare = (char*)push_array(part, char, match_size);
buffer_stringify(buffer, match_start, match_end, spare);
view_replace_range(system, mem, view, layout, word_start, word_end, spare, match_size, word_end);
end_temp_memory(temp1);
}
} }
COMMAND_DECL(goto_line){ COMMAND_DECL(goto_line){
@ -497,24 +570,29 @@ COMMAND_DECL(paste){
USE_LAYOUT(layout); USE_LAYOUT(layout);
USE_MEM(mem); USE_MEM(mem);
Panel *current_panel;
String *src;
File_View *current_view;
i32 pos_left, next_cursor_pos;
i32 panel_count;
i32 i;
if (working_set->clipboard_size > 0){ if (working_set->clipboard_size > 0){
view->next_mode.rewrite = 1; view->next_mode.rewrite = 1;
String *src = working_set_clipboard_head(working_set); src = working_set_clipboard_head(working_set);
i32 pos_left = view->cursor.pos; pos_left = view->cursor.pos;
i32 next_cursor_pos = pos_left+src->size; next_cursor_pos = pos_left+src->size;
view_replace_range(system, mem, view, layout, pos_left, pos_left, (u8*)src->str, src->size, next_cursor_pos); view_replace_range(system, mem, view, layout, pos_left, pos_left, src->str, src->size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos); view_cursor_move(view, next_cursor_pos);
view->mark = pos_left; view->mark = pos_left;
Editing_Layout *layout = command->layout; current_panel = layout->panels;
Panel *panels = layout->panels; panel_count = layout->panel_count;
i32 panel_count = layout->panel_count; for (i = 0; i < panel_count; ++i, ++current_panel){
for (i32 i = 0; i < panel_count; ++i){ current_view = view_to_file_view(current_panel->view);
Panel *current_panel = panels + i;
File_View *current_view = view_to_file_view(current_panel->view);
if (current_view && current_view->file == file){ if (current_view && current_view->file == file){
view_post_paste_effect(current_view, 20, pos_left, src->size, view_post_paste_effect(current_view, 20, pos_left, src->size,
@ -540,7 +618,7 @@ COMMAND_DECL(paste_next){
i32 next_cursor_pos = range.start+src->size; i32 next_cursor_pos = range.start+src->size;
view_replace_range(system, view_replace_range(system,
mem, view, layout, range.start, range.end, mem, view, layout, range.start, range.end,
(u8*)src->str, src->size, next_cursor_pos); src->str, src->size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos); view_cursor_move(view, next_cursor_pos);
view->mark = range.start; view->mark = range.start;
@ -2081,10 +2159,12 @@ setup_file_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Co
map_add(commands, 'l', MDFR_CTRL, command_toggle_line_wrap); map_add(commands, 'l', MDFR_CTRL, command_toggle_line_wrap);
map_add(commands, '?', MDFR_CTRL, command_toggle_show_whitespace); map_add(commands, '?', MDFR_CTRL, command_toggle_show_whitespace);
map_add(commands, '|', MDFR_CTRL, command_toggle_tokens); map_add(commands, '|', MDFR_CTRL, command_toggle_tokens);
map_add(commands, 'u', MDFR_CTRL, command_to_uppercase); map_add(commands, 'U', MDFR_CTRL, command_to_uppercase);
map_add(commands, 'j', MDFR_CTRL, command_to_lowercase); map_add(commands, 'u', MDFR_CTRL, command_to_lowercase);
map_add(commands, '~', MDFR_CTRL, command_clean_all_lines); map_add(commands, '~', MDFR_CTRL, command_clean_all_lines);
map_add(commands, 'f', MDFR_CTRL, command_search); map_add(commands, 'f', MDFR_CTRL, command_search);
map_add(commands, 'j', MDFR_CTRL, command_word_complete);
map_add(commands, 'r', MDFR_CTRL, command_rsearch); map_add(commands, 'r', MDFR_CTRL, command_rsearch);
map_add(commands, 'g', MDFR_CTRL, command_goto_line); map_add(commands, 'g', MDFR_CTRL, command_goto_line);
@ -2616,12 +2696,12 @@ App_Read_Command_Line_Sig(app_read_command_line){
vars = app_setup_memory(memory); vars = app_setup_memory(memory);
if (clparams.argc > 1){ if (clparams.argc > 1){
init_command_line_settings(&vars->settings, plat_settings, clparams); init_command_line_settings(&vars->settings, plat_settings, clparams);
*files = vars->settings.init_files;
*file_count = &vars->settings.init_files_count;
} }
else{ else{
vars->settings = {}; vars->settings = {};
} }
*files = vars->settings.init_files;
*file_count = &vars->settings.init_files_count;
} }
return(out_size); return(out_size);

View File

@ -896,7 +896,7 @@ do_font_option(Color_UI *ui, i16 font_id){
b32 result = 0; b32 result = 0;
Font_Info *info = get_font_info(ui->state.font_set, font_id); Font_Info *info = get_font_info(ui->state.font_set, font_id);
i32 sub_id = (i32)(info); i32 sub_id = (i32)(i64)(info);
i32_Rect orect = layout_rect(&ui->layout, info->height); i32_Rect orect = layout_rect(&ui->layout, info->height);
Widget_ID wid = make_sub0(&ui->state, sub_id); Widget_ID wid = make_sub0(&ui->state, sub_id);

View File

@ -2967,7 +2967,7 @@ view_do_white_batch_edit(System_Functions *system, Mem_Options *mem, File_View *
inline void inline void
view_replace_range(System_Functions *system, view_replace_range(System_Functions *system,
Mem_Options *mem, File_View *view, Editing_Layout *layout, Mem_Options *mem, File_View *view, Editing_Layout *layout,
i32 start, i32 end, u8 *str, i32 len, i32 next_cursor){ i32 start, i32 end, char *str, i32 len, i32 next_cursor){
if (view->locked) return; if (view->locked) return;
Edit_Spec spec = {}; Edit_Spec spec = {};
spec.step.type = ED_NORMAL; spec.step.type = ED_NORMAL;
@ -2976,7 +2976,7 @@ view_replace_range(System_Functions *system,
spec.step.edit.len = len; spec.step.edit.len = len;
spec.step.pre_pos = view->cursor.pos; spec.step.pre_pos = view->cursor.pos;
spec.step.post_pos = next_cursor; spec.step.post_pos = next_cursor;
spec.str = str; spec.str = (u8*)str;
file_do_single_edit(system, mem, view->file, layout, spec, hist_normal); file_do_single_edit(system, mem, view->file, layout, spec, hist_normal);
} }

View File

@ -238,6 +238,29 @@ buffer_seek_whitespace_left(Buffer_Type *buffer, int pos){
return(pos); return(pos);
} }
internal_4tech int
buffer_seek_word_right_assume_on_word(Buffer_Type *buffer, int pos){
Buffer_Stringify_Type loop;
char *data;
int end;
int size;
size = buffer_size(buffer);
loop = buffer_stringify_loop(buffer, pos, size);
for (;buffer_stringify_good(&loop);
buffer_stringify_next(&loop)){
end = loop.size + loop.absolute_pos;
data = loop.data - loop.absolute_pos;
for (; pos < end; ++pos){
if (!is_alphanumeric(data[pos])) goto double_break;
}
}
double_break:
return(pos);
}
internal_4tech int internal_4tech int
buffer_seek_alphanumeric_right(Buffer_Type *buffer, int pos){ buffer_seek_alphanumeric_right(Buffer_Type *buffer, int pos){
Buffer_Stringify_Type loop; Buffer_Stringify_Type loop;

View File

@ -451,6 +451,11 @@ is_alphanumeric_true(char c){
return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'); return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9');
} }
inline_4tech int
is_alphanumeric(char c){
return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_');
}
inline_4tech int inline_4tech int
is_upper(char c){ is_upper(char c){
return (c >= 'A' && c <= 'Z'); return (c >= 'A' && c <= 'Z');

View File

@ -43,22 +43,82 @@
#include <X11/extensions/XInput2.h> #include <X11/extensions/XInput2.h>
#include <linux/input.h> #include <linux/input.h>
#include <time.h> #include <time.h>
#include <dlfcn.h>
#include <unistd.h>
#include "4ed_internal.h" #include "4ed_internal.h"
#include "4ed_linux_keyboard.cpp" #include "4ed_linux_keyboard.cpp"
#include "system_shared.h"
#include <stdlib.h> #include <stdlib.h>
void* struct Linux_Vars{
LinuxGetMemory(i32 size){ void *app_code;
void *custom;
Plat_Settings settings;
System_Functions *system;
App_Functions app;
Custom_API custom_api;
b32 first;
#if FRED_INTERNAL
Sys_Bubble internal_bubble;
#endif
};
globalvar Linux_Vars linuxvars;
globalvar Application_Memory memory_vars;
globalvar Exchange exchange_vars;
internal
Sys_Get_Memory_Sig(system_get_memory_){
// TODO(allen): Implement without stdlib.h // TODO(allen): Implement without stdlib.h
return (malloc(size)); void *result = 0;
if (size != 0){
#if FRED_INTERNAL
Sys_Bubble *bubble;
result = malloc(size + sizeof(Sys_Bubble));
bubble = (Sys_Bubble*)result;
bubble->flags = MEM_BUBBLE_SYS_DEBUG;
bubble->line_number = line_number;
bubble->file_name = file_name;
bubble->size = size;
// TODO(allen): make Sys_Bubble list thread safe
insert_bubble(&linuxvars.internal_bubble, bubble);
result = bubble + 1;
#else
result = malloc(size);
#endif
}
return(result);
} }
void internal
LinuxFreeMemory(void *block){ Sys_Free_Memory_Sig(system_free_memory){
// TODO(allen): Implement without stdlib.h // TODO(allen): Implement without stdlib.h
free(block);
if (block){
#if FRED_INTERNAL
Sys_Bubble *bubble;
bubble = (Sys_Bubble*)block;
--bubble;
Assert((bubble->flags & MEM_BUBBLE_DEBUG_MASK) == MEM_BUBBLE_SYS_DEBUG);
// TODO(allen): make Sys_Bubble list thread safe
remove_bubble(bubble);
free(bubble);
#else
free(block);
#endif
}
} }
#if (defined(_BSD_SOURCE) || defined(_SVID_SOURCE)) #if (defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
@ -92,6 +152,7 @@ Sys_File_Time_Stamp_Sig(system_file_time_stamp){
return(result); return(result);
} }
// TODO(allen): DOES THIS AGREE WITH THE FILESTAMP TIMES?
Sys_Time_Sig(system_time){ Sys_Time_Sig(system_time){
struct timespec spec; struct timespec spec;
u64 result; u64 result;
@ -125,10 +186,8 @@ Sys_Set_File_List_Sig(system_set_file_list){
required_size = count + file_count * sizeof(File_Info); required_size = count + file_count * sizeof(File_Info);
if (file_list->block_size < required_size){ if (file_list->block_size < required_size){
if (file_list->block){ system_free_memory(file_list->block);
LinuxFreeMemory(file_list->block); file_list->block = system_get_memory(required_size);
}
file_list->block = LinuxGetMemory(required_size);
} }
file_list->infos = (File_Info*)file_list->block; file_list->infos = (File_Info*)file_list->block;
@ -143,11 +202,13 @@ Sys_Set_File_List_Sig(system_set_file_list){
fname = entry->d_name; fname = entry->d_name;
cursor_start = cursor; cursor_start = cursor;
for (; *fname; ) *cursor++ = *fname++; for (; *fname; ) *cursor++ = *fname++;
*cursor++ = 0;
// TODO(allen): detect file/folder status
// (also make sure this even GETS folders!!!)
info_ptr->folder = 0; info_ptr->folder = 0;
info_ptr->filename.str = cursor_start; info_ptr->filename.str = cursor_start;
info_ptr->filename.size = (i32)(cursor - cursor_start); info_ptr->filename.size = (i32)(cursor - cursor_start);
*cursor++ = 0;
info_ptr->filename.memory_size = info_ptr->filename.size + 1; info_ptr->filename.memory_size = info_ptr->filename.size + 1;
} }
} }
@ -161,6 +222,181 @@ Sys_Post_Clipboard_Sig(system_post_clipboard){
AllowLocal(str); AllowLocal(str);
} }
Sys_CLI_Call_Sig(system_cli_call){
// TODO(allen): Implement
AllowLocal(path);
AllowLocal(script_name);
AllowLocal(cli_out);
}
Sys_CLI_Begin_Update_Sig(system_cli_begin_update){
// TODO(allen): Implement
AllowLocal(cli);
}
Sys_CLI_Update_Step_Sig(system_cli_update_step){
// TODO(allen): Implement
AllowLocal(cli);
AllowLocal(dest);
AllowLocal(max);
AllowLocal(amount);
}
Sys_CLI_End_Update_Sig(system_cli_end_update){
// TODO(allen): Implement
AllowLocal(cli);
}
Sys_Post_Job_Sig(system_post_job){
// TODO(allen): Implement
AllowLocal(group_id);
AllowLocal(job);
}
Sys_Cancel_Job_Sig(system_cancel_job){
// TODO(allen): Implement
AllowLocal(group_id);
AllowLocal(job_id);
}
Sys_Acquire_Lock_Sig(system_acquire_lock){
// TODO(allen): Implement
AllowLocal(id);
}
Sys_Release_Lock_Sig(system_release_lock){
// TODO(allen): Implement
AllowLocal(id);
}
Sys_Grow_Thread_Memory_Sig(system_grow_thread_memory){
void *old_data;
i32 old_size, new_size;
system_acquire_lock(CANCEL_LOCK0 + memory->id - 1);
old_data = memory->data;
old_size = memory->size;
new_size = LargeRoundUp(memory->size*2, Kbytes(4));
memory->data = system_get_memory(new_size);
memory->size = new_size;
if (old_data){
memcpy(memory->data, old_data, old_size);
system_free_memory(old_data);
}
system_release_lock(CANCEL_LOCK0 + memory->id - 1);
}
INTERNAL_Sys_Sentinel_Sig(internal_sentinel){
Bubble *result;
#if FRED_INTERNAL
result = &linuxvars.internal_bubble;
#else
result = 0;
#endif
return(result);
}
INTERNAL_Sys_Get_Thread_States_Sig(internal_get_thread_states){
// TODO(allen): Implement
AllowLocal(id);
AllowLocal(running);
AllowLocal(pending);
}
INTERNAL_Sys_Debug_Message_Sig(internal_debug_message){
printf("%s", message);
}
DIRECTORY_HAS_FILE_SIG(system_directory_has_file){
int result = 0;
// TODO(allen): Implement
AllowLocal(dir);
AllowLocal(filename);
return(result);
}
DIRECTORY_CD_SIG(system_directory_cd){
int result = 0;
// TODO(allen): Implement
AllowLocal(dir);
AllowLocal(rel_path);
return(result);
}
internal
Sys_File_Can_Be_Made(system_file_can_be_made){
// TODO(allen): Implement
AllowLocal(filename);
return(0);
}
internal
Sys_Load_File_Sig(system_load_file){
Data result = {};
// TODO(allen): Implement
AllowLocal(filename);
return(result);
}
internal
Sys_Save_File_Sig(system_save_file){
b32 result = 0;
// TODO(allen): Implement
AllowLocal(filename);
AllowLocal(data);
AllowLocal(size);
return(result);
}
internal b32
LinuxLoadAppCode(){
b32 result = 0;
App_Get_Functions *get_funcs = 0;
linuxvars.app_code = dlopen("./4ed_app.so", RTLD_LAZY);
if (linuxvars.app_code){
get_funcs = (App_Get_Functions*)
dlsym(linuxvars.app_code, "app_get_functions");
}
if (get_funcs){
result = 1;
linuxvars.app = get_funcs();
}
return(result);
}
internal void
LinuxLoadSystemCode(){
linuxvars.system->file_time_stamp = system_file_time_stamp;
linuxvars.system->set_file_list = system_set_file_list;
linuxvars.system->directory_has_file = system_directory_has_file;
linuxvars.system->directory_cd = system_directory_cd;
linuxvars.system->post_clipboard = system_post_clipboard;
linuxvars.system->time = system_time;
linuxvars.system->cli_call = system_cli_call;
linuxvars.system->cli_begin_update = system_cli_begin_update;
linuxvars.system->cli_update_step = system_cli_update_step;
linuxvars.system->cli_end_update = system_cli_end_update;
linuxvars.system->post_job = system_post_job;
linuxvars.system->cancel_job = system_cancel_job;
linuxvars.system->grow_thread_memory = system_grow_thread_memory;
linuxvars.system->acquire_lock = system_acquire_lock;
linuxvars.system->release_lock = system_release_lock;
linuxvars.system->internal_sentinel = internal_sentinel;
linuxvars.system->internal_get_thread_states = internal_get_thread_states;
linuxvars.system->internal_debug_message = internal_debug_message;
}
#include "system_shared.cpp"
#include "4ed_rendering.cpp"
// NOTE(allen): Thanks to Casey for providing the linux OpenGL launcher. // NOTE(allen): Thanks to Casey for providing the linux OpenGL launcher.
static bool ctxErrorOccurred = false; static bool ctxErrorOccurred = false;
static int XInput2OpCode = 0; static int XInput2OpCode = 0;
@ -490,8 +726,77 @@ InitializeXInput(Display *dpy, Window XWindow)
} }
int int
main(int ArgCount, char **Args) main(int argc, char **argv)
{ {
linuxvars = {};
exchange_vars = {};
#if FRED_INTERNAL
linuxvars.internal_bubble.next = &linuxvars.internal_bubble;
linuxvars.internal_bubble.prev = &linuxvars.internal_bubble;
linuxvars.internal_bubble.flags = MEM_BUBBLE_SYS_DEBUG;
#endif
if (!LinuxLoadAppCode()){
// TODO(allen): Failed to load app code, serious problem.
return 99;
}
System_Functions system_;
System_Functions *system = &system_;
linuxvars.system = system;
LinuxLoadSystemCode();
memory_vars.vars_memory_size = Mbytes(2);
memory_vars.vars_memory = system_get_memory(memory_vars.vars_memory_size);
memory_vars.target_memory_size = Mbytes(512);
memory_vars.target_memory = system_get_memory(memory_vars.target_memory_size);
String current_directory;
i32 curdir_req, curdir_size;
char *curdir_mem;
curdir_req = (1 << 9);
curdir_mem = (char*)system_get_memory(curdir_req);
for (; getcwd(curdir_mem, curdir_req) == 0 && curdir_req < (1 << 13);){
system_free_memory(curdir_mem);
curdir_req *= 4;
curdir_mem = (char*)system_get_memory(curdir_req);
}
if (curdir_req >= (1 << 13)){
// TODO(allen): fuckin' bullshit string APIs makin' me pissed
return 57;
}
for (curdir_size = 0; curdir_mem[curdir_size]; ++curdir_size);
current_directory = make_string(curdir_mem, curdir_size, curdir_req);
Command_Line_Parameters clparams;
clparams.argv = argv;
clparams.argc = argc;
char **files;
i32 *file_count;
i32 output_size;
output_size =
linuxvars.app.read_command_line(system,
&memory_vars,
current_directory,
&linuxvars.settings,
&files, &file_count,
clparams);
if (output_size > 0){
// TODO(allen): crt free version
printf("%.*s", output_size, (char*)memory_vars.target_memory);
}
if (output_size != 0) return 0;
system_filter_real_files(files, file_count);
Display *XDisplay = XOpenDisplay(0); Display *XDisplay = XOpenDisplay(0);
if(XDisplay && GLXSupportsModernContexts(XDisplay)) if(XDisplay && GLXSupportsModernContexts(XDisplay))
{ {

28
system_shared.cpp Normal file
View File

@ -0,0 +1,28 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 09.02.2016
*
* Shared system functions
*
*/
// TOP
internal void
system_filter_real_files(char **files, i32 *file_count){
i32 i, j;
i32 end;
end = *file_count;
for (i = 0, j = 0; i < end; ++i){
if (system_file_can_be_made(files[i])){
files[j] = files[i];
++j;
}
}
*file_count = j;
}
// BOTTOM

32
system_shared.h Normal file
View File

@ -0,0 +1,32 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 09.02.2016
*
* Shared system functions
*
*/
// TOP
// NOTE(allen): This serves as a list of functions to implement
// in addition to those in 4ed_system.h These are not exposed to
// the application code, but system_shared.cpp and 4ed_rendering.cpp
// rely on the functions listed here.
#define Sys_Get_Memory_Sig(name) void* name(i32 size, i32 line_number, char *file_name)
#define Sys_Free_Memory_Sig(name) void name(void *block)
#define Sys_File_Can_Be_Made(name) b32 name(char *filename)
#define Sys_Load_File_Sig(name) Data name(char *filename)
#define Sys_Save_File_Sig(name) b32 name(char *filename, char *data, i32 size)
internal Sys_Get_Memory_Sig(system_get_memory_);
internal Sys_Free_Memory_Sig(system_free_memory);
internal Sys_File_Can_Be_Made(system_file_can_be_made);
internal Sys_Load_File_Sig(system_load_file);
internal Sys_Save_File_Sig(system_save_file);
#define system_get_memory(size) system_get_memory_((size), __LINE__, __FILE__)
// BOTTOM

View File

@ -11,146 +11,63 @@
#include "../4ed_meta.h" #include "../4ed_meta.h"
#define Debug(x) x #define FCPP_STRING_IMPLEMENTATION
#include "../4coder_string.h"
#include "../4cpp_types.h" #include "../4cpp_types.h"
#define FCPP_STRING_IMPLEMENTATION
#include "../4cpp_string.h"
#define FCPP_LEXER_IMPLEMENTATION #define FCPP_LEXER_IMPLEMENTATION
#include "../4cpp_lexer.h" #include "../4cpp_lexer.h"
#include "../4cpp_preprocessor.cpp" #include "../4cpp_preprocessor.cpp"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <stdlib.h>
internal bool Data
system_is_absoute_path(char *path){ file_dump(char *filename){
bool is_absolute = 0; Data result;
char c = 1; FILE *file;
while (c){ result = {};
c = *path++; file = fopen(filename, "rb");
if (c == ':'){
is_absolute = 1;
break;
}
}
return is_absolute;
}
#undef Assert
#undef TentativeAssert
#define Assert assert
#define TentativeAssert assert
Cpp_File
quickie_file(char *filename){
Cpp_File result = {};
FILE *file = fopen(filename, "rb");
if (file){ if (file){
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
result.size = ftell(file); result.size = ftell(file);
if (result.size > 0){ fseek(file, 0, SEEK_SET);
fseek(file, 0, SEEK_SET); result.data = (byte*)malloc(result.size);
result.data = (char*)malloc(result.size); fread(result.data, 1, result.size, file);
fread(result.data, 1, result.size, file);
}
fclose(file); fclose(file);
} }
return(result);
return result;
}
inline Cpp_File
quickie_file(String filename){
assert(filename.size < 511);
char buffer[512];
memcpy(buffer, filename.str, filename.size);
buffer[filename.size] = 0;
return quickie_file(buffer);
}
internal void
preproc_init(Cpp_PP_State *state, Cpp_PP_Definitions *definitions){
*state = {};
state->max = (32 << 10);
state->base = (byte*)malloc(state->max);
memset(state->base, 0, state->max);
*definitions = {};
definitions->max = 128 << 10;
definitions->items = (byte*)malloc(definitions->max);
memset(definitions->items, 0, definitions->max);
definitions->table.count = 0;
definitions->table.max = 4 << 10;
definitions->table.entries = (Cpp_PP_Table_Entry*)
malloc(definitions->table.max*sizeof(Cpp_PP_Table_Entry));
memset(definitions->table.entries, 0, definitions->table.max*sizeof(Cpp_PP_Table_Entry));
int str_size = 16 << 10;
int token_size = 16 << 10;
void *str_mem = malloc(str_size);
void *token_mem = malloc(token_size);
cpp_preproc_set_spare_space(definitions,
str_size, str_mem,
token_size, token_mem);
} }
int main(int argc, char **argv){ int main(int argc, char **argv){
if (argc < 2){ Data target_file;
printf("usage: %s <file>\n", argv[0]); Cpp_File file;
return 1; Cpp_Token_Stack tokens;
Cpp_Token *token;
int i;
if (argc != 2){
printf("usage: %s <cpp-file>\n", argv[0]);
exit(1);
} }
char *TEST_FILE = argv[1];
Cpp_File target_file;
target_file = quickie_file(TEST_FILE);
target_file = file_dump(argv[1]);
if (target_file.data == 0){ if (target_file.data == 0){
printf("could not open file %s\n", TEST_FILE); printf("couldn't open file %s\n", argv[1]);
return 1; exit(1);
} }
Cpp_Token_Stack target_tokens = {}; tokens = cpp_make_token_stack(1 << 10);
cpp_lex_file(target_file, &target_tokens);
Cpp_PP_State state = {}; file = data_as_cpp_file(target_file);
Cpp_PP_Definitions definitions = {}; cpp_lex_file(file, &tokens);
preproc_init(&state, &definitions); token = tokens.tokens;
for (i = 0; i < tokens.count; ++i, ++token){
cpp_preproc_target(&state, &definitions, target_file, target_tokens); printf("%.*s\n", token->size, file.data + token->start);
for (Cpp_PP_Step step = {};
step.finished == 0;){
step = cpp_preproc_step_nonalloc(&state, &definitions);
if (step.error_code){
printf("error: %s\n", cpp_preproc_error_str(step.error_code));
if (cpp_preproc_recommend_termination(step.error_code)){
return 1;
}
}
if (step.mem_size != 0){
void *memory = malloc(step.mem_size);
memory = cpp_preproc_provide_memory(&state, &definitions,
step.mem_type, step.mem_size, memory);
free(memory);
}
if (step.emit){
Cpp_File file = *cpp_preproc_get_file(&definitions, step.file_index);
Cpp_Token token = *cpp_preproc_get_token(&definitions, step.file_index, step.token_index);
printf("TOKEN: %.*s\n", token.size, file.data + token.start);
}
} }
return 0; return(0);
} }
// BOTTOM // BOTTOM

BIN
vc120.pdb

Binary file not shown.

View File

@ -35,6 +35,7 @@
#include "4ed_dll_reader.cpp" #include "4ed_dll_reader.cpp"
#include "4ed_internal.h" #include "4ed_internal.h"
#include "4ed_win32_keyboard.cpp" #include "4ed_win32_keyboard.cpp"
#include "system_shared.h"
#define FPS 30 #define FPS 30
#define frame_useconds (1000000 / FPS) #define frame_useconds (1000000 / FPS)
@ -59,7 +60,6 @@ struct Thread_Group{
}; };
#define UseWinDll 1 #define UseWinDll 1
#define UseThreadMemory 1
struct Control_Keys{ struct Control_Keys{
b8 l_ctrl; b8 l_ctrl;
@ -130,9 +130,7 @@ struct Win32_Vars{
HANDLE locks[LOCK_COUNT]; HANDLE locks[LOCK_COUNT];
HANDLE DEBUG_sysmem_lock; HANDLE DEBUG_sysmem_lock;
#if UseThreadMemory
Thread_Memory *thread_memory; Thread_Memory *thread_memory;
#endif
u64 performance_frequency; u64 performance_frequency;
u64 start_pcount; u64 start_pcount;
@ -179,6 +177,8 @@ INTERNAL_system_debug_message(char *message){
#endif #endif
// TODO(allen): Transition towards using system_shared functions
internal void* internal void*
Win32GetMemory_(i32 size, i32 line_number, char *file_name){ Win32GetMemory_(i32 size, i32 line_number, char *file_name){
void *ptr = 0; void *ptr = 0;
@ -202,6 +202,11 @@ Win32GetMemory_(i32 size, i32 line_number, char *file_name){
return ptr; return ptr;
} }
internal
Sys_Get_Memory_Sig(system_get_memory_){
return(Win32GetMemory_(size, line_number, file_name));
}
#define Win32GetMemory(size) Win32GetMemory_(size, __LINE__, __FILE__) #define Win32GetMemory(size) Win32GetMemory_(size, __LINE__, __FILE__)
internal void internal void
@ -291,8 +296,6 @@ system_load_file(char *filename){
return result; return result;
} }
#include "4ed_rendering.cpp"
internal b32 internal b32
system_save_file(char *filename, void *data, i32 size){ system_save_file(char *filename, void *data, i32 size){
HANDLE file; HANDLE file;
@ -392,10 +395,7 @@ Sys_Set_File_List_Sig(system_set_file_list){
i32 required_size = count + file_count * sizeof(File_Info); i32 required_size = count + file_count * sizeof(File_Info);
if (file_list->block_size < required_size){ if (file_list->block_size < required_size){
if (file_list->block){ Win32FreeMemory(file_list->block);
Win32FreeMemory(file_list->block);
}
file_list->block = Win32GetMemory(required_size); file_list->block = Win32GetMemory(required_size);
} }
@ -536,15 +536,6 @@ Sys_Release_Lock_Sig(system_release_lock){
ReleaseSemaphore(win32vars.locks[id], 1, 0); ReleaseSemaphore(win32vars.locks[id], 1, 0);
} }
internal void
Win32RedrawScreen(HDC hdc){
system_acquire_lock(RENDER_LOCK);
launch_rendering(&win32vars.target);
system_release_lock(RENDER_LOCK);
glFlush();
SwapBuffers(hdc);
}
internal void internal void
Win32RedrawFromUpdate(){ Win32RedrawFromUpdate(){
SendMessage( SendMessage(
@ -619,13 +610,10 @@ ThreadProc(LPVOID lpParameter){
if (safe_running_thread == THREAD_NOT_ASSIGNED){ if (safe_running_thread == THREAD_NOT_ASSIGNED){
thread->job_id = full_job->id; thread->job_id = full_job->id;
thread->running = 1; thread->running = 1;
#if UseThreadMemory
Thread_Memory *thread_memory = 0; Thread_Memory *thread_memory = 0;
#endif
// TODO(allen): remove memory_request // TODO(allen): remove memory_request
if (full_job->job.memory_request != 0){ if (full_job->job.memory_request != 0){
#if UseThreadMemory
thread_memory = win32vars.thread_memory + thread->id - 1; thread_memory = win32vars.thread_memory + thread->id - 1;
if (thread_memory->size < full_job->job.memory_request){ if (thread_memory->size < full_job->job.memory_request){
if (thread_memory->data){ if (thread_memory->data){
@ -635,7 +623,6 @@ ThreadProc(LPVOID lpParameter){
thread_memory->data = Win32GetMemory(new_size); thread_memory->data = Win32GetMemory(new_size);
thread_memory->size = new_size; thread_memory->size = new_size;
} }
#endif
} }
full_job->job.callback(win32vars.system, thread, thread_memory, full_job->job.callback(win32vars.system, thread, thread_memory,
&exchange_vars.thread, full_job->job.data); &exchange_vars.thread, full_job->job.data);
@ -708,13 +695,15 @@ Sys_Cancel_Job_Sig(system_cancel_job){
} }
} }
#if UseThreadMemory
internal void internal void
system_grow_thread_memory(Thread_Memory *memory){ system_grow_thread_memory(Thread_Memory *memory){
void *old_data;
i32 old_size, new_size;
system_acquire_lock(CANCEL_LOCK0 + memory->id - 1); system_acquire_lock(CANCEL_LOCK0 + memory->id - 1);
void *old_data = memory->data; old_data = memory->data;
i32 old_size = memory->size; old_size = memory->size;
i32 new_size = LargeRoundUp(memory->size*2, Kbytes(4)); new_size = LargeRoundUp(memory->size*2, Kbytes(4));
memory->data = Win32GetMemory(new_size); memory->data = Win32GetMemory(new_size);
memory->size = new_size; memory->size = new_size;
if (old_data){ if (old_data){
@ -723,7 +712,6 @@ system_grow_thread_memory(Thread_Memory *memory){
} }
system_release_lock(CANCEL_LOCK0 + memory->id - 1); system_release_lock(CANCEL_LOCK0 + memory->id - 1);
} }
#endif
#if FRED_INTERNAL #if FRED_INTERNAL
internal void internal void
@ -911,15 +899,13 @@ Font_Load_Sig(system_draw_font_load){
internal b32 internal b32
Win32LoadAppCode(){ Win32LoadAppCode(){
b32 result = 0; b32 result = 0;
App_Get_Functions *get_funcs = 0;
#if UseWinDll #if UseWinDll
win32vars.app_code = LoadLibraryA("4ed_app.dll"); win32vars.app_code = LoadLibraryA("4ed_app.dll");
if (win32vars.app_code){ if (win32vars.app_code){
result = 1; get_funcs = (App_Get_Functions*)
App_Get_Functions *get_funcs = (App_Get_Functions*)
GetProcAddress(win32vars.app_code, "app_get_functions"); GetProcAddress(win32vars.app_code, "app_get_functions");
win32vars.app = get_funcs();
} }
#else #else
@ -944,8 +930,7 @@ Win32LoadAppCode(){
PAGE_EXECUTE_READ, PAGE_EXECUTE_READ,
&extra_); &extra_);
result = 1; get_funcs = (App_Get_Functions*)
App_Get_Functions *get_functions = (App_Get_Functions*)
dll_load_function(&win32vars.app_dll, "app_get_functions", 17); dll_load_function(&win32vars.app_dll, "app_get_functions", 17);
} }
else{ else{
@ -962,6 +947,11 @@ Win32LoadAppCode(){
#endif #endif
if (get_funcs){
result = 1;
win32vars.app = get_funcs();
}
return result; return result;
} }
@ -992,6 +982,18 @@ Win32LoadSystemCode(){
win32vars.system->internal_debug_message = INTERNAL_system_debug_message; win32vars.system->internal_debug_message = INTERNAL_system_debug_message;
} }
#include "system_shared.cpp"
#include "4ed_rendering.cpp"
internal void
Win32RedrawScreen(HDC hdc){
system_acquire_lock(RENDER_LOCK);
launch_rendering(&win32vars.target);
system_release_lock(RENDER_LOCK);
glFlush();
SwapBuffers(hdc);
}
void void
ex__file_insert(File_Slot *pos, File_Slot *file){ ex__file_insert(File_Slot *pos, File_Slot *file){
file->next = pos->next; file->next = pos->next;
@ -1564,7 +1566,7 @@ main(int argc, char **argv){
DWORD required = GetCurrentDirectory(0, 0); DWORD required = GetCurrentDirectory(0, 0);
required += 1; required += 1;
required *= 4; required *= 4;
char *current_directory_mem = (char*)Win32GetMemory(required); char *current_directory_mem = (char*)system_get_memory(required);
DWORD written = GetCurrentDirectory(required, current_directory_mem); DWORD written = GetCurrentDirectory(required, current_directory_mem);
String current_directory = make_string(current_directory_mem, written, required); String current_directory = make_string(current_directory_mem, written, required);
@ -1590,24 +1592,13 @@ main(int argc, char **argv){
// //
if (output_size > 0){ if (output_size > 0){
// TODO // TODO(allen): crt free version
printf("%.*s", output_size, memory_vars.target_memory); printf("%.*s", output_size, memory_vars.target_memory);
} }
if (output_size != 0) return 0; if (output_size != 0) return 0;
FreeConsole(); FreeConsole();
if (files){ system_filter_real_files(files, file_count);
i32 i, j;
i32 end = *file_count;
for (i = 0, j = 0; i < end; ++i){
if (system_file_can_be_made(files[i])){
files[j] = files[i];
++j;
}
}
*file_count = j;
}
LARGE_INTEGER lpf; LARGE_INTEGER lpf;
QueryPerformanceFrequency(&lpf); QueryPerformanceFrequency(&lpf);
@ -1646,11 +1637,8 @@ main(int argc, char **argv){
win32vars.groups[BACKGROUND_THREADS].threads = background; win32vars.groups[BACKGROUND_THREADS].threads = background;
win32vars.groups[BACKGROUND_THREADS].count = ArrayCount(background); win32vars.groups[BACKGROUND_THREADS].count = ArrayCount(background);
#if UseThreadMemory
Thread_Memory thread_memory[ArrayCount(background)]; Thread_Memory thread_memory[ArrayCount(background)];
win32vars.thread_memory = thread_memory; win32vars.thread_memory = thread_memory;
#endif
exchange_vars.thread.queues[BACKGROUND_THREADS].semaphore = exchange_vars.thread.queues[BACKGROUND_THREADS].semaphore =
Win32GenHandle( Win32GenHandle(
@ -1662,11 +1650,9 @@ main(int argc, char **argv){
Thread_Context *thread = win32vars.groups[BACKGROUND_THREADS].threads + i; Thread_Context *thread = win32vars.groups[BACKGROUND_THREADS].threads + i;
thread->id = i + 1; thread->id = i + 1;
#if UseThreadMemory
Thread_Memory *memory = win32vars.thread_memory + i; Thread_Memory *memory = win32vars.thread_memory + i;
*memory = {}; *memory = {};
memory->id = thread->id; memory->id = thread->id;
#endif
thread->queue = &exchange_vars.thread.queues[BACKGROUND_THREADS]; thread->queue = &exchange_vars.thread.queues[BACKGROUND_THREADS];
thread->handle = CreateThread(0, 0, ThreadProc, thread, creation_flag, (LPDWORD)&thread->windows_id); thread->handle = CreateThread(0, 0, ThreadProc, thread, creation_flag, (LPDWORD)&thread->windows_id);
@ -1865,6 +1851,8 @@ main(int argc, char **argv){
win32vars.clipboard_contents, current_directory, win32vars.clipboard_contents, current_directory,
win32vars.custom_api); win32vars.custom_api);
system_free_memory(current_directory.str);
win32vars.input_chunk.pers.keep_playing = 1; win32vars.input_chunk.pers.keep_playing = 1;
win32vars.first = 1; win32vars.first = 1;
timeBeginPeriod(1); timeBeginPeriod(1);