Use a custom generic tagged union Either instead of std::variant for efficiency (#1476)

* Implement custom generic tagged union `Either`

This should be more efficient than `std::variant`, while still
keeping runtime safety as it `assert`s when `get`ting values.

* Use `Either` for RPN expressions

* Use `Either` for file stack node data

* Use `Either` for `File` buffer

* Use `Either` for `STRFMT` args

* Use `Either` for RGBLINK symbol values

* Support an equivalent of `std::monostate` for `Either`

* Use `Either` for lexer tokens

* Use `Either` for symbol values

* Use `Either` for lexer mmap/buffer state
This commit is contained in:
Sylvie
2024-08-20 15:19:11 -04:00
committed by GitHub
parent 7d98b9a900
commit 57c3d74b9e
17 changed files with 303 additions and 174 deletions

View File

@@ -12,16 +12,6 @@
std::unordered_map<std::string, Symbol *> symbols;
Label &Symbol::label() {
assume(std::holds_alternative<Label>(data));
return std::get<Label>(data);
}
Label const &Symbol::label() const {
assume(std::holds_alternative<Label>(data));
return std::get<Label>(data);
}
void sym_ForEach(void (*callback)(Symbol &)) {
for (auto &it : symbols)
callback(*it.second);
@@ -29,8 +19,9 @@ void sym_ForEach(void (*callback)(Symbol &)) {
void sym_AddSymbol(Symbol &symbol) {
Symbol *other = sym_GetSymbol(symbol.name);
auto *symValue = std::get_if<int32_t>(&symbol.data);
auto *otherValue = other ? std::get_if<int32_t>(&other->data) : nullptr;
int32_t *symValue = symbol.data.holds<int32_t>() ? &symbol.data.get<int32_t>() : nullptr;
int32_t *otherValue =
other && other->data.holds<int32_t>() ? &other->data.get<int32_t>() : nullptr;
// Check if the symbol already exists with a different value
if (other && !(symValue && otherValue && *symValue == *otherValue)) {