Use std::stack for IF stack

This commit is contained in:
Rangi42
2024-02-21 10:14:47 -05:00
committed by Sylvie
parent 36cfce40ad
commit feb342804b

View File

@@ -7,8 +7,10 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <math.h>
#include <limits.h> #include <limits.h>
#include <math.h>
#include <new>
#include <stack>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -301,8 +303,7 @@ struct Expansion {
bool owned; // Whether or not to free contents when this expansion is freed bool owned; // Whether or not to free contents when this expansion is freed
}; };
struct IfStack { struct IfStackEntry {
struct IfStack *next;
bool ranIfBlock; // Whether an IF/ELIF/ELSE block ran already bool ranIfBlock; // Whether an IF/ELIF/ELSE block ran already
bool reachedElseBlock; // Whether an ELSE block ran already bool reachedElseBlock; // Whether an ELSE block ran already
}; };
@@ -340,7 +341,7 @@ struct LexerState {
uint32_t colNo; uint32_t colNo;
int lastToken; int lastToken;
struct IfStack *ifStack; std::stack<struct IfStackEntry> *ifStack;
bool capturing; // Whether the text being lexed should be captured bool capturing; // Whether the text being lexed should be captured
size_t captureSize; // Amount of text captured size_t captureSize; // Amount of text captured
@@ -363,7 +364,9 @@ static void initState(struct LexerState *state)
state->atLineStart = true; // yylex() will init colNo due to this state->atLineStart = true; // yylex() will init colNo due to this
state->lastToken = T_EOF; state->lastToken = T_EOF;
state->ifStack = NULL; state->ifStack = new(std::nothrow) std::stack<struct IfStackEntry>();
if (!state->ifStack)
fatalerror("Unable to allocate new IF stack: %s\n", strerror(errno));
state->capturing = false; state->capturing = false;
state->captureBuf = NULL; state->captureBuf = NULL;
@@ -383,58 +386,40 @@ static void nextLine(void)
uint32_t lexer_GetIFDepth(void) uint32_t lexer_GetIFDepth(void)
{ {
uint32_t depth = 0; return lexerState->ifStack->size();
for (struct IfStack *stack = lexerState->ifStack; stack != NULL; stack = stack->next)
depth++;
return depth;
} }
void lexer_IncIFDepth(void) void lexer_IncIFDepth(void)
{ {
struct IfStack *ifStack = (struct IfStack *)malloc(sizeof(*ifStack)); lexerState->ifStack->push({ .ranIfBlock = false, .reachedElseBlock = false });
if (!ifStack)
fatalerror("Unable to allocate new IF depth: %s\n", strerror(errno));
ifStack->ranIfBlock = false;
ifStack->reachedElseBlock = false;
ifStack->next = lexerState->ifStack;
lexerState->ifStack = ifStack;
} }
void lexer_DecIFDepth(void) void lexer_DecIFDepth(void)
{ {
if (!lexerState->ifStack) if (lexerState->ifStack->empty())
fatalerror("Found ENDC outside an IF construct\n"); fatalerror("Found ENDC outside an IF construct\n");
struct IfStack *top = lexerState->ifStack->next; lexerState->ifStack->pop();
free(lexerState->ifStack);
lexerState->ifStack = top;
} }
bool lexer_RanIFBlock(void) bool lexer_RanIFBlock(void)
{ {
return lexerState->ifStack->ranIfBlock; return lexerState->ifStack->top().ranIfBlock;
} }
bool lexer_ReachedELSEBlock(void) bool lexer_ReachedELSEBlock(void)
{ {
return lexerState->ifStack->reachedElseBlock; return lexerState->ifStack->top().reachedElseBlock;
} }
void lexer_RunIFBlock(void) void lexer_RunIFBlock(void)
{ {
lexerState->ifStack->ranIfBlock = true; lexerState->ifStack->top().ranIfBlock = true;
} }
void lexer_ReachELSEBlock(void) void lexer_ReachELSEBlock(void)
{ {
lexerState->ifStack->reachedElseBlock = true; lexerState->ifStack->top().reachedElseBlock = true;
} }
struct LexerState *lexer_OpenFile(char const *path) struct LexerState *lexer_OpenFile(char const *path)
@@ -558,6 +543,7 @@ void lexer_DeleteState(struct LexerState *state)
close(state->cbuf.fd); close(state->cbuf.fd);
else if (state->isFile && !state->mmap.isReferenced) else if (state->isFile && !state->mmap.isReferenced)
munmap(state->mmap.ptr, state->mmap.size); munmap(state->mmap.ptr, state->mmap.size);
delete state->ifStack;
free(state); free(state);
} }