mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Refactor peek() to use a loop instead of tail recursion
This commit is contained in:
@@ -803,17 +803,14 @@ static std::shared_ptr<std::string> readInterpolation(size_t depth);
|
|||||||
static int peek() {
|
static int peek() {
|
||||||
int c = lexerState->peekChar();
|
int c = lexerState->peekChar();
|
||||||
|
|
||||||
if (lexerState->macroArgScanDistance > 0) {
|
for (; lexerState->macroArgScanDistance == 0; c = lexerState->peekChar()) {
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
++lexerState->macroArgScanDistance; // Do not consider again
|
++lexerState->macroArgScanDistance; // Do not consider again
|
||||||
|
|
||||||
if (c == '\\' && !lexerState->disableMacroArgs) {
|
if (c == '\\' && !lexerState->disableMacroArgs) {
|
||||||
// If character is a backslash, check for a macro arg
|
// If character is a backslash, check for a macro arg
|
||||||
++lexerState->macroArgScanDistance;
|
++lexerState->macroArgScanDistance;
|
||||||
if (!isMacroChar(lexerState->peekCharAhead())) {
|
if (!isMacroChar(lexerState->peekCharAhead())) {
|
||||||
return c;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If character is a macro arg char, do macro arg expansion
|
// If character is a macro arg char, do macro arg expansion
|
||||||
@@ -821,17 +818,16 @@ static int peek() {
|
|||||||
if (std::shared_ptr<std::string> str = readMacroArg(); str) {
|
if (std::shared_ptr<std::string> str = readMacroArg(); str) {
|
||||||
beginExpansion(str, std::nullopt);
|
beginExpansion(str, std::nullopt);
|
||||||
}
|
}
|
||||||
return peek(); // Tail recursion
|
|
||||||
} else if (c == '{' && !lexerState->disableInterpolation) {
|
} else if (c == '{' && !lexerState->disableInterpolation) {
|
||||||
// If character is an open brace, do symbol interpolation
|
// If character is an open brace, do symbol interpolation
|
||||||
shiftChar();
|
shiftChar();
|
||||||
if (std::shared_ptr<std::string> str = readInterpolation(0); str) {
|
if (std::shared_ptr<std::string> str = readInterpolation(0); str) {
|
||||||
beginExpansion(str, *str);
|
beginExpansion(str, *str);
|
||||||
}
|
}
|
||||||
return peek(); // Tail recursion
|
|
||||||
} else {
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void shiftChar() {
|
static void shiftChar() {
|
||||||
|
|||||||
Reference in New Issue
Block a user