]> git.sur5r.net Git - cc65/blob - src/ca65/toklist.c
Finished implemenation of commands to delete macros. Added the new commands to
[cc65] / src / ca65 / toklist.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 toklist.c                                 */
4 /*                                                                           */
5 /*                  Token list for the ca65 macroassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, 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 "lineinfo.h"
46 #include "nexttok.h"
47 #include "scanner.h"
48 #include "toklist.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Code                                    */
54 /*****************************************************************************/
55
56
57
58 TokNode* NewTokNode (void)
59 /* Create and return a token node with the current token value */
60 {
61
62     /* Allocate memory */
63     TokNode* N = xmalloc (sizeof (TokNode));
64
65     /* Initialize the token contents */
66     N->Next     = 0;
67     SB_Init (&N->T.SVal);
68     CopyToken (&N->T, &CurTok);
69
70     /* Return the node */
71     return N;
72 }
73
74
75
76 void FreeTokNode (TokNode* N)
77 /* Free the given token node */
78 {
79     SB_Done (&N->T.SVal);
80     xfree (N);
81 }
82
83
84
85 void TokSet (TokNode* N, unsigned LineInfoSlot)
86 /* Set the scanner token from the given token node. The given line info slot
87  * is used to store the position of the token fed into the scanner.
88  */
89 {
90     /* Set the values */
91     CopyToken (&CurTok, &N->T);
92     SB_Terminate (&CurTok.SVal);
93
94     /* Set the position */
95     GenLineInfo (LineInfoSlot, &CurTok.Pos);
96 }
97
98
99
100 enum TC TokCmp (const TokNode* N)
101 /* Compare the token given as parameter against the current token */
102 {
103     if (N->T.Tok != CurTok.Tok) {
104         /* Different token */
105         return tcDifferent;
106     }
107
108     /* If the token has string attribute, check it */
109     if (TokHasSVal (N->T.Tok)) {
110         if (SB_Compare (&CurTok.SVal, &N->T.SVal) != 0) {
111             return tcSameToken;
112         }
113     } else if (TokHasIVal (N->T.Tok)) {
114         if (N->T.IVal != CurTok.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_t GetTokListTerm (enum token_t 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 (CurTok.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, LI_SLOT_ASM);
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