]> Shamusworld >> Repos - rmac/blobdiff - token.c
Expand \~ in .REPTs to unique label names as in macros. (issue #75)
[rmac] / token.c
diff --git a/token.c b/token.c
index 807f08fc34b4f2789b90bc557e5758dc11711018..d6426600b8f5de1c94acf12256a80b0094eaac81 100644 (file)
--- a/token.c
+++ b/token.c
@@ -699,7 +699,7 @@ char * GetNextRepeatLine(void)
                        DEBUG { printf("end-repeat-block\n"); }
                        return NULL;
                }
-
+               reptuniq++;
 //             strp = irept->ir_nextln;
        }
        // Mark the current macro line in the irept object
@@ -708,8 +708,33 @@ char * GetNextRepeatLine(void)
        // error reporting anyway)
        irept->lineno = irept->ir_nextln->lineno;
 
-//     strcpy(irbuf, (char *)(irept->ir_nextln + 1));
-       strcpy(irbuf, irept->ir_nextln->line);
+       // Copy the rept lines verbatim, unless we're in nest level 0.
+       // Then, expand any \~ labels to unique numbers (Rn)
+       if (rptlevel)
+       {
+               strcpy(irbuf, irept->ir_nextln->line);
+       }
+       else
+       {
+               uint32_t linelen = strlen(irept->ir_nextln->line);
+               uint8_t *p_line = irept->ir_nextln->line;
+               char *irbufwrite = irbuf;
+               for (int i = 0; i <= linelen; i++)
+               {
+                       uint8_t c;
+                       c = *p_line++;
+                       if (c == '\\' && *p_line == '~')
+                       {
+                               p_line++;
+                               irbufwrite += sprintf(irbufwrite, "R%u", reptuniq);
+                       }
+                       else
+                       {
+                               *irbufwrite++ = c;
+                       }
+               }
+       }
+
        DEBUG { printf("repeat line='%s'\n", irbuf); }
 //     irept->ir_nextln = (LONG *)*strp;
        irept->ir_nextln = irept->ir_nextln->next;