Commit 75d776f9
75d776f9f88cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add hash compact/each_with_object/invert/flatten/assoc/rassoc, loop break value, zip
Hash methods added:
- Hash#compact: filter nil-valued entries (poly-valued hashes), dup for non-poly
- Hash#each_with_object: |(k,v), acc| and |k,v,acc| block forms for all hash kinds
- Hash#invert: typed variants (str_str→str_str, str_int/int_str→poly_poly) + generic
- Hash#flatten: interleave keys/values into flat PolyArray
- Hash#assoc / #rassoc: find pair by key or value, return [k,v] or nil
Other:
- loop { break val }: loop-as-expression via g_loop_break_var + GCC statement expr
- Array#zip without block: build PolyArray of pairs for typed/poly array sources
analyze.c: infer return types for all new hash methods; hash#each_with_object with empty [] seed returns poly_array when hash values are poly
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 75d776f9f88c
- Parent
- 5052ec7afcb7
src/analyze.cmodifiedView file
@@ -462,7 +462,7 @@
462462
}463463
}464464
465-
/* SomeClass.name / .to_s / .inspect -> the class-name string */465+
/* SomeClass.name / .to_s / .inspect / .superclass -> the class-name string */466466
if (recv >= 0 && argc == 0 &&467467
(!strcmp(name, "name") || !strcmp(name, "to_s") || !strcmp(name, "inspect") ||468468
!strcmp(name, "superclass")) &&@@ -470,6 +470,19 @@
470470
nt_str(nt, recv, "name") && comp_class_index(c, nt_str(nt, recv, "name")) >= 0)471471
return TY_STRING;472472
473+
/* SomeClass.ancestors -> PolyArray of class objects */474+
if (recv >= 0 && argc == 0 && !strcmp(name, "ancestors") &&475+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode") &&476+
nt_str(nt, recv, "name") && comp_class_index(c, nt_str(nt, recv, "name")) >= 0)477+
return TY_POLY_ARRAY;478+
479+
/* SomeClass.instance_methods / .public_instance_methods -> PolyArray of symbols */480+
if (recv >= 0 && argc <= 1 &&481+
(!strcmp(name, "instance_methods") || !strcmp(name, "public_instance_methods") ||482+
!strcmp(name, "private_instance_methods") || !strcmp(name, "protected_instance_methods")) &&483+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode"))484+
return TY_POLY_ARRAY;485+
473486
/* self.class.new(...) -> an instance of the enclosing class */474487
if (recv >= 0 && !strcmp(name, "new") && nt_type(nt, recv) &&475488
!strcmp(nt_type(nt, recv), "CallNode") && nt_str(nt, recv, "name") &&@@ -1472,6 +1485,28 @@
14721485
!strcmp(name, "include?") || !strcmp(name, "member?") ||14731486
!strcmp(name, "has_value?") || !strcmp(name, "value?") ||14741487
!strcmp(name, "empty?")) return TY_BOOL;1488+
if (!strcmp(name, "each_with_object") && argc > 0 && argv) {1489+
TyKind at = infer_type(c, argv[0]);1490+
if (at == TY_UNKNOWN) {1491+
const char *a0ty = nt_type(nt, argv[0]);1492+
int an0 = 0;1493+
if (a0ty && !strcmp(a0ty, "ArrayNode")) nt_arr(nt, argv[0], "elements", &an0);1494+
if (a0ty && !strcmp(a0ty, "ArrayNode") && an0 == 0) {1495+
/* When hash values are poly the block pushes poly values, so the1496+
accumulator widens to poly_array */1497+
return ty_hash_val(rt) == TY_POLY ? TY_POLY_ARRAY : TY_INT_ARRAY;1498+
}1499+
}1500+
return at;1501+
}1502+
if (!strcmp(name, "flatten") && argc <= 1) return TY_POLY_ARRAY;1503+
if (!strcmp(name, "invert") && argc == 0) {1504+
/* swap key/value types where we have a typed variant */1505+
if (rt == TY_STR_STR_HASH) return TY_STR_STR_HASH;1506+
return TY_POLY_POLY_HASH;1507+
}1508+
if ((!strcmp(name, "assoc") || !strcmp(name, "rassoc")) && argc == 1) return TY_POLY_ARRAY;1509+
if (!strcmp(name, "compact") && argc == 0) return rt;14751510
}14761511
14771512
/* <str>.encoding.name -> the encoding name string */src/codegen.cmodifiedView file
Loading diff…