main
parse.c
1#include <stdio.h>
2
3#include "util.h"
4#include "kav.h"
5
6static struct lexer lx;
7static struct token tok;
8
9static struct decl *curfn;
10static struct decl *lambdas;
11static struct decl **lambdaend;
12static usize nlambdas;
13
14static struct expr *expr(void);
15static struct expr *block(void);
16static struct expr *call(void);
17static struct expr *pipe(void);
18static struct expr *lambda(void);
19static struct param *sortparams(void);
20
21static void
22next(void) {
23 /* skip doc comments for now */
24 do {
25 lex(&lx, &tok);
26 } while(tok.kind == TDOC);
27}
28
29static int
30consume(enum tokenkind k) {
31 if(tok.kind == k) {
32 next();
33 return 1;
34 }
35 return 0;
36}
37
38static void
39expect(enum tokenkind k) {
40 if(!consume(k)) {
41 if(tokwhat(k)[0] != '\0')
42 errorat(&tok.loc, "expected '", tokwhat(k), "'");
43 else
44 errorat(&tok.loc, "expected ", tokstr(k));
45 }
46}
47
48static struct expr *
49type(void) {
50 struct expr *e;
51 /* it's an existential. */
52 if(tok.kind == TLBRACK) {
53 e = mkexpr(EEXISTS, tok);
54 e->sparams = sortparams();
55 e->body = type();
56 return e;
57 }
58 /* we're borrowing */
59 if(tok.kind == TBANG) {
60 e = mkexpr(EBORROW, tok);
61 next();
62 e->body = type();
63 return e;
64 }
65 /* a function type, fn(int, !List[int, n]) -> int */
66 if(tok.kind == TFN) {
67 struct expr *a;
68 struct expr **end;
69
70 e = mkexpr(EFNTYPE, tok);
71 next();
72 expect(TLPAREN);
73 end = &e->args;
74 if(tok.kind != TRPAREN) {
75 do {
76 a = type();
77 *end = a;
78 end = &a->next;
79 } while(consume(TCOMMA));
80 }
81 expect(TRPAREN);
82 expect(TARROW);
83 e->body = type();
84 return e;
85 }
86 /* a tuple type. the elements are types so they can be fn types */
87 if(tok.kind == TLPAREN) {
88 struct expr *a;
89 struct expr *head = nil;
90 struct expr **end = &head;
91 struct token opener = tok;
92
93 next();
94 do {
95 a = type();
96 *end = a;
97 end = &a->next;
98 } while(consume(TCOMMA));
99 expect(TRPAREN);
100 if(!head->next)
101 return head;
102 e = mkexpr(ETUPLE, opener);
103 e->args = head;
104 return e;
105 }
106 return call();
107}
108
109static struct param *
110param(void) {
111 struct param *p;
112
113 p = mkparam(tok);
114 expect(TIDENT);
115 expect(TCOLON);
116 p->ann = type();
117 return p;
118}
119
120
121static struct expr *
122primary(void) {
123 struct expr *e;
124 struct expr *a;
125 struct expr *tup;
126 struct expr *head = nil;
127 struct expr **end = &head;
128 struct token opener;
129
130 switch(tok.kind) {
131 case TNUMBER:
132 case TCHAR:
133 e = mkexpr(ENUMBER, tok);
134 next();
135 return e;
136 /* allow a hole anywhere an expression can go, so we can do List[int, _].
137 * later we will disallow things like val x = _. */
138 case TUS:
139 e = mkexpr(EHOLE, tok);
140 next();
141 return e;
142 case TSTRING:
143 e = mkexpr(ESTRING, tok);
144 next();
145 return e;
146 case TIDENT:
147 e = mkexpr(EIDENT, tok);
148 next();
149 if(consume(TDCOLON)) {
150 /* the ident tok was actually the namespace */
151 e->ns = e->tok;
152 /* the current token is the ident tok */
153 e->tok = tok;
154 expect(TIDENT);
155 }
156 return e;
157 case TLBRACE:
158 return block();
159 case TLPAREN:
160 opener = tok;
161 next();
162 e = expr();
163 if(tok.kind != TCOMMA) {
164 expect(TRPAREN);
165 return e;
166 }
167 *end = e;
168 end = &e->next;
169 while(consume(TCOMMA)) {
170 a = expr();
171 *end = a;
172 end = &a->next;
173 }
174 expect(TRPAREN);
175 tup = mkexpr(ETUPLE, opener);
176 tup->args = head;
177 return tup;
178 case TFN:
179 return lambda();
180 default:
181 errorat(&tok.loc, "expected expression");
182 }
183}
184
185/*
186 * parse an operand with an optional arg list following
187 * f(x, 1)
188 * List[a, n]
189 */
190static struct expr *
191call(void) {
192 struct expr *e;
193 struct expr *a;
194 struct expr *head = nil;
195 struct expr **end = &head;
196 enum tokenkind closer;
197
198 e = primary();
199 /* a ( or [ on a new line is never a call. */
200 if((tok.kind == TLPAREN || tok.kind == TLBRACK) && tok.loc.line == e->tok.loc.line) {
201 if(e->kind != EIDENT)
202 errorat(&e->tok.loc, "expected function name");
203 if(tok.kind == TLPAREN) {
204 e->kind = ECALL;
205 closer = TRPAREN;
206 } else {
207 e->kind = ESAPP;
208 closer = TRBRACK;
209 }
210 next();
211 if(tok.kind != closer) {
212 do {
213 a = expr();
214 *end = a;
215 end = &a->next;
216 } while(consume(TCOMMA));
217 }
218 expect(closer);
219 e->args = head;
220 }
221 return e;
222}
223
224static struct expr *
225unary(void) {
226 struct expr *e;
227
228 if(tok.kind == TSUB || tok.kind == TBANG) {
229 e = mkexpr(ECALL, tok);
230 next();
231 e->args = unary();
232 return e;
233 }
234 return call();
235}
236
237static struct expr *
238cast(void) {
239 struct expr *e;
240 struct expr *c;
241
242 e = unary();
243 while(tok.kind == TAS) {
244 c = mkexpr(ECAST, tok);
245 next();
246 c->args = e;
247 c->body = type();
248 e = c;
249 }
250 return e;
251}
252
253static int
254prec(enum tokenkind k) {
255 switch(k) {
256 case TOR:
257 return 1;
258 case TAND:
259 return 2;
260 case TEQ:
261 case TNEQ:
262 case TLESS:
263 case TMORE:
264 case TLEQ:
265 case TMEQ:
266 return 3;
267 case TADD:
268 case TSUB:
269 return 4;
270 case TMUL:
271 case TDIV:
272 case TMOD:
273 return 5;
274 default:
275 return 0;
276 }
277}
278
279static struct expr *
280binary(int min) {
281 struct expr *l;
282 struct token op;
283 int p;
284
285 l = cast();
286 while((p = prec(tok.kind)) >= min) {
287 op = tok;
288 next();
289 l = mkbinary(op, l, binary(p + 1));
290 if(p == 3 && prec(tok.kind) == 3)
291 errorat(&tok.loc, "comparisons can't be chained");
292 }
293 return l;
294}
295
296static struct pat *
297pattern(void) {
298 struct pat *p;
299 struct pat *a;
300 struct pat *head = nil;
301 struct pat **end = &head;
302 struct token opener;
303 int tilde;
304
305 tilde = consume(TTILDE);
306 if(tok.kind == TUS) {
307 p = mkpat(PWILD, tok);
308 next();
309 } else if(tok.kind == TLPAREN) {
310 opener = tok;
311 next();
312 p = pattern();
313 if(tok.kind != TCOMMA)
314 expect(TRPAREN);
315 else {
316 *end = p;
317 end = &p->next;
318 while(consume(TCOMMA)) {
319 a = pattern();
320 *end = a;
321 end = &a->next;
322 }
323 expect(TRPAREN);
324 p = mkpat(PTUPLE, opener);
325 p->args = head;
326 }
327 } else if(tok.kind == TNUMBER || tok.kind == TCHAR) {
328 p = mkpat(PNUM, tok);
329 next();
330 } else {
331 p = mkpat(PNAME, tok);
332 expect(TIDENT);
333 if(consume(TDCOLON)) {
334 /* the ident was the namespace */
335 p->ns = p->name;
336 p->name = tok;
337 expect(TIDENT);
338 }
339 if(consume(TLPAREN)) {
340 p->kind = PCON;
341 do {
342 a = pattern();
343 *end = a;
344 end = &a->next;
345 } while(consume(TCOMMA));
346 expect(TRPAREN);
347 p->args = head;
348 }
349 }
350 p->tilde = tilde;
351 return p;
352}
353
354
355static struct expr *
356expr(void) {
357 struct expr *e;
358 struct arm *a;
359 struct arm *head = nil;
360 struct arm **end = &head;
361
362 if(tok.kind == TIF) {
363 e = mkexpr(EGUARD, tok);
364 next();
365 a = mkarm(tok.loc);
366 a->cond = expr();
367 expect(TTHEN);
368 a->body = expr();
369 a->next = mkarm(tok.loc);
370 expect(TELSE);
371 a->next->body = expr();
372 e->arm = a;
373 return e;
374 }
375 /* guard { | cond_expr => expr | _ => expr } */
376 if(tok.kind == TGUARD) {
377 e = mkexpr(EGUARD, tok);
378 next();
379 expect(TLBRACE);
380 while(tok.kind == TBAR) {
381 a = mkarm(tok.loc);
382 next();
383 if(!consume(TUS))
384 a->cond = expr();
385 expect(TEQARROW);
386 a->body = expr();
387 *end = a;
388 end = &a->next;
389 }
390 if(!head)
391 errorat(&e->tok.loc, "guard needs atleast one arm");
392 expect(TRBRACE);
393 e->arm = head;
394 return e;
395 }
396 /* case thing of { | pat => expr | _ => expr } */
397 if(tok.kind == TCASE) {
398 e = mkexpr(ECASE, tok);
399 next();
400 e->scrut = expr();
401 expect(TOF);
402 expect(TLBRACE);
403 while(tok.kind == TBAR) {
404 a = mkarm(tok.loc);
405 next();
406 a->pat = pattern();
407 expect(TEQARROW);
408 a->body = expr();
409 *end = a;
410 end = &a->next;
411 }
412 if(!head)
413 errorat(&e->tok.loc, "case needs atleast one arm");
414 expect(TRBRACE);
415 e->arm = head;
416 return e;
417 }
418 return pipe();
419}
420
421static struct expr *
422block(void) {
423 struct expr *e;
424 struct val *v;
425 struct val *head = nil;
426 struct val **end = &head;
427
428 e = mkexpr(EBLOCK, tok);
429 expect(TLBRACE);
430 while(tok.kind == TVAL) {
431 next();
432 /* val (q, r) = expr */
433 v = mkval(pattern());
434 if(consume(TCOLON))
435 /* val thing: Type = expr */
436 v->ann = type();
437 expect(TASSIGN);
438 /* val thing = expr */
439 v->init = expr();
440 *end = v;
441 end = &v->next;
442 }
443 e->body = expr(); /*final expression, the return value */
444 expect(TRBRACE);
445 e->vals = head;
446 return e;
447}
448
449static struct expr *
450pipe(void) {
451 struct expr *l;
452 struct expr *r;
453 struct token op;
454 l = binary(1);
455 while(tok.kind == TPIPE) {
456 op = tok;
457 next();
458 r = call();
459 if(r->kind != ECALL)
460 errorat(&op.loc, "expected a function call after |>");
461 l->next = r->args;
462 r->args = l;
463 l = r;
464 }
465 return l;
466}
467
468static struct param *
469sortparam(void) {
470 struct param *p;
471
472 p = param();
473 if(consume(TBAR))
474 p->cond = expr();
475 return p;
476}
477
478
479/*
480 * static sorts in the form [n: type, x: type, etc: type] .
481 * allows refinement like [n: type | n > 0]
482 * */
483static struct param *
484sortparams(void) {
485 struct param *p;
486 struct param *head = nil;
487 struct param **end = &head;
488
489 if(consume(TLBRACK)) {
490 do {
491 p = sortparam();
492 *end = p;
493 end = &p->next;
494 } while(consume(TCOMMA));
495 expect(TRBRACK);
496 }
497 return head;
498}
499
500/*
501 * params in the form { a: thing, b: thing }
502 * or (n: int, x: int)
503 */
504static struct param *
505params(enum tokenkind closer) {
506 struct param *p;
507 struct param *head = nil;
508 struct param **end = &head;
509
510 if(tok.kind != closer) {
511 do {
512 p = param();
513 *end = p;
514 end = &p->next;
515 } while(consume(TCOMMA));
516 }
517 expect(closer);
518 return head;
519}
520
521static struct decl *
522fndecl(int ext) {
523 struct decl *d;
524
525 expect(TFN);
526 d = mkdecl(DFN, tok);
527 if(ext && tokis(d->name, "main"))
528 errorat(&d->name.loc, "main can't be extern");
529 expect(TIDENT);
530 d->sparams = sortparams();
531 expect(TLPAREN);
532 d->params = params(TRPAREN);
533 expect(TARROW);
534 d->ret = type();
535 if(ext) {
536 if(tok.kind == TLBRACE)
537 errorat(&tok.loc, "extern functions can't have a body");
538 } else {
539 curfn = d;
540 d->body = block();
541 curfn = nil;
542 }
543 return d;
544}
545
546/* lambda gets copies of the enclosing fns sort params */
547static struct param *
548outer(void) {
549 struct param *p, *q;
550 struct param *head = nil;
551 struct param **end = &head;
552
553 if(!curfn)
554 return nil;
555 for(p = curfn->sparams; p; p = p->next) {
556 q = mkparam(p->name);
557 q->ann = p->ann;
558 *end = q;
559 end = &q->next;
560 }
561 return head;
562}
563
564static struct expr *
565lambda(void) {
566 struct decl *d;
567 stralloc sa = {0};
568 char num[FMTUSIZE];
569 struct decl *saved;
570 struct param **end;
571
572 d = mkdecl(DFN, tok);
573 sacatl(&sa, "anon.");
574 sacatb(&sa, num, fmtusize(num, ++nlambdas));
575 d->name.str = sa.s;
576 d->name.length = sa.len;
577
578 expect(TFN);
579 /* the enclosing function's sorts */
580 d->sparams = outer();
581 for(end = &d->sparams; *end; end = &(*end)->next)
582 ;
583 /* lambda-local sorts */
584 *end = sortparams();
585 expect(TLPAREN);
586 d->params = params(TRPAREN);
587 expect(TARROW);
588 d->ret = type();
589 saved = curfn;
590 curfn = d;
591 d->body = block();
592 curfn = saved;
593 *lambdaend = d;
594 lambdaend = &d->next;
595 return mkexpr(EIDENT, d->name);
596}
597
598static struct decl *
599structdecl(void) {
600 struct decl *d;
601
602 expect(TSTRUCT);
603 d = mkdecl(DSTRUCT, tok);
604 expect(TIDENT);
605 expect(TLBRACE);
606 d->params = params(TRBRACE);
607 return d;
608}
609
610
611static struct expr *
612types(void) {
613 struct expr *t;
614 struct expr *head = nil;
615 struct expr **end = &head;
616
617 do {
618 t = type();
619 *end = t;
620 end = &t->next;
621 } while(consume(TCOMMA));
622 return head;
623}
624
625static struct decl *
626datadecl(void) {
627 struct decl *d;
628 struct ctor *c;
629 struct ctor *head = nil;
630 struct ctor **end = &head;
631 struct param *sp;
632
633 /* the declaration */
634 expect(TDATATYPE);
635 d = mkdecl(DDATA, tok);
636 expect(TIDENT);
637 d->sparams = sortparams();
638 expect(TLBRACE);
639 /* the constructors */
640 while(consume(TBAR)) {
641 /* constructor-local sorts */
642 sp = sortparams();
643 c = mkctor(tok);
644 expect(TIDENT);
645 c->sparams = sp;
646 if(consume(TLPAREN)) {
647 c->args = types();
648 expect(TRPAREN);
649 }
650 /*if we declared the consructors result type */
651 if(consume(TCOLON))
652 c->ret = type();
653 else if(!d->sparams)
654 c->ret = mkexpr(EIDENT, d->name);
655 *end = c;
656 end = &c->next;
657 }
658 expect(TRBRACE);
659 d->ctors = head;
660 return d;
661}
662
663/* desugar a newtype declaration to a single-constuctor datatype */
664static struct decl *
665newtypedecl(void) {
666 struct decl *d;
667 struct ctor *c;
668
669 expect(TNEWTYPE);
670 d = mkdecl(DDATA, tok);
671 expect(TIDENT);
672 expect(TASSIGN);
673 c = mkctor(d->name);
674 c->args = type();
675 c->ret = mkexpr(EIDENT, d->name);
676 d->ctors = c;
677 d->newtype = 1;
678 return d;
679}
680
681static struct decl *
682sortdefdecl(void) {
683 struct decl *d;
684
685 expect(TSORTDEF);
686 d = mkdecl(DSORTDEF, tok);
687 expect(TIDENT);
688 expect(TASSIGN);
689 if(tok.kind == TLBRACK) {
690 d->sparams = sortparams();
691 if(d->sparams->next)
692 errorat(&d->name.loc, "a sortdef takes only one parameter");
693 } else
694 d->ret = type();
695 return d;
696}
697
698static struct decl *
699typedefdecl(void) {
700 struct decl *d;
701
702 expect(TTYPEDEF);
703 d = mkdecl(DTYPEDEF, tok);
704 expect(TIDENT);
705 d->sparams = sortparams();
706 expect(TASSIGN);
707 d->ret = type();
708 return d;
709}
710
711static struct decl *
712usedecl(void) {
713 struct decl *d;
714
715 expect(TUSE);
716 d = mkdecl(DUSE, tok);
717 if(tok.kind != TSTRING && tok.kind != TUSEPATH)
718 errorat(&tok.loc, "expected module path");
719 next();
720 if(consume(TAS)) {
721 d->alias = tok;
722 expect(TIDENT);
723 }
724 return d;
725}
726
727static struct decl *
728decl(void) {
729 struct decl *d;
730 int pub;
731 int linear;
732 int abstract;
733 int ext;
734
735 pub = consume(TPUB);
736 abstract = consume(TABSTRACT);
737 linear = consume(TLINEAR);
738 ext = consume(TEXTERN);
739 if(abstract && tok.kind != TDATATYPE && tok.kind != TNEWTYPE)
740 errorat(&tok.loc, "only datatypes and newtypes can be abstract");
741 if(ext && tok.kind != TFN)
742 errorat(&tok.loc, "only functions can be extern");
743 if(tok.kind == TFN) {
744 if(linear)
745 errorat(&tok.loc, "functions cannot be linear");
746 d = fndecl(ext);
747 } else if(tok.kind == TSTRUCT)
748 d = structdecl();
749 else if(tok.kind == TDATATYPE)
750 d = datadecl();
751 else if(tok.kind == TNEWTYPE)
752 d = newtypedecl();
753 else if(tok.kind == TTYPEDEF) {
754 if(linear)
755 errorat(&tok.loc, "typedefs cannot be linear");
756 d = typedefdecl();
757 } else if(tok.kind == TSORTDEF) {
758 if(linear)
759 errorat(&tok.loc, "sortdefs cannot be linear");
760 d = sortdefdecl();
761 } else if(tok.kind == TUSE) {
762 if(linear)
763 errorat(&tok.loc, "module imports cannot be linear");
764 d = usedecl();
765 } else
766 errorat(&tok.loc, "malformed declaration");
767
768 d->pub = pub;
769 d->linear = linear;
770 d->abstract = abstract;
771 d->ext = ext;
772 return d;
773}
774
775struct decl *
776parse(const char *file, const char *src) {
777 struct decl *d;
778 struct decl *head = nil;
779 struct decl **end = &head;
780
781 lx.cur = src;
782 lx.linestart = src;
783 lx.line = 1;
784 lx.file = file;
785 lx.afteruse = 0;
786
787 next();
788 lambdas = nil;
789 lambdaend = &lambdas;
790 while(tok.kind != TEOF) {
791 d = decl();
792 *end = d;
793 end = &d->next;
794 }
795 *end = lambdas;
796 return head;
797}