2020-12-30 12:16:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "grammar.h"
|
|
|
|
#include "grammar.tab.h"
|
|
|
|
|
|
|
|
static Symbol *symlist = 0; /* symbol table: linked list */
|
|
|
|
|
2020-12-30 18:41:29 +00:00
|
|
|
char *emalloc(unsigned n) /* check return from malloc */
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = (char*) malloc(n);
|
|
|
|
if (p == 0)
|
|
|
|
abort(); //execerror("out of memory", (char *) 0);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-12-30 12:16:19 +00:00
|
|
|
Symbol *lookup(char *s) /* find s in symbol table */
|
|
|
|
{
|
|
|
|
Symbol *sp;
|
|
|
|
|
|
|
|
for (sp = symlist; sp != (Symbol *) 0; sp = sp->next)
|
|
|
|
if (strcmp(sp->name, s) == 0)
|
|
|
|
return sp;
|
|
|
|
return 0; /* 0 ==> not found */
|
|
|
|
}
|
|
|
|
|
2020-12-30 17:34:32 +00:00
|
|
|
Symbol *install(char *n, int t, int d, char *s) /* install s in symbol table */
|
2020-12-30 12:16:19 +00:00
|
|
|
{
|
|
|
|
Symbol *sp;
|
|
|
|
|
|
|
|
sp = (Symbol *) emalloc(sizeof(Symbol));
|
2020-12-30 17:34:32 +00:00
|
|
|
sp->name = emalloc(strlen(n)+1); /* +1 for '\0' */
|
|
|
|
strcpy(sp->name, n);
|
2020-12-30 12:16:19 +00:00
|
|
|
sp->type = t;
|
2020-12-30 17:34:32 +00:00
|
|
|
if (t == NUM || t == NAME)
|
|
|
|
sp->u.val = d;
|
|
|
|
else if (t == STRING)
|
|
|
|
sp->u.str = s;
|
|
|
|
else
|
|
|
|
abort();
|
|
|
|
|
2020-12-30 12:16:19 +00:00
|
|
|
sp->next = symlist; /* put at front of list */
|
|
|
|
symlist = sp;
|
|
|
|
return sp;
|
|
|
|
}
|