Commit eb6e34a8
eb6e34a85195Merged
Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: flatten nested interpolated-string parts
Adjacent string literals joined by \-continuation, each carrying its
own interpolation, parse to an InterpolatedStringNode whose parts are
themselves InterpolatedStringNodes. emit_interp only handled
StringNode / EmbeddedStatementsNode parts and rejected a nested
interpolated part with unsupported interpolation part.
Pre-flatten the part tree into a single list of leaf parts before the
format/arg assembly, so the whole adjacency emits one sp_sprintf call.
The assembly loop is unchanged; only the part list it walks is flattened.
The flattened part list is heap-allocated, so free it on each early
unsupported() exit as well as the normal return, matching the existing
buffer cleanup at those sites (the rejects longjmp out under
SP_COLLECT_ERRORS).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- eb6e34a85195
- Parent
- 913a83318df6
src/codegen_expr.cmodifiedView file
#include "codegen_internal.h"/* Adjacent string literals joined by `\`-continuation (or `+`-folded at parse time) produce an InterpolatedStringNode whose own parts are themselves InterpolatedStringNodes. Flatten the part tree into one list of leaf parts (StringNode / EmbeddedStatementsNode) so the format/arg assembly below sees a single flat sequence and emits one sp_sprintf call. */static void interp_flatten(const NodeTable *nt, int id, int **out, int *n, int *cap) { int pn = 0; const int *parts = nt_arr(nt, id, "parts", &pn); for (int k = 0; k < pn; k++) { int pid = parts[k]; const char *pty = nt_type(nt, pid); if (pty && !strcmp(pty, "InterpolatedStringNode")) { interp_flatten(nt, pid, out, n, cap); continue; } if (*n >= *cap) { int ncap = *cap ? *cap * 2 : 8; int *grown = realloc(*out, (size_t)ncap * sizeof(int)); if (!grown) { fprintf(stderr, "spinel: out of memory\n"); exit(1); } *out = grown; *cap = ncap; } (*out)[(*n)++] = pid; }}void emit_interp(Compiler *c, int id, Buf *b) { const NodeTable *nt = c->nt; int n = 0; const int *parts = nt_arr(nt, id, "parts", &n); int *flat = NULL, fcap = 0; interp_flatten(nt, id, &flat, &n, &fcap); const int *parts = flat; Buf fmt; memset(&fmt, 0, sizeof fmt); Buf argbuf; memset(&argbuf, 0, sizeof argbuf); Buf decls; memset(&decls, 0, sizeof decls); /* rooted %s-arg temp decls */ /* a bare empty array literal interpolates as "[]" */ int en = 0; nt_arr(nt, expr, "elements", &en); if (en == 0) { buf_puts(&fmt, "%s"); buf_puts(&conv, "\"[]\""); } else { free(fmt.p); free(argbuf.p); free(decls.p); free(conv.p); unsupported(c, pid, "interpolation value"); } else { free(fmt.p); free(argbuf.p); free(decls.p); free(conv.p); free(flat); unsupported(c, pid, "interpolation value"); } } else { free(fmt.p); free(argbuf.p); free(decls.p); free(conv.p); free(fmt.p); free(argbuf.p); free(decls.p); free(conv.p); free(flat); unsupported(c, pid, "interpolation value"); } #undef EMIT_IV nargs++; } else { free(fmt.p); free(argbuf.p); free(fmt.p); free(argbuf.p); free(flat); unsupported(c, pid, "interpolation part"); } } buf_printf(b, "({ %ssp_sprintf(\"%s\"%s); })", decls.p, fmt.p ? fmt.p : "", argbuf.p ? argbuf.p : ""); } free(fmt.p); free(argbuf.p); free(decls.p); free(fmt.p); free(argbuf.p); free(decls.p); free(flat);}/* ---- expression ---- */test/interp_adjacent_concat.rbaddedView file
Loading diff…
test/interp_adjacent_concat.rb.expectedaddedView file
Loading diff…