Commit 34d56ece
34d56ecea51aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 3 files · +1 added · ~2 modified
Description
feat: Symbol Phase 2 Step 4f - SP_TAG_SYM poly tag
Extend the NaN-boxed sp_RbVal tagged union with SP_TAG_SYM so symbols survive round-tripping through poly contexts (mixed-type arrays, heterogeneous hashes, poly return types).
sp_runtime.h:
- Forward declare sp_sym and sp_sym_to_s (program defines them later via emit_sym_runtime)
- #define SP_TAG_SYM 6
- sp_box_sym helper
- sp_poly_puts / sp_poly_to_s dispatch on SP_TAG_SYM → sp_sym_to_s
spinel_codegen.rb:
- box_expr_to_poly / box_val_to_poly: "symbol" → sp_box_sym
- compile_array_literal poly_array branch: symbol element → sp_box_sym
- Drop duplicate
typedef mrb_int sp_symfrom emit_sym_runtime
Add test/bm_sym_poly.rb: [1,"two",:three,4.0,true].each now
prints three for the symbol element instead of 0 (old int
fallback). 73/73 tests pass, bootstrap stable, benchmarks
unchanged (51 pass / 2 err).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 34d56ecea51a
- Parent
- 16b0f704ed86
lib/sp_runtime.hmodifiedView file
@@ -20,6 +20,10 @@
2020
typedef int64_t mrb_int;2121
typedef double mrb_float;2222
typedef bool mrb_bool;23+
/* sp_sym is defined per-program in emit_sym_runtime, but poly helpers24+
below need to reference it by forward declaration. */25+
typedef mrb_int sp_sym;26+
static const char *sp_sym_to_s(sp_sym id);2327
#ifndef TRUE2428
#define TRUE true2529
#endif@@ -388,6 +392,7 @@
388392
#define SP_TAG_BOOL 3389393
#define SP_TAG_NIL 4390394
#define SP_TAG_OBJ 5395+
#define SP_TAG_SYM 6391396
typedef struct { int tag; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; int cls_id; } v; } sp_RbVal;392397
static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.v.i = v; return r; }393398
static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.v.s = v; return r; }@@ -395,6 +400,7 @@
395400
static sp_RbVal sp_box_bool(mrb_bool v) { sp_RbVal r; r.tag = SP_TAG_BOOL; r.v.b = v; return r; }396401
static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.v.i = 0; return r; }397402
static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.v.p = p; r.v.cls_id = cls_id; return r; }403+
static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.v.i = (mrb_int)v; return r; }398404
static void sp_poly_puts(sp_RbVal v) {399405
switch (v.tag) {400406
case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;@@ -402,11 +408,12 @@
402408
case SP_TAG_FLT: { char _fb[64]; snprintf(_fb,64,"%g",v.v.f); if(!strchr(_fb,'.')&&!strchr(_fb,'e')&&!strchr(_fb,'i')&&!strchr(_fb,'n')){strcat(_fb,".0");} printf("%s\n",_fb); break; }403409
case SP_TAG_BOOL: puts(v.v.b ? "true" : "false"); break;404410
case SP_TAG_NIL: putchar('\n'); break;411+
case SP_TAG_SYM: { const char *_ss = sp_sym_to_s((sp_sym)v.v.i); fputs(_ss, stdout); putchar('\n'); break; }405412
default: printf("%lld\n", (long long)v.v.i); break;406413
}407414
}408415
static mrb_bool sp_poly_nil_p(sp_RbVal v) { return v.tag == SP_TAG_NIL; }409-
static const char *sp_poly_to_s(sp_RbVal v) { switch (v.tag) { case SP_TAG_INT: { char *b = sp_str_alloc_raw(32); snprintf(b, 32, "%lld", (long long)v.v.i); return b; } case SP_TAG_STR: return v.v.s ? v.v.s : sp_str_empty; case SP_TAG_FLT: { char *b = sp_str_alloc_raw(64); snprintf(b, 64, "%g", v.v.f); return b; } case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false"); case SP_TAG_NIL: return sp_str_empty; default: return sp_str_empty; } }416+
static const char *sp_poly_to_s(sp_RbVal v) { switch (v.tag) { case SP_TAG_INT: { char *b = sp_str_alloc_raw(32); snprintf(b, 32, "%lld", (long long)v.v.i); return b; } case SP_TAG_STR: return v.v.s ? v.v.s : sp_str_empty; case SP_TAG_FLT: { char *b = sp_str_alloc_raw(64); snprintf(b, 64, "%g", v.v.f); return b; } case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false"); case SP_TAG_NIL: return sp_str_empty; case SP_TAG_SYM: return sp_sym_to_s((sp_sym)v.v.i); default: return sp_str_empty; } }410417
static sp_RbVal sp_poly_add(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i + b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f + b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i + b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f + (mrb_float)b.v.i); if (a.tag == SP_TAG_STR && b.tag == SP_TAG_STR) return sp_box_str(sp_str_concat(a.v.s, b.v.s)); return sp_box_int(0); }411418
static sp_RbVal sp_poly_sub(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i - b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f - b.v.f); return sp_box_int(0); }412419
static sp_RbVal sp_poly_mul(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i * b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f * b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i * b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f * (mrb_float)b.v.i); return sp_box_int(0); }spinel_codegen.rbmodifiedView file
Loading diff…
test/bm_sym_poly.rbaddedView file
Loading diff…