Thorin 1.9.0
The Higher ORder 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 "thorin/util/dbg.h"
6#include "thorin/util/types.h"
7
8namespace thorin {
9
10class Def;
11class Lit;
12
13// clang-format off
14#define THORIN_KEY(m) \
15 m(K_module, ".module") \
16 m(K_import, ".import") \
17 m(K_plugin, ".plugin") \
18 m(K_ax, ".ax" ) \
19 m(K_def, ".def" ) \
20 m(K_let, ".let" ) \
21 m(K_ret, ".ret" ) \
22 m(K_Nat, ".Nat" ) \
23 m(K_Idx, ".Idx" ) \
24 m(K_extern, ".extern") \
25 m(K_Sigma, ".Sigma" ) \
26 m(K_Type, ".Type" ) \
27 m(K_Univ, ".Univ" ) \
28 m(K_Cn, ".Cn" ) \
29 m(K_Fn, ".Fn" ) \
30 m(K_Pi, ".Pi" ) \
31 m(K_con, ".con" ) \
32 m(K_fun, ".fun" ) \
33 m(K_lam, ".lam" ) \
34 m(K_cn, ".cn" ) \
35 m(K_fn, ".fn" ) \
36 m(K_ff, ".ff" ) \
37 m(K_tt, ".tt" ) \
38 m(K_ins, ".ins" ) \
39 m(K_i1, ".i1" ) \
40 m(K_i8, ".i8" ) \
41 m(K_i16, ".i16" ) \
42 m(K_i32, ".i32" ) \
43 m(K_i64, ".i64" ) \
44 m(K_Bool, ".Bool" ) \
45 m(K_I1, ".I1" ) \
46 m(K_I8, ".I8" ) \
47 m(K_I16, ".I16" ) \
48 m(K_I32, ".I32" ) \
49 m(K_I64, ".I64" ) \
50
51#define CODE(t, str) + size_t(1)
52constexpr auto Num_Keys = size_t(0) THORIN_KEY(CODE);
53#undef CODE
54
55#define THORIN_TOK(m) \
56 m(EoF, "<end of file>") \
57 /* literals */ \
58 m(L_s, "<signed integer literal>") \
59 m(L_u, "<integer literal>" ) \
60 m(L_i, "<index literal>" ) \
61 m(L_f, "<floating-point literal>") \
62 m(L_c, "<char literal>" ) \
63 /* misc */ \
64 m(M_id, "<identifier>") \
65 m(M_anx, "<annex name>") \
66 m(M_str, "<string>" ) \
67 /* delimiters */ \
68 m(D_angle_l, "‹") \
69 m(D_angle_r, "›") \
70 m(D_brace_l, "{") \
71 m(D_brace_r, "}") \
72 m(D_brckt_l, "[") \
73 m(D_brckt_r, "]") \
74 m(D_paren_l, "(") \
75 m(D_paren_r, ")") \
76 m(D_quote_l, "«") \
77 m(D_quote_r, "»") \
78 /* further tokens */ \
79 m(T_Pi, "Π") \
80 m(T_arrow, "→") \
81 m(T_assign, "=") \
82 m(T_at, "@") \
83 m(T_backtick, "`") \
84 m(T_bang, "!") \
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 THORIN_SUBST(m) \
99 m(".lm", T_lm ) \
100 m(".bot", T_bot ) \
101 m(".top", T_top ) \
102 m(".insert", K_ins ) \
103
104#define THORIN_PREC(m) \
105 /* left prec, right */ \
106 m(Nil, Bot, Nil ) \
107 m(Nil, Nil, Nil ) \
108 m(Lam, Arrow, Arrow ) \
109 m(Nil, Lam, Pi ) \
110 m(Nil, Pi, App ) \
111 m(App, App, Extract ) \
112 m(Extract, Extract, Lit ) \
113 m(Nil, Lit, Lit ) \
114
115class Tok {
116public:
117 /// @name Precedence
118 ///@{
119 enum class Prec {
120#define CODE(l, p, r) p,
122#undef CODE
123 };
124
125 static constexpr std::array<Prec, 2> prec(Prec p) {
126 switch (p) {
127#define CODE(l, p, r) \
128 case Prec::p: return {Prec::l, Prec::r};
129 default: fe::unreachable();
131#undef CODE
132 }
133 }
134 static Prec prec(const Def*);
135 ///@}
136
137 /// @name Tag
138 ///@{
139 enum class Tag {
140 Nil,
141#define CODE(t, str) t,
143#undef CODE
144 };
145
146 static std::string_view tag2str(Tok::Tag);
147 static constexpr Tok::Tag delim_l2r(Tag tag) { return Tok::Tag(int(tag) + 1); }
148 ///@}
149
150 // clang-format on
151
152 Tok() {}
154 : loc_(loc)
155 , tag_(tag) {}
156 Tok(Loc loc, char8_t c)
157 : loc_(loc)
158 , tag_(Tag::L_c)
159 , c_(c) {}
160 Tok(Loc loc, u64 u)
161 : loc_(loc)
162 , tag_(Tag::L_u)
163 , u_(u) {}
164 Tok(Loc loc, s64 s)
165 : loc_(loc)
166 , tag_(Tag::L_s)
167 , u_(std::bit_cast<u64>(s)) {}
168 Tok(Loc loc, f64 f)
169 : loc_(loc)
170 , tag_(Tag::L_f)
171 , u_(std::bit_cast<u64>(f)) {}
172 Tok(Loc loc, const Lit* i)
173 : loc_(loc)
174 , tag_(Tag::L_i)
175 , i_(i) {}
176 Tok(Loc loc, Tag tag, Sym sym)
177 : loc_(loc)
178 , tag_(tag)
179 , sym_(sym) {
180 assert(tag == Tag::M_id || tag == Tag::M_anx || tag == Tag::M_str);
181 }
182
183 bool isa(Tag tag) const { return tag == tag_; }
184 Tag tag() const { return tag_; }
185 Dbg dbg() const { return {loc(), sym()}; }
186 Loc loc() const { return loc_; }
187 explicit operator bool() const { return tag_ != Tag::Nil; }
188 // clang-format off
189 const Lit* lit_i() const { assert(isa(Tag::L_i)); return i_; }
190 char8_t lit_c() const { assert(isa(Tag::L_c)); return c_; }
191 u64 lit_u() const { assert(isa(Tag::L_u ) || isa(Tag::L_s ) || isa(Tag::L_f )); return u_; }
192 Sym sym() const { assert(isa(Tag::M_anx) || isa(Tag::M_id) || isa(Tag::M_str)); return sym_; }
193 // clang-format on
194 friend std::ostream& operator<<(std::ostream&, Tok);
195
196private:
197 Loc loc_;
198 Tag tag_ = Tag::Nil;
199 union {
200 Sym sym_;
201 u64 u_;
202 char8_t c_;
203 const Lit* i_;
204 };
205};
206
207} // namespace thorin
Base class for all Defs.
Definition def.h:222
Dbg dbg() const
Definition tok.h:185
Tok(Loc loc, const Lit *i)
Definition tok.h:172
Tok(Loc loc, Tag tag)
Definition tok.h:153
Tok(Loc loc, s64 s)
Definition tok.h:164
Sym sym() const
Definition tok.h:192
u64 lit_u() const
Definition tok.h:191
Tok(Loc loc, char8_t c)
Definition tok.h:156
bool isa(Tag tag) const
Definition tok.h:183
friend std::ostream & operator<<(std::ostream &, Tok)
Definition tok.cpp:24
Loc loc() const
Definition tok.h:186
static std::string_view tag2str(Tok::Tag)
Definition tok.cpp:10
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:147
const Lit * lit_i() const
Definition tok.h:189
Tok(Loc loc, u64 u)
Definition tok.h:160
Tok(Loc loc, f64 f)
Definition tok.h:168
Tok(Loc loc, Tag tag, Sym sym)
Definition tok.h:176
static constexpr std::array< Prec, 2 > prec(Prec p)
Definition tok.h:125
Tag tag() const
Definition tok.h:184
char8_t lit_c() const
Definition tok.h:190
#define CODE(node, name)
Definition def.h:40
Definition span.h:103
Definition cfg.h:11
uint64_t u64
Definition types.h:35
int64_t s64
Definition types.h:35
constexpr auto Num_Keys
Definition tok.h:52
double f64
Definition types.h:42
#define THORIN_KEY(m)
Definition tok.h:14
#define THORIN_PREC(m)
Definition tok.h:104
#define THORIN_TOK(m)
Definition tok.h:55