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