From 721ff28e6654c3841990312ed33a70be8d5df22b Mon Sep 17 00:00:00 2001 From: ggn Date: Tue, 31 Oct 2017 11:33:02 +0200 Subject: [PATCH] Expand \~ in .REPTs to unique label names as in macros. (issue #75) --- macro.c | 6 ++++-- macro.h | 2 ++ token.c | 31 ++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/macro.c b/macro.c index 3d2ea09..8da9bcb 100644 --- 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 b4a763a..753dabd 100644 --- 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 807f08f..d642660 100644 --- 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; -- 2.37.2