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