Commit 96e7f4b3

96e7f4b37fd9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified

Description

feat: complete sym_array - all array operations + lexical sort

Wire sym_array into all array operation paths:

Type inference:

  • pop/shift/first/last/sample → "symbol" for sym_array
  • Add sym_array to all int/str/float triple-check patterns (ternary detection, array concat, flat_map block, etc.)

Block compilation:

  • Fix double-dispatch bug: remove sym_array from the generic int/str/float each path (dedicated sym_array branch with sp_sym cast already handles it)
  • compile_select_expr/compile_reject_expr/compile_map_expr: add sym_array alongside int_array
  • each_with_index scan_locals: sym_array → "symbol" block param

Output:

  • puts on sym_array: sp_sym_to_s per element (was printing ints)

Sort:

  • Add sp_sym_sort_cmp + sp_sym_array_sort (qsort by strcmp on symbol names, i.e., lexical order per Ruby semantics)
  • .sort returns a new sorted copy via sp_IntArray_dup + sp_sym_array_sort; .sort! sorts in place

Add test/bm_sym_array_full.rb (comprehensive: length, [], each, push, pop, include?, map, select, each_with_index, sort, puts). Output matches Ruby exactly.

75/75 tests pass, bootstrap stable, benchmarks 51/2.

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

Details

Commit
96e7f4b37fd9
Parent
ef0ad01cfc3d

spinel_codegen.rbmodifiedView file

