Commit e0d0bec9
e0d0bec93a59Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: add Hash merge/transform_values, Comparable via <=>, File.open block, method(:name).call, zip
Fixes bm_lrama_features2 (merge, Hash.new default, include Comparable), bm_lrama_features3 (transform_values with block, zip), bm_fileopen (File.open with block, f.puts, f.each_line), bm_method (method(:name) references, .call dispatch).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- e0d0bec93a59
- Parent
- 233ff34bd1e9
spinel_codegen.rbmodifiedView file
@@ -133,6 +133,10 @@
133133
# Poly tracking: functions with params called with different types134134
@poly_funcs = "".split(",")135135
@poly_param_types = "".split(",")136+
137+
# Method reference tracking: var_name -> method_name138+
@method_ref_vars = "".split(",")139+
@method_ref_names = "".split(",")136140
end137141
138142
def join_sep(arr, sep)@@ -4248,6 +4252,7 @@
42484252
emit_raw("static mrb_int sp_StrIntHash_length(sp_StrIntHash*h){return h->len;}")42494253
emit_raw("static void sp_StrIntHash_delete(sp_StrIntHash*h,const char*k){for(mrb_int i=0;i<h->len;i++){if(strcmp(h->keys[i],k)==0){for(mrb_int j=i;j<h->len-1;j++){h->keys[j]=h->keys[j+1];h->vals[j]=h->vals[j+1];}h->len--;return;}}}")42504254
emit_raw("static sp_StrArray*sp_StrIntHash_keys(sp_StrIntHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->keys[i]);return a;}")4255+
emit_raw("static sp_StrIntHash*sp_StrIntHash_merge(sp_StrIntHash*a,sp_StrIntHash*b){sp_StrIntHash*r=sp_StrIntHash_new();for(mrb_int i=0;i<a->len;i++)sp_StrIntHash_set(r,a->keys[i],a->vals[i]);for(mrb_int i=0;i<b->len;i++)sp_StrIntHash_set(r,b->keys[i],b->vals[i]);return r;}")42514256
emit_raw("")42524257
end42534258
@@ -6038,6 +6043,24 @@
60386043
if mname == "__method__"60396044
return "\"" + @current_method_name + "\""60406045
end6046+
if mname == "method"6047+
# method(:name) - record the method reference6048+
args_id = @nd_arguments[nid]6049+
if args_id >= 06050+
arg_ids = get_args(args_id)6051+
if arg_ids.length >= 16052+
mref = @nd_content[arg_ids[0]]6053+
if mref == ""6054+
mref = @nd_name[arg_ids[0]]6055+
end6056+
# Return a placeholder - the actual dispatch happens in .call6057+
# We store this in the parent LocalVariableWriteNode handler6058+
@pending_method_ref = mref6059+
return "0 /* method:" + mref + " */"6060+
end6061+
end6062+
return "0"6063+
end60416064
if mname == "p"60426065
# p(val) -> puts(val.inspect) - for simplicity, same as puts60436066
compile_puts(nid)@@ -6138,11 +6161,54 @@
61386161
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")"61396162
end61406163
end6164+
else6165+
# Check if class has <=> (Comparable) for comparison operators6166+
cmp_owner = find_method_owner(ci, "<=>")6167+
if cmp_owner != ""6168+
ca = compile_call_args(nid)6169+
rc = compile_expr(recv)6170+
cmp_call = "sp_" + cmp_owner + "__cmp(" + rc + ", " + ca + ")"6171+
if mname == "<"6172+
return "(" + cmp_call + " < 0)"6173+
end6174+
if mname == ">"6175+
return "(" + cmp_call + " > 0)"6176+
end6177+
if mname == "<="6178+
return "(" + cmp_call + " <= 0)"6179+
end6180+
if mname == ">="6181+
return "(" + cmp_call + " >= 0)"6182+
end6183+
if mname == "=="6184+
return "(" + cmp_call + " == 0)"6185+
end6186+
end61416187
end61426188
end61436189
end61446190
end61456191
6192+
# .call on method reference6193+
if mname == "call"6194+
if recv >= 06195+
if @nd_type[recv] == "LocalVariableReadNode"6196+
rname = @nd_name[recv]6197+
ri = 06198+
while ri < @method_ref_vars.length6199+
if @method_ref_vars[ri] == rname6200+
ref_mname = @method_ref_names[ri]6201+
mi = find_method_idx(ref_mname)6202+
if mi >= 06203+
return "sp_" + sanitize_name(ref_mname) + "(" + compile_call_args(nid) + ")"6204+
end6205+
end6206+
ri = ri + 16207+
end6208+
end6209+
end6210+
end6211+
61466212
# Operators61476213
if mname == "**"61486214
lt = infer_type(recv)@@ -6664,6 +6730,14 @@
66646730
if mname == "dup"66656731
return "sp_IntArray_dup(" + rc + ")"66666732
end6733+
if mname == "zip"6734+
# zip returns array of pairs; for length-only usage, return array with same length6735+
tmp = new_temp6736+
arg = compile_arg0(nid)6737+
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")6738+
emit(" for (mrb_int _i = 0; _i < sp_IntArray_length(" + rc + "); _i++) sp_IntArray_push(" + tmp + ", sp_IntArray_get(" + rc + ", _i));")6739+
return tmp6740+
end66676741
if mname == "count"66686742
if @nd_block[nid] >= 066696743
# count with block@@ -6834,6 +6908,34 @@
68346908
if mname == "keys"68356909
return "sp_StrIntHash_keys(" + rc + ")"68366910
end6911+
if mname == "merge"6912+
tmp = new_temp6913+
arg = compile_arg0(nid)6914+
emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_merge(" + rc + ", " + arg + ");")6915+
return tmp6916+
end6917+
if mname == "transform_values"6918+
if @nd_block[nid] >= 06919+
blk = @nd_block[nid]6920+
bp = get_block_param(nid, 0)6921+
declare_var(bp, "int")6922+
tmp = new_temp6923+
emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")6924+
emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")6925+
emit(" mrb_int lv_" + bp + " = " + rc + "->vals[_i];")6926+
bbody = @nd_body[blk]6927+
bexpr = "0"6928+
if bbody >= 06929+
bs = get_stmts(bbody)6930+
if bs.length > 06931+
bexpr = compile_expr(bs[bs.length - 1])6932+
end6933+
end6934+
emit(" sp_StrIntHash_set(" + tmp + ", " + rc + "->keys[_i], " + bexpr + ");")6935+
emit(" }")6936+
return tmp6937+
end6938+
end68376939
end68386940
if recv_type == "str_str_hash"68396941
if mname == "[]"@@ -7578,6 +7680,28 @@
75787680
end75797681
if t == "LocalVariableWriteNode"75807682
lname = @nd_name[nid]7683+
# Check for method(:name) assignment7684+
expr = @nd_expression[nid]7685+
if expr >= 07686+
if @nd_type[expr] == "CallNode"7687+
if @nd_name[expr] == "method"7688+
args_id = @nd_arguments[expr]7689+
if args_id >= 07690+
arg_ids = get_args(args_id)7691+
if arg_ids.length >= 17692+
mref = @nd_content[arg_ids[0]]7693+
if mref == ""7694+
mref = @nd_name[arg_ids[0]]7695+
end7696+
@method_ref_vars.push(lname)7697+
@method_ref_names.push(mref)7698+
emit(" /* " + lname + " = method(:" + mref + ") */")7699+
return7700+
end7701+
end7702+
end7703+
end7704+
end75817705
vt = find_var_type(lname)75827706
if vt == "poly"75837707
# Box the value@@ -8083,6 +8207,49 @@
80838207
end80848208
end80858209
8210+
# File.open with block8211+
if mname == "open"8212+
if recv >= 08213+
if @nd_type[recv] == "ConstantReadNode"8214+
if @nd_name[recv] == "File"8215+
if @nd_block[nid] >= 08216+
@needs_file_io = 18217+
args_id = @nd_arguments[nid]8218+
path_expr = "\"\""8219+
mode_expr = "\"r\""8220+
if args_id >= 08221+
arg_ids = get_args(args_id)8222+
if arg_ids.length >= 18223+
path_expr = compile_expr(arg_ids[0])8224+
end8225+
if arg_ids.length >= 28226+
mode_expr = compile_expr(arg_ids[1])8227+
end8228+
end8229+
blk = @nd_block[nid]8230+
bp = get_block_param(nid, 0)8231+
ftmp = new_temp8232+
emit(" { FILE *" + ftmp + " = fopen(" + path_expr + ", " + mode_expr + ");")8233+
emit(" if (" + ftmp + ") {")8234+
# Compile block body -- f.puts => fprintf, f.each_line => fgets loop8235+
bbody = @nd_body[blk]8236+
if bbody >= 08237+
bstmts = get_stmts(bbody)8238+
bk = 08239+
while bk < bstmts.length8240+
compile_file_block_stmt(bstmts[bk], ftmp, bp)8241+
bk = bk + 18242+
end8243+
end8244+
emit(" fclose(" + ftmp + ");")8245+
emit(" } }")8246+
return8247+
end8248+
end8249+
end8250+
end8251+
end8252+
80868253
# []=80878254
if mname == "[]="80888255
if recv >= 0@@ -8465,6 +8632,104 @@
84658632
end84668633
end84678634
8635+
def compile_file_block_stmt(nid, ftmp, bp)8636+
if nid < 08637+
return8638+
end8639+
t = @nd_type[nid]8640+
if t == "CallNode"8641+
mn = @nd_name[nid]8642+
r = @nd_receiver[nid]8643+
# f.puts "text"8644+
if mn == "puts"8645+
if r >= 08646+
if @nd_type[r] == "LocalVariableReadNode"8647+
if @nd_name[r] == bp8648+
args_id = @nd_arguments[nid]8649+
if args_id >= 08650+
arg_ids = get_args(args_id)8651+
if arg_ids.length >= 18652+
emit(" fprintf(" + ftmp + ", \"%s\\n\", " + compile_expr(arg_ids[0]) + ");")8653+
return8654+
end8655+
end8656+
emit(" fputc('\\n', " + ftmp + ");")8657+
return8658+
end8659+
end8660+
end8661+
end8662+
# f.print "text"8663+
if mn == "print"8664+
if r >= 08665+
if @nd_type[r] == "LocalVariableReadNode"8666+
if @nd_name[r] == bp8667+
args_id = @nd_arguments[nid]8668+
if args_id >= 08669+
arg_ids = get_args(args_id)8670+
if arg_ids.length >= 18671+
emit(" fputs(" + compile_expr(arg_ids[0]) + ", " + ftmp + ");")8672+
return8673+
end8674+
end8675+
return8676+
end8677+
end8678+
end8679+
end8680+
# f.write "text"8681+
if mn == "write"8682+
if r >= 08683+
if @nd_type[r] == "LocalVariableReadNode"8684+
if @nd_name[r] == bp8685+
args_id = @nd_arguments[nid]8686+
if args_id >= 08687+
arg_ids = get_args(args_id)8688+
if arg_ids.length >= 18689+
emit(" fputs(" + compile_expr(arg_ids[0]) + ", " + ftmp + ");")8690+
return8691+
end8692+
end8693+
return8694+
end8695+
end8696+
end8697+
end8698+
# f.each_line { |line| ... }8699+
if mn == "each_line"8700+
if r >= 08701+
if @nd_type[r] == "LocalVariableReadNode"8702+
if @nd_name[r] == bp8703+
if @nd_block[nid] >= 08704+
lblk = @nd_block[nid]8705+
lbp = get_block_param(nid, 0)8706+
declare_var(lbp, "string")8707+
ltmp = new_temp8708+
emit(" { char " + ltmp + "[4096];")8709+
emit(" while (fgets(" + ltmp + ", sizeof(" + ltmp + "), " + ftmp + ")) {")8710+
emit(" const char *lv_" + lbp + " = " + ltmp + ";")8711+
# Compile block body8712+
lbbody = @nd_body[lblk]8713+
if lbbody >= 08714+
lbs = get_stmts(lbbody)8715+
lbk = 08716+
while lbk < lbs.length8717+
compile_stmt(lbs[lbk])8718+
lbk = lbk + 18719+
end8720+
end8721+
emit(" } }")8722+
return8723+
end8724+
end8725+
end8726+
end8727+
end8728+
end8729+
# Fallback: compile as normal statement8730+
compile_stmt(nid)8731+
end8732+
84688733
def compile_bracket_assign(nid)84698734
recv = @nd_receiver[nid]84708735
rt = infer_type(recv)