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