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 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 static TokList* CollectRepeatTokens (void)
55 /* Collect all tokens inside the .REPEAT body in a token list and return
56 * this list. In case of errors, NULL is returned.
59 /* Create the token list */
60 TokList* List = NewTokList ();
62 /* Read the token list */
64 while (Repeats != 0 || Tok != TOK_ENDREP) {
66 /* Check for end of input */
68 Error (ERR_UNEXPECTED_EOF);
73 /* If we find a token that is equal to the repeat counter name,
74 * replace it by a REPCOUNTER token. This way we have to do strcmps
75 * only once for each identifier, and not for each expansion.
76 * Note: This will fail for nested repeats using the same repeat
82 /* Collect all tokens in the list */
85 /* Check for and count nested .REPEATs */
86 if (Tok == TOK_REPEAT) {
88 } else if (Tok == TOK_ENDREP) {
92 /* Get the next token */
96 /* Eat the closing .ENDREP */
99 /* Return the list of collected tokens */
105 static void RepeatTokenCheck (TokList* L)
106 /* Called each time a token from a repeat token list is set. Is used to check
107 * for and replace identifiers that are the repeat counter.
110 if (Tok == TOK_IDENT && L->Data != 0 && strcmp (SVal, L->Data) == 0) {
111 /* Must replace by the repeat counter */
119 void ParseRepeat (void)
120 /* Parse and handle the .REPEAT statement */
125 /* Repeat count follows */
126 long RepCount = ConstExpression ();
132 /* Optional there is a comma and a counter variable */
134 if (Tok == TOK_COMMA) {
139 /* Check for an identifier */
140 if (Tok != TOK_IDENT) {
141 ErrorSkip (ERR_IDENT_EXPECTED);
143 /* Remember the name and skip it */
144 Name = xstrdup (SVal);
152 /* Read the token list */
153 List = CollectRepeatTokens ();
155 /* If we had an error, bail out */
161 /* Update the token list for replay */
162 List->RepMax = (unsigned) RepCount;
164 List->Check = RepeatTokenCheck;
166 /* If the list is empty, or repeat count zero, there is nothing
169 if (List->Count == 0 || RepCount == 0) {
174 /* Read input from the repeat descriptor */
175 PushTokList (List, ".REPEAT");