www.pudn.com > calc.rar > symbol.c


/* symbol.c */ 
 
#include  
#include  
#include  
#include "integer.h" 
#include "calc.h" 
#include "fun.h" 
#include "stack.h" 
#ifdef _WIN32 
#include "ytab.h" 
#else 
#include "y.tab.h" 
#endif 
 
static Symbol *symlist = NULL; /* symbol table */ 
 
 
Argument createArg(void *arg, int type, int defined) 
{ 
  Symbol *sym; 
  Argument Arg; 
  if (!(Arg = malloc(sizeof(struct _Argument)))) 
  { 
    printf("Not enough memory to allocate argument\n"); 
    exit(1); 
  } 
  sym = (Symbol *) arg;  
  Arg->defined = defined; 
  switch (type) { 
  case NUM:  
    Arg->type = NUM; 
    Arg->u.num = arg; 
 
    break; 
  case VARADR: 
    Arg->type = VARADR; 
    Arg->u.varAdr = &(sym->u.symval); 
 
    break; 
  case ARR:  
    Arg->type = ARR; 
    /* in this case a symbol has been passed to createArg. it's ok */ 
    Arg->u.array=sym->u.symarr; 
    break; 
  case ARRADR:  
    Arg->type = ARRADR; 
    Arg->u.arrayAdr=&(sym->u.symarr); 
    break; 
  case POLY:  
    Arg->type = POLY; 
    Arg->u.poly = arg; 
    break; 
  } 
  return Arg; 
} 
 
void freeArg(Argument Arg) 
{ 
 
  switch((Arg)->type) { 
  case NUM: 
    FREEMPI((Arg)->u.num); 
    break; 
  case POLY: 
    DELETEPI((Arg)->u.poly); 
    break; 
  } 
  free(Arg); 
} 
 
 
Symbol *lookup(char *s, int typ)  /* find s of type  typ  in symbol table */ 
{ 
	Symbol *sp; 
 
	for (sp = symlist; sp !=  NULL; sp = sp->next) 
	  if (strcmp(sp->name, s) == 0 && (sp->type==typ)) 
	    return sp; 
	return NULL;  /* NULL ==> not found */ 
} 
 
Symbol *installFunc(char *s, int t, int *argTypes) 
{ 
	Symbol *sp; 
 
	sp = (Symbol *)mmalloc(sizeof(Symbol)); 
	sp->name = (char *)mmalloc(1 + strlen(s)); 
	strcpy(sp->name, s); 
	sp->type = t; 
	sp->argTypes=argTypes; 
	sp->next = symlist;  /* put at front of list */ 
	symlist = sp; 
	return sp; 
} 
 
 
Symbol *install (char *s, int t)  /* install s in symbol table */ 
{ 
	Symbol *sp; 
 
	sp = (Symbol *)mmalloc(sizeof(Symbol)); 
	sp->name = (char *)mmalloc(1 + strlen(s)); 
	strcpy(sp->name, s); 
	sp->type = t; 
	sp->argTypes=NULL; 
	sp->next = symlist;  /* put at front of list */ 
	symlist = sp; 
	return sp; 
} 
 
void clean_symtab() 
/* deallocates all memory allocated during the CALC session. */ 
{ 
	Symbol *tmp; 
	int typ; 
 
 
	while (symlist) 
	{ 
		tmp = symlist->next; 
		typ = symlist->type; 
		if (typ == VAR && symlist->u.symval != NULL ) 
			FREEMPI(symlist->u.symval); 
		if (typ == ARRAY) 
		{ 
		  FREEMPIA(symlist->u.symarr); 
		}	 
		if (typ == POLYVAR && symlist->u.sympval != NULL) { 
		  DELETEPI(symlist->u.sympval); 
		} 
		ffree((char*)(symlist->name), 1 + strlen(symlist->name)); 
		ffree((char*)symlist, sizeof(Symbol)); 
		symlist=tmp; 
	} 
}