From: Matthias Kruk Date: Wed, 15 Jul 2020 15:12:05 +0000 (+0900) Subject: parser: Begin with implementation of parser for statements X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=37d2fbf26d146401246e7d12cbafa55930e7c68b;p=ccc parser: Begin with implementation of parser for statements --- diff --git a/src/grammar.c b/src/grammar.c index 70590c6..db1cb45 100644 --- a/src/grammar.c +++ b/src/grammar.c @@ -1857,3 +1857,105 @@ void identifier_list_free(struct identifier_list *list) return; } + +struct compound_statement *compound_statement_new(void) +{ + struct compound_statement *stmt; + + stmt = malloc(sizeof(*stmt)); + + if(stmt) { + memset(stmt, 0, sizeof(*stmt)); + } + + return(stmt); +} + +void compound_statement_free(struct compound_statement *stmt) +{ + if(stmt->decls) { + declaration_list_free(stmt->decls); + } + + if(stmt->stmts) { + statement_list_free(stmt->stmts); + } + + memset(stmt, 0, sizeof(*stmt)); + free(stmt); + + return; +} + +struct statement_list *statement_list_new(void) +{ + struct statement_list *list; + + list = malloc(sizeof(*list)); + + if(list) { + memset(list, 0, sizeof(*list)); + } + + return(list); +} + +void statement_list_free(struct statement_list *list) +{ + if(list->stmt) { + statement_free(list->stmt); + } + + if(list->next) { + statement_list_free(list->next); + } + + memset(list, 0, sizeof(*list)); + free(list); + + return; +} + +struct statement *statement_new(void) { + struct statement *stmt; + + stmt = malloc(sizeof(*stmt)); + + if(stmt) { + memset(stmt, 0, sizeof(*stmt)); + } + + return(stmt); +} + +void statement_free(struct statement *stmt) +{ + if(stmt->labeled) { + labeled_statement_free(stmt->labeled); + } + + if(stmt->expression) { + expression_statement_free(stmt->expression); + } + + if(stmt->compound) { + compound_statement_free(stmt->compound); + } + + if(stmt->selection) { + selection_statement_free(stmt->selection); + } + + if(stmt->iteration) { + iteration_statement_free(stmt->iteration); + } + + if(stmt->jump) { + jump_statement_free(stmt->jump); + } + + memset(stmt, 0, sizeof(*stmt)); + free(stmt); + + return; +} diff --git a/src/grammar.h b/src/grammar.h index 1ba60c1..ac3da50 100644 --- a/src/grammar.h +++ b/src/grammar.h @@ -52,6 +52,7 @@ struct postfix_expression { union { struct primary_expression *primary; + struct { struct postfix_expression *pfexpr; struct token *lbracket; @@ -479,11 +480,83 @@ struct declarator { }; struct declaration_list { + struct declaration *decl; + struct declaration_list *next; +}; +struct labeled_statement { + struct identifier *identifier; + struct token *tcase; + struct constant_expression *cexpr; + struct token *tdefault; + struct token *colon; + struct statement *statement; +}; + +struct expression_statement { + struct expression *expr; + struct token *semicolon; }; struct compound_statement { + struct token *lbrace; + struct declaration_list *decls; + struct statement_list *stmts; + struct token *rbrace; +}; + +struct if_statement { + struct token *tif; + struct token *tlparen; + struct token *trparen; + struct expression *expr; + struct statement *stmt; +}; + +struct if_else_statement { + struct token *tif; + struct token *tlparen; + struct token *trparen; + struct token *telse; + struct expression *expr; + struct statement *stmt; + struct statement *estmt; +}; + +struct switch_statement { + struct token *tswitch; + struct token *tlparen; + struct token *trparen; + struct expression *expr; + struct statement *stmt; +}; + +struct selection_statement { + struct if_statement *ifstmt; + struct if_else_statement *ifelsestmt; + struct switch_statement *switchstmt; +}; + +struct iteration_statement { + +}; + +struct jump_statement { + +}; +struct statement { + struct labeled_statement *labeled; + struct expression_statement *expression; + struct compound_statement *compound; + struct selection_statement *selection; + struct iteration_statement *iteration; + struct jump_statement *jump; +}; + +struct statement_list { + struct statement *stmt; + struct statement_list *next; }; struct function_definition { @@ -494,7 +567,9 @@ struct function_definition { }; struct declaration { - + struct declaration_specifiers *declspecs; + struct init_declarator_list *initdecls; + struct token *semicolon; }; struct external_declaration { @@ -669,4 +744,19 @@ void parameter_declaration_free(struct parameter_declaration*); struct identifier_list *identifier_list_new(void); void identifier_list_free(struct identifier_list*); +struct compound_statement *compound_statement_new(void); +void compound_statement_free(struct compound_statement*); + +struct statement *statement_new(void); +void statement_free(struct statement*); + +struct statement_list *statement_list_new(void); +void statement_list_free(struct statement_list*); + +struct declaration *declaration_new(void); +void declaration_free(struct declaration*); + +struct declaration_list *declaration_list_new(void); +void declaration_list_free(struct declaration_list*); + #endif /* GRAMMAR_H */ diff --git a/src/main.c b/src/main.c index 9dfda3e..7d5c4fa 100644 --- a/src/main.c +++ b/src/main.c @@ -32,7 +32,6 @@ int main(int argc, char *argv[]) if(s) { string_debug(s); } -#endif pexpr = parse_primary_expression(); @@ -42,5 +41,7 @@ int main(int argc, char *argv[]) primary_expression_debug(pexpr); } +#endif + return(0); } diff --git a/src/parser.c b/src/parser.c index c7e82be..9dee7c8 100644 --- a/src/parser.c +++ b/src/parser.c @@ -289,12 +289,6 @@ struct declaration_list *parse_declaration_list(void) return(NULL); } -struct compound_statement *parse_compound_statement(void) -{ - fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); - return(NULL); -} - struct direct_declarator *parse_direct_declarator(void) { struct direct_declarator *decl; @@ -423,8 +417,28 @@ struct declarator *parse_declarator(void) struct declaration *parse_declaration(void) { - fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); - return(NULL); + struct declaration *decl; + + decl = declaration_new(); + + if(decl) { + int pos; + + pos = lex_getpos(); + + decl->declspecs = parse_declaration_specifiers(); + decl->initdecls = parse_init_declarator_list(); + decl->semicolon = lex_gettoken(); + + if(!decl->declspecs || !decl->semicolon || decl->semicolon->type != TOKEN_SEMICOLON) { + lex_setpos(pos); + + declaration_free(decl); + decl = NULL; + } + } + + return(decl); } struct external_declaration *parse_external_declaration(void) @@ -1804,3 +1818,80 @@ struct identifier_list *parse_identifier_list(void) return(NULL); } + +struct statement *parse_statement(void) +{ + struct statement *stmt; + + stmt = statement_new(); + + if(stmt) { + if((stmt->labeled = parse_labeled_statement())) { + return(stmt); + } + + if((stmt->expression = parse_expression_statement())) { + return(stmt); + } + + if((stmt->compound = parse_compound_statement())) { + return(stmt); + } + + if((stmt->selection = parse_selection_statement())) { + return(stmt); + } + + if((stmt->iteration = parse_iteration_statement())) { + return(stmt); + } + + if((stmt->jump = parse_jump_statement())) { + return(stmt); + } + } + + statement_free(stmt); + return(NULL); +} + +struct statement_list *parse_statement_list(void) +{ + return(NULL); +} + +struct labeled_statement *parse_labeled_statement(void) +{ + fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); + return(NULL); +} + +struct expression_statement *parse_expression_statement(void) +{ + fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); + return(NULL); +} + +struct compound_statement *parse_compound_statement(void) +{ + fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); + return(NULL); +} + +struct selection_statement *parse_selection_statement(void) +{ + fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); + return(NULL); +} + +struct iteration_statement *parse_iteration_statement(void) +{ + fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); + return(NULL); +} + +struct jump_statement *parse_jump_statement(void) +{ + fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); + return(NULL); +} diff --git a/src/parser.h b/src/parser.h index 93660e5..2a863a6 100644 --- a/src/parser.h +++ b/src/parser.h @@ -43,7 +43,6 @@ struct type_specifier *parse_type_specifier(void); struct declaration_list *parse_declaration_list(void); struct declaration_specifiers *parse_declaration_specifiers(void); struct function_definition *parse_function_definition(void); -struct compound_statement *parse_compound_statement(void); struct direct_abstract_declarator *parse_direct_abstract_declarator(void); struct type_qualifier_list *parse_type_qualifier_list(void); struct declarator *parse_declarator(void); @@ -55,5 +54,16 @@ struct parameter_type_list *parse_parameter_type_list(void); struct parameter_list *parse_parameter_list(void); struct parameter_declaration *parse_parameter_declaration(void); struct identifier_list *parse_identifier_list(void); +struct init_declarator_list *parse_init_declarator_list(void); + +struct statement *parse_statement(void); +struct labeled_statement *parse_labeled_statement(void); +struct expression_statement *parse_expression_statement(void); +struct compound_statement *parse_compound_statement(void); +struct selection_statement *parse_selection_statement(void); +struct iteration_statement *parse_iteration_statement(void); +struct jump_statement *parse_jump_statement(void); +struct statement_list *parse_statement_list(void); + #endif /* PARSER_H */