From: Matthias Kruk Date: Sun, 7 Jun 2020 04:03:39 +0000 (+0900) Subject: parser: Implement parsing of type qualifiers X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=3caeacbf3af62da417133f734e18c3ccb503ca5e;p=ccc parser: Implement parsing of type qualifiers --- diff --git a/src/Makefile b/src/Makefile index 1a0b10f..632dc32 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 index 0000000..493e65d --- /dev/null +++ b/src/parser.c @@ -0,0 +1,108 @@ +#include +#include +#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); +}