From f768d9bdb84e846d89aac66a4f3433a44241c298 Mon Sep 17 00:00:00 2001 From: Galin Simeonov Date: Mon, 17 May 2021 17:29:08 +0300 Subject: parser formed --- program.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 8 deletions(-) (limited to 'program.c') diff --git a/program.c b/program.c index c2140c1..5cf8cc3 100644 --- a/program.c +++ b/program.c @@ -30,14 +30,56 @@ struct Source* extract_source(char *src_name) fclose(file); return ret; } -struct Options* parse_command_line(char **argv) +struct Options* parse_command_line(int argc,char **argv) { struct Options *ret; - size_t i; + int i; + + assert(argv!=NULL && argc>0); ret=malloc(sizeof(struct Options)); - ret->print_tokens=1; - ret->source=argv[1]; + ret->target=OPTION_DEFAULT; + ret->src_name=NULL; + ret->is_quiet=0; + + for(i=0;itarget=OPTION_TARGET_TOKENS; + else if(!strncmp(argv[i],"--print-ast",sizeof("--print-ast"))) + ret->target=OPTION_TARGET_AST; + else if(!strncmp(argv[i],"-o",sizeof("-o")) || !strncmp(argv[i],"--output",sizeof("--output"))) + { + if(++isrc_name=argv[i]; + else if(!ret->is_quiet) + { + fprintf(stderr,"Error: Output filename is too long"); + exit(1); + }else + { + exit(1); + } + } + }else if(strnlen(argv[i],101)<100) + { + ret->src_name=argv[i]; + }else if(!ret->is_quiet) + { + fprintf(stderr,"Error: Input filename is too long"); + exit(1); + }else + { + exit(1); + } + + } + + if(ret->target==OPTION_DEFAULT) + ret->target=OPTION_TARGET_AST; + return ret; } struct Translation_Data* get_translation_data() @@ -59,16 +101,18 @@ struct Error* get_error(char *message,size_t row,size_t column) struct Error *ret; ret=malloc(sizeof(struct Error)); ret->message=message; - ret->row=row; - ret->column=column; + ret->row=row+1; + ret->column=column+1; } void push_lexing_error(char *error_message,struct Source *src,struct Translation_Data *translation_data) { Queue_Push(translation_data->errors,get_error(error_message,src->current_row,src->current_column)); } -void push_parsing_error(char *error_message,struct token *token ,struct Translation_Data *translation_data) +void push_parsing_error(char *error_message,struct Translation_Data *translation_data) { - Queue_Push(translation_data->errors,get_error(error_message,token->row,token->column)); + struct token *error_token; + error_token=Queue_Pop(translation_data->tokens); + Queue_Push(translation_data->errors,get_error(error_message,error_token->row,error_token->column)); } char has_new_errors(struct Translation_Data *translation_data) { @@ -111,4 +155,29 @@ void delete_error(struct Error *error) free(error->message); free(error); } +char get_and_check(struct Translation_Data *translation_data,enum Keyword kw) +{ + if( ( (struct token *)translation_data->tokens->first->data)->type==kw) + { + delete_token(Queue_Pop(translation_data->tokens)); + return 1; + }else + { + return 0; + } +} +enum Keyword get_kw(struct Translation_Data *translation_data) +{ + return ( (struct token*)translation_data->tokens->first->data)->type; +} +void chomp(struct Translation_Data *translation_data) +{ + assert(translation_data->tokens->size>0); + delete_token(Queue_Pop(translation_data->tokens)); +} +void touch_errors(struct Translation_Data *translation_data) +{ + assert(translation_data->hold_number_of_errors>0); + --translation_data->hold_number_of_errors; +} #endif -- cgit v1.2.3