]> git.corax.cc Git - ccc/commitdiff
grammar: Add structures for all statement types
authorMatthias Kruk <m@m10k.eu>
Wed, 15 Jul 2020 16:42:51 +0000 (01:42 +0900)
committerMatthias Kruk <m@m10k.eu>
Wed, 15 Jul 2020 16:42:51 +0000 (01:42 +0900)
src/grammar.c
src/grammar.h

index db1cb452ce0ed515d38ab7d77fce374852626edf..27bafabc7d2783136c9d5f199b3fb6e56a64e7c2 100644 (file)
@@ -1858,6 +1858,137 @@ void identifier_list_free(struct identifier_list *list)
        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;
+}
+
+struct labeled_statement *labeled_statement_new(void)
+{
+       struct labeled_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void labeled_statement_free(struct labeled_statement *stmt)
+{
+       if(stmt->identifier) {
+               identifier_free(stmt->identifier);
+       }
+
+       if(stmt->cexpr) {
+               constant_expression_free(stmt->cexpr);
+       }
+
+       if(stmt->stmt) {
+               statement_free(stmt->stmt);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct expression_statement *expression_statement_new(void)
+{
+       struct expression_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void expression_statement_free(struct expression_statement *stmt)
+{
+       if(stmt->expr) {
+               expression_free(stmt->expr);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
 struct compound_statement *compound_statement_new(void)
 {
        struct compound_statement *stmt;
@@ -1887,37 +2018,38 @@ void compound_statement_free(struct compound_statement *stmt)
        return;
 }
 
-struct statement_list *statement_list_new(void)
+struct if_statement *if_statement_new(void)
 {
-       struct statement_list *list;
+       struct if_statement *stmt;
 
-       list = malloc(sizeof(*list));
+       stmt = malloc(sizeof(*stmt));
 
-       if(list) {
-               memset(list, 0, sizeof(*list));
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
        }
 
-       return(list);
+       return(stmt);
 }
 
-void statement_list_free(struct statement_list *list)
+void if_statement_free(struct if_statement *stmt)
 {
-       if(list->stmt) {
-               statement_free(list->stmt);
+       if(stmt->expr) {
+               expression_free(stmt->expr);
        }
 
-       if(list->next) {
-               statement_list_free(list->next);
+       if(stmt->stmt) {
+               statement_free(stmt->stmt);
        }
 
-       memset(list, 0, sizeof(*list));
-       free(list);
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
 
        return;
 }
 
-struct statement *statement_new(void) {
-       struct statement *stmt;
+struct if_else_statement *if_else_statement_new(void)
+{
+       struct if_else_statement *stmt;
 
        stmt = malloc(sizeof(*stmt));
 
@@ -1928,30 +2060,337 @@ struct statement *statement_new(void) {
        return(stmt);
 }
 
-void statement_free(struct statement *stmt)
+void if_else_statement_free(struct if_else_statement *stmt)
 {
-       if(stmt->labeled) {
-               labeled_statement_free(stmt->labeled);
+       if(stmt->expr) {
+               expression_free(stmt->expr);
        }
 
-       if(stmt->expression) {
-               expression_statement_free(stmt->expression);
+       if(stmt->stmt) {
+               statement_free(stmt->stmt);
        }
 
-       if(stmt->compound) {
-               compound_statement_free(stmt->compound);
+       if(stmt->estmt) {
+               statement_free(stmt->estmt);
        }
 
-       if(stmt->selection) {
-               selection_statement_free(stmt->selection);
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct switch_statement *switch_statement_new(void)
+{
+       struct switch_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
        }
 
-       if(stmt->iteration) {
-               iteration_statement_free(stmt->iteration);
+       return(stmt);
+}
+
+void switch_statement_free(struct switch_statement *stmt)
+{
+       if(stmt->expr) {
+               expression_free(stmt->expr);
        }
 
-       if(stmt->jump) {
-               jump_statement_free(stmt->jump);
+       if(stmt->stmt) {
+               statement_free(stmt->stmt);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct selection_statement *selection_statement_new(void)
+{
+       struct selection_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void selection_statement_free(struct selection_statement *stmt)
+{
+       if(stmt->ifstmt) {
+               if_statement_free(stmt->ifstmt);
+       }
+
+       if(stmt->ifelsestmt) {
+               if_else_statement_free(stmt->ifelsestmt);
+       }
+
+       if(stmt->switchstmt) {
+               switch_statement_free(stmt->switchstmt);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct while_statement *while_statement_new(void)
+{
+       struct while_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void while_statement_free(struct while_statement *stmt)
+{
+       if(stmt->expr) {
+               expression_free(stmt->expr);
+       }
+
+       if(stmt->stmt) {
+               statement_free(stmt->stmt);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct do_while_statement *do_while_statement_new(void)
+{
+       struct do_while_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void do_while_statement_free(struct do_while_statement *stmt)
+{
+       if(stmt->stmt) {
+               statement_free(stmt->stmt);
+       }
+
+       if(stmt->expr) {
+               expression_free(stmt->expr);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct for_statement *for_statement_new(void)
+{
+       struct for_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void for_statement_free(struct for_statement *stmt)
+{
+       if(stmt->expr1) {
+               expression_free(stmt->expr1);
+       }
+
+       if(stmt->expr2) {
+               expression_free(stmt->expr2);
+       }
+
+       if(stmt->expr3) {
+               expression_free(stmt->expr3);
+       }
+
+       if(stmt->stmt) {
+               statement_free(stmt->stmt);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct iteration_statement *iteration_statement_new(void)
+{
+       struct iteration_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void iteration_statement_free(struct iteration_statement *stmt)
+{
+       if(stmt->whilestmt) {
+               while_statement_free(stmt->whilestmt);
+       }
+
+       if(stmt->dowhilestmt) {
+               do_while_statement_free(stmt->dowhilestmt);
+       }
+
+       if(stmt->forstmt) {
+               for_statement_free(stmt->forstmt);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct goto_statement *goto_statement_new(void)
+{
+       struct goto_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void goto_statement_free(struct goto_statement *stmt)
+{
+       if(stmt->identifier) {
+               identifier_free(stmt->identifier);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct continue_statement *continue_statement_new(void)
+{
+       struct continue_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void continue_statement_free(struct continue_statement *stmt)
+{
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct break_statement *break_statement_new(void)
+{
+       struct break_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void break_statement_free(struct break_statement *stmt)
+{
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct return_statement *return_statement_new(void)
+{
+       struct return_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void return_statement_free(struct return_statement *stmt)
+{
+       if(stmt->expr) {
+               expression_free(stmt->expr);
+       }
+
+       memset(stmt, 0, sizeof(*stmt));
+       free(stmt);
+
+       return;
+}
+
+struct jump_statement *jump_statement_new(void)
+{
+       struct jump_statement *stmt;
+
+       stmt = malloc(sizeof(*stmt));
+
+       if(stmt) {
+               memset(stmt, 0, sizeof(*stmt));
+       }
+
+       return(stmt);
+}
+
+void jump_statement_free(struct jump_statement *stmt)
+{
+       if(stmt->gotostmt) {
+               goto_statement_free(stmt->gotostmt);
+       }
+
+       if(stmt->continuestmt) {
+               continue_statement_free(stmt->continuestmt);
+       }
+
+       if(stmt->breakstmt) {
+               break_statement_free(stmt->breakstmt);
+       }
+
+       if(stmt->returnstmt) {
+               return_statement_free(stmt->returnstmt);
        }
 
        memset(stmt, 0, sizeof(*stmt));
index ac3da506f41683a90edc4ca10248fa0cd193ca7e..6f0328401d6f1b59c724239711b65f9034dd16fa 100644 (file)
@@ -490,7 +490,7 @@ struct labeled_statement {
        struct constant_expression *cexpr;
        struct token *tdefault;
        struct token *colon;
-       struct statement *statement;
+       struct statement *stmt;
 };
 
 struct expression_statement {
@@ -537,12 +537,69 @@ struct selection_statement {
        struct switch_statement *switchstmt;
 };
 
+struct while_statement {
+       struct token *twhile;
+       struct token *tlparen;
+       struct token *trparen;
+       struct expression *expr;
+       struct statement *stmt;
+};
+
+struct do_while_statement {
+       struct token *tdo;
+       struct token *twhile;
+       struct token *lparen;
+       struct token *rparen;
+       struct token *semicolon;
+       struct statement *stmt;
+       struct expression *expr;
+};
+
+struct for_statement {
+       struct token *tfor;
+       struct token *tlparen;
+       struct token *trparen;
+       struct token *tsemicolonl;
+       struct token *tsemicolonr;
+       struct expression *expr1;
+       struct expression *expr2;
+       struct expression *expr3;
+       struct statement *stmt;
+};
+
 struct iteration_statement {
+       struct while_statement *whilestmt;
+       struct do_while_statement *dowhilestmt;
+       struct for_statement *forstmt;
+};
 
+struct goto_statement {
+       struct token *tgoto;
+       struct identifier *identifier;
+       struct token *tsemicolon;
 };
 
-struct jump_statement {
+struct continue_statement {
+       struct token *tcontinue;
+       struct token *tsemicolon;
+};
+
+struct break_statement {
+       struct token *tbreak;
+       struct token *tsemicolon;
+};
 
+struct return_statement {
+       struct token *treturn;
+       struct expression *expr;
+       struct token *tsemicolon;
+};
+
+struct jump_statement {
+       struct goto_statement *gotostmt;
+       struct continue_statement *continuestmt;
+       struct break_statement *breakstmt;
+       struct return_statement *returnstmt;
 };
 
 struct statement {
@@ -744,11 +801,56 @@ void parameter_declaration_free(struct parameter_declaration*);
 struct identifier_list *identifier_list_new(void);
 void identifier_list_free(struct identifier_list*);
 
+struct statement *statement_new(void);
+void statement_free(struct statement*);
+
+struct labeled_statement *labeled_statement_new(void);
+void labeled_statement_free(struct labeled_statement*);
+
+struct expression_statement *expression_statement_new(void);
+void expression_statement_free(struct expression_statement*);
+
 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 if_statement *if_statement_new(void);
+void if_statement_free(struct if_statement*);
+
+struct if_else_statement *if_else_statement_new(void);
+void if_else_statement_free(struct if_else_statement*);
+
+struct switch_statement *switch_statement_new(void);
+void switch_statement_free(struct switch_statement*);
+
+struct selection_statement *selection_statement_new(void);
+void selection_statement_free(struct selection_statement*);
+
+struct while_statement *while_statement_new(void);
+void while_statement_free(struct while_statement*);
+
+struct do_while_statement *do_while_statement_new(void);
+void do_while_statement_free(struct do_while_statement*);
+
+struct for_statement *for_statement_new(void);
+void for_statement_free(struct for_statement*);
+
+struct iteration_statement *iteration_statement_new(void);
+void iteration_statement_free(struct iteration_statement*);
+
+struct goto_statement *goto_statement_new(void);
+void goto_statement_free(struct goto_statement*);
+
+struct continue_statement *continue_statement_new(void);
+void continue_statement_free(struct continue_statement*);
+
+struct break_statement *break_statement_new(void);
+void break_statement_free(struct break_statement*);
+
+struct return_statement *return_statement_new(void);
+void return_statement_free(struct return_statement*);
+
+struct jump_statement *jump_statement_new(void);
+void jump_statement_free(struct jump_statement*);
 
 struct statement_list *statement_list_new(void);
 void statement_list_free(struct statement_list*);