Commit 4466d5a7

4466d5a72487Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · ~3 modified

Description

Add PM_RETURN_NODE expr, @var ||=, Array#<<, report_duration to reduce lrama warnings

  • Handle PM_RETURN_NODE as expression (node type 132) in expr.c
  • Handle PM_INSTANCE_VARIABLE_OR_WRITE_NODE (@var ||= expr) in expr.c, stmt.c, and type.c
  • Add Array#<< as alias for push on IntArray in expr.c and stmt.c
  • Add << and push type inference for ARRAY in type.c
  • Handle report_duration(:label) { block } as no-op wrapper that just executes the block body in both expr.c and stmt.c

All 60 tests pass.

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

Details

Commit
4466d5a72487
Parent
84a68794be0a

src/expr.cmodifiedView file

@@ -1370,7 +1370,8 @@
13701370 r = sfmt("sp_IntArray_join(%s, %s)", recv, arg);
13711371 free(arg);
13721372 }
1373- else if (strcmp(method, "push") == 0 && call->arguments &&
1373+ else if ((strcmp(method, "push") == 0 || strcmp(method, "<<") == 0) &&
1374+ call->arguments &&
13741375 call->arguments->arguments.size == 1) {
13751376 char *arg = codegen_expr(ctx, call->arguments->arguments.nodes[0]);
13761377 r = sfmt("sp_IntArray_push(%s, %s)", recv, arg);
@@ -3677,6 +3678,18 @@
36773678 return sfmt("_sl_%d", tmp);
36783679 }
36793680
3681+ /* report_duration(:label) { block } → just execute the block, return block result */
3682+ if (!call->receiver && strcmp(method, "report_duration") == 0 &&
3683+ call->block && PM_NODE_TYPE(call->block) == PM_BLOCK_NODE) {
3684+ pm_block_node_t *blk = (pm_block_node_t *)call->block;
3685+ if (blk->body) {
3686+ free(method);
3687+ return codegen_expr(ctx, blk->body);
3688+ }
3689+ free(method);
3690+ return xstrdup("0");
3691+ }
3692+
36803693 /* Fallback — unsupported method call */
36813694 {
36823695 /* Get receiver type description for warning */
@@ -4226,6 +4239,36 @@
42264239 case PM_KEYWORD_HASH_NODE:
42274240 return xstrdup("0 /* kwargs */");
42284241
4242+ case PM_RETURN_NODE: {
4243+ pm_return_node_t *ret = (pm_return_node_t *)node;
4244+ if (ret->arguments && ret->arguments->arguments.size > 0) {
4245+ char *val = codegen_expr(ctx, ret->arguments->arguments.nodes[0]);
4246+ emit(ctx, "return %s;\n", val);
4247+ free(val);
4248+ } else {
4249+ emit(ctx, "return 0;\n");
4250+ }
4251+ return xstrdup("0"); /* unreachable but needed for expression context */
4252+ }
4253+
4254+ case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
4255+ pm_instance_variable_or_write_node_t *n = (pm_instance_variable_or_write_node_t *)node;
4256+ char *ivname = cstr(ctx, n->name);
4257+ const char *field = ivname + 1;
4258+ char *val = codegen_expr(ctx, n->value);
4259+ char *r;
4260+ if (ctx->current_class && ctx->current_class->is_value_type)
4261+ r = sfmt("(self.%s ? self.%s : (self.%s = %s))", field, field, field, val);
4262+ else if (ctx->current_class)
4263+ r = sfmt("(self->%s ? self->%s : (self->%s = %s))", field, field, field, val);
4264+ else if (ctx->current_module)
4265+ r = sfmt("(sp_%s_%s ? sp_%s_%s : (sp_%s_%s = %s))", ctx->current_module->name, field, ctx->current_module->name, field, ctx->current_module->name, field, val);
4266+ else
4267+ r = sfmt("0");
4268+ free(ivname); free(val);
4269+ return r;
4270+ }
4271+
42294272 default:
42304273 fprintf(stderr, "spinel: warning: unsupported expression node type %d\n", PM_NODE_TYPE(node));
42314274 return sfmt("0 /* unsupported expr %d */", PM_NODE_TYPE(node));