Commit e245f491
e245f491ee75Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Poly Tier 2b: is_a?/instance_of?, truthiness, each, interpolation
- poly.is_a?/kind_of?(Class): runtime tag check for builtins (Integer/ String/Float/Symbol/Nil/True/False/Numeric/Array) and cls_id ancestry for user classes; instance_of? is exact-cls_id only.
- if/while over a poly condition -> sp_poly_truthy; nil condition -> false.
- poly_array.each { |x| } iterates with a boxed element.
- String interpolation of poly / poly_array / typed array / object (to_s).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- e245f491ee75
- Parent
- 8bd71dae0c9b
src/codegen.cmodifiedView file
@@ -710,6 +710,44 @@
710710
unsupported(c, id, "comparison");711711
}712712
713+
/* poly.is_a?(Class) / kind_of?: runtime tag/cls_id check */714+
if (recv >= 0 && rt == TY_POLY && argc == 1 &&715+
(!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?"))) {716+
const char *cty = nt_type(nt, argv[0]);717+
const char *cn = cty && !strcmp(cty, "ConstantReadNode") ? nt_str(nt, argv[0], "name") : NULL;718+
if (cn) {719+
int t = ++g_tmp;720+
buf_printf(b, "({ sp_RbVal _t%d = ", t); emit_expr(c, recv, b); buf_printf(b, "; ");721+
char v[32]; snprintf(v, sizeof v, "_t%d", t);722+
if (!strcmp(cn, "Integer") || !strcmp(cn, "Fixnum")) buf_printf(b, "%s.tag == SP_TAG_INT", v);723+
else if (!strcmp(cn, "String")) buf_printf(b, "%s.tag == SP_TAG_STR", v);724+
else if (!strcmp(cn, "Float")) buf_printf(b, "%s.tag == SP_TAG_FLT", v);725+
else if (!strcmp(cn, "Symbol")) buf_printf(b, "%s.tag == SP_TAG_SYM", v);726+
else if (!strcmp(cn, "NilClass")) buf_printf(b, "%s.tag == SP_TAG_NIL", v);727+
else if (!strcmp(cn, "TrueClass")) buf_printf(b, "(%s.tag == SP_TAG_BOOL && %s.v.b)", v, v);728+
else if (!strcmp(cn, "FalseClass")) buf_printf(b, "(%s.tag == SP_TAG_BOOL && !%s.v.b)", v, v);729+
else if (!strcmp(cn, "Numeric")) buf_printf(b, "(%s.tag == SP_TAG_INT || %s.tag == SP_TAG_FLT)", v, v);730+
else if (!strcmp(cn, "Array")) buf_printf(b, "(%s.tag == SP_TAG_OBJ && %s.cls_id <= -1 && %s.cls_id >= -8)", v, v, v);731+
else {732+
int cid = comp_class_index(c, cn);733+
int exact = !strcmp(name, "instance_of?");734+
if (cid >= 0) {735+
buf_printf(b, "(%s.tag == SP_TAG_OBJ && (", v);736+
int first = 1;737+
for (int k = 0; k < c->nclasses; k++)738+
if (k == cid || (!exact && is_descendant(c, k, cid))) {739+
buf_printf(b, "%s%s.cls_id == %d", first ? "" : " || ", v, k); first = 0;740+
}741+
if (first) buf_puts(b, "0");742+
buf_puts(b, "))");743+
}744+
else buf_puts(b, "0");745+
}746+
buf_puts(b, "; })");747+
return;748+
}749+
}750+
713751
/* poly receiver: nil? / conversions / a few type-agnostic queries */714752
if (recv >= 0 && rt == TY_POLY && argc == 0) {715753
if (!strcmp(name, "nil?")) { buf_puts(b, "sp_poly_nil_p("); emit_expr(c, recv, b); buf_puts(b, ")"); return; }@@ -1530,6 +1568,19 @@
15301568
}15311569
15321570
/* array.each { |x| ... } */1571+
if (!strcmp(name, "each") && rt == TY_POLY_ARRAY) {1572+
int t = ++g_tmp;1573+
Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);1574+
int ta = ++g_tmp;1575+
emit_indent(b, indent);1576+
buf_printf(b, "sp_PolyArray *_t%d = %s;\n", ta, rb.p ? rb.p : ""); free(rb.p);1577+
emit_indent(b, indent);1578+
buf_printf(b, "for (mrb_int _t%d = 0; _t%d < sp_PolyArray_length(_t%d); _t%d++) {\n", t, t, ta, t);1579+
if (p0) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = sp_PolyArray_get(_t%d, _t%d);\n", p0, ta, t); }1580+
emit_stmts(c, body, b, indent + 1);1581+
emit_indent(b, indent); buf_puts(b, "}\n");1582+
return 1;1583+
}15331584
if (!strcmp(name, "each") && ty_is_array(rt)) {15341585
const char *k = array_kind(rt);15351586
if (!k) return 0;@@ -1634,6 +1685,22 @@
16341685
buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_sym_to_s(");16351686
emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");16361687
}1688+
else if (t == TY_POLY) {1689+
buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_poly_to_s(");1690+
emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");1691+
}1692+
else if (t == TY_POLY_ARRAY) {1693+
buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_PolyArray_inspect(");1694+
emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");1695+
}1696+
else if (ty_is_array(t) && array_kind(t)) {1697+
buf_puts(&fmt, "%s"); buf_printf(&argbuf, "sp_%sArray_inspect(", array_kind(t));1698+
emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");1699+
}1700+
else if (ty_is_object(t) && comp_method_in_chain(c, ty_object_class(t), "to_s", NULL) >= 0) {1701+
buf_puts(&fmt, "%s"); buf_printf(&argbuf, "sp_%s_to_s(", c->classes[ty_object_class(t)].name);1702+
emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");1703+
}16371704
else {16381705
free(fmt.p); free(argbuf.p);16391706
unsupported(c, pid, "interpolation value");@@ -2094,7 +2161,10 @@
20942161
/* ---- control flow ---- */20952162
20962163
static void emit_cond(Compiler *c, int id, Buf *b) {2097-
if (comp_ntype(c, id) != TY_BOOL) unsupported(c, id, "condition (non-bool)");2164+
TyKind t = comp_ntype(c, id);2165+
if (t == TY_POLY) { buf_puts(b, "sp_poly_truthy("); emit_expr(c, id, b); buf_puts(b, ")"); return; }2166+
if (t == TY_NIL) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 0)"); return; }2167+
if (t != TY_BOOL) unsupported(c, id, "condition (non-bool)");20982168
emit_expr(c, id, b);20992169
}21002170