Commit 440f2775

440f2775f6c3Merged

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

Description

Support value-form || and && (a || default idiom)

a || b -> truthy(a) ? a : b; a && b -> truthy(a) ? b : a. The left is evaluated once into a temp; truthiness follows its static type (poly via sp_poly_truthy, bool directly, concrete types always truthy). Both arms widen to the unified result type, boxing when it is poly.

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

Details

Commit
440f2775f6c3
Parent
dc7467e98cbc

src/codegen.cmodifiedView file

@@ -1960,16 +1960,47 @@
19601960 if (!strcmp(ty, "CallNode")) { emit_call(c, id, b); return; }
19611961 if (!strcmp(ty, "SuperNode") || !strcmp(ty, "ForwardingSuperNode")) { emit_super(c, id, b); return; }
19621962 if (!strcmp(ty, "AndNode") || !strcmp(ty, "OrNode")) {
1963+ int is_and = !strcmp(ty, "AndNode");
19631964 int left = nt_ref(nt, id, "left"), right = nt_ref(nt, id, "right");
1964- if (comp_ntype(c, left) == TY_BOOL && comp_ntype(c, right) == TY_BOOL) {
1965+ TyKind lt = comp_ntype(c, left), res = comp_ntype(c, id);
1966+ if (lt == TY_BOOL && comp_ntype(c, right) == TY_BOOL) {
19651967 buf_puts(b, "(");
19661968 emit_expr(c, left, b);
1967- buf_puts(b, !strcmp(ty, "AndNode") ? " && " : " || ");
1969+ buf_puts(b, is_and ? " && " : " || ");
19681970 emit_expr(c, right, b);
19691971 buf_puts(b, ")");
19701972 return;
19711973 }
1972- unsupported(c, id, "&&/|| (non-bool operands)");
1974+ /* value form: a || b -> truthy(a) ? a : b ; a && b -> truthy(a) ? b : a.
1975+ Evaluate the left once into a temp; results widen to the unified type. */
1976+ int t = ++g_tmp;
1977+ buf_puts(b, "({ ");
1978+ emit_ctype(c, lt == TY_UNKNOWN ? res : lt, b);
1979+ buf_printf(b, " _t%d = ", t); emit_expr(c, left, b); buf_puts(b, "; ");
1980+ if (lt == TY_POLY) buf_printf(b, "sp_poly_truthy(_t%d)", t);
1981+ else if (lt == TY_BOOL) buf_printf(b, "_t%d", t);
1982+ else if (lt == TY_NIL) buf_puts(b, "0");
1983+ else buf_puts(b, "1"); /* concrete value: always truthy */
1984+ buf_puts(b, " ? ");
1985+ /* the "kept-left" arm and the "right" arm, each widened to res */
1986+ #define EMIT_ARM(IS_RIGHT) do { \
1987+ if (IS_RIGHT) { if (res == TY_POLY && comp_ntype(c, right) != TY_POLY) emit_boxed(c, right, b); else emit_expr(c, right, b); } \
1988+ else { if (res == TY_POLY && lt != TY_POLY) { /* box temp */ \
1989+ Buf _vb; memset(&_vb,0,sizeof _vb); buf_printf(&_vb, "_t%d", t); \
1990+ /* reuse emit_boxed by faking: just box by left type */ \
1991+ if (lt==TY_INT) buf_printf(b, "sp_box_int(_t%d)", t); \
1992+ else if (lt==TY_STRING) buf_printf(b, "sp_box_str(_t%d)", t); \
1993+ else if (lt==TY_FLOAT) buf_printf(b, "sp_box_float(_t%d)", t); \
1994+ else if (lt==TY_BOOL) buf_printf(b, "sp_box_bool(_t%d)", t); \
1995+ else if (lt==TY_SYMBOL) buf_printf(b, "sp_box_sym(_t%d)", t); \
1996+ else buf_printf(b, "_t%d", t); free(_vb.p); } \
1997+ else buf_printf(b, "_t%d", t); } \
1998+ } while (0)
1999+ if (is_and) { EMIT_ARM(1); buf_puts(b, " : "); EMIT_ARM(0); }
2000+ else { EMIT_ARM(0); buf_puts(b, " : "); EMIT_ARM(1); }
2001+ #undef EMIT_ARM
2002+ buf_printf(b, "; })");
2003+ return;
19732004 }
19742005
19752006 unsupported(c, id, "expression");