mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-23 19:42:08 +00:00
Replace Either with std::variant (#1731)
This commit is contained in:
@@ -49,7 +49,7 @@ static std::vector<std::string> includePaths = {""};
|
||||
static std::string preIncludeName;
|
||||
|
||||
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||
if (data.holds<std::vector<uint32_t>>()) {
|
||||
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
||||
assume(parent); // REPT nodes use their parent's name
|
||||
std::string const &lastName = parent->dump(lineNo);
|
||||
fputs(" -> ", stderr);
|
||||
|
||||
@@ -108,10 +108,10 @@ using namespace std::literals;
|
||||
|
||||
struct Token {
|
||||
int type;
|
||||
Either<uint32_t, std::string> value;
|
||||
std::variant<std::monostate, uint32_t, std::string> value;
|
||||
|
||||
Token() : type(T_(NUMBER)), value() {}
|
||||
Token(int type_) : type(type_), value() {}
|
||||
Token() : type(T_(NUMBER)), value(std::monostate{}) {}
|
||||
Token(int type_) : type(type_), value(std::monostate{}) {}
|
||||
Token(int type_, uint32_t value_) : type(type_), value(value_) {}
|
||||
Token(int type_, std::string const &value_) : type(type_), value(value_) {}
|
||||
Token(int type_, std::string &&value_) : type(type_), value(value_) {}
|
||||
@@ -489,8 +489,8 @@ void LexerState::setViewAsNextState(char const *name, ContentSpan const &span, u
|
||||
}
|
||||
|
||||
void lexer_RestartRept(uint32_t lineNo) {
|
||||
if (lexerState->content.holds<ViewedContent>()) {
|
||||
lexerState->content.get<ViewedContent>().offset = 0;
|
||||
if (std::holds_alternative<ViewedContent>(lexerState->content)) {
|
||||
std::get<ViewedContent>(lexerState->content).offset = 0;
|
||||
}
|
||||
lexerState->clear(lineNo);
|
||||
}
|
||||
@@ -755,13 +755,13 @@ int LexerState::peekChar() {
|
||||
}
|
||||
}
|
||||
|
||||
if (content.holds<ViewedContent>()) {
|
||||
auto &view = content.get<ViewedContent>();
|
||||
if (std::holds_alternative<ViewedContent>(content)) {
|
||||
auto &view = std::get<ViewedContent>(content);
|
||||
if (view.offset < view.span.size) {
|
||||
return static_cast<uint8_t>(view.span.ptr[view.offset]);
|
||||
}
|
||||
} else {
|
||||
auto &cbuf = content.get<BufferedContent>();
|
||||
auto &cbuf = std::get<BufferedContent>(content);
|
||||
if (cbuf.size == 0) {
|
||||
cbuf.refill();
|
||||
}
|
||||
@@ -791,13 +791,13 @@ int LexerState::peekCharAhead() {
|
||||
distance -= exp.size() - exp.offset;
|
||||
}
|
||||
|
||||
if (content.holds<ViewedContent>()) {
|
||||
auto &view = content.get<ViewedContent>();
|
||||
if (std::holds_alternative<ViewedContent>(content)) {
|
||||
auto &view = std::get<ViewedContent>(content);
|
||||
if (view.offset + distance < view.span.size) {
|
||||
return static_cast<uint8_t>(view.span.ptr[view.offset + distance]);
|
||||
}
|
||||
} else {
|
||||
auto &cbuf = content.get<BufferedContent>();
|
||||
auto &cbuf = std::get<BufferedContent>(content);
|
||||
assume(distance < std::size(cbuf.buf));
|
||||
if (cbuf.size <= distance) {
|
||||
cbuf.refill();
|
||||
@@ -882,10 +882,10 @@ static void shiftChar() {
|
||||
}
|
||||
} else {
|
||||
// Advance within the file contents
|
||||
if (lexerState->content.holds<ViewedContent>()) {
|
||||
lexerState->content.get<ViewedContent>().offset++;
|
||||
if (std::holds_alternative<ViewedContent>(lexerState->content)) {
|
||||
std::get<ViewedContent>(lexerState->content).offset++;
|
||||
} else {
|
||||
lexerState->content.get<BufferedContent>().advance();
|
||||
std::get<BufferedContent>(lexerState->content).advance();
|
||||
}
|
||||
}
|
||||
return;
|
||||
@@ -2030,12 +2030,12 @@ static Token yylex_NORMAL() {
|
||||
}
|
||||
|
||||
// `token` is either a `SYMBOL` or a `LOCAL`, and both have a `std::string` value.
|
||||
assume(token.value.holds<std::string>());
|
||||
assume(std::holds_alternative<std::string>(token.value));
|
||||
|
||||
// Raw symbols and local symbols cannot be string expansions
|
||||
if (!raw && token.type == T_(SYMBOL) && lexerState->expandStrings) {
|
||||
// Attempt string expansion
|
||||
Symbol const *sym = sym_FindExactSymbol(token.value.get<std::string>());
|
||||
Symbol const *sym = sym_FindExactSymbol(std::get<std::string>(token.value));
|
||||
|
||||
if (sym && sym->type == SYM_EQUS) {
|
||||
std::shared_ptr<std::string> str = sym->getEqus();
|
||||
@@ -2449,21 +2449,17 @@ yy::parser::symbol_type yylex() {
|
||||
|
||||
// Captures end at their buffer's boundary no matter what
|
||||
if (token.type == T_(YYEOF) && !lexerState->capturing) {
|
||||
// Doing `token = Token(T_(EOB));` here would be valid but redundant, because YYEOF and EOB
|
||||
// both have the same empty value. Furthermore, g++ 11.4.0 was giving a false-positive
|
||||
// '-Wmaybe-uninitialized' warning for `Token::value.Either<...>::_tag` that way.
|
||||
// (This was on a developer's local machine; GitHub Actions CI's g++ was not warning.)
|
||||
token.type = T_(EOB);
|
||||
}
|
||||
lexerState->lastToken = token.type;
|
||||
lexerState->atLineStart = token.type == T_(NEWLINE) || token.type == T_(EOB);
|
||||
|
||||
if (token.value.holds<uint32_t>()) {
|
||||
return yy::parser::symbol_type(token.type, token.value.get<uint32_t>());
|
||||
} else if (token.value.holds<std::string>()) {
|
||||
return yy::parser::symbol_type(token.type, token.value.get<std::string>());
|
||||
if (std::holds_alternative<uint32_t>(token.value)) {
|
||||
return yy::parser::symbol_type(token.type, std::get<uint32_t>(token.value));
|
||||
} else if (std::holds_alternative<std::string>(token.value)) {
|
||||
return yy::parser::symbol_type(token.type, std::get<std::string>(token.value));
|
||||
} else {
|
||||
assume(token.value.empty());
|
||||
assume(std::holds_alternative<std::monostate>(token.value));
|
||||
return yy::parser::symbol_type(token.type);
|
||||
}
|
||||
}
|
||||
@@ -2479,8 +2475,9 @@ static Capture startCapture() {
|
||||
lexerState->captureSize = 0;
|
||||
|
||||
uint32_t lineNo = lexer_GetLineNo();
|
||||
if (lexerState->content.holds<ViewedContent>() && lexerState->expansions.empty()) {
|
||||
auto &view = lexerState->content.get<ViewedContent>();
|
||||
if (std::holds_alternative<ViewedContent>(lexerState->content)
|
||||
&& lexerState->expansions.empty()) {
|
||||
auto &view = std::get<ViewedContent>(lexerState->content);
|
||||
return {
|
||||
.lineNo = lineNo, .span = {.ptr = view.makeSharedContentPtr(), .size = 0}
|
||||
};
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
%code requires {
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "either.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
|
||||
#include "asm/lexer.hpp"
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
struct StrFmtArgList {
|
||||
std::string format;
|
||||
std::vector<Either<uint32_t, std::string>> args;
|
||||
std::vector<std::variant<uint32_t, std::string>> args;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@
|
||||
static uint32_t adjustNegativePos(int32_t pos, size_t len, char const *functionName);
|
||||
static std::string strrpl(std::string_view str, std::string const &old, std::string const &rep);
|
||||
static std::string strfmt(
|
||||
std::string const &spec, std::vector<Either<uint32_t, std::string>> const &args
|
||||
std::string const &spec, std::vector<std::variant<uint32_t, std::string>> const &args
|
||||
);
|
||||
static void compoundAssignment(std::string const &symName, RPNCommand op, int32_t constValue);
|
||||
static void failAssert(AssertionType type);
|
||||
@@ -2964,7 +2964,7 @@ static std::string strrpl(std::string_view str, std::string const &old, std::str
|
||||
}
|
||||
|
||||
static std::string
|
||||
strfmt(std::string const &spec, std::vector<Either<uint32_t, std::string>> const &args) {
|
||||
strfmt(std::string const &spec, std::vector<std::variant<uint32_t, std::string>> const &args) {
|
||||
std::string str;
|
||||
size_t argIndex = 0;
|
||||
|
||||
@@ -3005,10 +3005,10 @@ static std::string
|
||||
} else if (argIndex >= args.size()) {
|
||||
// Will warn after formatting is done.
|
||||
str += '%';
|
||||
} else if (args[argIndex].holds<uint32_t>()) {
|
||||
fmt.appendNumber(str, args[argIndex].get<uint32_t>());
|
||||
} else if (std::holds_alternative<uint32_t>(args[argIndex])) {
|
||||
fmt.appendNumber(str, std::get<uint32_t>(args[argIndex]));
|
||||
} else {
|
||||
fmt.appendString(str, args[argIndex].get<std::string>());
|
||||
fmt.appendString(str, std::get<std::string>(args[argIndex]));
|
||||
}
|
||||
|
||||
argIndex++;
|
||||
|
||||
@@ -39,7 +39,7 @@ uint8_t *Expression::reserveSpace(uint32_t size, uint32_t patchSize) {
|
||||
|
||||
int32_t Expression::getConstVal() const {
|
||||
if (!isKnown()) {
|
||||
error("Expected constant expression: %s", data.get<std::string>().c_str());
|
||||
error("Expected constant expression: %s", std::get<std::string>(data).c_str());
|
||||
return 0;
|
||||
}
|
||||
return value();
|
||||
|
||||
Reference in New Issue
Block a user