commit 6e2a0fa
shrub
·
2026-11-23 16:55:53 +0000 UTC
parent e18da38
allow sorts in anon function
1 files changed,
+36,
-2
M
parse.c
M
parse.c
+36,
-2
1@@ -5,6 +5,8 @@
2
3 static struct lexer lx;
4 static struct token tok;
5+
6+static struct decl *curfn;
7 static struct decl *lambdas;
8 static struct decl **lambdaend;
9 static usize nlambdas;
10@@ -529,15 +531,39 @@ fndecl(int ext) {
11 if(ext) {
12 if(tok.kind == TLBRACE)
13 errorat(&tok.loc, "extern functions can't have a body");
14- } else d->body = block();
15+ } else {
16+ curfn = d;
17+ d->body = block();
18+ curfn = nil;
19+ }
20 return d;
21 }
22
23+/* lambda gets copies of the enclosing fns sort params */
24+static struct param *
25+outer(void) {
26+ struct param *p, *q;
27+ struct param *head = nil;
28+ struct param **end = &head;
29+
30+ if(!curfn)
31+ return nil;
32+ for(p = curfn->sparams; p; p = p->next) {
33+ q = mkparam(p->name);
34+ q->ann = p->ann;
35+ *end = q;
36+ end = &q->next;
37+ }
38+ return head;
39+}
40+
41 static struct expr *
42 lambda(void) {
43 struct decl *d;
44 stralloc sa = {0};
45 char num[FMTUSIZE];
46+ struct decl *saved;
47+ struct param **end;
48
49 d = mkdecl(DFN, tok);
50 sacatl(&sa, "anon.");
51@@ -546,12 +572,20 @@ lambda(void) {
52 d->name.length = sa.len;
53
54 expect(TFN);
55- /* XXX: sort params ? */
56+ /* the enclosing function's sorts */
57+ d->sparams = outer();
58+ for(end = &d->sparams; *end; end = &(*end)->next)
59+ ;
60+ /* lambda-local sorts */
61+ *end = sortparams();
62 expect(TLPAREN);
63 d->params = params(TRPAREN);
64 expect(TARROW);
65 d->ret = type();
66+ saved = curfn;
67+ curfn = d;
68 d->body = block();
69+ curfn = saved;
70 *lambdaend = d;
71 lambdaend = &d->next;
72 return mkexpr(EIDENT, d->name);