commit e18da38
shrub
·
2026-11-23 15:45:28 +0000 UTC
parent 7660917
anonymous functions
M
parse.c
+34,
-0
1@@ -5,10 +5,15 @@
2
3 static struct lexer lx;
4 static struct token tok;
5+static struct decl *lambdas;
6+static struct decl **lambdaend;
7+static usize nlambdas;
8+
9 static struct expr *expr(void);
10 static struct expr *block(void);
11 static struct expr *call(void);
12 static struct expr *pipe(void);
13+static struct expr *lambda(void);
14 static struct param *sortparams(void);
15
16 static void
17@@ -168,6 +173,8 @@ primary(void) {
18 tup = mkexpr(ETUPLE, opener);
19 tup->args = head;
20 return tup;
21+ case TFN:
22+ return lambda();
23 default:
24 errorat(&tok.loc, "expected expression");
25 }
26@@ -526,6 +533,30 @@ fndecl(int ext) {
27 return d;
28 }
29
30+static struct expr *
31+lambda(void) {
32+ struct decl *d;
33+ stralloc sa = {0};
34+ char num[FMTUSIZE];
35+
36+ d = mkdecl(DFN, tok);
37+ sacatl(&sa, "anon.");
38+ sacatb(&sa, num, fmtusize(num, ++nlambdas));
39+ d->name.str = sa.s;
40+ d->name.length = sa.len;
41+
42+ expect(TFN);
43+ /* XXX: sort params ? */
44+ expect(TLPAREN);
45+ d->params = params(TRPAREN);
46+ expect(TARROW);
47+ d->ret = type();
48+ d->body = block();
49+ *lambdaend = d;
50+ lambdaend = &d->next;
51+ return mkexpr(EIDENT, d->name);
52+}
53+
54 static struct decl *
55 structdecl(void) {
56 struct decl *d;
57@@ -711,10 +742,13 @@ parse(const char *file, const char *src) {
58 lx.afteruse = 0;
59
60 next();
61+ lambdas = nil;
62+ lambdaend = &lambdas;
63 while(tok.kind != TEOF) {
64 d = decl();
65 *end = d;
66 end = &d->next;
67 }
68+ *end = lambdas;
69 return head;
70 }
M
qbe.c
+2,
-1
1@@ -990,7 +990,8 @@ mangle(const char *mod, struct token name) {
2 * "dir/thing.kav" > k.dir.thing
3 * <thing.kav> > k.sys.thing
4 * <dir/thing.kav> k.sys.dir.thing
5- * anything not allowed gets turned to _ */
6+ * anything not allowed gets turned to _
7+ * lambda functions are k.mod.anon.n, n being the count of lambas, mod being the module they are in*/
8
9 static const char *
10 modname(struct module *m) {