--- a/c99.y +++ b/c99.y @@ -22,12 +22,35 @@ #define STMT_SIZE 128 #define FIFO_SIZE 20 -static char *s_fifo[FIFO_SIZE][STMT_SIZE]; -static unsigned int s_fifor, s_fifow; - +typedef enum +{ + decl_all, + decl_inout, + decl_var, + fifo_entry_num /* reflects the number of enum entries */ +} fifo_entry_t; + +typedef struct +{ + char fifo[FIFO_SIZE][STMT_SIZE]; + unsigned int read; + unsigned int write; +} fifo_t; + +static fifo_t s_fifos[fifo_entry_num]; void yyerror(char const *s); -void push(const char *psz); -char *pop(void); + +// push an entry (typically a declaration into the fifo +void fifo_push(fifo_entry_t type, const char *psz); + +// pop one entry from the specific category +char *fifo_pop(fifo_entry_t type); + +// tag all from category "all" to new type +// this way we can collect new stuff in all and push +// it to the correct section when we know more about +// our context +void fifo_tag(fifo_entry_t type); %} @@ -211,7 +234,14 @@ : declaration_specifiers ';' { sprintf($<str>$, "%s %s",$<str>1, $<str>2); } | declaration_specifiers init_declarator_list ';' - { char *p; while( (p=pop()) != NULL) { printf(p, $<str>1); strcpy($<str>$, ""); } } + { + char *p; + while( (p=fifo_pop(decl_all)) != NULL) + { + printf(p, $<str>1); + strcpy($<str>$, ""); + } + } ; declaration_specifiers @@ -341,28 +371,28 @@ : pointer direct_declarator { sprintf($<str>$, "%s POINTER TO %%s;\n",$<str>2); - push($<str>$); + fifo_push(decl_all, $<str>$); } | direct_declarator { if (strlen($<str>1) > 0) { sprintf($<str>$, "%s %%s;\n",$<str>1); - push($<str>$); + fifo_push(decl_all, $<str>$); } } ; direct_declarator : IDENTIFIER -// { sprintf($<str>$, "%s: %%s;\n",$<str>1); push($<str>$);} +// { sprintf($<str>$, "%s: %%s;\n",$<str>1); fifo_push($<str>$);} | '(' declarator ')' | direct_declarator '[' type_qualifier_list assignment_expression ']' | direct_declarator '[' type_qualifier_list ']' | direct_declarator '[' assignment_expression ']' { sprintf($<str>$, "%s: ARRAY[ %s ] OF %%s;\n",$<str>1, $<str>3); - push($<str>$); + fifo_push(decl_all, $<str>$); strcpy($<str>$, ""); } | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']' @@ -566,26 +596,32 @@ /* fifo implementation */ -void push(const char *psz) +void fifo_push(fifo_entry_t type, const char *psz) { - unsigned int fifow_next = (s_fifow + 1) % FIFO_SIZE; - if (fifow_next != s_fifor) + unsigned int fifow_next = (s_fifos[type].write + 1) % FIFO_SIZE; + if (fifow_next != s_fifos[type].read) { //s_fifo[s_fifow] = psz; - strcpy(s_fifo[s_fifow], psz); - s_fifow = fifow_next; + strcpy(s_fifos[type].fifo[s_fifos[type].write], psz); + s_fifos[type].write = fifow_next; } } -char *pop(void) +char *fifo_pop(fifo_entry_t type) { unsigned int fifor_prev; - if (s_fifor != s_fifow) + if (s_fifos[type].read != s_fifos[type].write) { - fifor_prev = s_fifor; - s_fifor = (s_fifor + 1) % FIFO_SIZE; - return s_fifo[fifor_prev]; + fifor_prev = s_fifos[type].read; + s_fifos[type].read = (s_fifos[type].read + 1) % FIFO_SIZE; + return s_fifos[type].fifo[fifor_prev]; } return NULL; } +void fifo_tag(fifo_entry_t type) +{ + char *p; + while ((p = fifo_pop(decl_all)) != NULL) + fifo_push(type, p); +}