]> git.corax.cc Git - ccc/commitdiff
parser: Begin with implementation of parser for statements
authorMatthias Kruk <m@m10k.eu>
Wed, 15 Jul 2020 15:12:05 +0000 (00:12 +0900)
committerMatthias Kruk <m@m10k.eu>
Wed, 15 Jul 2020 15:12:05 +0000 (00:12 +0900)
src/grammar.c
src/grammar.h
src/main.c
src/parser.c
src/parser.h

index 70590c6049702aceb11c01b370e69241770d3559..db1cb452ce0ed515d38ab7d77fce374852626edf 100644 (file)
@@ -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;
+}
index 1ba60c171d2d47d5d7bc1b731fadd0c8f90a81e8..ac3da506f41683a90edc4ca10248fa0cd193ca7e 100644 (file)
@@ -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 */
index 9dfda3e327f9978ad67884e14a90aec377b106df..7d5c4fa51cf4b924d3a689651e620a949572e9fb 100644 (file)
@@ -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);
 }
index c7e82bea51af162b8d95037aa7d059c6f28c718b..9dee7c8f93e4f0a89da757f3f6ccbdf4a3e3fcea 100644 (file)
@@ -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);
+}
index 93660e506f7058840cfff04af99cf4cc09c1a326..2a863a6a082f89994818def6d830ebb5334f9840 100644 (file)
@@ -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 */