From: Matthias Kruk Date: Sat, 18 Jul 2020 04:26:11 +0000 (+0900) Subject: parser: Implement parsing of while and goto statements X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=15f27c94a3d26214c907bf9787b6106b916d989e;p=ccc parser: Implement parsing of while and goto statements --- diff --git a/src/parser.c b/src/parser.c index 9dee7c8..12dda22 100644 --- a/src/parser.c +++ b/src/parser.c @@ -631,7 +631,6 @@ struct floating_constant *parse_floating_constant(void) struct enumeration_constant *parse_enumeration_constant(void) { - fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); /* TODO: Implement parsing of enumeration constants */ return(NULL); } @@ -1884,13 +1883,157 @@ struct selection_statement *parse_selection_statement(void) return(NULL); } +struct while_statement *parse_while_statement(void) +{ + struct while_statement *stmt; + + stmt = while_statement_new(); + + if(stmt) { + int pos; + + pos = lex_getpos(); + + stmt->twhile = lex_gettoken(); + stmt->tlparen = lex_gettoken(); + fprintf(stderr, "%s: expression?\n", __func__); + stmt->expr = parse_expression(); + stmt->trparen = lex_gettoken(); + fprintf(stderr, "%s: statement?\n", __func__); + stmt->stmt = parse_statement(); + + if(stmt->twhile && stmt->tlparen && stmt->expr && stmt->trparen && stmt->stmt && + stmt->twhile->type == TOKEN_WHILE && stmt->tlparen->type == TOKEN_LPAREN && + stmt->trparen->type == TOKEN_RPAREN) { + return(stmt); + } + + while_statement_free(stmt); + lex_setpos(pos); + } + + return(NULL); +} + +struct do_while_statement *parse_do_while_statement(void) +{ + return(NULL); +} + +struct for_statement *parse_for_statement(void) +{ + return(NULL); +} + struct iteration_statement *parse_iteration_statement(void) { - fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); + struct iteration_statement *stmt; + + stmt = iteration_statement_new(); + + if(stmt) { + fprintf(stderr, "%s: while?\n", __func__); + if((stmt->whilestmt = parse_while_statement())) { + return(stmt); + } + + fprintf(stderr, "%s: do while?\n", __func__); + if((stmt->dowhilestmt = parse_do_while_statement())) { + return(stmt); + } + + fprintf(stderr, "%s: for?\n", __func__); + if((stmt->forstmt = parse_for_statement())) { + return(stmt); + } + + fprintf(stderr, "%s: None of the above\n", __func__); + iteration_statement_free(stmt); + } + + return(NULL); +} + +struct goto_statement *parse_goto_statement(void) +{ + struct goto_statement *stmt; + + stmt = goto_statement_new(); + + if(stmt) { + int pos; + + pos = lex_getpos(); + + stmt->tgoto = lex_gettoken(); + stmt->identifier = parse_identifier(); + stmt->tsemicolon = lex_gettoken(); + + fprintf(stderr, "%s: %p %p %p\n", __func__, stmt->tgoto, + stmt->identifier, stmt->tsemicolon); + + if(stmt->tgoto && stmt->identifier && stmt->tsemicolon && + stmt->tgoto->type == TOKEN_GOTO && stmt->tsemicolon->type == TOKEN_SEMICOLON) { + return(stmt); + } + + goto_statement_free(stmt); + lex_setpos(pos); + } + + return(NULL); +} + +struct continue_statement *parse_continue_statement(void) +{ + return(NULL); +} + +struct break_statement *parse_break_statement(void) +{ + return(NULL); +} + +struct return_statement *parse_return_statement(void) +{ return(NULL); } struct jump_statement *parse_jump_statement(void) +{ + struct jump_statement *stmt; + + stmt = jump_statement_new(); + + if(stmt) { + int pos; + + pos = lex_getpos(); + + if((stmt->gotostmt = parse_goto_statement())) { + return(stmt); + } + + if((stmt->continuestmt = parse_continue_statement())) { + return(stmt); + } + + if((stmt->breakstmt = parse_break_statement())) { + return(stmt); + } + + if((stmt->returnstmt = parse_return_statement())) { + return(stmt); + } + + jump_statement_free(stmt); + lex_setpos(pos); + } + + return(NULL); +} + +struct init_declarator_list *parse_init_declarator_list(void) { fprintf(stderr, "FIXME: %s() is not implemented\n", __func__); return(NULL);