Only restore parent context's \@ value if it had one defined (#1366)

This way, if a child context initializes `\@`, the parent won't
reset it. And if the child context did not initialize `\@`,
then resetting it would be redundant.
This commit is contained in:
Sylvie
2024-03-21 19:53:49 -04:00
committed by GitHub
parent 0f772932a5
commit f792580816
7 changed files with 108 additions and 5 deletions

View File

@@ -14,8 +14,6 @@
#include "linkdefs.hpp" #include "linkdefs.hpp"
#include "asm/lexer.hpp"
struct FileStackNode { struct FileStackNode {
FileStackNodeType type; FileStackNodeType type;
std::variant< std::variant<

View File

@@ -9,6 +9,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <new> #include <new>
#include <stack> #include <stack>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string_view> #include <string_view>
@@ -207,20 +208,23 @@ bool yywrap() {
return true; return true;
} }
Context oldContext = contextStack.top(); Context oldContext = std::move(contextStack.top());
contextStack.pop(); contextStack.pop();
lexer_CleanupState(oldContext.lexerState); lexer_CleanupState(oldContext.lexerState);
// Restore args if a macro (not REPT) saved them // Restore args if a macro (not REPT) saved them
if (oldContext.fileInfo->type == NODE_MACRO) if (oldContext.fileInfo->type == NODE_MACRO)
macro_UseNewArgs(contextStack.top().macroArgs); macro_UseNewArgs(contextStack.top().macroArgs);
// Restore the `\@` value if the old context was supposed to define one
// (i.e. it either is a macro or REPT, or it initialized `\@` for its parent)
if (oldContext.fileInfo->type == NODE_REPT || oldContext.fileInfo->type == NODE_MACRO
|| contextStack.top().uniqueID > 0)
macro_SetUniqueID(contextStack.top().uniqueID);
// Free the file stack node // Free the file stack node
if (!oldContext.fileInfo->referenced) if (!oldContext.fileInfo->referenced)
delete oldContext.fileInfo; delete oldContext.fileInfo;
lexer_SetState(&contextStack.top().lexerState); lexer_SetState(&contextStack.top().lexerState);
macro_SetUniqueID(contextStack.top().uniqueID);
return false; return false;
} }

View File

@@ -0,0 +1,27 @@
REPT 2
println "Within REPT"
INCLUDE "unique-id-include.inc"
println "Outside INCLUDE: \@"
ENDR
MACRO m1
println "Within MACRO"
INCLUDE "unique-id-include.inc"
println "Outside INCLUDE: \@"
ENDM
m1
println
REPT 2
println "Within REPT: \@"
INCLUDE "unique-id-include.inc"
println "Outside INCLUDE: \@"
ENDR
MACRO m2
println "Within MACRO: \@"
INCLUDE "unique-id-include.inc"
println "Outside INCLUDE: \@"
ENDM
m2

View File

@@ -0,0 +1 @@
println "Within INCLUDE: \@"

View File

@@ -0,0 +1,19 @@
Within REPT
Within INCLUDE: _u1
Outside INCLUDE: _u1
Within REPT
Within INCLUDE: _u2
Outside INCLUDE: _u2
Within MACRO
Within INCLUDE: _u3
Outside INCLUDE: _u3
Within REPT: _u4
Within INCLUDE: _u4
Outside INCLUDE: _u4
Within REPT: _u5
Within INCLUDE: _u5
Outside INCLUDE: _u5
Within MACRO: _u6
Within INCLUDE: _u6
Outside INCLUDE: _u6

View File

@@ -0,0 +1,33 @@
MACRO m1
PRINTLN "Begin MACRO"
DEF nested EQUS """MACRO mm
PRINTLN "Within nested MACRO: \\@"
\n ENDM
mm"""
nested
PURGE nested, mm
PRINTLN "Within MACRO: \@"
ENDM
REPT 2
PRINTLN "Begin REPT"
m1
PRINTLN "Within REPT: \@"
ENDR
PRINTLN
MACRO m2
PRINTLN "Begin MACRO: \@"
DEF nested EQUS """MACRO mm
PRINTLN "Within nested MACRO: \\@"
\n ENDM
mm"""
nested
PURGE nested, mm
PRINTLN "Within MACRO: \@"
ENDM
REPT 2
PRINTLN "Begin REPT: \@"
m2
PRINTLN "Within REPT: \@"
ENDR

View File

@@ -0,0 +1,21 @@
Begin REPT
Begin MACRO
Within nested MACRO: _u1
Within MACRO: _u2
Within REPT: _u3
Begin REPT
Begin MACRO
Within nested MACRO: _u4
Within MACRO: _u5
Within REPT: _u6
Begin REPT: _u7
Begin MACRO: _u8
Within nested MACRO: _u9
Within MACRO: _u8
Within REPT: _u7
Begin REPT: _u10
Begin MACRO: _u11
Within nested MACRO: _u12
Within MACRO: _u11
Within REPT: _u10