Use std::stack for options

This commit is contained in:
Rangi42
2024-02-21 10:00:39 -05:00
committed by Sylvie
parent 52c80c2740
commit 36cfce40ad

View File

@@ -2,6 +2,7 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <stack>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -28,10 +29,9 @@ struct OptStackEntry {
bool warningsAreErrors; bool warningsAreErrors;
size_t maxRecursionDepth; size_t maxRecursionDepth;
enum WarningState warningStates[numWarningStates]; enum WarningState warningStates[numWarningStates];
struct OptStackEntry *next;
}; };
static struct OptStackEntry *stack = NULL; static std::stack<struct OptStackEntry> stack;
void opt_B(char const chars[2]) void opt_B(char const chars[2])
{ {
@@ -241,65 +241,59 @@ void opt_Parse(char *s)
void opt_Push(void) void opt_Push(void)
{ {
struct OptStackEntry *entry = (struct OptStackEntry *)malloc(sizeof(*entry)); struct OptStackEntry entry;
if (entry == NULL) // Both of these are pulled from lexer.hpp
fatalerror("Failed to alloc option stack entry: %s\n", strerror(errno)); entry.binary[0] = binDigits[0];
entry.binary[1] = binDigits[1];
// Both of these pulled from lexer.hpp entry.gbgfx[0] = gfxDigits[0];
entry->binary[0] = binDigits[0]; entry.gbgfx[1] = gfxDigits[1];
entry->binary[1] = binDigits[1]; entry.gbgfx[2] = gfxDigits[2];
entry.gbgfx[3] = gfxDigits[3];
entry->gbgfx[0] = gfxDigits[0]; entry.fixPrecision = fixPrecision; // Pulled from fixpoint.hpp
entry->gbgfx[1] = gfxDigits[1];
entry->gbgfx[2] = gfxDigits[2];
entry->gbgfx[3] = gfxDigits[3];
entry->fixPrecision = fixPrecision; // Pulled from fixpoint.hpp entry.fillByte = fillByte; // Pulled from section.hpp
entry->fillByte = fillByte; // Pulled from section.hpp
// Both of these are pulled from main.hpp // Both of these are pulled from main.hpp
entry->haltNop = haltNop; entry.haltNop = haltNop;
entry->warnOnHaltNop = warnOnHaltNop; entry.warnOnHaltNop = warnOnHaltNop;
// Both of these are pulled from main.hpp // Both of these are pulled from main.hpp
entry->optimizeLoads = optimizeLoads; entry.optimizeLoads = optimizeLoads;
entry->warnOnLdOpt = warnOnLdOpt; entry.warnOnLdOpt = warnOnLdOpt;
// Both of these pulled from warning.hpp // Both of these pulled from warning.hpp
entry->warningsAreErrors = warningsAreErrors; entry.warningsAreErrors = warningsAreErrors;
memcpy(entry->warningStates, warningStates, numWarningStates); memcpy(entry.warningStates, warningStates, numWarningStates);
entry->maxRecursionDepth = maxRecursionDepth; // Pulled from fstack.h entry.maxRecursionDepth = maxRecursionDepth; // Pulled from fstack.h
entry->next = stack; stack.push(entry);
stack = entry;
} }
void opt_Pop(void) void opt_Pop(void)
{ {
if (stack == NULL) { if (stack.empty()) {
error("No entries in the option stack\n"); error("No entries in the option stack\n");
return; return;
} }
struct OptStackEntry *entry = stack; struct OptStackEntry entry = stack.top();
stack.pop();
opt_B(entry->binary); opt_B(entry.binary);
opt_G(entry->gbgfx); opt_G(entry.gbgfx);
opt_P(entry->fillByte); opt_P(entry.fillByte);
opt_Q(entry->fixPrecision); opt_Q(entry.fixPrecision);
opt_H(entry->warnOnHaltNop); opt_H(entry.warnOnHaltNop);
opt_h(entry->haltNop); opt_h(entry.haltNop);
opt_L(entry->optimizeLoads); opt_L(entry.optimizeLoads);
opt_l(entry->warnOnLdOpt); opt_l(entry.warnOnLdOpt);
opt_R(entry->maxRecursionDepth); opt_R(entry.maxRecursionDepth);
// opt_W does not apply a whole warning state; it processes one flag string // opt_W does not apply a whole warning state; it processes one flag string
warningsAreErrors = entry->warningsAreErrors; warningsAreErrors = entry.warningsAreErrors;
memcpy(warningStates, entry->warningStates, numWarningStates); memcpy(warningStates, entry.warningStates, numWarningStates);
stack = entry->next;
free(entry);
} }