]> git.sur5r.net Git - cc65/blob - src/ca65/toklist.c
Fixed minor and rather obscure problem: When including files, the line after
[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 "nexttok.h"
46 #include "scanner.h"
47 #include "toklist.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Code                                    */
53 /*****************************************************************************/
54
55
56
57 TokNode* NewTokNode (void)
58 /* Create and return a token node with the current token value */
59 {
60     TokNode* T;
61
62     /* Allocate memory */
63     unsigned Len = TokHasSVal (Tok)? strlen (SVal) : 0;
64     T = xmalloc (sizeof (TokNode) + Len);
65
66     /* Initialize the token contents */
67     T->Next     = 0;
68     T->Tok      = Tok;
69     T->WS       = WS;
70     T->IVal     = IVal;
71     memcpy (T->SVal, SVal, Len);
72     T->SVal [Len] = '\0';
73
74     /* Return the node */
75     return T;
76 }
77
78
79
80 void FreeTokNode (TokNode* T)
81 /* Free the given token node */
82 {
83     xfree (T);
84 }
85
86
87
88 void TokSet (TokNode* T)
89 /* Set the scanner token from the given token node */
90 {
91     /* Set the values */
92     Tok  = T->Tok;
93     WS   = T->WS;
94     IVal = T->IVal;
95     strcpy (SVal, T->SVal);
96 }
97
98
99
100 enum TC TokCmp (const TokNode* T)
101 /* Compare the token given as parameter against the current token */
102 {
103     if (T->Tok != Tok) {
104         /* Different token */
105         return tcDifferent;
106     }
107
108     /* If the token has string attribute, check it */
109     if (TokHasSVal (T->Tok)) {
110         if (strcmp  (T->SVal, SVal) != 0) {
111             return tcSameToken;
112         }
113     } else if (TokHasIVal (T->Tok)) {
114         if (T->IVal != IVal) {
115             return tcSameToken;
116         }
117     }
118
119     /* Tokens are identical */
120     return tcIdentical;
121 }
122
123
124
125 void InitTokList (TokList* T)
126 /* Initialize a token list structure for later use */
127 {
128     /* Initialize the fields */
129     T->Next     = 0;
130     T->Root     = 0;
131     T->Last     = 0;
132     T->RepCount = 0;
133     T->RepMax   = 1;
134     T->Count    = 0;
135     T->Check    = 0;
136     T->Data     = 0;
137 }
138
139
140
141 TokList* NewTokList (void)
142 /* Create a new, empty token list */
143 {
144     /* Allocate memory for the list structure */
145     TokList* T = xmalloc (sizeof (TokList));
146
147     /* Initialize the fields */
148     InitTokList (T);
149
150     /* Return the new list */
151     return T;
152 }
153
154
155
156 void FreeTokList (TokList* List)
157 /* Delete the token list including all token nodes */
158 {
159     /* Free the token list */
160     TokNode* T = List->Root;
161     while (T) {
162         TokNode* Tmp = T;
163         T = T->Next;
164         FreeTokNode (Tmp);
165     }
166
167     /* If we have associated data, free it */
168     if (List->Data) {
169         xfree (List->Data);
170     }
171
172     /* Free the list structure itself */
173     xfree (List);
174 }
175
176
177
178 enum Token GetTokListTerm (enum Token Term)
179 /* Determine if the following token list is enclosed in curly braces. This is
180  * the case if the next token is the opening brace. If so, skip it and return
181  * a closing brace, otherwise return Term.
182  */
183 {
184     if (Tok == TOK_LCURLY) {
185         NextTok ();
186         return TOK_RCURLY;
187     } else {
188         return Term;
189     }
190 }
191
192
193
194 void AddCurTok (TokList* List)
195 /* Add the current token to the token list */
196 {
197     /* Create a token node with the current token value */
198     TokNode* T = NewTokNode ();
199
200     /* Insert the node into the list */
201     if (List->Root == 0) {
202         List->Root = T;
203     } else {
204         List->Last->Next = T;
205     }
206     List->Last = T;
207
208     /* Count nodes */
209     List->Count++;
210 }
211
212
213
214 static int ReplayTokList (void* List)
215 /* Function that gets the next token from a token list and sets it. This
216  * function may be used together with the PushInput function from the istack
217  * module.
218  */
219 {
220     /* Cast the generic pointer to an actual list */
221     TokList* L = List;
222
223     /* Last may never be a NULL pointer, otherwise there's a bug in the code */
224     CHECK (L->Last != 0);
225
226     /* Set the next token from the list */
227     TokSet (L->Last);
228
229     /* If a check function is defined, call it, so it may look at the token
230      * just set and changed it as apropriate.
231      */
232     if (L->Check) {
233         L->Check (L);
234     }
235
236     /* Set the pointer to the next token */
237     L->Last = L->Last->Next;
238
239     /* If this was the last token, decrement the repeat counter. If it goes
240      * zero, delete the list and remove the function from the stack.
241      */
242     if (L->Last == 0) {
243         if (++L->RepCount >= L->RepMax) {
244             /* Done with this list */
245             FreeTokList (L);
246             PopInput ();
247         } else {
248             /* Replay one more time */
249             L->Last = L->Root;
250         }
251     }
252
253     /* We have a token */
254     return 1;
255 }
256
257
258
259 void PushTokList (TokList* List, const char* Desc)
260 /* Push a token list to be used as input for InputFromStack. This includes
261  * several initializations needed in the token list structure, so don't use
262  * PushInput directly.
263  */
264 {
265     /* If the list is empty, just delete it and bail out */
266     if (List->Count == 0) {
267         FreeTokList (List);
268         return;
269     }
270
271     /* Reset the last pointer to the first element */
272     List->Last = List->Root;
273
274     /* Insert the list specifying our input function */
275     PushInput (ReplayTokList, List, Desc);
276 }
277
278
279