ref: ee42812835eb35735b73cc2f4588a8c25326355b
parent: 371dbd08d948b3679df21e8304ecfb20724d0976
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Jul 17 11:40:58 EDT 2014
Create Symbol structure in cc2 The symbols in cc2 can have different type and different type of class storage, so we need a structure to can store these values.
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -8,7 +8,13 @@
#define NR_STACKSIZ 32
#define NR_NODEPOOL 128
#define NR_EXPRESSIONS 64
+#define NR_SYMBOLS 1024
+typedef struct {
+ char vartype;
+ char storage;
+} Symbol;
+
typedef struct node {
char op;
char type;
@@ -22,7 +28,7 @@
static Node *stack[NR_STACKSIZ], **stackp = stack;
static Node *listexp[NR_EXPRESSIONS], **listp = &listexp[0];
static Node nodepool[NR_NODEPOOL], *newp = nodepool;
-char vars[1024];
+static Symbol symtbl[NR_SYMBOLS];
extern void esyntax(void);
@@ -32,7 +38,7 @@
int i;
scanf("%d", &i);
- if (i < 0 || i >= 1024)
+ if (i < 0 || i >= NR_SYMBOLS)
esyntax();
return i;
}
@@ -86,9 +92,11 @@
variable(char op)
{
Node *np = newnode();
+ short id = getid();
np->op = op;
- np->type = vars[np->u.id = getid()];
+ np->u.id = id;
+ np->type = symtbl[id].vartype;
np->left = np->right = NULL;
push(np);
}
@@ -160,11 +168,11 @@
static void
declaration(char sclass)
{
- short id;
+ Symbol *sym = &symtbl[getid()];
- id = getid();
getchar(); /* skip tab */
- vars[id] = gettype();
+ sym->storage = sclass;
+ sym->vartype = gettype();
if (getchar() != '\n')
esyntax();
}