From: Matthias Kruk Date: Sun, 7 Jun 2020 23:11:31 +0000 (+0900) Subject: parser: Move structs that describe the C grammar to grammar.h and their methods to... X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=d72056ed883b3a1bf4a70858b75ffba30113510a;p=ccc parser: Move structs that describe the C grammar to grammar.h and their methods to grammar.c --- diff --git a/src/Makefile b/src/Makefile index 632dc32..1d163b2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -OBJECTS = str.o token.o list.o lex.o main.o parser.o +OBJECTS = str.o token.o list.o lex.o main.o parser.o grammar.o OUTPUT = c3 PHONY = clean diff --git a/src/grammar.c b/src/grammar.c new file mode 100644 index 0000000..71d049f --- /dev/null +++ b/src/grammar.c @@ -0,0 +1,71 @@ +#include +#include +#include "grammar.h" + +struct type_qualifier *type_qualifier_new(struct token *tok) +{ + struct type_qualifier *tq; + + tq = malloc(sizeof(*tq)); + + if(tq) { + memset(tq, 0, sizeof(*tq)); + tq->token = tok; + } + + return(tq); +} + +struct storage_class_specifier *storage_class_specifier_new(struct token *tok) +{ + struct storage_class_specifier *sp; + + sp = malloc(sizeof(*sp)); + + if(sp) { + memset(sp, 0, sizeof(*sp)); + sp->token = tok; + } + + return(sp); +} + +struct type_specifier *type_specifier_new(void) +{ + struct type_specifier *ts; + + ts = malloc(sizeof(*ts)); + + if(ts) { + memset(ts, 0, sizeof(*ts)); + } + + return(ts); +} + +struct translation_unit *translation_unit_new(struct external_declaration *edecl) +{ + struct translation_unit *tu; + + tu = malloc(sizeof(*tu)); + + if(tu) { + memset(tu, 0, sizeof(*tu)); + tu->decl = edecl; + } + + return(tu); +} + +struct external_declaration *external_declaration_new(void) +{ + struct external_declaration *ed; + + ed = malloc(sizeof(*ed)); + + if(ed) { + memset(ed, 0, sizeof(*ed)); + } + + return(ed); +} diff --git a/src/grammar.h b/src/grammar.h new file mode 100644 index 0000000..051263e --- /dev/null +++ b/src/grammar.h @@ -0,0 +1,74 @@ +#ifndef GRAMMAR_H +#define GRAMMAR_H + +struct token; + +struct type_qualifier { + struct token *token; +}; + +struct storage_class_specifier { + struct token *token; +}; + +enum type_specifier_type { + TYPE_SPECIFIER_INVALID, + TYPE_SPECIFIER_PRIMITIVE, + TYPE_SPECIFIER_STRUCT, + TYPE_SPECIFIER_UNION, + TYPE_SPECIFIER_ENUM, + TYPE_SPECIFIER_TYPEDEF +}; + +struct struct_specifier { + struct token *token; +}; + +struct union_specifier { + struct token *token; +}; + +struct enum_specifier { + struct token *token; +}; + +struct typedef_name { + /* add a reference to the typedef? */ + struct token *token; +}; + +struct type_specifier { + enum type_specifier_type type; + struct token *token; + + struct struct_specifier *ss; + struct union_specifier *us; + struct enum_specifier *es; + struct typedef_name *tn; +}; + +struct function_definition { + +}; + +struct declaration { + +}; + +struct external_declaration { + struct function_definition *fdef; + struct declaration *decl; +}; + +struct translation_unit { + struct translation_unit *unit; + struct external_declaration *decl; +}; + +struct type_qualifier *type_qualifier_new(struct token*); +struct storage_class_specifier *storage_class_specifier_new(struct token*); +struct type_specifier *type_specifier_new(void); +struct translation_unit *translation_unit_new(struct external_declaration*); +struct external_declaration *external_declaration_new(void); + +#endif /* GRAMMAR_H */ diff --git a/src/parser.c b/src/parser.c index d25dd5c..f3fb35d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3,98 +3,7 @@ #include #include "token.h" #include "lex.h" - -#define sizeof_a(_x) (sizeof(_x) / sizeof((_x)[0])) - -struct type_qualifier { - struct token *token; -}; - -struct storage_class_specifier { - struct token *token; -}; - -enum type_specifier_type { - TYPE_SPECIFIER_INVALID, - TYPE_SPECIFIER_PRIMITIVE, - TYPE_SPECIFIER_STRUCT, - TYPE_SPECIFIER_UNION, - TYPE_SPECIFIER_ENUM, - TYPE_SPECIFIER_TYPEDEF -}; - -struct struct_specifier { - struct token *token; -}; - -struct union_specifier { - struct token *token; -}; - -struct enum_specifier { - struct token *token; -}; - -struct typedef_name { - /* add a reference to the typedef? */ - struct token *token; -}; - -struct type_specifier { - enum type_specifier_type type; - struct token *token; - - struct struct_specifier *ss; - struct union_specifier *us; - struct enum_specifier *es; - struct typedef_name *tn; -}; - -struct function_definition { - -}; - -struct declaration { - -}; - -struct external_declaration { - struct function_definition *fdef; - struct declaration *decl; -}; - -struct translation_unit { - struct translation_unit *unit; - struct external_declaration *decl; -}; - -struct type_qualifier *type_qualifier_new(struct token *tok) -{ - struct type_qualifier *tq; - - tq = malloc(sizeof(*tq)); - - if(tq) { - memset(tq, 0, sizeof(*tq)); - tq->token = tok; - } - - return(tq); -} - -struct storage_class_specifier *storage_class_specifier_new(struct token *tok) -{ - struct storage_class_specifier *sp; - - sp = malloc(sizeof(*sp)); - - if(sp) { - memset(sp, 0, sizeof(*sp)); - sp->token = tok; - } - - return(sp); -} +#include "grammar.h" struct type_qualifier *parse_type_qualifier(void) { @@ -152,33 +61,6 @@ struct storage_class_specifier *parse_storage_class_specifier(void) return(ret_val); } -struct type_specifier *type_specifier_new(void) -{ - struct type_specifier *ts; - - ts = malloc(sizeof(*ts)); - - if(ts) { - memset(ts, 0, sizeof(*ts)); - } - - return(ts); -} - -struct translation_unit *translation_unit_new(struct external_declaration *edecl) -{ - struct translation_unit *tu; - - tu = malloc(sizeof(*tu)); - - if(tu) { - memset(tu, 0, sizeof(*tu)); - tu->decl = edecl; - } - - return(tu); -} - struct struct_specifier *parse_struct_specifier(void) { return(NULL); @@ -255,7 +137,8 @@ struct type_specifier *parse_type_specifier(void) case TYPE_SPECIFIER_STRUCT: if(!ret_val->ss) { /* syntax error */ - fprintf(stderr, "%s:%d:%d [E] Expected struct-specifier\n", + fprintf(stderr, + "%s:%d:%d [E] Expected struct-specifier\n", tok->file, tok->line, tok->column); ret_val->type = TYPE_SPECIFIER_INVALID; } @@ -264,7 +147,8 @@ struct type_specifier *parse_type_specifier(void) case TYPE_SPECIFIER_UNION: if(!ret_val->us) { /* syntax error */ - fprintf(stderr, "%s:%d:%d [E] Expected union-specifier\n", + fprintf(stderr, + "%s:%d:%d [E] Expected union-specifier\n", tok->file, tok->line, tok->column); ret_val->type = TYPE_SPECIFIER_INVALID; } @@ -273,7 +157,8 @@ struct type_specifier *parse_type_specifier(void) case TYPE_SPECIFIER_ENUM: if(!ret_val->es) { /* syntax error */ - fprintf(stderr, "%s:%d:%d [E] Expected enum-specifier\n", + fprintf(stderr, + "%s:%d:%d [E] Expected enum-specifier\n", tok->file, tok->line, tok->column); ret_val->type = TYPE_SPECIFIER_INVALID; } @@ -282,7 +167,8 @@ struct type_specifier *parse_type_specifier(void) case TYPE_SPECIFIER_TYPEDEF: if(!ret_val->tn) { /* syntax error */ - fprintf(stderr, "%s:%d:%d [E] Expected typedef-name\n", + fprintf(stderr, + "%s:%d:%d [E] Expected typedef-name\n", tok->file, tok->line, tok->column); ret_val->type = TYPE_SPECIFIER_INVALID; } @@ -291,7 +177,8 @@ struct type_specifier *parse_type_specifier(void) default: if(ret_val->type != TYPE_SPECIFIER_PRIMITIVE) { /* syntax error */ - fprintf(stderr, "%s:%d:%d [E] Expected type-specifier\n", + fprintf(stderr, + "%s:%d:%d [E] Expected type-specifier\n", tok->file, tok->line, tok->column); ret_val->type = TYPE_SPECIFIER_INVALID; } @@ -309,19 +196,6 @@ struct type_specifier *parse_type_specifier(void) return(ret_val); } -struct external_declaration *external_declaration_new(void) -{ - struct external_declaration *ed; - - ed = malloc(sizeof(*ed)); - - if(ed) { - memset(ed, 0, sizeof(*ed)); - } - - return(ed); -} - struct function_definition *parse_function_definition(void) { return(NULL);