]> Shamusworld >> Repos - rmac/commitdiff
Expand \~ in .REPTs to unique label names as in macros. (issue #75)
authorggn <ggn.dbug@gmail.com>
Tue, 31 Oct 2017 09:33:02 +0000 (11:33 +0200)
committerShamus Hammons <jlhamm@acm.org>
Fri, 20 Aug 2021 14:59:09 +0000 (09:59 -0500)
macro.c
macro.h
token.c

diff --git a/macro.c b/macro.c
index 3d2ea0945648230473e731e1574ed26e2c87f4f5..8da9bcb540223b19b32f16c45ba5ff968d50f5e5 100644 (file)
--- a/macro.c
+++ b/macro.c
@@ -22,11 +22,12 @@ int macnum;                                 // Unique number for macro definition
 
 static LONG macuniq;           // Unique-per-macro number
 static SYM * curmac;           // Macro currently being defined
-static uint32_t argno;                 // Formal argument count
+static uint32_t argno;         // Formal argument count
+LONG reptuniq;                         // Unique-per-rept number
 
 static LLIST * firstrpt;       // First .rept line
 static LLIST * nextrpt;                // Last .rept line
-static int rptlevel;           // .rept nesting level
+int rptlevel;                          // .rept nesting level
 
 // Function prototypes
 static int KWMatch(char *, char *);
@@ -40,6 +41,7 @@ void InitMacro(void)
 {
        macuniq = 0;
        macnum = 1;
+       reptuniq = 0;
 }
 
 
diff --git a/macro.h b/macro.h
index b4a763a9c9f687f6f535182d011a3fd0c56546e4..753dabda2c16a1b8d447b6ddd36349e13eb51f04 100644 (file)
--- a/macro.h
+++ b/macro.h
@@ -14,6 +14,8 @@
 // Exported variables
 extern LONG curuniq;
 extern TOKEN * argPtrs[];
+extern LONG reptuniq;
+extern int rptlevel;
 
 // Exported functions
 void InitMacro(void);
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;