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;
+}
union {
struct primary_expression *primary;
+
struct {
struct postfix_expression *pfexpr;
struct token *lbracket;
};
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 {
};
struct declaration {
-
+ struct declaration_specifiers *declspecs;
+ struct init_declarator_list *initdecls;
+ struct token *semicolon;
};
struct external_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 */
if(s) {
string_debug(s);
}
-#endif
pexpr = parse_primary_expression();
primary_expression_debug(pexpr);
}
+#endif
+
return(0);
}
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;
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)
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);
+}
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);
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 */