Use std::shared_ptr for MacroArgs

This commit is contained in:
Rangi42
2024-03-22 10:02:22 -04:00
committed by Sylvie
parent e255af9e10
commit b85c5cde8f
6 changed files with 25 additions and 23 deletions

View File

@@ -61,7 +61,7 @@ std::optional<std::string> fstk_FindFile(std::string const &path);
bool yywrap();
void fstk_RunInclude(std::string const &path);
void fstk_RunMacro(std::string const &macroName, MacroArgs &args);
void fstk_RunMacro(std::string const &macroName, std::shared_ptr<MacroArgs> args);
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char const *body, size_t size);
void fstk_RunFor(
std::string const &symName,

View File

@@ -15,12 +15,12 @@ struct MacroArgs {
void append(std::shared_ptr<std::string> arg);
};
MacroArgs *macro_GetCurrentArgs();
void macro_UseNewArgs(MacroArgs *args);
bool macro_HasCurrentArgs();
std::shared_ptr<MacroArgs> macro_GetCurrentArgs();
void macro_UseNewArgs(std::shared_ptr<MacroArgs> args);
uint32_t macro_NbArgs();
std::shared_ptr<std::string> macro_GetArg(uint32_t i);
std::shared_ptr<std::string> macro_GetAllArgs();
void macro_ShiftCurrentArgs(int32_t count);
uint32_t macro_NbArgs();
#endif // RGBDS_MACRO_H

View File

@@ -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<std::string> uniqueIDStr;
MacroArgs *macroArgs = nullptr; // Macro args are *saved* here
std::shared_ptr<MacroArgs> 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 &macroName, MacroArgs &args) {
void fstk_RunMacro(std::string const &macroName, std::shared_ptr<MacroArgs> args) {
Symbol *macro = sym_FindExactSymbol(macroName);
if (!macro) {
@@ -325,7 +325,7 @@ void fstk_RunMacro(std::string const &macroName, 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) {

View File

@@ -12,7 +12,7 @@
#define MAXMACROARGS 99999
static MacroArgs *macroArgs = nullptr;
static std::shared_ptr<MacroArgs> macroArgs = nullptr;
void MacroArgs::append(std::shared_ptr<std::string> arg) {
if (arg->empty())
@@ -22,14 +22,22 @@ void MacroArgs::append(std::shared_ptr<std::string> arg) {
args.push_back(arg);
}
MacroArgs *macro_GetCurrentArgs() {
bool macro_HasCurrentArgs() {
return macroArgs != nullptr;
}
std::shared_ptr<MacroArgs> macro_GetCurrentArgs() {
return macroArgs;
}
void macro_UseNewArgs(MacroArgs *args) {
void macro_UseNewArgs(std::shared_ptr<MacroArgs> args) {
macroArgs = args;
}
uint32_t macro_NbArgs() {
return macroArgs->args.size() - macroArgs->shift;
}
std::shared_ptr<std::string> 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;
}

View File

@@ -252,7 +252,7 @@
%type <bool> capture_macro
%type <SectionModifier> sect_mod
%type <MacroArgs *> macro_args
%type <std::shared_ptr<MacroArgs>> macro_args
%type <AlignmentSpec> 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<MacroArgs>();
}
| macro_args STRING {
$$ = $1;
$$ = std::move($1);
$$->append(std::make_shared<std::string>($2));
}
;

View File

@@ -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;