Commit 9eebaacd

9eebaacd9502Merged

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

Description

Compound assignment to a namespaced constant

Handle ConstantPathOperatorWriteNode / OrWriteNode / AndWriteNode (M::X += 2, M::X ||= v, M::X &&= v) by reading and writing the backing cst_ slot of the constant named by the path's final component, mirroring the local-variable compound-assignment forms.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
9eebaacd9502
Parent
671aab78a460

src/codegen.cmodifiedView file

@@ -6036,6 +6036,45 @@
60366036 }
60376037 return;
60386038 }
6039+ if (!strcmp(ty, "ConstantPathOperatorWriteNode")) {
6040+ int tgt = nt_ref(nt, id, "target");
6041+ const char *nm = tgt >= 0 ? nt_str(nt, tgt, "name") : NULL;
6042+ LocalVar *cv = nm ? comp_const(c, nm) : NULL;
6043+ if (!cv) { unsupported(c, id, "constant path operator write"); return; }
6044+ const char *op = nt_str(nt, id, "binary_operator");
6045+ int v = nt_ref(nt, id, "value");
6046+ emit_indent(b, indent);
6047+ if (cv->type == TY_STRING && op && !strcmp(op, "+")) {
6048+ buf_printf(b, "cst_%s = sp_str_concat(cst_%s, ", nm, nm); emit_expr(c, v, b); buf_puts(b, ");\n");
6049+ }
6050+ else {
6051+ buf_printf(b, "cst_%s %s= ", nm, op ? op : "+"); emit_expr(c, v, b); buf_puts(b, ";\n");
6052+ }
6053+ return;
6054+ }
6055+ if (!strcmp(ty, "ConstantPathOrWriteNode") || !strcmp(ty, "ConstantPathAndWriteNode")) {
6056+ int is_or = !strcmp(ty, "ConstantPathOrWriteNode");
6057+ int tgt = nt_ref(nt, id, "target");
6058+ const char *nm = tgt >= 0 ? nt_str(nt, tgt, "name") : NULL;
6059+ LocalVar *cv = nm ? comp_const(c, nm) : NULL;
6060+ if (!cv) { unsupported(c, id, "constant path or/and write"); return; }
6061+ int v = nt_ref(nt, id, "value");
6062+ if (cv->type == TY_POLY) {
6063+ emit_indent(b, indent);
6064+ buf_printf(b, "if (%ssp_poly_truthy(cst_%s)) cst_%s = ", is_or ? "!" : "", nm, nm);
6065+ emit_boxed(c, v, b); buf_puts(b, ";\n");
6066+ }
6067+ else if (cv->type == TY_BOOL) {
6068+ emit_indent(b, indent);
6069+ buf_printf(b, "if (%scst_%s) cst_%s = ", is_or ? "!" : "", nm, nm); emit_expr(c, v, b); buf_puts(b, ";\n");
6070+ }
6071+ else if (!is_or) { /* &&= on an always-truthy constant: always assign */
6072+ emit_indent(b, indent);
6073+ buf_printf(b, "cst_%s = ", nm); emit_expr(c, v, b); buf_puts(b, ";\n");
6074+ }
6075+ /* ||= on an always-truthy constant: no-op */
6076+ return;
6077+ }
60396078 if (!strcmp(ty, "GlobalVariableOperatorWriteNode")) {
60406079 const char *nm = nt_str(nt, id, "name");
60416080 LocalVar *lv = nm ? comp_gvar(c, nm + 1) : NULL;