1 /*****************************************************************************/
5 /* Handle the .REPEAT pseudo instruction */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
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.
61 /* Create the token list */
62 TokList* List = NewTokList ();
64 /* Read the token list */
66 while (Repeats != 0 || Tok != TOK_ENDREP) {
68 /* Check for end of input */
70 Error (ERR_UNEXPECTED_EOF);
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
84 /* Collect all tokens in the list */
87 /* Check for and count nested .REPEATs */
88 if (Tok == TOK_REPEAT) {
90 } else if (Tok == TOK_ENDREP) {
94 /* Get the next token */
98 /* Eat the closing .ENDREP */
101 /* Return the list of collected tokens */
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.
112 if (Tok == TOK_IDENT && L->Data != 0 && strcmp (SVal, L->Data) == 0) {
113 /* Must replace by the repeat counter */
121 void ParseRepeat (void)
122 /* Parse and handle the .REPEAT statement */
127 /* Repeat count follows */
128 long RepCount = ConstExpression ();
134 /* Optional there is a comma and a counter variable */
136 if (Tok == TOK_COMMA) {
141 /* Check for an identifier */
142 if (Tok != TOK_IDENT) {
143 ErrorSkip (ERR_IDENT_EXPECTED);
145 /* Remember the name and skip it */
146 Name = xstrdup (SVal);
154 /* Read the token list */
155 List = CollectRepeatTokens ();
157 /* If we had an error, bail out */
163 /* Update the token list for replay */
164 List->RepMax = (unsigned) RepCount;
166 List->Check = RepeatTokenCheck;
168 /* If the list is empty, or repeat count zero, there is nothing
171 if (List->Count == 0 || RepCount == 0) {
176 /* Read input from the repeat descriptor */
177 PushTokList (List, ".REPEAT");