Commit 108810da
108810da9006Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Add M3: typed arrays (int/str/float)
Extend the type lattice with int_array/float_array/str_array/poly_array and implement homogeneous typed arrays end to end.
- types: array kinds + ty_array_of/ty_array_elem/ty_is_array helpers.
- analyze: ArrayNode infers its kind from the unified element type; array-receiver methods ([], length/size, sum, first/last, join, inspect/to_s, empty?/include?, push/<<, reverse/sort/uniq) get return types; array literals flow through the param/return/local fixpoint.
- codegen: a statement-prelude mechanism (array/hash literals lower to temp construction that runs before the using statement); array literal build via sp_Array_new + _push temps; index get; index set / push / << as statements; length/sum/join/inspect; GC-rooted array locals and params; p of arrays via _inspect.
A program building int/str/float arrays with indexing, push/<<, join,
sum, and p matches CRuby. make test: 4 pass. Iteration-based array
tests await block support (each/map), the next major lever.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 108810da9006
- Parent
- c57e39a832a0
src/analyze.cmodifiedView file
@@ -50,6 +50,23 @@
5050
if (mi >= 0) return c->scopes[mi].ret;5151
}5252
53+
/* array receiver methods */54+
if (recv >= 0 && ty_is_array(rt)) {55+
if (!strcmp(name, "[]")) return ty_array_elem(rt);56+
if (!strcmp(name, "length") || !strcmp(name, "size") ||57+
!strcmp(name, "count") || !strcmp(name, "index")) return TY_INT;58+
if (!strcmp(name, "sum")) return ty_array_elem(rt);59+
if (!strcmp(name, "first") || !strcmp(name, "last") ||60+
!strcmp(name, "min") || !strcmp(name, "max")) return ty_array_elem(rt);61+
if (!strcmp(name, "join")) return TY_STRING;62+
if (!strcmp(name, "inspect") || !strcmp(name, "to_s")) return TY_STRING;63+
if (!strcmp(name, "empty?") || !strcmp(name, "include?")) return TY_BOOL;64+
if (!strcmp(name, "push") || !strcmp(name, "<<") ||65+
!strcmp(name, "reverse") || !strcmp(name, "sort") ||66+
!strcmp(name, "uniq") || !strcmp(name, "to_a")) return rt;67+
if (!strcmp(name, "[]=")) return ty_array_elem(rt);68+
}69+
5370
if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0)5471
return ty_is_numeric(rt) ? rt : TY_UNKNOWN;5572
if (!strcmp(name, "!")) return TY_BOOL;@@ -131,6 +148,14 @@
131148
int s = nt_ref(nt, id, "statements");132149
return s >= 0 ? infer_type(c, s) : TY_NIL;133150
}151+
if (!strcmp(ty, "ArrayNode")) {152+
int n = 0;153+
const int *els = nt_arr(nt, id, "elements", &n);154+
if (n == 0) return TY_POLY_ARRAY;155+
TyKind e = TY_UNKNOWN;156+
for (int k = 0; k < n; k++) e = ty_unify(e, infer_type(c, els[k]));157+
return ty_array_of(e);158+
}134159
if (!strcmp(ty, "CallNode")) return infer_call(c, id);135160
136161
return TY_UNKNOWN;src/codegen.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…