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_plugin, "plugin") \
51 m(K_rec, "rec" ) \
52 m(K_ret, "ret" ) \
53 m(K_tt, "tt" ) \
54 m(K_where, "where" ) \
55 m(K_with, "with" ) \
56
57#define CODE(t, str) + size_t(1)
58constexpr auto Num_Keys = size_t(0) MIM_KEY(CODE);
59#undef CODE
60
61#define MIM_TOK(m) \
62 m(EoF, "<end of file>" ) \
63 /* literals */ \
64 m(L_s, "<signed integer literal>") \
65 m(L_u, "<integer literal>" ) \
66 m(L_i, "<index literal>" ) \
67 m(L_f, "<floating-point literal>") \
68 m(L_c, "<char literal>" ) \
69 m(L_str, "<string literal>" ) \
70 /* misc */ \
71 m(M_id, "<identifier>" ) \
72 m(M_anx, "<annex name>" ) \
73 /* delimiters */ \
74 m(D_angle_l, "‹") \
75 m(D_angle_r, "›") \
76 m(D_brace_l, "{") \
77 m(D_brace_r, "}") \
78 m(D_brckt_l, "[") \
79 m(D_brckt_r, "]") \
80 m(D_curly_l, "⦃") \
81 m(D_curly_r, "⦄") \
82 m(D_paren_l, "(") \
83 m(D_paren_r, ")") \
84 m(D_quote_l, "«") \
85 m(D_quote_r, "»") \
86 /* further tokens */ \
87 m(T_arrow, "→") \
88 m(T_fat_arrow, "=>") \
89 m(T_assign, "=") \
90 m(T_at, "@") \
91 m(T_bot, "⊥") \
92 m(T_top, "⊤") \
93 m(T_box, "□") \
94 m(T_colon, ":") \
95 m(T_comma, ",") \
96 m(T_dollar, "$") \
97 m(T_dot, ".") \
98 m(T_extract, "#") \
99 m(T_lm, "λ") \
100 m(T_semicolon, ";") \
101 m(T_star, "*") \
102 m(T_union, "∪") \
103
104#define MIM_SUBST(m) \
105 m("lm", T_lm ) \
106 m("bot", T_bot ) \
107 m("top", T_top ) \
108 m("insert", K_ins ) \
109
110class Tok {
111public:
112 /// @name Tag
113 ///@{
114 enum class Tag {
116#define CODE(t, str) t,
118#undef CODE
119 };
120
121 static const char* tag2str(Tok::Tag);
122 static constexpr Tok::Tag delim_l2r(Tag tag) { return Tok::Tag(int(tag) + 1); }
123 ///@}
124
125 // clang-format on
126
127 Tok() {}
129 : loc_(loc)
130 , tag_(tag) {}
131 Tok(Loc loc, char8_t c)
132 : loc_(loc)
133 , tag_(Tag::L_c)
134 , c_(c) {}
135 Tok(Loc loc, uint64_t u)
136 : loc_(loc)
137 , tag_(Tag::L_u)
138 , u_(u) {}
139 Tok(Loc loc, int64_t s)
140 : loc_(loc)
141 , tag_(Tag::L_s)
142 , u_(std::bit_cast<uint64_t>(s)) {}
143 Tok(Loc loc, double d)
144 : loc_(loc)
145 , tag_(Tag::L_f)
146 , u_(std::bit_cast<uint64_t>(d)) {}
147 Tok(Loc loc, const Lit* i)
148 : loc_(loc)
149 , tag_(Tag::L_i)
150 , i_(i) {}
151 Tok(Loc loc, Tag tag, Sym sym)
152 : loc_(loc)
153 , tag_(tag)
154 , sym_(sym) {
155 assert(tag == Tag::M_id || tag == Tag::M_anx || tag == Tag::L_str);
156 }
157
158 bool isa(Tag tag) const { return tag == tag_; }
159 Tag tag() const { return tag_; }
160 Dbg dbg() const { return {loc(), sym()}; }
161 Loc loc() const { return loc_; }
162 explicit operator bool() const { return tag_ != Tag::Nil; }
163 // clang-format off
164 const Lit* lit_i() const { assert(isa(Tag::L_i)); return i_; }
165 char8_t lit_c() const { assert(isa(Tag::L_c)); return c_; }
166 uint64_t lit_u() const { assert(isa(Tag::L_u ) || isa(Tag::L_s ) || isa(Tag::L_f )); return u_; }
167 Sym sym() const { assert(isa(Tag::M_anx) || isa(Tag::M_id) || isa(Tag::L_str)); return sym_; }
168 // clang-format on
169 std::string str() const;
170
171 friend std::ostream& operator<<(std::ostream&, Tok);
172 friend std::ostream& operator<<(std::ostream& os, Tok::Tag tag) { return os << tag2str(tag); }
173
174private:
175 Loc loc_;
176 Tag tag_ = Tag::Nil;
177 union {
178 Sym sym_;
179 uint64_t u_;
180 char8_t c_;
181 const Lit* i_;
182 };
183};
184
185} // namespace ast
186} // namespace mim
Base class for all Defs.
Definition def.h:203
Tok(Loc loc, uint64_t u)
Definition tok.h:135
friend std::ostream & operator<<(std::ostream &, Tok)
Definition tok.cpp:30
const Lit * lit_i() const
Definition tok.h:164
Sym sym() const
Definition tok.h:167
Tok(Loc loc, Tag tag)
Definition tok.h:128
Tok(Loc loc, Tag tag, Sym sym)
Definition tok.h:151
Tok(Loc loc, const Lit *i)
Definition tok.h:147
Dbg dbg() const
Definition tok.h:160
Loc loc() const
Definition tok.h:161
uint64_t lit_u() const
Definition tok.h:166
bool isa(Tag tag) const
Definition tok.h:158
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:122
Tok(Loc loc, int64_t s)
Definition tok.h:139
Tok(Loc loc, char8_t c)
Definition tok.h:131
friend std::ostream & operator<<(std::ostream &os, Tok::Tag tag)
Definition tok.h:172
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:143
CODE(t, str)
Definition tok.h:116
Tag tag() const
Definition tok.h:159
char8_t lit_c() const
Definition tok.h:165
Definition ast.h:14
constexpr auto Num_Keys
Definition tok.h:58
Tok::Tag Tag
Definition bind.cpp:7
Definition ast.h:14
CODE(node, name, _)
Definition def.h:84
Definition span.h:114
#define MIM_TOK(m)
Definition tok.h:61
#define MIM_KEY(m)
Definition tok.h:13