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 <fe/assert.h>
4
5#include "mim/util/dbg.h"
6
7namespace mim {
8
9class Def;
10class Lit;
11
12namespace ast {
13
14/// @name Precedence Table
15/// X-macro listing all expression precedences from lowest to highest.
16/// Each entry is `m(name, assoc)` where @p assoc is `L`, `R`, or `N`.
17///@{
18// clang-format off
19#define MIM_PREC(m) \
20 m(Err, N) \
21 m(Bot, N) \
22 m(Where, L) \
23 m(Arrow, R) \
24 m(Pi, N) \
25 m(Inj, R) \
26 m(App, L) \
27 m(Union, L) \
28 m(Extract, L) \
29 m(Lit, N)
30// clang-format on
31///@}
32
33/// Associativity of an infix expression.
34enum class Assoc { N, L, R };
35
36/// Expression precedences used by the parser and the dumper; ordered low to high.
37enum class Prec {
38#define CODE(name, ...) name,
40#undef CODE
41};
42
43/// Associativity of precedence level @p p.
44constexpr Assoc prec_assoc(Prec p) {
45 switch (p) {
46#define CODE(name, assoc) case Prec::name: return Assoc::assoc;
48#undef CODE
49 }
50 fe::unreachable();
51}
52
53constexpr bool is_rassoc(Prec p) { return prec_assoc(p) == Assoc::R; }
54constexpr bool is_lassoc(Prec p) { return prec_assoc(p) == Assoc::L; }
55
56/// Should a Pratt parser reduce when the current binding power is @p curr
57/// and the infix operator has precedence @p op?
58constexpr bool should_reduce(Prec curr, Prec op) { return is_rassoc(op) ? curr > op : curr >= op; }
59
60// clang-format off
61#define MIM_KEY(m) \
62 m(K_Bool, "Bool" ) \
63 m(K_Cn, "Cn" ) \
64 m(K_Fn, "Fn" ) \
65 m(K_I1, "I1" ) \
66 m(K_I16, "I16" ) \
67 m(K_I32, "I32" ) \
68 m(K_I64, "I64" ) \
69 m(K_I8, "I8" ) \
70 m(K_Idx, "Idx" ) \
71 m(K_Nat, "Nat" ) \
72 m(K_Type, "Type" ) \
73 m(K_Univ, "Univ" ) \
74 m(K_and, "and" ) \
75 m(K_as, "as" ) \
76 m(K_axm, "axm" ) \
77 m(K_ccon, "ccon" ) \
78 m(K_cfun, "cfun" ) \
79 m(K_cn, "cn" ) \
80 m(K_con, "con" ) \
81 m(K_end, "end" ) \
82 m(K_extern, "extern") \
83 m(K_ff, "ff" ) \
84 m(K_fn, "fn" ) \
85 m(K_fun, "fun" ) \
86 m(K_i1, "i1" ) \
87 m(K_i16, "i16" ) \
88 m(K_i32, "i32" ) \
89 m(K_i64, "i64" ) \
90 m(K_i8, "i8" ) \
91 m(K_import, "import") \
92 m(K_inj, "inj" ) \
93 m(K_ins, "ins" ) \
94 m(K_lam, "lam" ) \
95 m(K_let, "let" ) \
96 m(K_match, "match" ) \
97 m(K_module, "module") \
98 m(K_norm, "norm" ) \
99 m(K_plugin, "plugin") \
100 m(K_rec, "rec" ) \
101 m(K_ret, "ret" ) \
102 m(K_rule, "rule" ) \
103 m(K_Rule, "Rule" ) \
104 m(K_tt, "tt" ) \
105 m(K_when, "when" ) \
106 m(K_where, "where" ) \
107 m(K_with, "with" ) \
108
109#define CODE(t, str) + size_t(1)
110constexpr auto Num_Keys = size_t(0) MIM_KEY(CODE);
111#undef CODE
112
113#define MIM_TOK(m) \
114 m(EoF, "<end of file>" ) \
115 /* literals */ \
116 m(L_s, "<signed integer literal>") \
117 m(L_u, "<integer literal>" ) \
118 m(L_i, "<index literal>" ) \
119 m(L_f, "<floating-point literal>") \
120 m(L_c, "<char literal>" ) \
121 m(L_str, "<string literal>" ) \
122 /* misc */ \
123 m(M_id, "<identifier>" ) \
124 m(M_anx, "<annex name>" ) \
125 /* delimiters */ \
126 m(D_angle_l, "‹") \
127 m(D_angle_r, "›") \
128 m(D_brace_l, "{") \
129 m(D_brace_r, "}") \
130 m(D_brckt_l, "[") \
131 m(D_brckt_r, "]") \
132 m(D_curly_l, "⦃") \
133 m(D_curly_r, "⦄") \
134 m(D_paren_l, "(") \
135 m(D_paren_r, ")") \
136 m(D_quote_l, "«") \
137 m(D_quote_r, "»") \
138 /* further tokens */ \
139 m(T_arrow, "→") \
140 m(T_fat_arrow, "=>") \
141 m(T_assign, "=") \
142 m(T_at, "@") \
143 m(T_bot, "⊥") \
144 m(T_top, "⊤") \
145 m(T_box, "□") \
146 m(T_colon, ":") \
147 m(T_comma, ",") \
148 m(T_dollar, "$") \
149 m(T_dot, ".") \
150 m(T_extract, "#") \
151 m(T_lm, "λ") \
152 m(T_semicolon, ";") \
153 m(T_star, "*") \
154 m(T_union, "∪") \
155 m(T_pipe, "|") \
156
157#define MIM_SUBST(m) \
158 m("lm", T_lm ) \
159 m("bot", T_bot ) \
160 m("top", T_top ) \
161 m("insert", K_ins ) \
162
163class Tok {
164public:
165 /// @name Tag
166 ///@{
167 enum class Tag {
169#define CODE(t, str) t,
171#undef CODE
172 };
173
174 static const char* tag2str(Tok::Tag);
175 static constexpr Tok::Tag delim_l2r(Tag tag) { return Tok::Tag(int(tag) + 1); }
176 ///@}
177
178 // clang-format on
179
180 Tok() {}
182 : loc_(loc)
183 , tag_(tag) {}
184 Tok(Loc loc, char8_t c)
185 : loc_(loc)
186 , tag_(Tag::L_c)
187 , c_(c) {}
188 Tok(Loc loc, uint64_t u)
189 : loc_(loc)
190 , tag_(Tag::L_u)
191 , u_(u) {}
192 Tok(Loc loc, int64_t s)
193 : loc_(loc)
194 , tag_(Tag::L_s)
195 , u_(std::bit_cast<uint64_t>(s)) {}
196 Tok(Loc loc, double d)
197 : loc_(loc)
198 , tag_(Tag::L_f)
199 , u_(std::bit_cast<uint64_t>(d)) {}
200 Tok(Loc loc, const Lit* i)
201 : loc_(loc)
202 , tag_(Tag::L_i)
203 , i_(i) {}
204 Tok(Loc loc, Tag tag, Sym sym)
205 : loc_(loc)
206 , tag_(tag)
207 , sym_(sym) {
208 assert(tag == Tag::M_id || tag == Tag::M_anx || tag == Tag::L_str);
209 }
210
211 bool isa(Tag tag) const { return tag == tag_; }
212 Tag tag() const { return tag_; }
213 Dbg dbg() const { return {loc(), sym()}; }
214 Loc loc() const { return loc_; }
215 explicit operator bool() const { return tag_ != Tag::Nil; }
216 // clang-format off
217 const Lit* lit_i() const { assert(isa(Tag::L_i)); return i_; }
218 char8_t lit_c() const { assert(isa(Tag::L_c)); return c_; }
219 uint64_t lit_u() const { assert(isa(Tag::L_u ) || isa(Tag::L_s ) || isa(Tag::L_f )); return u_; }
220 Sym sym() const { assert(isa(Tag::M_anx) || isa(Tag::M_id) || isa(Tag::L_str)); return sym_; }
221 // clang-format on
222 std::string str() const;
223
224 friend std::ostream& operator<<(std::ostream&, Tok);
225 friend std::ostream& operator<<(std::ostream& os, Tok::Tag tag) { return os << tag2str(tag); }
226
227private:
228 Loc loc_;
229 Tag tag_ = Tag::Nil;
230 union {
231 Sym sym_;
232 uint64_t u_;
233 char8_t c_;
234 const Lit* i_;
235 };
236};
237
238} // namespace ast
239} // namespace mim
Base class for all Defs.
Definition def.h:252
Tok(Loc loc, uint64_t u)
Definition tok.h:188
friend std::ostream & operator<<(std::ostream &, Tok)
Definition tok.cpp:30
const Lit * lit_i() const
Definition tok.h:217
Sym sym() const
Definition tok.h:220
Tok(Loc loc, Tag tag)
Definition tok.h:181
Tok(Loc loc, Tag tag, Sym sym)
Definition tok.h:204
Tok(Loc loc, const Lit *i)
Definition tok.h:200
Dbg dbg() const
Definition tok.h:213
Loc loc() const
Definition tok.h:214
uint64_t lit_u() const
Definition tok.h:219
bool isa(Tag tag) const
Definition tok.h:211
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:175
Tok(Loc loc, int64_t s)
Definition tok.h:192
Tok(Loc loc, char8_t c)
Definition tok.h:184
friend std::ostream & operator<<(std::ostream &os, Tok::Tag tag)
Definition tok.h:225
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:196
Tag tag() const
Definition tok.h:212
char8_t lit_c() const
Definition tok.h:218
Definition ast.h:14
constexpr bool is_lassoc(Prec p)
Definition tok.h:54
Assoc
Associativity of an infix expression.
Definition tok.h:34
constexpr bool is_rassoc(Prec p)
Definition tok.h:53
constexpr bool should_reduce(Prec curr, Prec op)
Should a Pratt parser reduce when the current binding power is curr and the infix operator has preced...
Definition tok.h:58
constexpr auto Num_Keys
Definition tok.h:110
Tok::Tag Tag
Definition bind.cpp:7
constexpr Assoc prec_assoc(Prec p)
Associativity of precedence level p.
Definition tok.h:44
Prec
Expression precedences used by the parser and the dumper; ordered low to high.
Definition tok.h:37
Definition ast.h:14
Definition span.h:122
#define MIM_PREC(m)
Definition tok.h:19
#define CODE(name,...)
Definition tok.h:38
#define MIM_TOK(m)
Definition tok.h:113
#define MIM_KEY(m)
Definition tok.h:61