Home | History | Annotate | Download | only in h
      1 /*
      2  * SList.C
      3  *
      4  * SOFTWARE RIGHTS
      5  *
      6  * We reserve no LEGAL rights to SORCERER -- SORCERER is in the public
      7  * domain.  An individual or company may do whatever they wish with
      8  * source code distributed with SORCERER or the code generated by
      9  * SORCERER, including the incorporation of SORCERER, or its output, into
     10  * commerical software.
     11  *
     12  * We encourage users to develop software with SORCERER.  However, we do
     13  * ask that credit is given to us for developing SORCERER.  By "credit",
     14  * we mean that if you incorporate our source code into one of your
     15  * programs (commercial product, research project, or otherwise) that you
     16  * acknowledge this fact somewhere in the documentation, research report,
     17  * etc...  If you like SORCERER and have developed a nice tool with the
     18  * output, please mention that you developed it using SORCERER.  In
     19  * addition, we ask that this header remain intact in our source code.
     20  * As long as these guidelines are kept, we expect to continue enhancing
     21  * this system and expect to make other tools available as they are
     22  * completed.
     23  *
     24  * PCCTS 1.33
     25  * Terence Parr
     26  * Parr Research Corporation
     27  * with Purdue University and AHPCRC, University of Minnesota
     28  * 1992-2000
     29  */
     30 
     31 #define ANTLR_SUPPORT_CODE
     32 
     33 #include "SList.h"
     34 #include "pccts_stdarg.h" // MR23
     35 
     36 /* Iterate over a list of elements; returns ptr to a new element
     37  * in list upon every call and NULL when no more are left.
     38  * Very useful like this:
     39  *
     40  *		cursor = mylist;
     41  *		while ( (p=mylist->iterate(&cursor)) ) {
     42  *			// place with element p
     43  *		}
     44  *
     45  * The cursor must be initialized to point to the list to iterate over.
     46  */
     47 void *SList::
     48 iterate(SListNode **cursor)
     49 {
     50 	void *e;
     51 
     52 	if ( cursor == NULL || *cursor==NULL ) return NULL;
     53 	if ( head == *cursor ) { *cursor = (*cursor)->next(); }
     54 	e = (*cursor)->elem();
     55 	(*cursor) = (*cursor)->next();
     56 	return e;
     57 }
     58 
     59 /* add an element to end of list. */
     60 void SList::
     61 add(void *e)
     62 {
     63 	SListNode *p, *tail=NULL;
     64 	require(e!=NULL, "slist_add: attempting to add NULL list element");
     65 
     66 	p = new SListNode;
     67 	require(p!=NULL, "add: cannot alloc new list node");
     68 	p->setElem(e);
     69 	if ( head == NULL )
     70 	{
     71 		head = tail = p;
     72 	}
     73 	else								/* find end of list */
     74 	{
     75 		tail->setNext(p);
     76 		tail = p;
     77 	}
     78 }
     79 
     80 void SList::
     81 lfree()
     82 {
     83 	SListNode *p,*q;
     84 
     85 	if ( head==NULL ) return;	/* empty list */
     86 	for (p = head; p!=NULL; p=q)
     87 	{
     88 		q = p->next();
     89 		free(p);
     90 	}
     91 }
     92 
     93 PCCTS_AST *SList::
     94 to_ast(SList list)
     95 {
     96 	PCCTS_AST *t=NULL, *last=NULL;
     97 	SListNode *p;
     98 
     99 	for (p = head; p!=NULL; p=p->next())
    100 	{
    101 		PCCTS_AST *u = (PCCTS_AST *)p->elem();
    102 		if ( last==NULL ) last = t = u;
    103 		else { last->setRight(u); last = u; }
    104 	}
    105 	return t;
    106 }
    107 
    108 // MR23
    109 int SList::printMessage(FILE* pFile, const char* pFormat, ...)
    110 {
    111 	va_list marker;
    112 	va_start( marker, pFormat );
    113   	int iRet = vfprintf(pFile, pFormat, marker);
    114 	va_end( marker );
    115 	return iRet;
    116 }
    117