Commit fc7e37e1

fc7e37e1cd43Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Add Array.new(n, v) and String.new constructors

Array.new(n, v) builds n copies of v (n and v evaluated once into temps, element type from v); String.new / String.new(s) -> empty / the string.

make test: 24.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
fc7e37e1cd43
Parent
db12b8a76fa0

src/analyze.cmodifiedView file

@@ -51,12 +51,15 @@
5151 !strcmp(name, "dup") || !strcmp(name, "clone")))
5252 return rt;
5353
54- /* Class.new(...) -> an instance of that class */
54+ /* Class.new(...) -> an instance of that class; built-in .new constructors */
5555 if (recv >= 0 && !strcmp(name, "new")) {
5656 const char *rty = nt_type(nt, recv);
5757 if (rty && !strcmp(rty, "ConstantReadNode")) {
58- int ci = comp_class_index(c, nt_str(nt, recv, "name"));
58+ const char *cn = nt_str(nt, recv, "name");
59+ int ci = comp_class_index(c, cn);
5960 if (ci >= 0) return ty_object(ci);
61+ if (cn && !strcmp(cn, "Array") && argc == 2) return ty_array_of(infer_type(c, argv[1]));
62+ if (cn && !strcmp(cn, "String")) return TY_STRING;
6063 }
6164 }
6265