]> git.sur5r.net Git - cc65/blob - src/ca65/repeat.c
Allow conditional directives within .STRUCT7:UNION and .ENUM
[cc65] / src / ca65 / repeat.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 repeat.c                                  */
4 /*                                                                           */
5 /*                   Handle the .REPEAT pseudo instruction                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     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 "xmalloc.h"
40
41 /* ca65 */
42 #include "error.h"
43 #include "expr.h"
44 #include "nexttok.h"
45 #include "toklist.h"
46 #include "repeat.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Code                                    */
52 /*****************************************************************************/
53
54
55
56 static TokList* CollectRepeatTokens (void)
57 /* Collect all tokens inside the .REPEAT body in a token list and return
58  * this list. In case of errors, NULL is returned.
59  */
60 {
61     /* Create the token list */
62     TokList* List = NewTokList ();
63
64     /* Read the token list */
65     unsigned Repeats = 0;
66     while (Repeats != 0 || Tok != TOK_ENDREP) {
67
68         /* Check for end of input */
69         if (Tok == TOK_EOF) {
70             Error ("Unexpected end of file");
71             FreeTokList (List);
72             return 0;
73         }
74
75         /* If we find a token that is equal to the repeat counter name,
76          * replace it by a REPCOUNTER token. This way we have to do strcmps
77          * only once for each identifier, and not for each expansion.
78          * Note: This will fail for nested repeats using the same repeat
79          * counter name, but
80          */
81
82
83
84         /* Collect all tokens in the list */
85         AddCurTok (List);
86
87         /* Check for and count nested .REPEATs */
88         if (Tok == TOK_REPEAT) {
89             ++Repeats;
90         } else if (Tok == TOK_ENDREP) {
91             --Repeats;
92         }
93
94         /* Get the next token */
95         NextTok ();
96     }
97
98     /* Eat the closing .ENDREP */
99     NextTok ();
100
101     /* Return the list of collected tokens */
102     return List;
103 }
104
105
106
107 static void RepeatTokenCheck (TokList* L)
108 /* Called each time a token from a repeat token list is set. Is used to check
109  * for and replace identifiers that are the repeat counter.
110  */
111 {
112     if (Tok == TOK_IDENT && L->Data != 0 && strcmp (SVal, L->Data) == 0) {
113         /* Must replace by the repeat counter */
114         Tok  = TOK_INTCON;
115         IVal = L->RepCount;
116     }
117 }
118
119
120
121 void ParseRepeat (void)
122 /* Parse and handle the .REPEAT statement */
123 {
124     char* Name;
125     TokList* List;
126
127     /* Repeat count follows */
128     long RepCount = ConstExpression ();
129     if (RepCount < 0) {
130         Error ("Range error");
131         RepCount = 0;
132     }
133
134     /* Optional there is a comma and a counter variable */
135     Name = 0;
136     if (Tok == TOK_COMMA) {
137
138         /* Skip the comma */
139         NextTok ();
140
141         /* Check for an identifier */
142         if (Tok != TOK_IDENT) {
143             ErrorSkip ("Identifier expected");
144         } else {
145             /* Remember the name and skip it */
146             Name = xstrdup (SVal);
147             NextTok ();
148         }
149     }
150
151     /* Separator */
152     ConsumeSep ();
153
154     /* Read the token list */
155     List = CollectRepeatTokens ();
156
157     /* If we had an error, bail out */
158     if (List == 0) {
159         xfree (Name);
160         return;
161     }
162
163     /* Update the token list for replay */
164     List->RepMax = (unsigned) RepCount;
165     List->Data   = Name;
166     List->Check  = RepeatTokenCheck;
167
168     /* If the list is empty, or repeat count zero, there is nothing
169      * to repeat.
170      */
171     if (List->Count == 0 || RepCount == 0) {
172         FreeTokList (List);
173         return;
174     }
175
176     /* Read input from the repeat descriptor */
177     PushTokList (List, ".REPEAT");
178 }
179
180
181