commit c9abbf6
shrub
·
2026-11-19 12:13:08 +0000 UTC
parent f9465c3
higher order functions
M
check.c
+7,
-0
1@@ -59,6 +59,13 @@ totype(struct expr *e) {
2 return t;
3 case EEXISTS:
4 return totype(e->body);
5+ case EFNTYPE:
6+ for(x = e->args; x; x = x->next) {
7+ a = totype(x);
8+ *end = a;
9+ end = &a->next;
10+ }
11+ return mkfntype(head, totype(e->body));
12 default:
13 errorat(&e->tok.loc, "not a type");
14 }
M
exprs.h
+1,
-0
1@@ -10,3 +10,4 @@ EXPR(ESTRING)
2 EXPR(EHOLE)
3 EXPR(ETUPLE)
4 EXPR(EBORROW)
5+EXPR(EFNTYPE)
M
parse.c
+41,
-0
1@@ -54,6 +54,47 @@ type(void) {
2 e->body = type();
3 return e;
4 }
5+ /* a function type, fn(int, !List[int, n]) -> int */
6+ if(tok.kind == TFN) {
7+ struct expr *a;
8+ struct expr **end;
9+
10+ e = mkexpr(EFNTYPE, tok);
11+ next();
12+ expect(TLPAREN);
13+ end = &e->args;
14+ if(tok.kind != TRPAREN) {
15+ do {
16+ a = type();
17+ *end = a;
18+ end = &a->next;
19+ } while(consume(TCOMMA));
20+ }
21+ expect(TRPAREN);
22+ expect(TARROW);
23+ e->body = type();
24+ return e;
25+ }
26+ /* a tuple type, or a parenthesized one. the elements are types so they can be fn types */
27+ if(tok.kind == TLPAREN) {
28+ struct expr *a;
29+ struct expr *head = nil;
30+ struct expr **end = &head;
31+ struct token opener = tok;
32+
33+ next();
34+ do {
35+ a = type();
36+ *end = a;
37+ end = &a->next;
38+ } while(consume(TCOMMA));
39+ expect(TRPAREN);
40+ if(!head->next)
41+ return head;
42+ e = mkexpr(ETUPLE, opener);
43+ e->args = head;
44+ return e;
45+ }
46 return call();
47 }
48
+5,
-0
1@@ -238,6 +238,11 @@ walk(struct scope *s, enum space sp, struct expr *e) {
2 case EBORROW:
3 walk(s, sp, e->body);
4 break;
5+ case EFNTYPE:
6+ for(a = e->args; a; a = a->next)
7+ walk(s, sp, a);
8+ walk(s, sp, e->body);
9+ break;
10 }
11 }
12
M
run.c
+24,
-2
1@@ -5,6 +5,7 @@ enum valkind {
2 VINT,
3 VCON,
4 VTUPLE,
5+ VFN,
6 };
7
8 struct value {
9@@ -12,6 +13,7 @@ struct value {
10 i64 num;
11 struct binding *con;
12 struct value **fields;
13+ struct decl *fn; /* VFN */
14 };
15
16 static struct value *
17@@ -44,6 +46,16 @@ mktuple(void) {
18 }
19
20
21+static struct value *
22+mkfn(struct decl *d) {
23+ struct value *v;
24+
25+ v = zalloc(sizeof(*v));
26+ v->kind = VFN;
27+ v->fn = d;
28+ return v;
29+}
30+
31 static struct value *
32 mkbool(int b) {
33 return mkcon(b ? btrue : bfalse);
34@@ -188,6 +200,8 @@ eval(struct env *env, struct expr *e) {
35 case EIDENT:
36 if(e->binding->kind == BCTOR)
37 return mkcon(e->binding);
38+ if(e->binding->kind == BFN)
39+ return mkfn(e->binding->decl);
40 return getvar(env, e->binding);
41 case ECALL:
42 if(e->tok.kind != TIDENT) {
43@@ -210,9 +224,17 @@ eval(struct env *env, struct expr *e) {
44 }
45 return l;
46 }
47- if(!e->binding || e->binding->kind != BFN)
48+ if(!e->binding)
49 errorat(&e->tok.loc, "sorry, no idea what to do!");
50- f = e->binding->decl;
51+ if(e->binding->kind == BFN) {
52+ f = e->binding->decl;
53+ } else {
54+ /* calling a function value held in a param or val */
55+ l = getvar(env, e->binding);
56+ if(l->kind != VFN)
57+ errorat(&e->tok.loc, "sorry, no idea what to do!");
58+ f = l->fn;
59+ }
60 for(p = f->params, a = e->args; p && a; p = p->next, a = a->next)
61 callenv = bind(callenv, p->binding, eval(env, a));
62 return eval(callenv, f->body);
M
type.c
+5,
-1
1@@ -53,9 +53,13 @@ unify(struct type *a, struct type *b) {
2 return 0;
3 if(a->kind == YFN && !unify(a->ret, b->ret))
4 return 0;
5- for(x = a->args, y = b->args; x && y; x = x->next, y = y->next)
6+ for(x = a->args, y = b->args; x && y; x = x->next, y = y->next) {
7 if(!unify(x, y))
8 return 0;
9+ /* a function that borrows a param isn't the same as one that takes it */
10+ if(a->kind == YFN && deref(x)->borrow != deref(y)->borrow)
11+ return 0;
12+ }
13 return (!x && !y);
14 }
15