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
107#define MIM_SUBST(m) \
108 m("lm", T_lm ) \
109 m("bot", T_bot ) \
110 m("top", T_top ) \
111 m("insert", K_ins ) \
112
113class Tok {
114public:
115 /// @name Tag
116 ///@{
117 enum class Tag {
119#define CODE(t, str) t,
121#undef CODE
122 };
123
124 static const char* tag2str(Tok::Tag);
125 static constexpr Tok::Tag delim_l2r(Tag tag) { return Tok::Tag(int(tag) + 1); }
126 ///@}
127
128 // clang-format on
129
130 Tok() {}
132 : loc_(loc)
133 , tag_(tag) {}
134 Tok(Loc loc, char8_t c)
135 : loc_(loc)
136 , tag_(Tag::L_c)
137 , c_(c) {}
138 Tok(Loc loc, uint64_t u)
139 : loc_(loc)
140 , tag_(Tag::L_u)
141 , u_(u) {}
142 Tok(Loc loc, int64_t s)
143 : loc_(loc)
144 , tag_(Tag::L_s)
145 , u_(std::bit_cast<uint64_t>(s)) {}
146 Tok(Loc loc, double d)
147 : loc_(loc)
148 , tag_(Tag::L_f)
149 , u_(std::bit_cast<uint64_t>(d)) {}
150 Tok(Loc loc, const Lit* i)
151 : loc_(loc)
152 , tag_(Tag::L_i)
153 , i_(i) {}
154 Tok(Loc loc, Tag tag, Sym sym)
155 : loc_(loc)
156 , tag_(tag)
157 , sym_(sym) {
158 assert(tag == Tag::M_id || tag == Tag::M_anx || tag == Tag::L_str);
159 }
160
161 bool isa(Tag tag) const { return tag == tag_; }
162 Tag tag() const { return tag_; }
163 Dbg dbg() const { return {loc(), sym()}; }
164 Loc loc() const { return loc_; }
165 explicit operator bool() const { return tag_ != Tag::Nil; }
166 // clang-format off
167 const Lit* lit_i() const { assert(isa(Tag::L_i)); return i_; }
168 char8_t lit_c() const { assert(isa(Tag::L_c)); return c_; }
169 uint64_t lit_u() const { assert(isa(Tag::L_u ) || isa(Tag::L_s ) || isa(Tag::L_f )); return u_; }
170 Sym sym() const { assert(isa(Tag::M_anx) || isa(Tag::M_id) || isa(Tag::L_str)); return sym_; }
171 // clang-format on
172 std::string str() const;
173
174 friend std::ostream& operator<<(std::ostream&, Tok);
175 friend std::ostream& operator<<(std::ostream& os, Tok::Tag tag) { return os << tag2str(tag); }
176
177private:
178 Loc loc_;
179 Tag tag_ = Tag::Nil;
180 union {
181 Sym sym_;
182 uint64_t u_;
183 char8_t c_;
184 const Lit* i_;
185 };
186};
187
188} // namespace ast
189} // namespace mim
Base class for all Defs.
Definition def.h:251
Tok(Loc loc, uint64_t u)
Definition tok.h:138
friend std::ostream & operator<<(std::ostream &, Tok)
Definition tok.cpp:30
const Lit * lit_i() const
Definition tok.h:167
Sym sym() const
Definition tok.h:170
Tok(Loc loc, Tag tag)
Definition tok.h:131
Tok(Loc loc, Tag tag, Sym sym)
Definition tok.h:154
Tok(Loc loc, const Lit *i)
Definition tok.h:150
Dbg dbg() const
Definition tok.h:163
Loc loc() const
Definition tok.h:164
uint64_t lit_u() const
Definition tok.h:169
bool isa(Tag tag) const
Definition tok.h:161
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:125
Tok(Loc loc, int64_t s)
Definition tok.h:142
Tok(Loc loc, char8_t c)
Definition tok.h:134
friend std::ostream & operator<<(std::ostream &os, Tok::Tag tag)
Definition tok.h:175
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:146
CODE(t, str)
Definition tok.h:119
Tag tag() const
Definition tok.h:162
char8_t lit_c() const
Definition tok.h:168
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