ref: 4e94d4039945fec694b6a74b2bcf03c5ffc71186
parent: 242c473f7221643384498dd67ee2717f31482ffd
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu May 31 19:11:42 EDT 2012
Moved type stack to types.c All this stuff is related to types, and this file it is a better place for it. Also added control of number of modifiers.
--- a/decl.c
+++ b/decl.c
@@ -9,21 +9,12 @@
#include "types.h"
-/* ANSI C says minimum maximum for indirection level is 12 */
-#define PTRLEVEL_MAX 12
-
char parser_out_home;
static unsigned char symhash;
static char symname[TOKSIZ_MAX + 1];
-static unsigned char stack[30];
-static unsigned char *stackp = stack;
-#define push(x) (*stackp++ = (x))
-#define pop() (*--stackp)
-#define empty() (stackp == stack)
-
#include <stdio.h> /* TODO: remove this */
void decl(void);
@@ -49,7 +40,7 @@
for (;;) {
switch (yytoken) {
case '(':
- push(FTN);
+ pushtype(FTN);
if (gettok() == ')')
gettok();
else
@@ -56,7 +47,7 @@
/* TODO: prototyped function */;
continue;
case '[':
- push(ARY);
+ pushtype(ARY);
if (gettok() == ']')
gettok();
else
@@ -229,10 +220,10 @@
dirdcl();
while (bp-- != qlf) {
- if (*bp & 1) push(CONST);
- if (*bp & 2) push(RESTRICTED);
- if (*bp & 4) push(VOLATILE);
- push(PTR);
+ if (*bp & 1) pushtype(CONST);
+ if (*bp & 2) pushtype(RESTRICTED);
+ if (*bp & 4) pushtype(VOLATILE);
+ pushtype(PTR);
}
printf("leaving dcl %c\n", yytoken);
@@ -246,11 +237,11 @@
void declaration(void)
{
- struct type *t;
+ struct type *t, *spec;
struct symbol *sym;
repeat:
- t = specifier();
+ spec = specifier();
for (; ; gettok()) {
/* TODO: put here savepoint for error recovering */
@@ -257,11 +248,8 @@
decl();
if (yytoken != ',' && yytoken != ';')
error("unexpected", yytext);
- while (!empty())
- t = mktype(t, pop());
+ t = decl_type(spec);
ptype(t);
- sym = alloca(sizeof(*sym));
- addsym(siden, sym, symhash);
if (yytoken == ',')
continue;
--- a/types.c
+++ b/types.c
@@ -5,7 +5,7 @@
#include "tokens.h"
#include "types.h"
-
+/* TODO: create wrapper file */
#define xcalloc calloc
struct type tschar = {.btype = CHAR, .sign = 1};
@@ -22,6 +22,26 @@
struct type tllong = {.btype = LLONG, .sign = 1};
struct type tullong = {.btype = LLONG, .sign = 0};
struct type tvoid = {.btype = VOID, .sign = 0};
+
+#define TYPEOP_MAX PTRLEVEL_MAX /* TODO: take a look of the ANSI standard */
+
+static unsigned char stack[TYPEOP_MAX];
+static unsigned char *stackp = stack;
+
+
+void pushtype(char op)
+{
+ if (stackp == stack + TYPEOP_MAX)
+ error("Too much type modifiers");
+ *stackp++ = op;
+}
+
+struct type *decl_type(struct type *t)
+{
+ while (stackp != stack)
+ t = mktype(t, *--stackp);
+ return t;
+}
struct type *mktype(register struct type *base, unsigned char op)
{
--- a/types.h
+++ b/types.h
@@ -49,6 +49,11 @@
struct type *mktype(register struct type *base, unsigned char op);
+/* ANSI C says minimum maximum for indirection level is 12 */
+#define PTRLEVEL_MAX 12
+
+extern struct type *decl_type(struct type *t);
+extern void pushtype(char op);
#ifndef NDEBUG
extern void ptype(register struct type *t);