Commit 1dd6a2db

1dd6a2db58f7Merged

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

Description

style: generated C code uses 2-space indent and newline before else

  • emit() indent per level: 4 spaces → 2 spaces
  • emit string literals: " " → " " for generated code indentation
  • } else { → }\nelse { in generated if/else/case/rescue output
  • Fix "return 0;" indent in main()

60/60 tests, 39/39 benchmarks pass.

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

Details

Commit
1dd6a2db58f7
Parent
9d6698f8c346

src/codegen.cmodifiedView file

@@ -88,7 +88,7 @@
8888 /* ------------------------------------------------------------------ */
8989
9090 void emit(codegen_ctx_t *ctx, const char *fmt, ...) {
91- for (int i = 0; i < ctx->indent; i++) fprintf(ctx->out, " ");
91+ for (int i = 0; i < ctx->indent; i++) fprintf(ctx->out, " ");
9292 va_list ap;
9393 va_start(ap, fmt);
9494 vfprintf(ctx->out, fmt, ap);
@@ -2947,16 +2947,16 @@
29472947 if (!has_gc_fields) continue;
29482948
29492949 emit_raw(ctx, "static void sp_%s_gc_scan(void *obj) {\n", cls->name);
2950- emit_raw(ctx, " sp_%s *o = (sp_%s *)obj;\n", cls->name, cls->name);
2950+ emit_raw(ctx, " sp_%s *o = (sp_%s *)obj;\n", cls->name, cls->name);
29512951 for (int j = 0; j < cls->ivar_count; j++) {
29522952 ivar_info_t *iv = &cls->ivars[j];
29532953 /* Special: Scene.spheres array */
29542954 if (strcmp(cls->name, "Scene") == 0 && strcmp(iv->name, "spheres") == 0) {
2955- emit_raw(ctx, " for (int _i = 0; _i < 3; _i++) sp_gc_mark(o->spheres[_i]);\n");
2955+ emit_raw(ctx, " for (int _i = 0; _i < 3; _i++) sp_gc_mark(o->spheres[_i]);\n");
29562956 continue;
29572957 }
29582958 if (is_gc_type(ctx, iv->type))
2959- emit_raw(ctx, " sp_gc_mark(o->%s);\n", iv->name);
2959+ emit_raw(ctx, " sp_gc_mark(o->%s);\n", iv->name);
29602960 }
29612961 emit_raw(ctx, "}\n\n");
29622962 }
@@ -2992,12 +2992,12 @@
29922992 emit_raw(ctx, "/* ---- sp_Proc runtime ---- */\n");
29932993 emit_raw(ctx, "typedef struct { sp_block_fn fn; void *env; } sp_Proc;\n");
29942994 emit_raw(ctx, "static sp_Proc *sp_Proc_new(sp_block_fn fn, void *env) {\n");
2995- emit_raw(ctx, " sp_Proc *p = (sp_Proc *)malloc(sizeof(sp_Proc));\n");
2996- emit_raw(ctx, " p->fn = fn; p->env = env;\n");
2997- emit_raw(ctx, " return p;\n");
2995+ emit_raw(ctx, " sp_Proc *p = (sp_Proc *)malloc(sizeof(sp_Proc));\n");
2996+ emit_raw(ctx, " p->fn = fn; p->env = env;\n");
2997+ emit_raw(ctx, " return p;\n");
29982998 emit_raw(ctx, "}\n");
29992999 emit_raw(ctx, "static mrb_int sp_Proc_call(sp_Proc *p, mrb_int arg) {\n");
3000- emit_raw(ctx, " return p->fn(p->env, arg);\n");
3000+ emit_raw(ctx, " return p->fn(p->env, arg);\n");
30013001 emit_raw(ctx, "}\n\n");
30023002 }
30033003
@@ -3246,8 +3246,8 @@
32463246
32473247 emit_raw(ctx, "static const char *sp_program_name = \"\";\n");
32483248 emit_raw(ctx, "int main(int argc, char **argv) {\n");
3249- emit_raw(ctx, " (void)argc; (void)argv;\n");
3250- emit_raw(ctx, " sp_program_name = argv[0];\n");
3249+ emit_raw(ctx, " (void)argc; (void)argv;\n");
3250+ emit_raw(ctx, " sp_program_name = argv[0];\n");
32513251
32523252 /* Variable declarations for top-level */
32533253 for (int i = 0; i < ctx->var_count; i++) {
@@ -3256,43 +3256,43 @@
32563256 char *ct = vt_ctype(ctx, v->type, false);
32573257 char *cn = make_cname(v->name, v->is_constant);
32583258 if (v->type.kind == SPINEL_TYPE_POLY) {
3259- emit_raw(ctx, " sp_RbValue %s = sp_box_nil();\n", cn);
3259+ emit_raw(ctx, " sp_RbValue %s = sp_box_nil();\n", cn);
32603260 }
32613261 else if (v->type.kind == SPINEL_TYPE_RB_ARRAY) {
3262- emit_raw(ctx, " sp_RbArray *%s = NULL;\n", cn);
3262+ emit_raw(ctx, " sp_RbArray *%s = NULL;\n", cn);
32633263 }
32643264 else if (v->type.kind == SPINEL_TYPE_RB_HASH) {
3265- emit_raw(ctx, " sp_RbHash *%s = NULL;\n", cn);
3265+ emit_raw(ctx, " sp_RbHash *%s = NULL;\n", cn);
32663266 }
32673267 else if (v->type.kind == SPINEL_TYPE_PROC) {
3268- emit_raw(ctx, " sp_Val *%s = NULL;\n", cn);
3268+ emit_raw(ctx, " sp_Val *%s = NULL;\n", cn);
32693269 }
32703270 else if (v->type.kind == SPINEL_TYPE_ARRAY) {
3271- emit_raw(ctx, " sp_IntArray *%s = NULL;\n", cn);
3271+ emit_raw(ctx, " sp_IntArray *%s = NULL;\n", cn);
32723272 }
32733273 else if (v->type.kind == SPINEL_TYPE_FLOAT_ARRAY) {
3274- emit_raw(ctx, " sp_FloatArray *%s = NULL;\n", cn);
3274+ emit_raw(ctx, " sp_FloatArray *%s = NULL;\n", cn);
32753275 }
32763276 else if (v->type.kind == SPINEL_TYPE_SP_STRING) {
3277- emit_raw(ctx, " sp_String *%s = NULL;\n", cn);
3277+ emit_raw(ctx, " sp_String *%s = NULL;\n", cn);
32783278 }
32793279 else if (v->type.kind == SPINEL_TYPE_VALUE || v->type.kind == SPINEL_TYPE_UNKNOWN) {
32803280 /* In lambda mode, VALUE vars might hold sp_StrArray* or other pointers */
3281- emit_raw(ctx, " void *%s = NULL;\n", cn);
3281+ emit_raw(ctx, " void *%s = NULL;\n", cn);
32823282 }
32833283 else {
3284- emit_raw(ctx, " %s %s%s;\n", ct, cn, default_init_for_type(v->type.kind));
3284+ emit_raw(ctx, " %s %s%s;\n", ct, cn, default_init_for_type(v->type.kind));
32853285 }
32863286 free(ct); free(cn);
32873287 }
32883288 emit_raw(ctx, "\n");
32893289
32903290 if (ctx->needs_regexp)
3291- emit_raw(ctx, " sp_regexp_init();\n\n");
3291+ emit_raw(ctx, " sp_regexp_init();\n\n");
32923292 /* Generate top-level code (this will collect lambda functions into lambda_buf) */
32933293 codegen_stmts(ctx, (pm_node_t *)prog->statements);
32943294
3295- emit_raw(ctx, "\n return 0;\n");
3295+ emit_raw(ctx, "\n return 0;\n");
32963296 emit_raw(ctx, "}\n");
32973297 fclose(main_buf);
32983298
@@ -3396,8 +3396,8 @@
33963396
33973397 emit_raw(ctx, "static const char *sp_program_name = \"\";\n");
33983398 emit_raw(ctx, "int main(int argc, char **argv) {\n");
3399- emit_raw(ctx, " sp_program_name = argv[0];\n");
3400- emit_raw(ctx, " sp_argv.data = (const char **)(argv + 1); sp_argv.len = argc - 1;\n");
3399+ emit_raw(ctx, " sp_program_name = argv[0];\n");
3400+ emit_raw(ctx, " sp_argv.data = (const char **)(argv + 1); sp_argv.len = argc - 1;\n");
34013401
34023402 /* Variable declarations for top-level (skip constants — they're global statics) */
34033403 const char *vol = ctx->needs_exc ? "volatile " : "";
@@ -3408,78 +3408,78 @@
34083408 char *cn = make_cname(v->name, v->is_constant);
34093409 const char *init = default_init_for_type(v->type.kind);
34103410 if (v->type.kind == SPINEL_TYPE_ARRAY) {
3411- emit_raw(ctx, " sp_IntArray *%s = NULL;\n", cn);
3411+ emit_raw(ctx, " sp_IntArray *%s = NULL;\n", cn);
34123412 if (ctx->needs_gc)
3413- emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
3413+ emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
34143414 free(ct); free(cn);
34153415 continue;
34163416 }
34173417 else if (v->type.kind == SPINEL_TYPE_FLOAT_ARRAY) {
3418- emit_raw(ctx, " sp_FloatArray *%s = NULL;\n", cn);
3418+ emit_raw(ctx, " sp_FloatArray *%s = NULL;\n", cn);
34193419 if (ctx->needs_gc)
3420- emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
3420+ emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
34213421 free(ct); free(cn);
34223422 continue;
34233423 }
34243424 else if (v->type.kind == SPINEL_TYPE_HASH) {
3425- emit_raw(ctx, " sp_StrIntHash *%s = NULL;\n", cn);
3425+ emit_raw(ctx, " sp_StrIntHash *%s = NULL;\n", cn);
34263426 if (ctx->needs_gc)
3427- emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
3427+ emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
34283428 free(ct); free(cn);
34293429 continue;
34303430 }
34313431 else if (v->type.kind == SPINEL_TYPE_POLY) {
3432- emit_raw(ctx, " sp_RbValue %s%s = sp_box_nil();\n", vol, cn);
3432+ emit_raw(ctx, " sp_RbValue %s%s = sp_box_nil();\n", vol, cn);
34333433 free(ct); free(cn);
34343434 continue;
34353435 }
34363436 else if (v->type.kind == SPINEL_TYPE_RB_ARRAY) {
3437- emit_raw(ctx, " sp_RbArray *%s = NULL;\n", cn);
3437+ emit_raw(ctx, " sp_RbArray *%s = NULL;\n", cn);
34383438 free(ct); free(cn);
34393439 continue;
34403440 }
34413441 else if (v->type.kind == SPINEL_TYPE_RB_HASH) {
3442- emit_raw(ctx, " sp_RbHash *%s = NULL;\n", cn);
3442+ emit_raw(ctx, " sp_RbHash *%s = NULL;\n", cn);
34433443 free(ct); free(cn);
34443444 continue;
34453445 }
34463446 else if (v->type.kind == SPINEL_TYPE_PROC && !ctx->lambda_mode) {
3447- emit_raw(ctx, " sp_Proc *%s = NULL;\n", cn);
3447+ emit_raw(ctx, " sp_Proc *%s = NULL;\n", cn);
34483448 free(ct); free(cn);
34493449 continue;
34503450 }
34513451 else if (v->type.kind == SPINEL_TYPE_SP_STRING) {
3452- emit_raw(ctx, " sp_String *%s = NULL;\n", cn);
3452+ emit_raw(ctx, " sp_String *%s = NULL;\n", cn);
34533453 free(ct); free(cn);
34543454 continue;
34553455 }
34563456 else if (v->type.kind == SPINEL_TYPE_FILE) {
3457- emit_raw(ctx, " sp_File *%s = NULL;\n", cn);
3457+ emit_raw(ctx, " sp_File *%s = NULL;\n", cn);
34583458 free(ct); free(cn);
34593459 continue;
34603460 }
34613461 else if (v->type.kind == SPINEL_TYPE_OBJECT) {
34623462 class_info_t *vc = find_class(ctx, v->type.klass);
34633463 if (vc && !vc->is_value_type) {
3464- emit_raw(ctx, " %s *%s = NULL;\n", ct, cn);
3464+ emit_raw(ctx, " %s *%s = NULL;\n", ct, cn);
34653465 if (ctx->needs_gc)
3466- emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
3466+ emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
34673467 free(ct); free(cn);
34683468 continue;
34693469 }
34703470 init = "";
34713471 }
3472- emit_raw(ctx, " %s%s %s%s;\n", vol, ct, cn, init);
3472+ emit_raw(ctx, " %s%s %s%s;\n", vol, ct, cn, init);
34733473 free(ct); free(cn);
34743474 }
34753475 emit_raw(ctx, "\n");
34763476
34773477 if (ctx->needs_regexp)
3478- emit_raw(ctx, " sp_regexp_init();\n\n");
3478+ emit_raw(ctx, " sp_regexp_init();\n\n");
34793479 /* Top-level code */
34803480 codegen_stmts(ctx, (pm_node_t *)prog->statements);
34813481
3482- emit_raw(ctx, "\n return 0;\n");
3482+ emit_raw(ctx, "\n return 0;\n");
34833483 emit_raw(ctx, "}\n");
34843484
34853485 fclose(code_buf);