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