diff --git a/include/asm/fstack.hpp b/include/asm/fstack.hpp index 16a6545e..6b6e0e93 100644 --- a/include/asm/fstack.hpp +++ b/include/asm/fstack.hpp @@ -61,7 +61,7 @@ std::optional fstk_FindFile(std::string const &path); bool yywrap(); void fstk_RunInclude(std::string const &path); -void fstk_RunMacro(std::string const ¯oName, MacroArgs &args); +void fstk_RunMacro(std::string const ¯oName, std::shared_ptr args); void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t size); void fstk_RunFor( std::string const &symName, diff --git a/include/asm/macro.hpp b/include/asm/macro.hpp index a758460a..a70203c9 100644 --- a/include/asm/macro.hpp +++ b/include/asm/macro.hpp @@ -15,12 +15,12 @@ struct MacroArgs { void append(std::shared_ptr arg); }; -MacroArgs *macro_GetCurrentArgs(); -void macro_UseNewArgs(MacroArgs *args); +bool macro_HasCurrentArgs(); +std::shared_ptr macro_GetCurrentArgs(); +void macro_UseNewArgs(std::shared_ptr args); +uint32_t macro_NbArgs(); std::shared_ptr macro_GetArg(uint32_t i); std::shared_ptr macro_GetAllArgs(); - void macro_ShiftCurrentArgs(int32_t count); -uint32_t macro_NbArgs(); #endif // RGBDS_MACRO_H diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index 2b557be8..ff6b54f1 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -35,7 +35,7 @@ struct Context { // Note that several contexts can share the same unique ID (since `INCLUDE` preserves its // parent's, and likewise "back-propagates" a unique ID if requested), hence using `shared_ptr`. std::shared_ptr uniqueIDStr; - MacroArgs *macroArgs = nullptr; // Macro args are *saved* here + std::shared_ptr macroArgs = nullptr; // Macro args are *saved* here uint32_t nbReptIters = 0; bool isForLoop = false; int32_t forValue = 0; @@ -285,7 +285,7 @@ static void runPreIncludeFile() { lexer_SetState(&context.lexerState); } -void fstk_RunMacro(std::string const ¯oName, MacroArgs &args) { +void fstk_RunMacro(std::string const ¯oName, std::shared_ptr args) { Symbol *macro = sym_FindExactSymbol(macroName); if (!macro) { @@ -325,7 +325,7 @@ void fstk_RunMacro(std::string const ¯oName, MacroArgs &args) { context.lexerState, "MACRO", macroView->data(), macroView->size(), macro->fileLine ); lexer_SetStateAtEOL(&context.lexerState); - macro_UseNewArgs(&args); + macro_UseNewArgs(args); } static bool newReptContext(int32_t reptLineNo, char const *body, size_t size) { diff --git a/src/asm/macro.cpp b/src/asm/macro.cpp index 9d10a08e..b3fd9301 100644 --- a/src/asm/macro.cpp +++ b/src/asm/macro.cpp @@ -12,7 +12,7 @@ #define MAXMACROARGS 99999 -static MacroArgs *macroArgs = nullptr; +static std::shared_ptr macroArgs = nullptr; void MacroArgs::append(std::shared_ptr arg) { if (arg->empty()) @@ -22,14 +22,22 @@ void MacroArgs::append(std::shared_ptr arg) { args.push_back(arg); } -MacroArgs *macro_GetCurrentArgs() { +bool macro_HasCurrentArgs() { + return macroArgs != nullptr; +} + +std::shared_ptr macro_GetCurrentArgs() { return macroArgs; } -void macro_UseNewArgs(MacroArgs *args) { +void macro_UseNewArgs(std::shared_ptr args) { macroArgs = args; } +uint32_t macro_NbArgs() { + return macroArgs->args.size() - macroArgs->shift; +} + std::shared_ptr macro_GetArg(uint32_t i) { if (!macroArgs) return nullptr; @@ -83,7 +91,3 @@ void macro_ShiftCurrentArgs(int32_t count) { macroArgs->shift += count; } } - -uint32_t macro_NbArgs() { - return macroArgs->args.size() - macroArgs->shift; -} diff --git a/src/asm/parser.y b/src/asm/parser.y index 8f32fb96..b680a04d 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -252,7 +252,7 @@ %type capture_macro %type sect_mod -%type macro_args +%type > macro_args %type align_spec @@ -512,18 +512,16 @@ macro: // Parsing 'macroargs' will restore the lexer's normal mode lexer_SetMode(LEXER_RAW); } macro_args { - fstk_RunMacro($1, *$3); + fstk_RunMacro($1, $3); } ; macro_args: %empty { - $$ = new (std::nothrow) MacroArgs(); - if (!$$) - fatalerror("Failed to allocate memory for macro arguments: %s\n", strerror(errno)); + $$ = std::make_shared(); } | macro_args STRING { - $$ = $1; + $$ = std::move($1); $$->append(std::make_shared($2)); } ; diff --git a/src/asm/symbol.cpp b/src/asm/symbol.cpp index 2e0bc798..65652800 100644 --- a/src/asm/symbol.cpp +++ b/src/asm/symbol.cpp @@ -43,7 +43,7 @@ void sym_ForEach(void (*callback)(Symbol &)) { } static int32_t Callback_NARG() { - if (!macro_GetCurrentArgs()) { + if (!macro_HasCurrentArgs()) { error("_NARG does not make sense outside of a macro\n"); return 0; } @@ -147,7 +147,7 @@ Symbol *sym_FindScopedValidSymbol(std::string const &symName) { return nullptr; } // `_NARG` has no value outside a macro - if (sym == _NARGSymbol && !macro_GetCurrentArgs()) { + if (sym == _NARGSymbol && !macro_HasCurrentArgs()) { return nullptr; } return sym;