]> pd.if.org Git - pccts/blob - h/SList.cpp
auto commit for import
[pccts] / h / SList.cpp
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-1995
29  */
30
31 #define ANTLR_SUPPORT_CODE
32
33 #include "SList.h"
34
35 /* Iterate over a list of elements; returns ptr to a new element
36  * in list upon every call and NULL when no more are left.
37  * Very useful like this:
38  *
39  *              cursor = mylist;
40  *              while ( (p=mylist->iterate(&cursor)) ) {
41  *                      // place with element p
42  *              }
43  *
44  * The cursor must be initialized to point to the list to iterate over.
45  */
46 void *SList::
47 iterate(SListNode **cursor)
48 {
49         void *e;
50
51         if ( cursor == NULL || *cursor==NULL ) return NULL;
52         if ( head == *cursor ) { *cursor = (*cursor)->next(); }
53         e = (*cursor)->elem();
54         (*cursor) = (*cursor)->next();
55         return e;
56 }
57
58 /* add an element to end of list. */
59 void SList::
60 add(void *e)
61 {
62         SListNode *p, *tail;
63         require(e!=NULL, "slist_add: attempting to add NULL list element");
64
65         p = new SListNode;
66         require(p!=NULL, "add: cannot alloc new list node");
67         p->setElem(e);
68         if ( head == NULL )
69         {
70                 head = tail = p;
71         }
72         else                                                            /* find end of list */
73         {
74                 tail->setNext(p);
75                 tail = p;
76         }
77 }
78
79 void SList::
80 lfree()
81 {
82         SListNode *p,*q;
83
84         if ( head==NULL ) return;       /* empty list */
85         for (p = head; p!=NULL; p=q)
86         {
87                 q = p->next();
88                 free(p);
89         }
90 }
91
92 PCCTS_AST *SList::
93 to_ast(SList list)
94 {
95         PCCTS_AST *t=NULL, *last=NULL;
96         SListNode *p;
97
98         for (p = head; p!=NULL; p=p->next())
99         {
100                 PCCTS_AST *u = (PCCTS_AST *)p->elem();
101                 if ( last==NULL ) last = t = u;
102                 else { last->setRight(u); last = u; }
103         }
104         return t;
105 }