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, "axm" ) \
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_arrow, "→") \
82 m(T_assign, "=") \
83 m(T_at, "@") \
84 m(T_backtick, "`") \
85 m(T_bot, "⊥") \
86 m(T_top, "⊤") \
87 m(T_box, "□") \
88 m(T_colon, ":") \
89 m(T_colon_colon,"::") \
90 m(T_comma, ",") \
91 m(T_dollar, "$") \
92 m(T_dot, ".") \
93 m(T_extract, "#") \
94 m(T_lm, "λ") \
95 m(T_semicolon, ";") \
96 m(T_star, "*") \
97
98#define MIM_SUBST(m) \
99 m("lm", T_lm ) \
100 m("bot", T_bot ) \
101 m("top", T_top ) \
102 m("insert", K_ins ) \
103
104/// @name Precedence
105///@{
106enum class Prec {
107 Err,
108 Bot,
109 Where,
110 Arrow,
111 Pi,
112 App,
113 Extract,
114 Lit,
115};
116
117inline constexpr bool is_rassoc(Prec p) { return p == Prec::Arrow; }
118///@}
119
120class Tok {
121public:
122 /// @name Tag
123 ///@{
124 enum class Tag {
125 Nil,
126#define CODE(t, str) t,
128#undef CODE
129 };
130
131 static const char* tag2str(Tok::Tag);
132 static constexpr Tok::Tag delim_l2r(Tag tag) { return Tok::Tag(int(tag) + 1); }
133 ///@}
134
135 // clang-format on
136
137 Tok() {}
139 : loc_(loc)
140 , tag_(tag) {}
141 Tok(Loc loc, char8_t c)
142 : loc_(loc)
143 , tag_(Tag::L_c)
144 , c_(c) {}
145 Tok(Loc loc, uint64_t u)
146 : loc_(loc)
147 , tag_(Tag::L_u)
148 , u_(u) {}
149 Tok(Loc loc, int64_t s)
150 : loc_(loc)
151 , tag_(Tag::L_s)
152 , u_(std::bit_cast<uint64_t>(s)) {}
153 Tok(Loc loc, double d)
154 : loc_(loc)
155 , tag_(Tag::L_f)
156 , u_(std::bit_cast<uint64_t>(d)) {}
157 Tok(Loc loc, const Lit* i)
158 : loc_(loc)
159 , tag_(Tag::L_i)
160 , i_(i) {}
161 Tok(Loc loc, Tag tag, Sym sym)
162 : loc_(loc)
163 , tag_(tag)
164 , sym_(sym) {
165 assert(tag == Tag::M_id || tag == Tag::M_anx || tag == Tag::L_str);
166 }
167
168 bool isa(Tag tag) const { return tag == tag_; }
169 Tag tag() const { return tag_; }
170 Dbg dbg() const { return {loc(), sym()}; }
171 Loc loc() const { return loc_; }
172 explicit operator bool() const { return tag_ != Tag::Nil; }
173 // clang-format off
174 const Lit* lit_i() const { assert(isa(Tag::L_i)); return i_; }
175 char8_t lit_c() const { assert(isa(Tag::L_c)); return c_; }
176 uint64_t lit_u() const { assert(isa(Tag::L_u ) || isa(Tag::L_s ) || isa(Tag::L_f )); return u_; }
177 Sym sym() const { assert(isa(Tag::M_anx) || isa(Tag::M_id) || isa(Tag::L_str)); return sym_; }
178 // clang-format on
179 std::string str() const;
180
181 friend std::ostream& operator<<(std::ostream&, Tok);
182 friend std::ostream& operator<<(std::ostream& os, Tok::Tag tag) { return os << tag2str(tag); }
183
184private:
185 Loc loc_;
186 Tag tag_ = Tag::Nil;
187 union {
188 Sym sym_;
189 uint64_t u_;
190 char8_t c_;
191 const Lit* i_;
192 };
193};
194
195} // namespace ast
196} // namespace mim
Tok(Loc loc, uint64_t u)
Definition tok.h:145
friend std::ostream & operator<<(std::ostream &, Tok)
Definition tok.cpp:30
const Lit * lit_i() const
Definition tok.h:174
Sym sym() const
Definition tok.h:177
Tok(Loc loc, Tag tag)
Definition tok.h:138
Tok(Loc loc, Tag tag, Sym sym)
Definition tok.h:161
Tok(Loc loc, const Lit *i)
Definition tok.h:157
Dbg dbg() const
Definition tok.h:170
Loc loc() const
Definition tok.h:171
uint64_t lit_u() const
Definition tok.h:176
bool isa(Tag tag) const
Definition tok.h:168
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:132
Tok(Loc loc, int64_t s)
Definition tok.h:149
Tok(Loc loc, char8_t c)
Definition tok.h:141
friend std::ostream & operator<<(std::ostream &os, Tok::Tag tag)
Definition tok.h:182
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:153
Tag tag() const
Definition tok.h:169
char8_t lit_c() const
Definition tok.h:175
@ Lit
Definition def.h:40
constexpr bool is_rassoc(Prec p)
Definition tok.h:117
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