Commit db4cb880
db4cb88035feMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add rand/srand, empty hash promotion, $stderr.puts, hash fallback fixes
- analyze.c: rand() -> TY_INT/TY_FLOAT, srand() -> TY_INT; fetch/[] on TY_UNKNOWN locals promotes to typed hash (sym/str/int key); int-key hash with unknown value falls back to TY_POLY_POLY_HASH
- codegen.c: emit rand/srand calls; $stderr/$stdout .puts/.print/.flush dispatch to C fprintf/fputs
Fixes bundle_misc_c_36.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- db4cb88035fe
- Parent
- 014ef53d48f1
src/analyze.cmodifiedView file
@@ -610,6 +610,8 @@
610610
if (!strcmp(name, "Float") && argc == 1) return TY_FLOAT;611611
if (!strcmp(name, "system") && argc >= 1) return TY_BOOL;612612
if (!strcmp(name, "trap") && argc >= 1) return TY_STRING;613+
if (!strcmp(name, "rand")) return argc > 0 ? TY_INT : TY_FLOAT;614+
if (!strcmp(name, "srand")) return TY_INT;613615
}614616
/* Signal.trap / ::Signal.trap */615617
if (recv >= 0 && !strcmp(name, "trap") && argc >= 1) {@@ -913,6 +915,11 @@
913915
!strcmp(nt_type(nt, recv), "KeywordHashNode"))) {914916
return TY_POLY; /* {}.default -> nil (poly nil) */915917
}918+
/* fetch(key, default) on an unknown/empty hash: return the default type */919+
if (recv >= 0 && !strcmp(name, "fetch") && argc >= 2 && !ty_is_hash(rt)) {920+
TyKind dt = infer_type(c, argv[1]);921+
if (dt != TY_UNKNOWN) return dt;922+
}916923
if (recv >= 0 && ty_is_hash(rt)) {917924
if (!strcmp(name, "[]")) return ty_hash_val(rt);918925
if (!strcmp(name, "[]=")) return argc >= 2 ? infer_type(c, argv[1]) : ty_hash_val(rt);@@ -2682,6 +2689,26 @@
26822689
else if (name && !strcmp(name, "[]=") && an == 2) {26832690
kt = infer_type(c, argv[0]); vt = infer_type(c, argv[1]);26842691
}2692+
else if (name && (!strcmp(name, "fetch") || !strcmp(name, "[]")) && an >= 1) {2693+
/* hash.fetch(key,..) / hash[key]: promote TY_UNKNOWN local to a typed hash.2694+
Only fires when the slot is currently TY_UNKNOWN (empty hash). */2695+
TyKind rslot = TY_UNKNOWN;2696+
const char *rrty = nt_type(nt, recv);2697+
if (rrty && !strcmp(rrty, "LocalVariableReadNode")) {2698+
const char *rnm2 = nt_str(nt, recv, "name");2699+
LocalVar *lv2 = rnm2 ? scope_local(comp_scope_of(c, recv), rnm2) : NULL;2700+
if (lv2) rslot = lv2->type;2701+
}2702+
else if (rrty && !strcmp(rrty, "InstanceVariableReadNode")) {2703+
/* handled via slot below if it's TY_UNKNOWN */2704+
}2705+
if (rslot != TY_UNKNOWN) continue; /* already typed, skip */2706+
kt = infer_type(c, argv[0]);2707+
if (kt == TY_SYMBOL) { vt = TY_INT; /* dummy: sym hash val is always poly */ }2708+
else if (kt == TY_STRING) { vt = TY_POLY; /* will map to STR_POLY */ }2709+
else if (kt == TY_INT) { vt = TY_POLY; /* will map to POLY_POLY */ }2710+
else continue;2711+
}26852712
else continue;26862713
}26872714
else if (!strcmp(ty, "IndexOperatorWriteNode")) {@@ -2739,8 +2766,12 @@
27392766
if (*slot != TY_UNKNOWN && ty_is_array(*slot)) continue;27402767
if (*slot != TY_UNKNOWN && !ty_is_hash(*slot)) continue;27412768
TyKind hv = ty_hash_of(TY_INT, vt);2742-
if (hv == TY_UNKNOWN) continue;2743-
if (*slot != TY_UNKNOWN && *slot != hv) continue;2769+
if (hv == TY_UNKNOWN) hv = TY_POLY_POLY_HASH; /* int key + unknown val type */2770+
if (*slot != TY_UNKNOWN && *slot != hv) {2771+
/* widen to poly-poly if mismatch */2772+
if (ty_is_hash(*slot)) { *slot = TY_POLY_POLY_HASH; }2773+
continue;2774+
}27442775
*slot = hv;27452776
}27462777
else if (kt == TY_STRING) {src/codegen.cmodifiedView file
Loading diff…