MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
tok.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/util/dbg.h"
4
5namespace mim {
6
7class Def;
8class Lit;
9
10namespace ast {
11
12// clang-format off
13#define MIM_KEY(m) \
14 m(K_module, "module") \
15 m(K_import, "import") \
16 m(K_plugin, "plugin") \
17 m(K_and, "and" ) \
18 m(K_as, "as" ) \
19 m(K_axm, "axm" ) \
20 m(K_let, "let" ) \
21 m(K_rec, "rec" ) \
22 m(K_ret, "ret" ) \
23 m(K_where, "where" ) \
24 m(K_end, "end" ) \
25 m(K_Nat, "Nat" ) \
26 m(K_Idx, "Idx" ) \
27 m(K_extern, "extern") \
28 m(K_Type, "Type" ) \
29 m(K_Univ, "Univ" ) \
30 m(K_Cn, "Cn" ) \
31 m(K_Fn, "Fn" ) \
32 m(K_con, "con" ) \
33 m(K_fun, "fun" ) \
34 m(K_lam, "lam" ) \
35 m(K_ccon, "ccon" ) \
36 m(K_cfun, "cfun" ) \
37 m(K_cn, "cn" ) \
38 m(K_fn, "fn" ) \
39 m(K_ff, "ff" ) \
40 m(K_tt, "tt" ) \
41 m(K_ins, "ins" ) \
42 m(K_i1, "i1" ) \
43 m(K_i8, "i8" ) \
44 m(K_i16, "i16" ) \
45 m(K_i32, "i32" ) \
46 m(K_i64, "i64" ) \
47 m(K_Bool, "Bool" ) \
48 m(K_I1, "I1" ) \
49 m(K_I8, "I8" ) \
50 m(K_I16, "I16" ) \
51 m(K_I32, "I32" ) \
52 m(K_I64, "I64" ) \
53
54#define CODE(t, str) + size_t(1)
55constexpr auto Num_Keys = size_t(0) MIM_KEY(CODE);
56#undef CODE
57
58#define MIM_TOK(m) \
59 m(EoF, "<end of file>" ) \
60 /* literals */ \
61 m(L_s, "<signed integer literal>") \
62 m(L_u, "<integer literal>" ) \
63 m(L_i, "<index literal>" ) \
64 m(L_f, "<floating-point literal>") \
65 m(L_c, "<char literal>" ) \
66 m(L_str, "<string literal>" ) \
67 /* misc */ \
68 m(M_id, "<identifier>" ) \
69 m(M_anx, "<annex name>" ) \
70 /* delimiters */ \
71 m(D_angle_l, "‹") \
72 m(D_angle_r, "›") \
73 m(D_brace_l, "{") \
74 m(D_brace_r, "}") \
75 m(D_brckt_l, "[") \
76 m(D_brckt_r, "]") \
77 m(D_curly_l, "⦃") \
78 m(D_curly_r, "⦄") \
79 m(D_paren_l, "(") \
80 m(D_paren_r, ")") \
81 m(D_quote_l, "«") \
82 m(D_quote_r, "»") \
83 /* further tokens */ \
84 m(T_arrow, "→") \
85 m(T_assign, "=") \
86 m(T_at, "@") \
87 m(T_bot, "⊥") \
88 m(T_top, "⊤") \
89 m(T_box, "□") \
90 m(T_colon, ":") \
91 m(T_comma, ",") \
92 m(T_dollar, "$") \
93 m(T_dot, ".") \
94 m(T_extract, "#") \
95 m(T_lm, "λ") \
96 m(T_semicolon, ";") \
97 m(T_star, "*") \
98
99#define MIM_SUBST(m) \
100 m("lm", T_lm ) \
101 m("bot", T_bot ) \
102 m("top", T_top ) \
103 m("insert", K_ins ) \
104
105class Tok {
106public:
107 /// @name Tag
108 ///@{
109 enum class Tag {
110 Nil,
111#define CODE(t, str) t,
113#undef CODE
114 };
115
116 static const char* tag2str(Tok::Tag);
117 static constexpr Tok::Tag delim_l2r(Tag tag) { return Tok::Tag(int(tag) + 1); }
118 ///@}
119
120 // clang-format on
121
122 Tok() {}
124 : loc_(loc)
125 , tag_(tag) {}
126 Tok(Loc loc, char8_t c)
127 : loc_(loc)
128 , tag_(Tag::L_c)
129 , c_(c) {}
130 Tok(Loc loc, uint64_t u)
131 : loc_(loc)
132 , tag_(Tag::L_u)
133 , u_(u) {}
134 Tok(Loc loc, int64_t s)
135 : loc_(loc)
136 , tag_(Tag::L_s)
137 , u_(std::bit_cast<uint64_t>(s)) {}
138 Tok(Loc loc, double d)
139 : loc_(loc)
140 , tag_(Tag::L_f)
141 , u_(std::bit_cast<uint64_t>(d)) {}
142 Tok(Loc loc, const Lit* i)
143 : loc_(loc)
144 , tag_(Tag::L_i)
145 , i_(i) {}
146 Tok(Loc loc, Tag tag, Sym sym)
147 : loc_(loc)
148 , tag_(tag)
149 , sym_(sym) {
150 assert(tag == Tag::M_id || tag == Tag::M_anx || tag == Tag::L_str);
151 }
152
153 bool isa(Tag tag) const { return tag == tag_; }
154 Tag tag() const { return tag_; }
155 Dbg dbg() const { return {loc(), sym()}; }
156 Loc loc() const { return loc_; }
157 explicit operator bool() const { return tag_ != Tag::Nil; }
158 // clang-format off
159 const Lit* lit_i() const { assert(isa(Tag::L_i)); return i_; }
160 char8_t lit_c() const { assert(isa(Tag::L_c)); return c_; }
161 uint64_t lit_u() const { assert(isa(Tag::L_u ) || isa(Tag::L_s ) || isa(Tag::L_f )); return u_; }
162 Sym sym() const { assert(isa(Tag::M_anx) || isa(Tag::M_id) || isa(Tag::L_str)); return sym_; }
163 // clang-format on
164 std::string str() const;
165
166 friend std::ostream& operator<<(std::ostream&, Tok);
167 friend std::ostream& operator<<(std::ostream& os, Tok::Tag tag) { return os << tag2str(tag); }
168
169private:
170 Loc loc_;
171 Tag tag_ = Tag::Nil;
172 union {
173 Sym sym_;
174 uint64_t u_;
175 char8_t c_;
176 const Lit* i_;
177 };
178};
179
180} // namespace ast
181} // namespace mim
Tok(Loc loc, uint64_t u)
Definition tok.h:130
friend std::ostream & operator<<(std::ostream &, Tok)
Definition tok.cpp:30
const Lit * lit_i() const
Definition tok.h:159
Sym sym() const
Definition tok.h:162
Tok(Loc loc, Tag tag)
Definition tok.h:123
Tok(Loc loc, Tag tag, Sym sym)
Definition tok.h:146
Tok(Loc loc, const Lit *i)
Definition tok.h:142
Dbg dbg() const
Definition tok.h:155
Loc loc() const
Definition tok.h:156
uint64_t lit_u() const
Definition tok.h:161
bool isa(Tag tag) const
Definition tok.h:153
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:117
Tok(Loc loc, int64_t s)
Definition tok.h:134
Tok(Loc loc, char8_t c)
Definition tok.h:126
friend std::ostream & operator<<(std::ostream &os, Tok::Tag tag)
Definition tok.h:167
static const char * tag2str(Tok::Tag)
Definition tok.cpp:10
std::string str() const
Definition tok.cpp:22
Tok(Loc loc, double d)
Definition tok.h:138
Tag tag() const
Definition tok.h:154
char8_t lit_c() const
Definition tok.h:160
@ Lit
Definition def.h:40
constexpr auto Num_Keys
Definition tok.h:55
Definition cfg.h:11
Definition span.h:104
#define MIM_TOK(m)
Definition tok.h:58
#define CODE(t, str)
Definition tok.h:54
#define MIM_KEY(m)
Definition tok.h:13