]> git.corax.cc Git - ccc/commitdiff
parser: Implement parsing of type qualifiers
authorMatthias Kruk <m@m10k.eu>
Sun, 7 Jun 2020 04:03:39 +0000 (13:03 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 7 Jun 2020 04:03:39 +0000 (13:03 +0900)
src/Makefile
src/parser.c [new file with mode: 0644]

index 1a0b10f3d651be8997e01743c6dd7f90fefd33f7..632dc32b428b56cef228a48623e8163b53b420e0 100644 (file)
@@ -1,4 +1,4 @@
-OBJECTS = str.o token.o list.o lex.o main.o
+OBJECTS = str.o token.o list.o lex.o main.o parser.o
 OUTPUT = c3
 PHONY = clean
 
diff --git a/src/parser.c b/src/parser.c
new file mode 100644 (file)
index 0000000..493e65d
--- /dev/null
@@ -0,0 +1,108 @@
+#include <stdlib.h>
+#include <string.h>
+#include "token.h"
+#include "lex.h"
+
+#define sizeof_a(_x) (sizeof(_x) / sizeof((_x)[0]))
+
+struct type_qualifier {
+       struct token *token;
+};
+
+const char *_type_qualifiers[] = {
+       "const",
+       "volatile",
+       "restrict",
+       NULL
+};
+
+#if 0
+struct translation_unit {
+       struct translation_unit     *tunit;
+       struct external_declaration *edecl;
+};
+
+struct external_declaration {
+       external_declaration_type type;
+};
+
+struct function_definition {
+       struct external_declaration parent;
+};
+
+struct declaration {
+       struct external_declaration parent;
+};
+
+struct translation_unit *parse_translation_unit(void)
+{
+       struct translation_unit *tu;
+       struct token *tok;
+
+       tu = translation_unit_new();
+
+       if(tu) {
+               tu->edecx = parse_external_declaration(void);
+
+               if(lex_peek()) {
+                       tu->tunit = parse_translation_unit();
+               }
+       }
+
+       return(tu);
+}
+
+struct external_declaration *parse_external_declaration(void)
+{
+       struct external_declaration *ed;
+
+       ed = (struct external_declaration*)parse_function_definition();
+
+       if(!ed) {
+               ed = (struct external_declaration*)parse_declaration();
+       }
+
+       return(ed);
+}
+#endif
+
+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 type_qualifier *parse_type_qualifier(void)
+{
+       struct type_qualifier *ret_val;
+       struct token *tok;
+       int pos;
+
+       pos = lex_getpos();
+       tok = lex_gettoken();
+       ret_val = NULL;
+
+       if(tok) {
+               switch(tok->type) {
+               case TOKEN_CONST:
+               case TOKEN_VOLATILE:
+               case TOKEN_RESTRICT:
+                       ret_val = type_qualifier_new(tok);
+                       break;
+
+               default:
+                       lex_setpos(pos);
+                       break;
+               }
+       }
+
+       return(ret_val);
+}