MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
parser.cpp
Go to the documentation of this file.
1#include "mim/ast/parser.h"
2
3#include <filesystem>
4#include <fstream>
5
6#include "mim/driver.h"
7
8// clang-format off
9#define C_PRIMARY \
10 K_Univ: \
11 case Tag::K_Nat: \
12 case Tag::K_Idx: \
13 case Tag::K_Bool: \
14 case Tag::K_ff: \
15 case Tag::K_tt: \
16 case Tag::K_i1: \
17 case Tag::K_i8: \
18 case Tag::K_i16: \
19 case Tag::K_i32: \
20 case Tag::K_i64: \
21 case Tag::K_I1: \
22 case Tag::K_I8: \
23 case Tag::K_I16: \
24 case Tag::K_I32: \
25 case Tag::K_I64: \
26 case Tag::T_star: \
27 case Tag::T_box
28
29#define C_ID \
30 M_anx: \
31 case Tag::M_id
32
33#define C_LIT \
34 T_bot: \
35 case Tag::T_top: \
36 case Tag::L_str: \
37 case Tag::L_c: \
38 case Tag::L_s: \
39 case Tag::L_u: \
40 case Tag::L_f: \
41 case Tag::L_i
42
43#define C_LAM \
44 K_lam: \
45 case Tag::K_con: \
46 case Tag::K_fun
47
48#define C_DECL \
49 K_ax: \
50 case Tag::K_let: \
51 case Tag::K_rec: \
52 case Tag::K_ccon: \
53 case Tag::K_cfun: \
54 case Tag::C_LAM
55
56#define C_PI \
57 D_brace_l: \
58 case Tag::K_Cn: \
59 case Tag::K_Fn
60
61#define C_LM \
62 T_lm: \
63 case Tag::K_cn: \
64 case Tag::K_fn
65
66#define C_EXPR \
67 C_PRIMARY: \
68 case Tag::C_ID: \
69 case Tag::C_LIT: \
70 case Tag::C_DECL: \
71 case Tag::C_PI: \
72 case Tag::C_LM: \
73 case Tag::K_Type: /*TypeExpr*/ \
74 case Tag::K_ins: /*InsertExpr*/ \
75 case Tag::K_ret: /*RetExpr*/ \
76 case Tag::D_angle_l: /*PackExpr*/ \
77 case Tag::D_brckt_l: /*SigmaExpr*/ \
78 case Tag::D_paren_l: /*TupleExpr*/ \
79 case Tag::D_quote_l /*ArrExpr*/
80
81#define C_PTRN \
82 M_id: \
83 case Tag::T_backtick: \
84 case Tag::D_brace_l: \
85 case Tag::D_brckt_l: \
86 case Tag::D_paren_l
87// clang-format on
88
89using namespace std::string_literals;
90
91namespace mim::ast {
92
93using Tag = Tok::Tag;
94
95/*
96 * entry points
97 */
98
99Ptr<Module> Parser::parse_module() {
100 auto track = tracker();
101 Ptrs<Import> imports;
102 while (true) {
103 if (ahead().isa(Tag::K_import) || ahead().isa(Tag::K_plugin)) {
104 if (auto import = parse_import_or_plugin()) imports.emplace_back(std::move(import));
105 } else {
106 break;
107 }
108 }
109 auto decls = parse_decls();
110 bool where = ahead().isa(Tag::K_where);
111 expect(Tag::EoF, "module");
112 auto mod = ptr<Module>(track, std::move(imports), std::move(decls));
113 if (where) ast().note(mod->loc().anew_finis(), "did you accidentally end your declaration expression with a ';'?");
114 return mod;
115}
116
117Ptr<Module> Parser::import(Dbg dbg, std::ostream* md) {
118 auto name = dbg.sym();
119 auto filename = fs::path(name.view());
120 driver().VLOG("import: {}", name);
121
122 if (!filename.has_extension()) filename.replace_extension("mim"); // TODO error cases
123
124 fs::path rel_path;
125 for (const auto& path : driver().search_paths()) {
126 std::error_code ignore;
127 rel_path = path / filename;
128 if (bool reg_file = fs::is_regular_file(rel_path, ignore); reg_file && !ignore) break;
129 rel_path = path / name.view() / filename;
130 if (bool reg_file = fs::is_regular_file(rel_path, ignore); reg_file && !ignore) break;
131 }
132
133 if (auto path = driver().add_import(std::move(rel_path), name)) {
134 auto ifs = std::ifstream(*path);
135 return import(ifs, dbg.loc(), path, md);
136 }
137 return {};
138}
139
140Ptr<Module> Parser::import(std::istream& is, Loc loc, const fs::path* path, std::ostream* md) {
141 driver().VLOG("reading: {}", path ? path->string() : "<unknown file>"s);
142 if (!is) {
143 ast().error(loc, "cannot read file {}", *path);
144 return {};
145 }
146
147 auto state = std::tuple(prev_, ahead_, lexer_);
148 auto lexer = Lexer(ast(), is, path, md);
149 lexer_ = &lexer;
150 init(path);
151 auto mod = parse_module();
152 std::tie(prev_, ahead_, lexer_) = state;
153 return mod;
154}
155
157 if (!driver().flags().bootstrap && !driver().is_loaded(dbg.sym())) driver().load(dbg.sym());
158 return import(dbg);
159}
160
161/*
162 * misc
163 */
164
165Ptr<Import> Parser::parse_import_or_plugin() {
166 auto track = tracker();
167 auto tag = lex().tag();
168 auto name = expect(Tag::M_id, "{} name", tag == Tag::K_import ? "import" : "plugin");
169 auto dbg = name.dbg();
170 expect(Tag::T_semicolon, "end of {}", tag == Tag::K_import ? "import" : "plugin");
171 if (auto module = tag == Tag::K_import ? import(dbg) : plugin(dbg))
172 return ptr<Import>(track, tag, name.dbg(), std::move(module));
173 return {};
174}
175
176Dbg Parser::parse_id(std::string_view ctxt) {
177 if (auto id = accept(Tag::M_id)) return id.dbg();
178 syntax_err("identifier", ctxt);
179 return {prev_, driver().sym("<error>")};
180}
181
182Dbg Parser::parse_name(std::string_view ctxt) {
183 if (auto tok = accept(Tag::M_anx)) return tok.dbg();
184 if (auto tok = accept(Tag::M_id)) return tok.dbg();
185 syntax_err("identifier or annex name", ctxt);
186 return Dbg(prev_, ast().sym("<error>"));
187}
188
189Ptr<Expr> Parser::parse_type_ascr(std::string_view ctxt) {
190 if (accept(Tag::T_colon)) return parse_expr(ctxt);
191 if (ctxt.empty()) return nullptr;
192 syntax_err("':'", ctxt);
193 return ptr<ErrorExpr>(prev_);
194}
195
196/*
197 * exprs
198 */
199
200Ptr<Expr> Parser::parse_expr(std::string_view ctxt, Prec curr_prec) {
201 auto track = tracker();
202 auto lhs = parse_primary_expr(ctxt);
203 return parse_infix_expr(track, std::move(lhs), curr_prec);
204}
205
206Ptr<Expr> Parser::parse_infix_expr(Tracker track, Ptr<Expr>&& lhs, Prec curr_prec) {
207 while (true) {
208 // If operator in ahead has less left precedence: reduce (break).
209 switch (ahead().tag()) {
210 case Tag::T_extract: {
211 if (curr_prec >= Prec::Extract) return lhs;
212 lex();
213 if (auto tok = accept(Tag::M_id))
214 lhs = ptr<ExtractExpr>(track.loc(), std::move(lhs), tok.dbg());
215 else {
216 auto rhs = parse_expr("right-hand side of an extract", Prec::Extract);
217 lhs = ptr<ExtractExpr>(track.loc(), std::move(lhs), std::move(rhs));
218 }
219 continue;
220 }
221 case Tag::T_arrow: {
222 if (curr_prec > Prec::Arrow) return lhs; // ">" - Arrow is rassoc
223 lex();
224 auto rhs = parse_expr("right-hand side of a function type", Prec::Arrow);
225 lhs = ptr<ArrowExpr>(track.loc(), std::move(lhs), std::move(rhs));
226 continue;
227 }
228 case Tag::T_at: {
229 if (curr_prec >= Prec::App) return lhs;
230 lex();
231 auto rhs = parse_expr("explicit argument to an application", Prec::App);
232 lhs = ptr<AppExpr>(track.loc(), true, std::move(lhs), std::move(rhs));
233 continue;
234 }
235 case Tag::C_EXPR: {
236 if (curr_prec >= Prec::App) return lhs;
237 switch (ahead().tag()) {
238 case Tag::C_DECL:
239 ast().warn(ahead().loc(), "you are passing a declaration expression as argument");
240 ast().note(lhs->loc(), "to this expression");
241 ast().note(ahead().loc(),
242 "if this was your intention, consider to parenthesize the declaration expression");
243 ast().note(lhs->loc().anew_finis(), "otherwise, you are probably missing a ';'");
244 default: break;
245 }
246 auto rhs = parse_expr("argument to an application", Prec::App);
247 lhs = ptr<AppExpr>(track.loc(), false, std::move(lhs), std::move(rhs));
248 continue;
249 }
250 case Tag::K_where: {
251 if (curr_prec >= Prec::Where) return lhs;
252 lex();
253 auto decls = parse_decls();
254 lhs = ptr<DeclExpr>(track, std::move(decls), std::move(lhs), true);
255
256 bool where = ahead().tag() == Tag::K_where;
257 expect(Tag::K_end, "end of a where declaration block");
258 if (where)
259 ast().note(lhs->loc().anew_finis(),
260 "did you accidentally end your declaration expression with a ';'?");
261 return lhs;
262 }
263 default: return lhs;
264 }
265 }
266}
267
268Ptr<Expr> Parser::parse_insert_expr() {
269 eat(Tag::K_ins);
270 auto track = tracker();
271 expect(Tag::D_paren_l, "opening paren for insert arguments");
272 auto tuple = parse_expr("the tuple to insert into");
273 expect(Tag::T_comma, "comma after tuple to insert into");
274 auto index = parse_expr("insert index");
275 expect(Tag::T_comma, "comma after insert index");
276 auto value = parse_expr("insert value");
277 expect(Tag::D_paren_r, "closing paren for insert arguments");
278 return ptr<InsertExpr>(track.loc(), std::move(tuple), std::move(index), std::move(value));
279}
280
281Ptr<Expr> Parser::parse_primary_expr(std::string_view ctxt) {
282 // clang-format off
283 switch (ahead().tag()) {
284 case Tag::C_PRIMARY: return ptr<PrimaryExpr>(lex());
285 case Tag::C_ID: return ptr<IdExpr>(lex().dbg());
286 case Tag::C_LIT: return parse_lit_expr();
287 case Tag::C_DECL: return parse_decl_expr();
288 case Tag::C_PI: return parse_pi_expr();
289 case Tag::C_LM: return parse_lam_expr();
290 case Tag::K_ins: return parse_insert_expr();
291 case Tag::K_ret: return parse_ret_expr();
292 case Tag::D_quote_l: return parse_arr_or_pack_expr<true>();
293 case Tag::D_angle_l: return parse_arr_or_pack_expr<false>();
294 case Tag::D_brckt_l: return parse_sigma_expr();
295 case Tag::D_paren_l: return parse_tuple_expr();
296 case Tag::K_Type: return parse_type_expr();
297 default:
298 if (ctxt.empty()) return nullptr;
299 syntax_err("primary expression", ctxt);
300 }
301 // clang-format on
302 return ptr<ErrorExpr>(prev_);
303}
304
305template<bool arr> Ptr<Expr> Parser::parse_arr_or_pack_expr() {
306 auto track = tracker();
307 eat(arr ? Tag::D_quote_l : Tag::D_angle_l);
308
309 Dbg dbg;
310 if (ahead(0).isa(Tag::M_id) && ahead(1).isa(Tag::T_colon)) {
311 dbg = eat(Tag::M_id).dbg();
312 eat(Tag::T_colon);
313 }
314
315 auto shape = parse_expr(arr ? "shape of an array" : "shape of a pack");
316 auto ptrn = IdPtrn::mk_id(ast(), dbg, std::move(shape));
317 expect(Tag::T_semicolon, arr ? "array" : "pack");
318 auto body = parse_expr(arr ? "body of an array" : "body of a pack");
319 expect(arr ? Tag::D_quote_r : Tag::D_angle_r,
320 arr ? "closing delimiter of an array" : "closing delimiter of a pack");
321
322 return ptr<ArrOrPackExpr<arr>>(track, std::move(ptrn), std::move(body));
323}
324
325Ptr<Expr> Parser::parse_decl_expr() {
326 auto track = tracker();
327 auto decls = parse_decls();
328 auto expr = parse_expr("final expression of a declaration expression");
329 return ptr<DeclExpr>(track, std::move(decls), std::move(expr), false);
330}
331
332Ptr<Expr> Parser::parse_lit_expr() {
333 auto track = tracker();
334 auto tok = lex();
335 auto type = accept(Tag::T_colon) ? parse_expr("literal", Prec::Lit) : nullptr;
336 return ptr<LitExpr>(track, tok, std::move(type));
337}
338
339Ptr<Expr> Parser::parse_sigma_expr() {
340 auto ptrn = parse_tuple_ptrn();
341 if (ahead().isa(Tag::D_brace_l) || ahead().isa(Tag::D_brckt_l) || ahead().isa(Tag::T_arrow))
342 return parse_pi_expr(std::move(ptrn));
343 return ptr<SigmaExpr>(std::move(ptrn));
344}
345
346Ptr<Expr> Parser::parse_tuple_expr() {
347 auto track = tracker();
348 Ptrs<Expr> elems;
349 parse_list("tuple", Tag::D_paren_l, [&]() { elems.emplace_back(parse_expr("tuple element")); });
350 return ptr<TupleExpr>(track, std::move(elems));
351}
352
353Ptr<Expr> Parser::parse_type_expr() {
354 auto track = tracker();
355 eat(Tag::K_Type);
356 auto level = parse_expr("type level", Prec::App);
357 return ptr<TypeExpr>(track, std::move(level));
358}
359
360Ptr<Expr> Parser::parse_pi_expr(Ptr<Ptrn>&& ptrn) {
361 auto track = tracker();
362 auto tag = ahead().tag();
363 auto entity = "dependent function type"s;
364 bool has_first = ptrn.get();
365
366 if (accept(Tag::K_Cn))
367 entity = "continuation type";
368 else if (accept(Tag::K_Fn))
369 entity = "returning continuation type";
370
372 if (has_first) doms.emplace_back(ptr<PiExpr::Dom>(ptrn->loc(), false, std::move(ptrn)));
373
374 if (!has_first || !ahead().isa(Tag::T_arrow)) {
375 while (true) {
376 auto track = tracker();
377 auto implicit = false;
378 Ptr<Ptrn> ptrn;
379 if (ahead().isa(Tok::Tag::D_brace_l)) {
380 implicit = true;
381 ptrn = parse_tuple_ptrn();
382 } else {
383 auto prec = tag == Tag::K_Cn ? Prec::Bot : Prec::Pi;
384 ptrn = parse_ptrn(Tag::D_brckt_l, "domain of a "s + entity, prec);
385 }
386
387 doms.emplace_back(ptr<PiExpr::Dom>(track, implicit, std::move(ptrn)));
388
389 switch (ahead().tag()) {
390 case Tag::C_PTRN: continue;
391 default: break;
392 }
393 break;
394 }
395 }
396
397 auto codom = tag != Tag::K_Cn ? (expect(Tag::T_arrow, entity), parse_expr("codomain of a "s + entity, Prec::Arrow))
398 : nullptr;
399
400 if (tag == Tag::K_Fn) doms.back()->add_ret(ast(), codom ? std::move(codom) : ptr<InferExpr>(prev_));
401
402 // TODO loc
403 auto loc = ptrn ? ptrn->loc() + track.loc() : track.loc();
404 return ptr<PiExpr>(loc, tag, std::move(doms), std::move(codom));
405}
406
407Ptr<Expr> Parser::parse_lam_expr() { return ptr<LamExpr>(parse_lam_decl()); }
408
409Ptr<Expr> Parser::parse_ret_expr() {
410 auto track = tracker();
411 eat(Tag::K_ret);
412 auto ptrn = parse_ptrn(Tag::D_paren_l, "binding pattern of a ret expression");
413 expect(Tag::T_assign, "ret expression");
414 auto callee = parse_expr("continuation expression of a ret expression");
415 expect(Tag::T_dollar, "separator of a ret expression");
416 auto arg = parse_expr("argument of ret expression");
417 expect(Tag::T_semicolon, "ret expression");
418 auto body = parse_expr("body of a ret expression");
419 return ptr<RetExpr>(track, std::move(ptrn), std::move(callee), std::move(arg), std::move(body));
420}
421
422/*
423 * ptrns
424 */
425
426Ptr<Ptrn> Parser::parse_ptrn(Tag delim_l, std::string_view ctxt, Prec prec, bool allow_annex) {
427 auto track = tracker();
428 bool p = delim_l == Tag::D_paren_l;
429 bool b = delim_l == Tag::D_brckt_l;
430 assert((p ^ b) && "left delimiter must either be '(' or '['");
431
432 // p -> anx
433 // p -> anx: e
434 if (allow_annex) {
435 assert(p);
436 if (auto anx = accept(Tok::Tag::M_anx)) {
437 auto type = parse_type_ascr();
438 return ptr<IdPtrn>(track, false, anx.dbg(), std::move(type));
439 }
440 }
441
442 // p -> `s: e b -> `s: e
443 if (accept(Tok::Tag::T_backtick)) {
444 auto dbg = expect(Tok::Tag::M_id, "identifier pattern").dbg();
445 auto type = accept(Tok::Tag::T_colon) ? parse_expr("identifier pattern", prec) : nullptr;
446 return ptr<IdPtrn>(track, true, dbg, std::move(type));
447 }
448
449 // p -> (p, ..., p)
450 // p -> [b, ..., b] b -> [b, ..., b]
451 if ((p && ahead().isa(Tag::D_paren_l)) || ahead().isa(Tag::D_brckt_l)) return parse_tuple_ptrn();
452
453 if (ahead(0).isa(Tag::M_id)) {
454 if (ahead(1).isa(Tag::T_colon)) {
455 // p -> s: e b -> s: e
456 auto dbg = eat(Tag::M_id).dbg();
457 eat(Tag::T_colon);
458 auto type = parse_expr(ctxt, prec);
459 return ptr<IdPtrn>(track, false, dbg, std::move(type));
460 } else if (p) {
461 // p -> s
462 // p -> `s
463 auto dbg = eat(Tag::M_id).dbg();
464 return ptr<IdPtrn>(track, false, dbg, nullptr);
465 } else {
466 // b -> e where e == id
467 auto type = parse_expr(ctxt, prec);
468 return ptr<IdPtrn>(track, false, type->loc().anew_begin(), std::move(type));
469 }
470 } else if (b) {
471 // b -> e where e != id
472 auto type = parse_expr(ctxt, prec);
473 auto loc = type->loc().anew_begin();
474 return ptr<IdPtrn>(track, false, Dbg(loc), std::move(type));
475 } else if (!ctxt.empty()) {
476 // p -> ↯
477 syntax_err("pattern", ctxt);
478 return ptr<ErrorPtrn>(prev_);
479 }
480
481 return nullptr;
482}
483
484Ptr<TuplePtrn> Parser::parse_tuple_ptrn() {
485 auto track = tracker();
486 auto delim_l = ahead().tag();
487
488 Ptrs<Ptrn> ptrns;
489 parse_list("tuple pattern", delim_l, [&]() {
490 auto track = tracker();
491 Ptr<Ptrn> ptrn;
492
493 if (ahead(0).isa(Tag::M_id) && ahead(1).isa(Tag::M_id)) {
494 Dbgs dbgs;
495 while (auto tok = accept(Tag::M_id)) dbgs.emplace_back(tok.dbg());
496
497 if (accept(Tag::T_colon)) { // identifier group: x y x: T
498 auto dbg = dbgs.back();
499 auto type = parse_expr("type of an identifier group within a tuple pattern");
500 auto id = ptr<IdPtrn>(dbg.loc() + type->loc().finis, false, dbg, std::move(type));
501
502 for (auto dbg : dbgs | std::views::take(dbgs.size() - 1))
503 ptrns.emplace_back(ptr<GrpPtrn>(dbg, id.get()));
504 ptrns.emplace_back(std::move(id));
505 return;
506 }
507
508 // "x y z" is a curried app and maybe the prefix of a longer type expression
509 Ptr<Expr> lhs = ptr<IdExpr>(dbgs.front());
510 for (auto dbg : dbgs | std::views::drop(1)) {
511 auto rhs = ptr<IdExpr>(dbg);
512 lhs = ptr<AppExpr>(track, false, std::move(lhs), std::move(rhs));
513 }
514 auto expr = parse_infix_expr(track, std::move(lhs), Prec::App);
515 ptrn = IdPtrn::mk_type(ast(), std::move(expr));
516 } else {
517 auto dl = delim_l == Tag::D_brace_l ? Tag::D_brckt_l : delim_l; // brace behaves just as bracket
518 ptrn = parse_ptrn(dl, "element of a tuple pattern");
519
520 if (dl == Tag::D_brckt_l) {
521 // [..., [Nat, Nat] -> Nat, ...] ==> [..., _: [Nat, Nat] -> Nat, ...]
522 if (ahead().isa(Tag::T_arrow)) {
523 auto loc = ptrn->loc();
524 auto expr = parse_pi_expr(std::move(ptrn));
525 ptrn = ptr<IdPtrn>(loc, false, Dbg(loc.anew_begin(), Sym()), std::move(expr));
526 } else if (auto expr = Ptrn::to_expr(ast(), std::move(ptrn))) {
527 // If we are able to parse more stuff, we got an expr instead of a binder
528 auto addr = expr.get();
529 expr = parse_infix_expr(track, std::move(expr));
530 if (expr.get() != addr) {
531 auto loc = expr->loc();
532 ptrn = ptr<IdPtrn>(loc, false, Dbg(loc.anew_begin(), Sym()), std::move(expr));
533 } else {
534 if (!ptrn) ptrn = Ptrn::to_ptrn(std::move(expr));
535 }
536 }
537 }
538 }
539
540 ptrns.emplace_back(std::move(ptrn));
541 });
542
543 Dbg dbg;
544 bool rebind = false;
545 if (accept(Tag::T_colon_colon)) {
546 rebind = (bool)accept(Tag::T_backtick);
547 dbg = eat(Tag::M_id).dbg();
548 }
549 return ptr<TuplePtrn>(track, delim_l, std::move(ptrns), rebind, dbg);
550}
551
552/*
553 * decls
554 */
555
556Ptrs<ValDecl> Parser::parse_decls() {
557 Ptrs<ValDecl> decls;
558 while (true) {
559 // clang-format off
560 switch (ahead().tag()) {
561 case Tag::T_semicolon: lex(); break; // eat up stray semicolons
562 case Tag::K_ax: decls.emplace_back(parse_axiom_decl()); break;
563 case Tag::K_ccon:
564 case Tag::K_cfun: decls.emplace_back(parse_c_decl()); break;
565 case Tag::K_let: decls.emplace_back(parse_let_decl()); break;
566 case Tag::K_rec: decls.emplace_back(parse_rec_decl(true)); break;
567 case Tag::K_con:
568 case Tag::K_fun:
569 case Tag::K_lam: decls.emplace_back(parse_lam_decl()); break;
570 default: return decls;
571 }
572 // clang-format on
573 }
574}
575
576Ptr<ValDecl> Parser::parse_axiom_decl() {
577 auto track = tracker();
578 eat(Tag::K_ax);
579 Dbg dbg, normalizer;
580 Tok curry, trip;
581 if (auto name = expect(Tag::M_anx, "annex name of an axiom"))
582 dbg = name.dbg();
583 else
584 dbg = Dbg(prev_, ast().sym("<error annex name>"));
585
586 std::deque<Ptrs<AxiomDecl::Alias>> subs;
587 if (ahead().isa(Tag::D_paren_l)) {
588 parse_list("tag list of an axiom", Tag::D_paren_l, [&]() {
589 auto& aliases = subs.emplace_back();
590 aliases.emplace_back(ptr<AxiomDecl::Alias>(parse_id("tag of an axiom")));
591 while (accept(Tag::T_assign))
592 aliases.emplace_back(ptr<AxiomDecl::Alias>(parse_id("alias of an axiom tag")));
593 });
594 }
595
596 auto type = parse_type_ascr("type ascription of an axiom");
597
598 if (ahead(0).isa(Tag::T_comma) && ahead(1).isa(Tag::M_id)) {
599 lex();
600 normalizer = lex().dbg();
601 }
602 if (accept(Tag::T_comma)) {
603 if (auto c = expect(Tag::L_u, "curry counter for axiom")) curry = c;
604 if (accept(Tag::T_comma)) {
605 if (auto t = expect(Tag::L_u, "trip count for axiom")) trip = t;
606 }
607 }
608
609 return ptr<AxiomDecl>(track, dbg, std::move(subs), std::move(type), normalizer, curry, trip);
610}
611
612Ptr<ValDecl> Parser::parse_let_decl() {
613 auto track = tracker();
614 eat(Tag::K_let);
615 auto ptrn = parse_ptrn(Tag::D_paren_l, "binding pattern of a let declaration", Prec::Bot, true);
616 expect(Tag::T_assign, "let");
617 auto type = parse_type_ascr();
618 auto value = parse_expr("value of a let declaration");
619 return ptr<LetDecl>(track, std::move(ptrn), std::move(value));
620}
621
622Ptr<ValDecl> Parser::parse_c_decl() {
623 auto track = tracker();
624 auto tag = lex().tag();
625 auto id = expect(Tag::M_id, "C function declaration");
626 auto dom = parse_ptrn(Tag::D_brckt_l, "domain of a C function"s, Prec::App);
627 Ptr<Expr> codom;
628 if (tag == Tag::K_cfun) {
629 expect(Tag::T_colon, "codomain of a C function");
630 codom = parse_expr("codomain of a C function");
631 }
632 return ptr<CDecl>(track, tag, id.dbg(), std::move(dom), std::move(codom));
633}
634
635Ptr<RecDecl> Parser::parse_rec_decl(bool first) {
636 auto track = tracker();
637 eat(first ? Tag::K_rec : Tag::K_and);
638 auto dbg = parse_name("recursive declaration");
639 auto type = accept(Tag::T_colon) ? parse_expr("type of a recursive declaration") : ptr<InferExpr>(prev_);
640 expect(Tag::T_assign, "recursive declaration");
641 auto body = parse_expr("body of a recursive declaration");
642 auto next = ahead().isa(Tag::K_and) ? parse_and_decl() : nullptr;
643 return ptr<RecDecl>(track, dbg, std::move(type), std::move(body), std::move(next));
644}
645
646Ptr<LamDecl> Parser::parse_lam_decl() {
647 auto track = tracker();
648 auto tag = lex().tag();
649 auto prec = tag == Tag::K_cn || tag == Tag::K_con ? Prec::Bot : Prec::Pi;
650 bool external = (bool)accept(Tag::K_extern);
651
652 bool decl;
653 std::string entity;
654 // clang-format off
655 switch (tag) {
656 case Tag::T_lm: decl = false; entity = "function expression"; break;
657 case Tag::K_cn: decl = false; entity = "continuation expression"; break;
658 case Tag::K_fn: decl = false; entity = "returning continuation expression"; break;
659 case Tag::K_lam: decl = true ; entity = "function declaration"; break;
660 case Tag::K_con: decl = true ; entity = "continuation declaration"; break;
661 case Tag::K_fun: decl = true ; entity = "returning continuation declaration"; break;
662 default: fe::unreachable();
663 }
664 // clang-format on
665
666 auto dbg = decl ? parse_name(entity) : Dbg();
668 while (true) {
669 auto track = tracker();
670 bool implicit = false;
671 Ptr<Ptrn> ptrn;
672 if (ahead().isa(Tok::Tag::D_brace_l)) {
673 implicit = true;
674 ptrn = parse_tuple_ptrn();
675 } else {
676 ptrn = parse_ptrn(Tag::D_paren_l, "domain pattern of a "s + entity, prec);
677 }
678 auto filter = accept(Tag::T_at) ? parse_expr("filter") : nullptr;
679
680 doms.emplace_back(ptr<LamDecl::Dom>(track, implicit, std::move(ptrn), std::move(filter)));
681 switch (ahead().tag()) {
682 case Tag::C_PTRN: continue;
683 default: break;
684 }
685 break;
686 }
687
688 auto codom = accept(Tag::T_colon) ? parse_expr("codomain of a "s + entity, Prec::Arrow) : nullptr;
689 if (tag == Tag::K_fn || tag == Tag::K_fun)
690 doms.back()->add_ret(ast(), codom ? std::move(codom) : ptr<InferExpr>(prev_));
691
692 expect(Tag::T_assign, "body of a "s + entity);
693 auto body = parse_expr("body of a "s + entity);
694 auto next = ahead().isa(Tag::K_and) ? parse_and_decl() : nullptr;
695
696 return ptr<LamDecl>(track, tag, external, dbg, std::move(doms), std::move(codom), std::move(body), std::move(next));
697}
698
699Ptr<RecDecl> Parser::parse_and_decl() {
700 switch (ahead(1).tag()) {
701 case Tag::C_LAM: return lex(), parse_lam_decl();
702 default: return parse_rec_decl(false);
703 }
704}
705
706} // namespace mim::ast
void load(Sym name)
Definition driver.cpp:53
Error & note(Loc loc, const char *fmt, Args &&... args) const
Definition ast.h:86
Error & error()
Definition ast.h:63
Error & warn(Loc loc, const char *fmt, Args &&... args) const
Definition ast.h:85
static Ptr< IdPtrn > mk_id(AST &ast, Dbg dbg, Ptr< Expr > &&type)
Definition ast.h:218
static Ptr< IdPtrn > mk_type(AST &ast, Ptr< Expr > &&type)
Definition ast.h:214
Ptr< Module > import(std::string_view sv)
Definition parser.h:39
AST & ast()
Definition parser.h:37
Driver & driver()
Definition parser.h:38
Ptr< Module > plugin(Dbg)
Definition parser.cpp:156
static Ptr< Ptrn > to_ptrn(Ptr< Expr > &&)
Definition ast.cpp:177
static Ptr< Expr > to_expr(AST &, Ptr< Ptrn > &&)
Definition ast.cpp:167
Dbg dbg() const
Definition tok.h:170
Definition ast.h:14
std::deque< Dbg > Dbgs
Definition ast.h:23
fe::Arena::Ptr< const T > Ptr
Definition ast.h:21
std::deque< Ptr< T > > Ptrs
Definition ast.h:22
void * get(void *handle, const char *symbol_name)
Definition dl.cpp:36