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