Skip to content

Instantly share code, notes, and snippets.

@Lisoph
Created August 31, 2018 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lisoph/07e20f9fd39734b115e965ea06872e5a to your computer and use it in GitHub Desktop.
Save Lisoph/07e20f9fd39734b115e965ea06872e5a to your computer and use it in GitHub Desktop.
C++17 Token struct
#include <cstdint>
#include <cstddef>
#include <string>
#include <iostream>
struct TokIdentifier {
std::string name;
TokIdentifier() = delete;
TokIdentifier(TokIdentifier const &) = default;
TokIdentifier(TokIdentifier &&) = default;
TokIdentifier(std::string && name)
: name(std::move(name))
{}
~TokIdentifier() = default;
};
struct TokIntLit {
int64_t value;
};
struct Token {
enum class Id : uint8_t {
Identifier,
Equals,
IntLit,
} id;
union {
TokIdentifier ident;
TokIntLit intlit;
};
private:
explicit Token(Id id)
: id(id)
{}
public:
Token() = delete;
Token(Token const &) = default;
Token(Token && other) = default;
Token(TokIdentifier && ident)
: id(Id::Identifier)
, ident(std::move(ident))
{}
Token(TokIntLit && intlit)
: id(Id::IntLit)
, intlit(std::move(intlit))
{}
~Token() {
switch (id) {
case Id::Identifier:
ident.~TokIdentifier();
break;
default:
break;
}
}
static inline Token new_identifier(std::string && name) {
return Token{TokIdentifier{std::move(name)}};
}
static inline Token new_equals() {
return Token{Id::Equals};
}
static inline Token new_intlit(int64_t val) {
return Token{TokIntLit{val}};
}
};
std::ostream &operator << (std::ostream &os, Token const &tok) {
switch (tok.id) {
case Token::Id::Identifier:
os << "Identifier(\"" << tok.ident.name << "\")";
break;
case Token::Id::Equals:
os << '=';
break;
case Token::Id::IntLit:
os << "IntLit(" << tok.intlit.value << ')';
break;
}
return os;
}
int main() {
Token const tokens[] = {
Token::new_identifier("my_var"),
Token::new_equals(),
Token::new_intlit(123),
};
for (auto const &t : tokens) {
std::cout << t << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment