]> git.sur5r.net Git - cc65/blob - src/ca65/toklist.c
Small fixes for Watcom-C
[cc65] / src / ca65 / toklist.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 toklist.c                                 */
4 /*                                                                           */
5 /*                  Token list for the ca65 macroassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* ca65 */
43 #include "error.h"
44 #include "istack.h"
45 #include "scanner.h"
46 #include "toklist.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Code                                    */
52 /*****************************************************************************/
53
54
55
56 TokNode* NewTokNode (void)
57 /* Create and return a token node with the current token value */
58 {
59     TokNode* T;
60
61     /* Allocate memory */
62     unsigned Len = TokHasSVal (Tok)? strlen (SVal) : 0;
63     T = xmalloc (sizeof (TokNode) + Len);
64
65     /* Initialize the token contents */
66     T->Next     = 0;
67     T->Tok      = Tok;
68     T->WS       = WS;
69     T->IVal     = IVal;
70     memcpy (T->SVal, SVal, Len);
71     T->SVal [Len] = '\0';
72
73     /* Return the node */
74     return T;
75 }
76
77
78
79 void FreeTokNode (TokNode* T)
80 /* Free the given token node */
81 {
82     xfree (T);
83 }
84
85
86
87 void TokSet (TokNode* T)
88 /* Set the scanner token from the given token node */
89 {
90     /* Set the values */
91     Tok  = T->Tok;
92     WS   = T->WS;
93     IVal = T->IVal;
94     strcpy (SVal, T->SVal);
95 }
96
97
98
99 enum TC TokCmp (const TokNode* T)
100 /* Compare the token given as parameter against the current token */
101 {
102     if (T->Tok != Tok) {
103         /* Different token */
104         return tcDifferent;
105     }
106
107     /* If the token has string attribute, check it */
108     if (TokHasSVal (T->Tok)) {
109         if (strcmp  (T->SVal, SVal) != 0) {
110             return tcSameToken;
111         }
112     } else if (TokHasIVal (T->Tok)) {
113         if (T->IVal != IVal) {
114             return tcSameToken;
115         }
116     }
117
118     /* Tokens are identical */
119     return tcIdentical;
120 }
121
122
123
124 void InitTokList (TokList* T)
125 /* Initialize a token list structure for later use */
126 {
127     /* Initialize the fields */
128     T->Next     = 0;
129     T->Root     = 0;
130     T->Last     = 0;
131     T->RepCount = 0;
132     T->RepMax   = 1;
133     T->Count    = 0;
134     T->Check    = 0;
135     T->Data     = 0;
136 }
137
138
139
140 TokList* NewTokList (void)
141 /* Create a new, empty token list */
142 {
143     /* Allocate memory for the list structure */
144     TokList* T = xmalloc (sizeof (TokList));
145
146     /* Initialize the fields */
147     InitTokList (T);
148
149     /* Return the new list */
150     return T;
151 }
152
153
154
155 void FreeTokList (TokList* List)
156 /* Delete the token list including all token nodes */
157 {
158     /* Free the token list */
159     TokNode* T = List->Root;
160     while (T) {
161         TokNode* Tmp = T;
162         T = T->Next;
163         FreeTokNode (Tmp);
164     }
165
166     /* If we have associated data, free it */
167     if (List->Data) {
168         xfree (List->Data);
169     }
170
171     /* Free the list structure itself */
172     xfree (List);
173 }
174
175
176
177 void AddCurTok (TokList* List)
178 /* Add the current token to the token list */
179 {
180     /* Create a token node with the current token value */
181     TokNode* T = NewTokNode ();
182
183     /* Insert the node into the list */
184     if (List->Root == 0) {
185         List->Root = T;
186     } else {
187         List->Last->Next = T;
188     }
189     List->Last = T;
190
191     /* Count nodes */
192     List->Count++;
193 }
194
195
196
197 static int ReplayTokList (void* List)
198 /* Function that gets the next token from a token list and sets it. This
199  * function may be used together with the PushInput function from the istack
200  * module.
201  */
202 {
203     /* Cast the generic pointer to an actual list */
204     TokList* L = List;
205
206     /* Last may never be a NULL pointer, otherwise there's a bug in the code */
207     CHECK (L->Last != 0);
208
209     /* Set the next token from the list */
210     TokSet (L->Last);
211
212     /* If a check function is defined, call it, so it may look at the token
213      * just set and changed it as apropriate.
214      */
215     if (L->Check) {
216         L->Check (L);
217     }
218
219     /* Set the pointer to the next token */
220     L->Last = L->Last->Next;
221
222     /* If this was the last token, decrement the repeat counter. If it goes
223      * zero, delete the list and remove the function from the stack.
224      */
225     if (L->Last == 0) {
226         if (++L->RepCount >= L->RepMax) {
227             /* Done with this list */
228             FreeTokList (L);
229             PopInput ();
230         } else {
231             /* Replay one more time */
232             L->Last = L->Root;
233         }
234     }
235
236     /* We have a token */
237     return 1;
238 }
239
240
241
242 void PushTokList (TokList* List, const char* Desc)
243 /* Push a token list to be used as input for InputFromStack. This includes
244  * several initializations needed in the token list structure, so don't use
245  * PushInput directly.
246  */
247 {
248     /* If the list is empty, just delete it and bail out */
249     if (List->Count == 0) {
250         FreeTokList (List);
251         return;
252     }
253
254     /* Reset the last pointer to the first element */
255     List->Last = List->Root;
256
257     /* Insert the list specifying our input function */
258     PushInput (ReplayTokList, List, Desc);
259 }
260
261
262