home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.parl.clemson.edu
/
2015-02-07.ftp.parl.clemson.edu.tar
/
ftp.parl.clemson.edu
/
pub
/
coven
/
coven-utils-1.1.tgz
/
coven-utils-1.1.tar
/
utils
/
coven-language
/
symbol.c
< prev
next >
Wrap
C/C++ Source or Header
|
2003-01-28
|
7KB
|
299 lines
/*
* (C) 2001 Clemson University
*
* See COPYING in top-level directory.
*/
#ifndef lint
static char symbol_c_rcsid[] = "$Header: /projects/cvsroot/coven-language/symbol.c,v 1.3 2003/01/28 14:55:09 ndebard Exp $";
#endif
/* $Log: symbol.c,v $
/* Revision 1.3 2003/01/28 14:55:09 ndebard
/* New GPL.
/*
/* Revision 1.2 2003/01/23 13:06:54 ndebard
/* GPLed this dir.
/*
/* Revision 1.1.1.1 2003/01/23 11:47:48 ndebard
/* Imported source - from Coven project, reorganizing
/*
/* Revision 1.4 2002/11/05 23:46:41 ndebard
/* Working on the language. It should handle everything up to dropping the
/* output for module calls right now.
/*
/* Revision 1.3 2002/07/31 14:44:48 ndebard
/* Lots of changes while in Scotland. This now parses the MODULES and
/* VARIABLES sections correctly (it is believed). The backend of the Coven
/* driver needs to be redesigned now to handle some new data types before
/* the PROGRAM section can be completed.
/*
/* Revision 1.2 2002/07/03 20:45:53 walt
/* initial added symbol table stuff
/*
/* Revision 1.1 2002/07/03 18:55:12 walt
/* First set of files for Coven compiler - WBL
/*
*
* Revision 1.1 90/02/02 10:19:51 walt
* Initial revision
*
*/
/*
* symbol --- symbol table and mapping routines
*/
#include <stdio.h>
#include "symbol.h"
#include "stringtbl.h"
#include "y.tab.h"
extern int line;
/*
* MAXHASH --- determines the hash table width.
* symtab --- the symbol table structure
*/
#define MAXHASH 311
static sym_ent_p symtab[MAXHASH];
/*
* external routine declarations
*/
int strcmp();
/*
* hash --- scramble a name (hopefully) uniformly to fit in a table
*/
static unsigned int hash(char *name)
{
register unsigned int h = 0;
while (*name)
{
h <<= 4;
h ^= *name++;
}
return(h % MAXHASH);
}
/*
* symenter --- enter a name into the symbol table
*/
sym_ent_p symenter(char *name, sym_ent_p context, int type)
{
register sym_ent_p p;
unsigned int h;
/* first check for conflicts */
p = symlook(name, context, type);
if(p != NULL) {
yyerror("Symbol name conflict when adding name to symbol table.");
exit(1);
}
if(type == TYPE_TYPE ||
type == TYPE_VARIABLE ||
type == TYPE_CONST ||
(context == NULL && type == TYPE_MODULE)
)
{
/* create an entry and insert it at the front of the table */
h = hash(name);
p = (sym_ent_p)emalloc(sizeof(sym_ent));
p->name = name;
p->next = symtab[h];
p->type = type;
switch(type) {
case TYPE_MODULE:
p->value.mod = (module_ent_p)emalloc(sizeof(module_ent));
p->value.mod->args = NULL;
break;
case TYPE_VARIABLE:
p->value.var = (variable_ent_p)emalloc(sizeof(variable_ent));
break;
case TYPE_CONST:
p->value.con = (const_ent_p)emalloc(sizeof(const_ent));
break;
case TYPE_TYPE:
/* we don't do anything here, yet */
break;
}
symtab[h] = p;
}
else {
p = (sym_ent_p)emalloc(sizeof(sym_ent));
p->name = name;
p->next = NULL;
p->type = type;
}
return(p);
}
/*
* symlook --- lookup a symbol in the symbol table
*
* symlook scans the symbol table and returns a pointer to
* the symbol table entry
*/
sym_ent_p symlook(char *name, sym_ent_p context, int type)
{
register sym_ent_p p;
unsigned int h;
if(context == NULL) {
h = hash(name);
for (p = symtab[h]; p != 0; p = p->next)
if(strcmp(p->name, name) == 0)
break;
}
else {
if(type == TYPE_MODULE)
p = context->value.mod->args;
else
p = context;
while(p != NULL) {
if(strcmp(p->name, name) == 0)
break;
p = p->next;
}
}
return(p);
}
sym_ent_p lookup_type(char *name)
{
sym_ent_p p;
p = symlook(name, NULL, TYPE_TYPE);
if(p != NULL && p->type == TYPE_TYPE) return p;
return NULL;
}
void init_symbol_table()
{
bzero(symtab, MAXHASH * sizeof(sym_ent_p));
}
void sym_print (sym_ent_p sym, char *prepend)
{
printf(prepend);
printf("SYMBOL [%s]\n", sym->name);
switch(sym->type) {
case TYPE_MODULE:
printf(prepend);
printf("\ttype: MODULE\n");
printf(prepend);
printf("\tmodule path: %s\n", sym->value.mod->path);
{
sym_ent_p cur = sym->value.mod->args;
while(cur != NULL) {
sym_print(cur, "\t");
cur = cur->next;
}
}
break;
case TYPE_VARIABLE:
printf(prepend);
printf("\ttype: VARIABLE\n");
switch(sym->value.var->vclass) {
case BUFFER:
printf(prepend);
printf("\tclass: BUFFER\n");
break;
case PARAMETER:
printf(prepend);
printf("\tclass: PARAMETER\n");
break;
case -1:
printf(prepend);
printf("\tclass: NONE DEFINED\n");
break;
default:
printf(prepend);
printf("\tclass: * UNDEFINED * ERROR!\n");
break;
}
printf(prepend);
printf("\ttype: %s\n", sym->value.var->type->name);
break;
case TYPE_CONST:
printf(prepend);
printf("\ttype: CONST\n");
printf(prepend);
printf("\ttype: %s\n", sym->value.con->type->name);
printf(prepend);
printf("\tvalue (as a string): %s\n", sym->value.con->cvalue);
break;
case TYPE_TYPE:
printf(prepend);
printf("\ttype: TYPE\n");
break;
case TYPE_ARGUMENT:
printf(prepend);
printf("\ttype: ARGUMENT\n");
switch(sym->value.arg->direction) {
case INPUT:
printf(prepend);
printf("\tdirection: INPUT\n");
break;
case OUTPUT:
printf(prepend);
printf("\tdirection: OUTPUT\n");
break;
case INOUT:
printf(prepend);
printf("\tdirection: INOUT\n");
break;
case STATIC:
printf(prepend);
printf("\tdirection: STATIC\n");
break;
default:
printf(prepend);
printf("\tdirection: * UNDEFINED * ERROR!\n");
break;
}
switch(sym->value.arg->mclass) {
case BUFFER:
printf(prepend);
printf("\tclass: BUFFER\n");
break;
case PARAMETER:
printf(prepend);
printf("\tclass: PARAMETER\n");
break;
default:
printf(prepend);
printf("\tclass: * UNDEFINED * ERROR!\n");
break;
}
printf(prepend);
printf("\ttype: %s\n", sym->value.arg->type->name);
break;
default:
printf(prepend);
printf("\ttype: * UNDEFINED * ERROR!\n");
break;
}
}
void symtab_print(void)
{
int i;
for(i=0; i<MAXHASH; i++) {
sym_ent_p cur = symtab[i];
if(cur != NULL)
printf("*** [ HASH VALUE ] *** %d\n", i);
while(cur != NULL) {
sym_print(cur, "");
cur = cur->next;
}
}
}