Commit 107d0bd9
107d0bd9a0adMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Add M7: typed hashes (str/int keys, int/str values)
Implement four homogeneous hash variants (StrInt, StrStr, IntInt, IntStr) end to end.
- types: hash kinds + ty_hash_of/ty_hash_key/ty_hash_val/ty_hash_cname/ ty_is_hash (table-driven).
- analyze: HashNode infers its variant from unified AssocNode key/value types; hash-receiver methods ([], fetch, length/size, has_key?/key?/ include?/member?, keys/values, empty?) get return types; hash.each binds both block params (k, v).
- codegen: hash C types + GC-rooted locals; hash literal build via sp_Hash_new/_set temps; [] (get_opt), []=, fetch, length, has_key?, keys, inspect; hash.each lowered to a for-loop over ->order[]; h[k] op= v via IndexOperatorWriteNode (receiver/key evaluated once into temps); p of hashes via _inspect.
A hash program (literal, [], []=, length, has_key?, each, op-assign)
matches CRuby. make test: 8 pass.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 107d0bd9a0ad
- Parent
- e4f29309c880
src/analyze.cmodifiedView file
@@ -67,6 +67,21 @@
6767
if (!strcmp(name, "[]=")) return ty_array_elem(rt);6868
}6969
70+
/* hash receiver methods */71+
if (recv >= 0 && ty_is_hash(rt)) {72+
if (!strcmp(name, "[]")) return ty_hash_val(rt);73+
if (!strcmp(name, "[]=")) return ty_hash_val(rt);74+
if (!strcmp(name, "fetch")) return ty_hash_val(rt);75+
if (!strcmp(name, "length") || !strcmp(name, "size") ||76+
!strcmp(name, "count")) return TY_INT;77+
if (!strcmp(name, "keys")) return ty_array_of(ty_hash_key(rt));78+
if (!strcmp(name, "values")) return ty_array_of(ty_hash_val(rt));79+
if (!strcmp(name, "has_key?") || !strcmp(name, "key?") ||80+
!strcmp(name, "include?") || !strcmp(name, "member?") ||81+
!strcmp(name, "has_value?") || !strcmp(name, "value?") ||82+
!strcmp(name, "empty?")) return TY_BOOL;83+
}84+
7085
/* string receiver methods */7186
if (recv >= 0 && rt == TY_STRING) {7287
if (!strcmp(name, "upcase") || !strcmp(name, "downcase") ||@@ -177,6 +192,19 @@
177192
for (int k = 0; k < n; k++) e = ty_unify(e, infer_type(c, els[k]));178193
return ty_array_of(e);179194
}195+
if (!strcmp(ty, "HashNode")) {196+
int n = 0;197+
const int *els = nt_arr(nt, id, "elements", &n);198+
if (n == 0) return TY_UNKNOWN;199+
TyKind kt = TY_UNKNOWN, vt = TY_UNKNOWN;200+
for (int k = 0; k < n; k++) {201+
const char *aty = nt_type(nt, els[k]);202+
if (!aty || strcmp(aty, "AssocNode")) return TY_UNKNOWN;203+
kt = ty_unify(kt, infer_type(c, nt_ref(nt, els[k], "key")));204+
vt = ty_unify(vt, infer_type(c, nt_ref(nt, els[k], "value")));205+
}206+
return ty_hash_of(kt, vt);207+
}180208
if (!strcmp(ty, "CallNode")) return infer_call(c, id);181209
182210
return TY_UNKNOWN;@@ -353,6 +381,21 @@
353381
ty_is_array(rt))354382
pt = ty_array_elem(rt);355383
384+
/* hash.each { |k, v| } binds two params */385+
if (!strcmp(name, "each") && ty_is_hash(rt)) {386+
Scope *hs = comp_scope_of(c, block);387+
LocalVar *kp = scope_local_intern(hs, p0);388+
TyKind km = ty_unify(kp->type, ty_hash_key(rt));389+
if (km != kp->type) { kp->type = km; changed = 1; }390+
const char *p1 = block_param_name(c, block, 1);391+
if (p1) {392+
LocalVar *vp = scope_local_intern(hs, p1);393+
TyKind vm = ty_unify(vp->type, ty_hash_val(rt));394+
if (vm != vp->type) { vp->type = vm; changed = 1; }395+
}396+
continue;397+
}398+
356399
if (pt == TY_UNKNOWN) continue;357400
Scope *s = comp_scope_of(c, block);358401
LocalVar *lv = scope_local_intern(s, p0);src/codegen.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…