Ultra 0.9.4
Loading...
Searching...
No Matches
symbol.h
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_SYMBOL_H)
14#define ULTRA_SYMBOL_H
15
16#include "utility/assert.h"
17
18#include <limits>
19#include <string>
20
21namespace ultra
22{
23
41class symbol
42{
43public:
44 // ---- Member types ----
45
55 using category_t = unsigned;
56
58 static constexpr category_t default_category = 0;
59
61 static constexpr category_t undefined_category =
62 std::numeric_limits<category_t>::max();
63
69 using opcode_t = unsigned;
70
72 enum format {c_format, cpp_format, python_format, sup_format};
73
74 // ---- Constructors ----
75 explicit symbol(const std::string &, category_t = default_category);
76
78 virtual ~symbol() = default;
79
80 // ---- Observers ----
81 [[nodiscard]] category_t category() const noexcept;
82 [[nodiscard]] opcode_t opcode() const noexcept;
83 [[nodiscard]] const std::string &name() const noexcept;
84
85 [[nodiscard]] virtual std::string to_string(format = c_format) const = 0;
86
87 // ---- Modifiers ----
88 void category(category_t) noexcept;
89
90 // ---- Misc ----
91 [[nodiscard]] virtual bool is_valid() const;
92
93private:
94 std::string name_;
95 category_t category_;
96 opcode_t opcode_;
97};
98
99template<class S> concept Symbol = std::derived_from<S, symbol>;
100
115template<Symbol S>
116[[nodiscard]] auto get_if(const symbol *s)
117{
118 return dynamic_cast<const S *>(s);
119}
120
133template<Symbol S>
134[[nodiscard]] auto get_if(const symbol &s)
135{
136 return get_if<S>(&s);
137}
138
152template<Symbol S>
153[[nodiscard]] bool is(const symbol *s)
154{
155 return get_if<S>(s);
156}
157
170template<Symbol S>
171[[nodiscard]] bool is(const symbol &s)
172{
173 return is<S>(&s);
174}
175
176} // namespace ultra
177
178#endif // include guard
Definition symbol.h:42
static constexpr category_t undefined_category
Sentinel value indicating that the category has not yet been assigned.
Definition symbol.h:61
category_t category() const noexcept
Definition symbol.cc:79
format
Supported rendering formats for symbol stringification.
Definition symbol.h:72
unsigned category_t
Definition symbol.h:55
opcode_t opcode() const noexcept
Definition symbol.cc:93
symbol(const std::string &, category_t=default_category)
Definition symbol.cc:30
unsigned opcode_t
Definition symbol.h:69
virtual bool is_valid() const
Definition symbol.cc:107
virtual ~symbol()=default
Virtual destructor to allow safe polymorphic deletion.
static constexpr category_t default_category
Default category assigned when typing is not used.
Definition symbol.h:58
const std::string & name() const noexcept
Definition symbol.cc:51
Definition symbol.h:99
STL namespace.
auto get_if(const symbol *s)
Definition symbol.h:116
bool is(const symbol *s)
Definition symbol.h:153