mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Use std::shared_ptr for MacroArgs
This commit is contained in:
@@ -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 ¯oName, MacroArgs &args);
|
||||
void fstk_RunMacro(std::string const ¯oName, 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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 ¯oName, MacroArgs &args) {
|
||||
void fstk_RunMacro(std::string const ¯oName, std::shared_ptr<MacroArgs> 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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user