@@ -1137,7 +1137,7 @@
11371137 return "poly_array"
11381138 end
11391139 # Check if all elements are the same array type → array of arrays
1140- if et == "int_array" || et == "str_array" || et == "float_array"
1140+ if et == "int_array" || et == "str_array" || et == "float_array" || et == "sym_array"
11411141 all_same = 1
11421142 k = 1
11431143 while k < elems.length
@@ -1406,7 +1406,7 @@
14061406 if lt == "poly"
14071407 return "poly"
14081408 end
1409- if lt == "int_array" || lt == "str_array" || lt == "float_array"
1409+ if lt == "int_array" || lt == "str_array" || lt == "float_array" || lt == "sym_array"
14101410 return lt
14111411 end
14121412 if lt == "float"
@@ -1893,6 +1893,9 @@
18931893 if rt == "str_array"
18941894 return "string"
18951895 end
1896+ if rt == "sym_array"
1897+ return "symbol"
1898+ end
18961899 if rt == "float_array"
18971900 return "float"
18981901 end
@@ -1979,6 +1982,9 @@
19791982 if rt == "str_array"
19801983 return "string"
19811984 end
1985+ if rt == "sym_array"
1986+ return "symbol"
1987+ end
19821988 end
19831989 return "int"
19841990 end
@@ -1988,6 +1994,9 @@
19881994 if rt == "str_array"
19891995 return "string"
19901996 end
1997+ if rt == "sym_array"
1998+ return "symbol"
1999+ end
19912000 end
19922001 return "int"
19932002 end
@@ -2016,6 +2025,9 @@
20162025 if rt == "str_array"
20172026 return "string"
20182027 end
2028+ if rt == "sym_array"
2029+ return "symbol"
2030+ end
20192031 if rt == "float_array"
20202032 return "float"
20212033 end
@@ -2066,7 +2078,7 @@
20662078 if bbs.length > 0
20672079 bret = infer_type(bbs.last)
20682080 # If block returns an array type, use it as result type
2069- if bret == "int_array" || bret == "str_array" || bret == "float_array"
2081+ if bret == "int_array" || bret == "str_array" || bret == "float_array" || bret == "sym_array"
20702082 return bret
20712083 end
20722084 end
@@ -2814,7 +2826,7 @@
28142826 if bt == "string" || bt == "mutable_str"
28152827 return 1
28162828 end
2817- if bt == "int_array" || bt == "str_array" || bt == "float_array"
2829+ if bt == "int_array" || bt == "str_array" || bt == "float_array" || bt == "sym_array"
28182830 return 1
28192831 end
28202832 if bt == "str_int_hash" || bt == "str_str_hash"
@@ -7698,6 +7710,9 @@
76987710 emit_raw("static sp_sym sp_sym_intern(const char *s){mrb_int i;for(i=0;i<SP_SYM_COUNT;i++){if(strcmp(sp_sym_names[i],s)==0)return (sp_sym)i;}for(i=0;i<sp_sym_dyn_count;i++){if(strcmp(sp_sym_dyn_names[i],s)==0)return (sp_sym)(SP_SYM_COUNT+i);}if(sp_sym_dyn_count>=sp_sym_dyn_cap){sp_sym_dyn_cap=sp_sym_dyn_cap?sp_sym_dyn_cap*2:8;sp_sym_dyn_names=(const char**)realloc(sp_sym_dyn_names,sizeof(char*)*sp_sym_dyn_cap);}{size_t sl=strlen(s);char*dup=(char*)malloc(sl+1);memcpy(dup,s,sl+1);sp_sym_dyn_names[sp_sym_dyn_count]=dup;}return (sp_sym)(SP_SYM_COUNT+sp_sym_dyn_count++);}")
76997711 emit_raw("static const char *sp_sym_to_s(sp_sym id) __attribute__((unused));")
77007712 emit_raw("static const char *sp_sym_to_s(sp_sym id){if(id<0)return \"\";if(id<SP_SYM_COUNT)return sp_sym_names[id];mrb_int idx=(mrb_int)id-SP_SYM_COUNT;if(idx>=sp_sym_dyn_count)return \"\";return sp_sym_dyn_names[idx];}")
7713+ # Sort comparator for sym arrays: lexical by symbol name.
7714+ emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b){return strcmp(sp_sym_to_s(*(const sp_sym*)a),sp_sym_to_s(*(const sp_sym*)b));}")
7715+ emit_raw("static void sp_sym_array_sort(sp_IntArray*a){qsort(a->data+a->start,a->len,sizeof(mrb_int),sp_sym_sort_cmp);}")
77017716 # Emit SPS_<name> defines for symbols that form valid C identifiers.
77027717 i = 0
77037718 while i < @sym_names.length
@@ -10015,6 +10030,8 @@
1001510030 # Element
1001610031 if recv_type == "str_array"
1001710032 types.push("string")
10033+ elsif recv_type == "sym_array"
10034+ types.push("symbol")
1001810035 elsif recv_type == "float_array"
1001910036 types.push("float")
1002010037 elsif is_ptr_array_type(recv_type) == 1
@@ -12108,7 +12125,7 @@
1210812125 @needs_string_helpers = 1
1210912126 return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")"
1211012127 end
12111- if lt == "int_array" || lt == "str_array" || lt == "float_array"
12128+ if lt == "int_array" || lt == "str_array" || lt == "float_array" || lt == "sym_array"
1211212129 rc = compile_expr(recv)
1211312130 arg = compile_arg0(nid)
1211412131 pfx = array_c_prefix(lt)
@@ -13357,6 +13374,11 @@
1335713374 end
1335813375 end
1335913376 if mname == "sort"
13377+ if recv_type == "sym_array"
13378+ tmp = new_temp
13379+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_dup(" + rc + "); sp_sym_array_sort(" + tmp + ");")
13380+ return tmp
13381+ end
1336013382 return "sp_IntArray_sort(" + rc + ")"
1336113383 end
1336213384 if mname == "first"
@@ -16148,7 +16170,7 @@
1614816170 if mname == "concat"
1614916171 if recv >= 0
1615016172 rt = infer_type(recv)
16151- if rt == "int_array" || rt == "str_array" || rt == "float_array"
16173+ if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array"
1615216174 rc = compile_expr(recv)
1615316175 arg = compile_arg0(nid)
1615416176 pfx = array_c_prefix(rt)
@@ -16280,6 +16302,10 @@
1628016302 if recv >= 0
1628116303 rt = infer_type(recv)
1628216304 rc = compile_expr(recv)
16305+ if rt == "sym_array"
16306+ emit(" sp_sym_array_sort(" + rc + ");")
16307+ return 1
16308+ end
1628316309 if rt == "int_array"
1628416310 emit(" sp_IntArray_sort_bang(" + rc + ");")
1628516311 return 1
@@ -17918,12 +17944,12 @@
1791817944 else
1791917945 if at == "str_array"
1792017946 emit(" { sp_StrArray *_pa = " + val + "; for (mrb_int _pi = 0; _pi < _pa->len; _pi++) puts(_pa->data[_pi]); }")
17947+ elsif at == "sym_array"
17948+ emit(" { sp_IntArray *_pa = " + val + "; for (mrb_int _pi = 0; _pi < _pa->len; _pi++) puts(sp_sym_to_s((sp_sym)_pa->data[_pa->start + _pi])); }")
17949+ elsif at == "int_array"
17950+ emit(" { sp_IntArray *_pa = " + val + "; for (mrb_int _pi = 0; _pi < _pa->len; _pi++) printf(\"%lld" + bsl_n + "\", (long long)_pa->data[_pa->start + _pi]); }")
1792117951 else
17922- if at == "int_array"
17923- emit(" { sp_IntArray *_pa = " + val + "; for (mrb_int _pi = 0; _pi < _pa->len; _pi++) printf(\"%lld" + bsl_n + "\", (long long)_pa->data[_pa->start + _pi]); }")
17924- else
17925- emit(" printf(\"%lld" + bsl_n + "\", (long long)" + val + ");")
17926- end
17952+ emit(" printf(\"%lld" + bsl_n + "\", (long long)" + val + ");")
1792717953 end
1792817954 end
1792917955 end
@@ -18094,7 +18120,7 @@
1809418120 bp2 = "_obj"
1809518121 end
1809618122 tmp_i = new_temp
18097- if rt == "int_array" || rt == "str_array" || rt == "float_array"
18123+ if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array"
1809818124 pfx = array_c_prefix(rt)
1809918125 emit(" lv_" + bp2 + " = " + obj_arg + ";")
1810018126 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
@@ -18792,7 +18818,7 @@
1879218818 end
1879318819 end
1879418820 # Fall back to receiver type if block doesn't return an array
18795- if block_ret != "int_array" && block_ret != "str_array" && block_ret != "float_array"
18821+ if block_ret != "int_array" && block_ret != "str_array" && block_ret != "float_array" && block_ret != "sym_array"
1879618822 block_ret = rt
1879718823 end
1879818824 @needs_gc = 1
@@ -18847,7 +18873,7 @@
1884718873 end
1884818874 tmp_arr = new_temp
1884918875 tmp_i = new_temp
18850- if rt == "int_array"
18876+ if rt == "int_array" || rt == "sym_array"
1885118877 @needs_int_array = 1
1885218878 @needs_gc = 1
1885318879 # Check if block body returns string (for map that produces StrArray)
@@ -18957,7 +18983,7 @@
1895718983 end
1895818984 tmp_arr = new_temp
1895918985 tmp_i = new_temp
18960- if rt == "int_array"
18986+ if rt == "int_array" || rt == "sym_array"
1896118987 @needs_int_array = 1
1896218988 @needs_gc = 1
1896318989 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
@@ -19034,7 +19060,7 @@
1903419060 bp1 = "_x"
1903519061 end
1903619062 rt = infer_type(@nd_receiver[nid])
19037- if rt == "int_array"
19063+ if rt == "int_array" || rt == "sym_array"
1903819064 @needs_int_array = 1
1903919065 tmp_arr = new_temp
1904019066 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
@@ -19929,7 +19955,7 @@
1992919955 end
1993019956 tmp_arr = new_temp
1993119957 tmp_i = new_temp
19932- if rt == "int_array"
19958+ if rt == "int_array" || rt == "sym_array"
1993319959 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
1993419960 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
1993519961 emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")