F diff --git a/src/backend/text/print/print.c b/src/backend/text/print/print.c --- a/src/backend/text/print/print.c +++ b/src/backend/text/print/print.cdo{- src=get_source_file_from_string(*base_source_names,gstrnlen(*base_source_names,1000));+ src=get_source_file_from_string(*base_source_names,gstrnlen(*base_source_names,1000),program);hold_unit=lex(src,program);if(program->errors->size>0)break;}- fprintf(out,"\nTOKENS OF %s {\n",src->src_name->full_name);+ fprintf(out,"\nTOKENS OF %s {\n",base_source_names[0]);ptr=get_token_ptr(hold_unit,program);print_tokens(out,ptr,data);F diff --git a/src/common.h b/src/common.h --- a/src/common.h +++ b/src/common.h#ifndef WONKY_COMMON_H#define WONKY_COMMON_H WONKY_COMMON_H+ #include <config.h>+#define SHOULD_NOT_REACH_HERE (!"should not reach here")#define FIRST_TOKEN_IN_TOKEN_QUEUE 0#define SECOND_TOKEN_IN_TOKEN_QUEUE 1+ #define wonky_default_include_paths_size (sizeof(wonky_default_include_paths)/sizeof(wonky_default_include_paths[0]))#endifF diff --git a/src/config.h b/src/config.h new file mode 100644 --- /dev/null +++ b/src/config.h+ #ifndef WONKY_CONFIG_H+ #define WONKY_CONFIG_H++ static const char *wonky_default_include_paths[]+ =+ {+ "./",+ "/usr/include/",+ "/usr/include/x86_64-linux-gnu/",+ };+ #endifF diff --git a/src/environment/error/gcc_error.c b/src/environment/error/gcc_error.c --- a/src/environment/error/gcc_error.c +++ b/src/environment/error/gcc_error.c{Queue_Push(compile_data->errors,gstr_to_heap(message));}+ void push_program_error(const char *message_format,struct Program *program, ...)+ {+ va_list args;+ va_start(args,program);+ push_translation_message_inner("[Error] ",+ message_format,+ program,+ "",+ 0,+ 0,+ 0,+ args);+ va_end(args);+ }char* get_string_for_type_error(struct Type *type){char *ret;F diff --git a/src/environment/error/gcc_error.h b/src/environment/error/gcc_error.h --- a/src/environment/error/gcc_error.h +++ b/src/environment/error/gcc_error.hvoid push_compile_error(const char *message,struct Compile_Data *compile_data);void push_compile_note(const char *message,struct Compile_Data *compile_data);++ void push_program_error(const char *message_format,struct Program *program, ...);+char* get_string_for_type_error(struct Type *type);char* get_string_for_denoted_error(struct Denoted *denoted);char* get_string_for_id_error(struct identifier *id);F diff --git a/src/frontend/lex/lex_preprocessing_directive.c b/src/frontend/lex/lex_preprocessing_directive.c --- a/src/frontend/lex/lex_preprocessing_directive.c +++ b/src/frontend/lex/lex_preprocessing_directive.c}while(state!=END);}+ struct token* preprocessing_lex_include_directive(struct Lexer_Data *lexer_data,struct Source_Location *where)+ {+ struct token_include_directive *ret;++ ret=wonky_malloc(sizeof(struct token_include_directive));+ ret->type=PKW_INCLUDE;+ ret->delta=get_source_location_delta(lexer_data->previous_token_location,where);+ ret->tokens=wonky_malloc(sizeof(struct Queue));++ Queue_Init(ret->tokens);++ while(!preprocessing_eol(lexer_data))+ Queue_Push(ret->tokens,preprocessing_extract_next_token(lexer_data));++ return (struct token*)ret;+ }#endifF diff --git a/src/frontend/lex/lexer.c b/src/frontend/lex/lexer.c --- a/src/frontend/lex/lexer.c +++ b/src/frontend/lex/lexer.cMap_Push(program->preprocessing_translation_units,- src->src_name->full_name,- src->src_name->full_name_size,+ src->src_name->name,+ src->src_name->name_size,lexed_unit);delete_lexer_data(lexer_data);++ if(lexed_unit->tokens->size==0)+ push_program_error("Empty translation unit",program);return lexed_unit;}F diff --git a/src/misc/gcc_string.c b/src/misc/gcc_string.c --- a/src/misc/gcc_string.c +++ b/src/misc/gcc_string.creturn ret;}+ char* gstrn_append(const char *lead,const char *follower,size_t overall_limit)+ {+ char *ret,*hold;+ size_t lead_size;+ size_t follower_size;++ lead_size=gstrnlen(lead,overall_limit);+ follower_size=gstrnlen(lead,overall_limit);++ if(lead_size+follower_size>overall_limit)+ return gstrncpy("",1);++ hold=ret=wonky_malloc(lead_size+follower_size);+ while(*(hold++)=*(lead++));+ hold--;+ while(*(hold++)=*(follower++));+ return ret;+ }+void strmv(char *target,char *source){while(*(target++)=*(source++));F diff --git a/src/misc/gcc_string.h b/src/misc/gcc_string.h --- a/src/misc/gcc_string.h +++ b/src/misc/gcc_string.hchar* gstr_dup(const char *first,const char *last,size_t limit);char* gstr_to_heap(const char *literal);void gmemmove(void *where_to,void *from_where,size_t how_many_bytes);-+ char* gstrn_append(const char *lead,const char *follower,size_t overall_limit);#endifF diff --git a/src/semantics/program/program.c b/src/semantics/program/program.c --- a/src/semantics/program/program.c +++ b/src/semantics/program/program.cdo{- base_file=get_source_file_from_string(*base_source_names,gstrnlen(*base_source_names,1000));+ base_file=get_source_file_from_string(*base_source_names,gstrnlen(*base_source_names,1000),program);Queue_Push(program->preprocessing_translation_units_to_be_compiled,lex(base_file,program));}while(*(++base_source_names) != NULL);return program;}+ struct Preprocessing_Translation_Unit* program_get_translation_unit(struct Program *program,char *filename,size_t filename_size)+ {+ struct Preprocessing_Translation_Unit *ret;+ struct Source_File *hold_src;+++ ret=Map_Check(program->preprocessing_translation_units,filename,filename_size);++ if(ret==NULL)+ {+ hold_src=get_source_file_from_string(filename,filename_size,program);+ ret=lex(hold_src,program);+ Map_Push(program->preprocessing_translation_units,filename,filename_size,ret);+ }+ return ret;+ }void program_stop_parsing(struct Program *program){program->continue_to_parse=0;F diff --git a/src/semantics/program/program.h b/src/semantics/program/program.h --- a/src/semantics/program/program.h +++ b/src/semantics/program/program.hstruct Program* get_program();struct Translation_Data* get_translation_data(struct Preprocessing_Translation_Unit *start_unit,struct Linkage *internal_linkage,struct Program *program);struct Program* parse_program(char **base_source_names);+ struct Preprocessing_Translation_Unit* program_get_translation_unit(struct Program *program,char *filename,size_t filename_size);void entype_program(struct Program *program);void program_stop_parsing(struct Program *program);F diff --git a/src/semantics/program/translation_unit.c b/src/semantics/program/translation_unit.c --- a/src/semantics/program/translation_unit.c +++ b/src/semantics/program/translation_unit.cret=wonky_malloc(sizeof(struct Token_Pointer));ret->context=get_token_ptr_context(unit->tokens->first);ret->call_stack=wonky_malloc(sizeof(struct Stack));+ ret->state=TOKEN_POINTER_STATE_NORMAL;Stack_Init(ret->call_stack);void token_ptr_execute_include_directive(struct Token_Pointer *ptr,struct token_include_directive *include_directive){+ struct token *hold_token;+++ ptr->state=TOKEN_POINTER_STATE_PREPROCESSING;++ hold_token=token_ptr_get_token_under_pointer(ptr);+ if(hold_token->type==KW_STRING)+ {++ }++ ptr->state=TOKEN_POINTER_STATE_NORMAL;token_ptr_goto_next_token(ptr);}void token_ptr_execute_if_directive(struct Token_Pointer *ptr,struct token_if_directive *if_directive)ret->line=hold_location->line;ret->column=hold_location->column;- ret->filename=hold_location->src_name->full_name;- ret->filename_size=hold_location->src_name->full_name_size;+ ret->filename=hold_location->src_name->name;+ ret->filename_size=hold_location->src_name->name_size;Queue_Init(ret->ungeted_tokens);return ret;F diff --git a/src/semantics/program/translation_unit.h b/src/semantics/program/translation_unit.h --- a/src/semantics/program/translation_unit.h +++ b/src/semantics/program/translation_unit.hstruct Stack *call_stack;struct Token_Pointer_Context *context;struct Program *program;++ enum Token_Pointer_State state;};struct Preprocessing_Translation_Unit* get_preprocessing_translation_unit(struct Source_File *source);void delete_preprocessing_translation_unit(struct Preprocessing_Translation_Unit *unit);F diff --git a/src/semantics/program/translation_unit.hh b/src/semantics/program/translation_unit.hh --- a/src/semantics/program/translation_unit.hh +++ b/src/semantics/program/translation_unit.hh#ifndef WONKY_TRANSLATION_UNIT_HH#define WONKY_TRANSLATION_UNIT_HH WONKY_TRANSLATION_UNIT_HH+ enum Token_Pointer_State+ {+ TOKEN_POINTER_STATE_NORMAL,+ TOKEN_POINTER_STATE_PREPROCESSING,+ TOKEN_POINTER_STATE_END+ };+struct Preprocessing_Translation_Unit;struct Token_Pointer;F diff --git a/src/syntax/source_file.c b/src/syntax/source_file.c --- a/src/syntax/source_file.c +++ b/src/syntax/source_file.c#define WONKY_SOURCE_FILE_C WONKY_SOURCE_FILE_C#include <source_file.h>- char *well_known_locations_base[]={"","/usr/include/","/usr/include/x86_64-linux-gnu/",NULL};struct Source_Location start_of_file=.on_which_byte=0,.src_name=NULL,};- /*TODO this is not giving correct results, but was left as it is because of time constraits*/- struct Source_File* get_source_file_from_string(char *filename,size_t filename_size)+ struct Source_File* get_source_file_from_string(char *filename,size_t filename_size,struct Program *program){- struct Source_Name *src_name;struct Source_File *ret;FILE *file;+ size_t selected_base_directory;+ size_t file_size;+ char *hold_constructed_filename;- src_name=get_source_name_from_string(filename,filename_size);ret=wonky_malloc(sizeof(struct Source_File));- ret->src_name=src_name;+ ret->type=SOURCE_TEXT_FULL_TEXT;+ ret->src="";+ ret->src_size=0;+ ret->src_name=NULL;- file=fopen(filename,"r");- if(file==NULL)+ for(selected_base_directory=0;selected_base_directory<wonky_default_include_paths_size;++selected_base_directory){- filename[filename_size-1]='\0';- hard_error("Could not open file %s",filename);-- ret->src="";- ret->src_size=0;- return ret;+ hold_constructed_filename=gstr_append(+ wonky_default_include_paths[selected_base_directory],+ filename+ );+ file=fopen(hold_constructed_filename,"r");+ if(file!=NULL)+ break;+ else+ wonky_free(hold_constructed_filename);}- fseek(file,0,SEEK_END);-- ret->src_size=ftell(file);-- rewind(file);-- ret->src=wonky_malloc(ret->src_size);-- fread(ret->src,1,ret->src_size,file);- fclose(file);--- ret->type=SOURCE_TEXT_FULL_TEXT;-- return ret;- }- struct Source_Name* get_source_name_from_string(char *filename,size_t filename_size)- {- struct Source_Name *ret;-- ret=wonky_malloc(sizeof(struct Source_Name));- ret->filename=ret->base=ret->full_name=filename;- ret->filename_size=ret->base_size=ret->full_name_size=filename_size;-- return ret;- }- struct Source_Name* get_source_name_from_string_inner(char *filename,size_t filename_size)- {- struct Source_Name *ret;- ret=wonky_malloc(sizeof(struct Source_Name));- return ret;- }- /*this might cause compatability issues TODO*/- void normalise_source_name(struct Source_Name *name)- {- size_t offset;- size_t i;- size_t last_slash;- char *hold_base;- for(last_slash=offset=0;name->filename[offset];++offset)+ if(file==NULL){- if(name->filename[offset]=='/')+ file=fopen(filename,"r");+ if(file==NULL){- last_slash=offset;+ push_program_error("Could not open filename %s",program,filename);+ ret->src_name=get_source_name("");+ return ret;}}- if(last_slash==0)- return;-- if(name->base==NULL)- {- offset=0;- name->base=wonky_malloc(last_slash+1);- name->base[last_slash]='\0';- name->base[last_slash-1]='/';-- }else- {- offset=gstrlen((char*)name->base);- hold_base=wonky_malloc(offset+last_slash+2);- strmv(hold_base,(char*)name->base);-- hold_base[last_slash+offset]='/';- hold_base[last_slash+offset+1]='\0';- wonky_free((void*)name->base);-- name->base=hold_base;- }- for(i=0;i<last_slash;++i)- name->base[offset+i]=name->filename[i];+ fseek(file,0,SEEK_END);+ file_size=ftell(file);+ rewind(file);+ ret->src=wonky_malloc(file_size);+ ret->src_size=file_size;+ ret->src_name=get_source_name(hold_constructed_filename);- ++i;- /*prune the filename*/- offset=gstrlen(name->filename+i);- hold_base=wonky_malloc(offset+1);- strmv(hold_base,name->filename+i);- wonky_free(name->filename);- name->filename=hold_base;+ fread(ret->src,file_size,sizeof(char),file);+ fclose(file);+ return ret;}+struct Source_Location* get_source_location(size_t line,size_t column,size_t on_which_byte,struct Source_Name *src_name){struct Source_Location *ret;return ret;}+void delete_source_file(struct Source_File *src){wonky_free(src);}+ struct Source_Name* get_source_name(char *constructed_name)+ {+ struct Source_Name *ret;+ ret=wonky_malloc(sizeof(struct Source_Name));+ ret->name=constructed_name;+ ret->name_size=gstrnlen(constructed_name,100);+ return ret;+ }#endifF diff --git a/src/syntax/source_file.h b/src/syntax/source_file.h --- a/src/syntax/source_file.h +++ b/src/syntax/source_file.h#include <translation_unit.h>#include <gcc_error.h>- extern char *well_known_locations_base[];- extern struct Source_Location start_of_file;struct Source_Name{- char *filename;- char *base;- char *full_name;-- size_t filename_size;- size_t base_size;- size_t full_name_size;+ char *name;+ size_t name_size;};struct Source_Location{};struct Source_File{+ struct Source_Name *src_name;enum Source_Text_Type type;char *src;size_t src_size;-- struct Source_Name *src_name;+ size_t canonic_name_size;};- struct Source_File* extract_source_file(FILE *in,struct Source_Name *name);- struct Source_File* get_source_file_from_tokens(struct Token_Pointer *ptr);- struct Source_File* get_source_file_from_string(char *filename,size_t filename_size);+ extern struct Source_Location start_of_file;+- struct Source_Name* get_source_name_from_string(char *unprocessed_filename,size_t filename_size);- struct Source_Name* get_source_name_from_tokens(struct Token_Pointer *ptr);- struct Source_Location* get_source_location(size_t line,size_t column,size_t on_which_byte,struct Source_Name *src_name);+ struct Source_File* get_source_file_from_string(char *filename,size_t filename_size,struct Program *program);+ struct Source_Location* get_source_location(size_t line,size_t column,size_t on_which_byte,struct Source_Name *src_name);struct Source_Location_Delta* get_source_location_delta(struct Source_Location *begining,struct Source_Location *ending);- void normalise_source_name(struct Source_Name *name);+ struct Source_File* get_source_file_from_tokens(struct Token_Pointer *ptr);++ struct Source_Name* get_source_name(char *constructed_name);void delete_source_file(struct Source_File *src);- void delete_source_name(struct Source_Name *name);#endifF diff --git a/src/syntax/token/token.c b/src/syntax/token/token.c --- a/src/syntax/token/token.c +++ b/src/syntax/token/token.cret->error=get_translation_message(msg,program,- current_location->src_name->full_name,- current_location->src_name->full_name_size,+ current_location->src_name->name,+ current_location->src_name->name_size,current_location->line,current_location->column,args);