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_ax, ".ax" ) \
19 m(K_let, ".let" ) \
20 m(K_rec, ".rec" ) \
21 m(K_ret, ".ret" ) \
22 m(K_where, ".where" ) \
23 m(K_end, ".end" ) \
24 m(K_Nat, ".Nat" ) \
25 m(K_Idx, ".Idx" ) \
26 m(K_extern, ".extern") \
27 m(K_Type, ".Type" ) \
28 m(K_Univ, ".Univ" ) \
29 m(K_Cn, ".Cn" ) \
30 m(K_Fn, ".Fn" ) \
31 m(K_con, ".con" ) \
32 m(K_fun, ".fun" ) \
33 m(K_lam, ".lam" ) \
34 m(K_ccon, ".ccon" ) \
35 m(K_cfun, ".cfun" ) \
36 m(K_cn, ".cn" ) \
37 m(K_fn, ".fn" ) \
38 m(K_ff, ".ff" ) \
39 m(K_tt, ".tt" ) \
40 m(K_ins, ".ins" ) \
41 m(K_i1, ".i1" ) \
42 m(K_i8, ".i8" ) \
43 m(K_i16, ".i16" ) \
44 m(K_i32, ".i32" ) \
45 m(K_i64, ".i64" ) \
46 m(K_Bool, ".Bool" ) \
47 m(K_I1, ".I1" ) \
48 m(K_I8, ".I8" ) \
49 m(K_I16, ".I16" ) \
50 m(K_I32, ".I32" ) \
51 m(K_I64, ".I64" ) \
52
53#define CODE(t, str) + size_t(1)
54constexpr auto Num_Keys = size_t(0) MIM_KEY(CODE);
55#undef CODE
56
57#define MIM_TOK(m) \
58 m(EoF, "<end of file>" ) \
59 /* literals */ \
60 m(L_s, "<signed integer literal>") \
61 m(L_u, "<integer literal>" ) \
62 m(L_i, "<index literal>" ) \
63 m(L_f, "<floating-point literal>") \
64 m(L_c, "<char literal>" ) \
65 m(L_str, "<string literal>" ) \
66 /* misc */ \
67 m(M_id, "<identifier>" ) \
68 m(M_anx, "<annex name>" ) \
69 /* delimiters */ \
70 m(D_angle_l, "‹") \
71 m(D_angle_r, "›") \
72 m(D_brace_l, "{") \
73 m(D_brace_r, "}") \
74 m(D_brckt_l, "[") \
75 m(D_brckt_r, "]") \
76 m(D_paren_l, "(") \
77 m(D_paren_r, ")") \
78 m(D_quote_l, "«") \
79 m(D_quote_r, "»") \
80 /* further tokens */ \
81 m(T_Pi, "Π") \
82 m(T_arrow, "→") \
83 m(T_assign, "=") \
84 m(T_at, "@") \
85 m(T_backtick, "`") \
86 m(T_bot, "⊥") \
87 m(T_top, "⊤") \
88 m(T_box, "□") \
89 m(T_colon, ":") \
90 m(T_colon_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
105/// @name Precedence
106///@{
107enum class Prec {
108 Err,
109 Bot,
110 Where,
111 Arrow,
112 Pi,
113 App,
114 Extract,
115 Lit,
116};
117
118inline constexpr bool is_rassoc(Prec p) { return p == Prec::Arrow; }
119///@}
120
121class Tok {
122public:
123 /// @name Tag
124 ///@{
125 enum class Tag {
126 Nil,
127#define CODE(t, str) t,
129#undef CODE
130 };
131
132 static const char* tag2str(Tok::Tag);
133 static constexpr Tok::Tag delim_l2r(Tag tag) { return Tok::Tag(int(tag) + 1); }
134 ///@}
135
136 // clang-format on
137
138 Tok() {}
140 : loc_(loc)
141 , tag_(tag) {}
142 Tok(Loc loc, char8_t c)
143 : loc_(loc)
144 , tag_(Tag::L_c)
145 , c_(c) {}
146 Tok(Loc loc, uint64_t u)
147 : loc_(loc)
148 , tag_(Tag::L_u)
149 , u_(u) {}
150 Tok(Loc loc, int64_t s)
151 : loc_(loc)
152 , tag_(Tag::L_s)
153 , u_(std::bit_cast<uint64_t>(s)) {}
154 Tok(Loc loc, double d)
155 : loc_(loc)
156 , tag_(Tag::L_f)
157 , u_(std::bit_cast<uint64_t>(d)) {}
158 Tok(Loc loc, const Lit* i)
159 : loc_(loc)
160 , tag_(Tag::L_i)
161 , i_(i) {}
162 Tok(Loc loc, Tag tag, Sym sym)
163 : loc_(loc)
164 , tag_(tag)
165 , sym_(sym) {
166 assert(tag == Tag::M_id || tag == Tag::M_anx || tag == Tag::L_str);
167 }
168
169 bool isa(Tag tag) const { return tag == tag_; }
170 Tag tag() const { return tag_; }
171 Dbg dbg() const { return {loc(), sym()}; }
172 Loc loc() const { return loc_; }
173 explicit operator bool() const { return tag_ != Tag::Nil; }
174 // clang-format off
175 const Lit* lit_i() const { assert(isa(Tag::L_i)); return i_; }
176 char8_t lit_c() const { assert(isa(Tag::L_c)); return c_; }
177 uint64_t lit_u() const { assert(isa(Tag::L_u ) || isa(Tag::L_s ) || isa(Tag::L_f )); return u_; }
178 Sym sym() const { assert(isa(Tag::M_anx) || isa(Tag::M_id) || isa(Tag::L_str)); return sym_; }
179 // clang-format on
180 std::string str() const;
181
182 friend std::ostream& operator<<(std::ostream&, Tok);
183 friend std::ostream& operator<<(std::ostream& os, Tok::Tag tag) { return os << tag2str(tag); }
184
185private:
186 Loc loc_;
187 Tag tag_ = Tag::Nil;
188 union {
189 Sym sym_;
190 uint64_t u_;
191 char8_t c_;
192 const Lit* i_;
193 };
194};
195
196} // namespace ast
197} // namespace mim
Tok(Loc loc, uint64_t u)
Definition tok.h:146
friend std::ostream & operator<<(std::ostream &, Tok)
Definition tok.cpp:30
const Lit * lit_i() const
Definition tok.h:175
Sym sym() const
Definition tok.h:178
Tok(Loc loc, Tag tag)
Definition tok.h:139
Tok(Loc loc, Tag tag, Sym sym)
Definition tok.h:162
Tok(Loc loc, const Lit *i)
Definition tok.h:158
Dbg dbg() const
Definition tok.h:171
Loc loc() const
Definition tok.h:172
uint64_t lit_u() const
Definition tok.h:177
bool isa(Tag tag) const
Definition tok.h:169
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:133
Tok(Loc loc, int64_t s)
Definition tok.h:150
Tok(Loc loc, char8_t c)
Definition tok.h:142
friend std::ostream & operator<<(std::ostream &os, Tok::Tag tag)
Definition tok.h:183
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:154
Tag tag() const
Definition tok.h:170
char8_t lit_c() const
Definition tok.h:176
@ Lit
Definition def.h:39
constexpr bool is_rassoc(Prec p)
Definition tok.h:118
constexpr auto Num_Keys
Definition tok.h:54
Definition cfg.h:11
Definition span.h:104
#define MIM_TOK(m)
Definition tok.h:57
#define CODE(t, str)
Definition tok.h:53
#define MIM_KEY(m)
Definition tok.h:13