]> git.corax.cc Git - ccc/commitdiff
parser: Move structs that describe the C grammar to grammar.h and their methods to...
authorMatthias Kruk <m@m10k.eu>
Sun, 7 Jun 2020 23:11:31 +0000 (08:11 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 7 Jun 2020 23:11:31 +0000 (08:11 +0900)
src/Makefile
src/grammar.c [new file with mode: 0644]
src/grammar.h [new file with mode: 0644]
src/parser.c

index 632dc32b428b56cef228a48623e8163b53b420e0..1d163b2baf74e096360a8ab39362dc21aeb2fbed 100644 (file)
@@ -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 (file)
index 0000000..71d049f
--- /dev/null
@@ -0,0 +1,71 @@
+#include <stdlib.h>
+#include <string.h>
+#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 (file)
index 0000000..051263e
--- /dev/null
@@ -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 */
index d25dd5ca63e18fb063245eb73ee63740fa924492..f3fb35dcc6e7328ea563a21d298e5a4f848ae3ab 100644 (file)
@@ -3,98 +3,7 @@
 #include <stdio.h>
 #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);