]> git.sur5r.net Git - cc65/blob - src/ca65/toklist.c
Fix problematic code. Use more stuff from the shared modules.
[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 #include "../common/xmalloc.h"
39
40 #include "scanner.h"
41 #include "toklist.h"
42
43
44
45 /*****************************************************************************/
46 /*                                   Code                                    */
47 /*****************************************************************************/
48
49
50
51 TokNode* NewTokNode (void)
52 /* Create and return a token node with the current token value */
53 {
54     TokNode* T;
55
56     /* Allocate memory */
57     unsigned Len = TokHasSVal (Tok)? strlen (SVal) : 0;
58     T = xmalloc (sizeof (TokNode) + Len);
59
60     /* Initialize the token contents */
61     T->Next     = 0;
62     T->Tok      = Tok;
63     T->WS       = WS;
64     T->IVal     = IVal;
65     memcpy (T->SVal, SVal, Len);
66     T->SVal [Len] = '\0';
67
68     /* Return the node */
69     return T;
70 }
71
72
73
74 void FreeTokNode (TokNode* T)
75 /* Free the given token node */
76 {
77     xfree (T);
78 }
79
80
81
82 void TokSet (TokNode* T)
83 /* Set the scanner token from the given token node */
84 {
85     /* Set the values */
86     Tok  = T->Tok;
87     WS   = T->WS;
88     IVal = T->IVal;
89     strcpy (SVal, T->SVal);
90 }
91
92
93
94 enum TC TokCmp (const TokNode* T)
95 /* Compare the token given as parameter against the current token */
96 {
97     if (T->Tok != Tok) {
98         /* Different token */
99         return tcDifferent;
100     }
101
102     /* If the token has string attribute, check it */
103     if (TokHasSVal (T->Tok)) {
104         if (strcmp  (T->SVal, SVal) != 0) {
105             return tcSameToken;
106         }
107     } else if (TokHasIVal (T->Tok)) {
108         if (T->IVal != IVal) {
109             return tcSameToken;
110         }
111     }
112
113     /* Tokens are identical */
114     return tcIdentical;
115 }
116
117
118
119 void InitTokList (TokList* T)
120 /* Initialize a token list structure for later use */
121 {
122     /* Initialize the fields */
123     T->Next     = 0;
124     T->Root     = 0;
125     T->Last     = 0;
126     T->Repeat   = 1;
127     T->Count    = 0;
128 }
129
130
131
132 TokList* NewTokList (void)
133 /* Create a new, empty token list */
134 {
135     /* Allocate memory for the list structure */
136     TokList* T = xmalloc (sizeof (TokList));
137
138     /* Initialize the fields */
139     InitTokList (T);
140
141     /* Return the new list */
142     return T;
143 }
144
145
146
147 void FreeTokList (TokList* List)
148 /* Delete the token list including all token nodes */
149 {
150     /* Free the token list */
151     TokNode* T = List->Root;
152     while (T) {
153         TokNode* Tmp = T;
154         T = T->Next;
155         FreeTokNode (Tmp);
156     }
157
158     /* Free the list structure itself */
159     xfree (List);
160 }
161
162
163
164 void AddCurTok (TokList* List)
165 /* Add the current token to the token list */
166 {
167     /* Create a token node with the current token value */
168     TokNode* T = NewTokNode ();
169
170     /* Insert the node into the list */
171     if (List->Root == 0) {
172         List->Root = T;
173     } else {
174         List->Last->Next = T;
175     }
176     List->Last = T;
177
178     /* Count nodes */
179     List->Count++;
180 }
181
182
183