Commit 4a3d6d93

4a3d6d932be1Merged

Ryan Seys <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

feat(types): ty_block_yield builtin block-protocol oracle

A single source of truth for the params a block bound to recv.<iter> receives, expressed against the receiver's element/key/value -- the context-free iterators whose yield depends only on the receiver shape. Consumed by the forwarded-proc desugar (and, later, builtin yield-stubs) so that knowledge is encoded once instead of re-derived as scattered method-name lists.

Details

Commit
4a3d6d932be1
Parent
79fbbebb33ce

src/types.cmodifiedView file

@@ -1,5 +1,6 @@
11 #include "types.h"
22 #include <stddef.h>
3+#include <string.h>
34
45 const char *ty_name(TyKind t) {
56 switch (t) {
@@ -135,3 +136,51 @@
135136 }
136137 return ty_unify(a, b);
137138 }
139+
140+/* The single-element-arg array iterators: a block bound to one of these
141+ receives exactly one param = the array element. Enumerated once here so the
142+ knowledge is not re-encoded as scattered method-name lists. */
143+static int ty_is_array_elem_iter(const char *n) {
144+ return !strcmp(n, "each") || !strcmp(n, "map") || !strcmp(n, "collect") ||
145+ !strcmp(n, "select") || !strcmp(n, "reject") || !strcmp(n, "filter") ||
146+ !strcmp(n, "find") || !strcmp(n, "detect") || !strcmp(n, "find_all") ||
147+ !strcmp(n, "sort_by") || !strcmp(n, "min_by") || !strcmp(n, "max_by") ||
148+ !strcmp(n, "count") || !strcmp(n, "sum") || !strcmp(n, "flat_map") ||
149+ !strcmp(n, "filter_map") || !strcmp(n, "partition") || !strcmp(n, "group_by") ||
150+ !strcmp(n, "any?") || !strcmp(n, "all?") || !strcmp(n, "none?") ||
151+ !strcmp(n, "one?") || !strcmp(n, "take_while") || !strcmp(n, "drop_while") ||
152+ !strcmp(n, "reverse_each") || !strcmp(n, "each_entry") || !strcmp(n, "find_index");
153+}
154+
155+int ty_block_yield(TyKind recv, const char *name, TyKind *out, int max) {
156+ if (!name || max < 1) return 0;
157+#define BY_PUT(i, t) do { if ((i) < max) out[i] = (t); } while (0)
158+ if (ty_is_array(recv)) {
159+ TyKind e = ty_array_elem(recv);
160+ if (ty_is_array_elem_iter(name)) { BY_PUT(0, e); return 1; }
161+ if (!strcmp(name, "each_with_index")) { BY_PUT(0, e); BY_PUT(1, TY_INT); return 2; }
162+ return 0;
163+ }
164+ if (ty_is_hash(recv)) {
165+ if (!strcmp(name, "each") || !strcmp(name, "each_pair")) {
166+ BY_PUT(0, ty_hash_key(recv)); BY_PUT(1, ty_hash_val(recv)); return 2;
167+ }
168+ if (!strcmp(name, "each_key")) { BY_PUT(0, ty_hash_key(recv)); return 1; }
169+ if (!strcmp(name, "each_value")) { BY_PUT(0, ty_hash_val(recv)); return 1; }
170+ return 0;
171+ }
172+ if (recv == TY_RANGE) {
173+ /* a range yields ints to its element iterators */
174+ if (!strcmp(name, "each_with_index")) { BY_PUT(0, TY_INT); BY_PUT(1, TY_INT); return 2; }
175+ if (ty_is_array_elem_iter(name)) { BY_PUT(0, TY_INT); return 1; }
176+ return 0;
177+ }
178+ if (recv == TY_INT) {
179+ if (!strcmp(name, "times") || !strcmp(name, "upto") || !strcmp(name, "downto")) {
180+ BY_PUT(0, TY_INT); return 1;
181+ }
182+ return 0;
183+ }
184+ return 0;
185+#undef BY_PUT
186+}