Commit ffc1416a

ffc1416ac3b1Merged

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

Description

Interpolation: exception/nil values and multi-statement blocks

String interpolation now handles an Exception value (#{e} -> its message) and nil (empty string). A multi-statement #{ s1; s2; ...; sN } now runs every statement for its side effects (emitted into the setup buffer) and uses sN's value; when sN is itself an assignment, it is performed and the assigned variable read back as the value.

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

Details

Commit
ffc1416ac3b1
Parent
4fab9fcd5e0d

src/codegen.cmodifiedView file

@@ -3086,46 +3086,73 @@
30863086 int bn = 0;
30873087 const int *body = s >= 0 ? nt_arr(nt, s, "body", &bn) : NULL;
30883088 int expr = bn > 0 ? body[bn - 1] : -1;
3089+ /* `#{ s1; s2; ...; sN }` evaluates every statement in order and uses sN's
3090+ value. Run the leading statements for side effects; if sN is itself an
3091+ assignment, perform it and read the assigned variable back as the value. */
3092+ char vexpr[48]; vexpr[0] = 0;
3093+ for (int si = 0; si + 1 < bn; si++) emit_stmt(c, body[si], g_pre, g_indent);
3094+ const char *ety = expr >= 0 ? nt_type(nt, expr) : NULL;
30893095 TyKind t = comp_ntype(c, expr);
3096+ if (ety && (!strcmp(ety, "LocalVariableWriteNode") || !strcmp(ety, "LocalVariableOperatorWriteNode") ||
3097+ !strcmp(ety, "LocalVariableOrWriteNode") || !strcmp(ety, "LocalVariableAndWriteNode"))) {
3098+ emit_stmt(c, expr, g_pre, g_indent);
3099+ const char *vn = nt_str(nt, expr, "name");
3100+ LocalVar *lvp = vn ? scope_local(comp_scope_of(c, expr), vn) : NULL;
3101+ if (lvp) t = lvp->type;
3102+ int tv = ++g_tmp;
3103+ emit_indent(g_pre, g_indent); emit_ctype(c, t, g_pre);
3104+ buf_printf(g_pre, " _t%d = ", tv); emit_local_ref(c, expr, vn, g_pre); buf_puts(g_pre, ";\n");
3105+ snprintf(vexpr, sizeof vexpr, "_t%d", tv);
3106+ }
3107+ #define EMIT_IV() do { if (vexpr[0]) buf_puts(&argbuf, vexpr); else emit_expr(c, expr, &argbuf); } while (0)
30903108 buf_puts(&argbuf, ", ");
30913109 if (t == TY_INT) {
30923110 buf_puts(&fmt, "%lld"); buf_puts(&argbuf, "(long long)");
3093- emit_expr(c, expr, &argbuf);
3111+ EMIT_IV();
30943112 }
30953113 else if (t == TY_STRING) {
3096- buf_puts(&fmt, "%s"); emit_expr(c, expr, &argbuf);
3114+ buf_puts(&fmt, "%s"); EMIT_IV();
30973115 }
30983116 else if (t == TY_FLOAT) {
30993117 buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_float_to_s(");
3100- emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");
3118+ EMIT_IV(); buf_puts(&argbuf, ")");
31013119 }
31023120 else if (t == TY_BOOL) {
31033121 buf_puts(&fmt, "%s"); buf_puts(&argbuf, "(");
3104- emit_expr(c, expr, &argbuf); buf_puts(&argbuf, " ? \"true\" : \"false\")");
3122+ EMIT_IV(); buf_puts(&argbuf, " ? \"true\" : \"false\")");
31053123 }
31063124 else if (t == TY_SYMBOL) {
31073125 buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_sym_to_s(");
3108- emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");
3126+ EMIT_IV(); buf_puts(&argbuf, ")");
31093127 }
31103128 else if (t == TY_POLY) {
31113129 buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_poly_to_s(");
3112- emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");
3130+ EMIT_IV(); buf_puts(&argbuf, ")");
3131+ }
3132+ else if (t == TY_EXCEPTION) {
3133+ buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_exc_message(");
3134+ EMIT_IV(); buf_puts(&argbuf, ")");
3135+ }
3136+ else if (t == TY_NIL) {
3137+ buf_puts(&fmt, "%s"); buf_puts(&argbuf, "((void)(");
3138+ EMIT_IV(); buf_puts(&argbuf, "), \"\")");
31133139 }
31143140 else if (t == TY_POLY_ARRAY) {
31153141 buf_puts(&fmt, "%s"); buf_puts(&argbuf, "sp_PolyArray_inspect(");
3116- emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");
3142+ EMIT_IV(); buf_puts(&argbuf, ")");
31173143 }
31183144 else if (ty_is_array(t) && array_kind(t)) {
31193145 buf_puts(&fmt, "%s"); buf_printf(&argbuf, "sp_%sArray_inspect(", array_kind(t));
3120- emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");
3146+ EMIT_IV(); buf_puts(&argbuf, ")");
31213147 }
31223148 else if (ty_is_object(t) && comp_method_in_chain(c, ty_object_class(t), "to_s", NULL) >= 0) {
31233149 buf_puts(&fmt, "%s"); buf_printf(&argbuf, "sp_%s_to_s(", c->classes[ty_object_class(t)].name);
3124- emit_expr(c, expr, &argbuf); buf_puts(&argbuf, ")");
3150+ EMIT_IV(); buf_puts(&argbuf, ")");
31253151 }
31263152 else {
31273153 free(fmt.p); free(argbuf.p);
31283154 unsupported(c, pid, "interpolation value");
3155+ #undef EMIT_IV
31293156 }
31303157 nargs++;
31313158 }