From b845e4754be86d2216733d9bea75cb301f38739d Mon Sep 17 00:00:00 2001 From: Galin Simeonov Date: Fri, 4 Jun 2021 13:04:36 +0300 Subject: added expressions and the if statement --- src/frontend/lexer.c | 4 ++-- src/frontend/parser.c | 36 +++++++++++++++++++++++++----------- src/frontend/parser.h | 2 ++ 3 files changed, 29 insertions(+), 13 deletions(-) (limited to 'src/frontend') diff --git a/src/frontend/lexer.c b/src/frontend/lexer.c index de8ff95..668d7cd 100644 --- a/src/frontend/lexer.c +++ b/src/frontend/lexer.c @@ -49,10 +49,10 @@ struct token* lex_step(struct Source *src,struct Translation_Data *translation_d return get_token(src->src+src->where_in_src-sizeof("]")+1,sizeof("]")-1,KW_CLOSE_SQUARE,src->current_row,src->current_column); if(check_and_move_if_on_word(";",sizeof(";")-1,src,0)) return get_token(src->src+src->where_in_src-sizeof(";")+1,sizeof(";")-1,KW_SEMI_COLUMN,src->current_row,src->current_column); - if(check_and_move_if_on_word("|",sizeof("|")-1,src,0)) - return get_token(src->src+src->where_in_src-sizeof("|")+1,sizeof("|")-1,KW_PIPE,src->current_row,src->current_column); if(check_and_move_if_on_word("||",sizeof("||")-1,src,0)) return get_token(src->src+src->where_in_src-sizeof("||")+1,sizeof("||")-1,KW_OR,src->current_row,src->current_column); + if(check_and_move_if_on_word("|",sizeof("|")-1,src,0)) + return get_token(src->src+src->where_in_src-sizeof("|")+1,sizeof("|")-1,KW_PIPE,src->current_row,src->current_column); if(check_and_move_if_on_word("&&",sizeof("&&")-1,src,0)) return get_token(src->src+src->where_in_src-sizeof("&&")+1,sizeof("&&")-1,KW_AND,src->current_row,src->current_column); if(check_and_move_if_on_word("!",sizeof("!")-1,src,0)) diff --git a/src/frontend/parser.c b/src/frontend/parser.c index a260622..fa692f5 100644 --- a/src/frontend/parser.c +++ b/src/frontend/parser.c @@ -25,7 +25,6 @@ struct AST_Translation_Unit* parse_translation_unit(struct Translation_Data *tra Map_Init(hold_machines_map); translation_data->hold_command_map=hold_command_map; - translation_data->hold_machines_map=hold_machines_map; while(!get_and_check(translation_data,KW_EOF)) { @@ -332,7 +331,7 @@ struct AST_Transitions* parse_transitions_inner(struct Translation_Data *transla struct AST_Transition* parse_transition(struct Translation_Data *translation_data,struct AST_States *states,struct AST_Events *events) { struct AST_Transition *ret; - struct AST_State *hold_from; + struct AST *hold_from; struct AST_State *hold_to; struct AST_Event *hold_event; struct AST_Pipeline *hold_pipeline=NULL; @@ -448,9 +447,11 @@ struct AST_State* get_ast_state(struct token *id) struct AST_State *ret; ret=malloc(sizeof(struct AST_State)); + ret->type=AST_TYPE_STATE; ret->name=id; /*number is assigned in get_ast_states*/ + /*parent machine is determined in anotation stage*/ return ret; } @@ -522,7 +523,7 @@ struct AST_Events* get_ast_events(struct Queue *events) return ret; } -struct AST_Transition* get_ast_transition(struct AST *from,struct AST_State *to,struct AST_Event *event,struct AST_Pipeline *pipeline); +struct AST_Transition* get_ast_transition(struct AST *from,struct AST_State *to,struct AST_Event *event,struct AST_Pipeline *pipeline) { struct AST_Transition *ret; ret=malloc(sizeof(struct AST_Transition)); @@ -617,6 +618,21 @@ void delete_ast(struct AST* ast) case AST_TYPE_PIPELINE: delete_ast_pipeline((struct AST_Pipeline*)ast); break; + case AST_TYPE_TRANSLATION_UNIT: + delete_ast_translation_unit((struct AST_Translation_Unit*)ast); + break; + case AST_TYPE_OP_AND: + case AST_TYPE_OP_OR: + case AST_TYPE_OP_SELECTOR: + delete_ast_binary_expression((struct AST_Binary_Expression*)ast); + break; + case AST_TYPE_OP_NOT: + delete_ast_unary_expression((struct AST_Unary_Expression*)ast); + break; + case AST_TYPE_UNFINISHED_STATE: + delete_ast_unchecked_state((struct AST_Unchecked_State*)ast); + break; + } } void delete_ast_event(struct AST_Event* ast) @@ -660,8 +676,6 @@ void delete_ast_pipeline(struct AST_Pipeline* ast) { size_t i; if(ast==NULL)return; - for(i=0;isize;++i) - delete_ast_command(ast->pipeline[i]); free(ast); } void delete_ast_machine(struct AST_Machine* ast) @@ -773,7 +787,7 @@ struct AST* parse_or_expression(struct Translation_Data *translation_data) delete_ast(hold_left_expression); return NULL; } - return (struct AST*)get_ast_binary_expression(left,right,AST_TYPE_OP_OR); + return (struct AST*)get_ast_binary_expression(hold_left_expression,hold_right_expression,AST_TYPE_OP_OR); }else { return hold_left_expression; @@ -801,7 +815,7 @@ struct AST* parse_and_expression(struct Translation_Data *translation_data) delete_ast(hold_left_expression); return NULL; } - return (struct AST*)get_ast_binary_expression(left,right,AST_TYPE_OP_AND); + return (struct AST*)get_ast_binary_expression(hold_left_expression,hold_right_expression,AST_TYPE_OP_AND); }else { return hold_left_expression; @@ -818,7 +832,7 @@ struct AST* parse_not_expression(struct Translation_Data *translation_data) hold_expression=parse_primary_expression(translation_data); if(hold_expression!=NULL) { - return get_ast_unary_expression(hold_expression,AST_TYPE_OP_NOT); + return (struct AST*)get_ast_unary_expression(hold_expression,AST_TYPE_OP_NOT); }else { push_parsing_error("in '!' expression",translation_data); @@ -857,16 +871,16 @@ struct AST* parse_primary_expression(struct Translation_Data *translation_data) if(get_kw(translation_data)==KW_ID) { hold_id2=get_ast_unchecked_state(Queue_Pop(translation_data->tokens)); - return get_ast_binary_expression(hold_id1,hold_id2,AST_TYPE_OP_SELECTOR); + return (struct AST*)get_ast_binary_expression((struct AST*)hold_id1,(struct AST*)hold_id2,AST_TYPE_OP_SELECTOR); }else { push_parsing_error("expected a state id in selector",translation_data); - delete_ast(hold_id1); + delete_ast((struct AST*)hold_id1); return NULL; } }else { - return hold_id1; + return (struct AST*)hold_id1; } }else { diff --git a/src/frontend/parser.h b/src/frontend/parser.h index 74f2ed8..a9f6b89 100644 --- a/src/frontend/parser.h +++ b/src/frontend/parser.h @@ -32,7 +32,9 @@ struct AST }; struct AST_State { + enum AST_Type type; struct token *name; + struct AST_Machine *parent; size_t number; }; struct AST_Event -- cgit v1.2.3