Commit 51cb7738
51cb7738c8ddMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: add itself/succ, block variable scope, define_method support
- Integer#itself and Float#itself now return the receiver
- Integer#succ returns receiver + 1
- Block variable declarations are skipped when no block param exists (fixes lv__x undeclared error in bm_send_cfunc_block)
- define_method(:name) { body } is collected as top-level method (fixes bm_send_bmethod)
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 51cb7738c8dd
- Parent
- 44d9090a484d
spinel_codegen.rbmodifiedView file
@@ -1106,6 +1106,15 @@
11061106
if mname == "clamp"11071107
return "int"11081108
end1109+
if mname == "itself"1110+
if recv >= 01111+
return infer_type(recv)1112+
end1113+
return "int"1114+
end1115+
if mname == "succ"1116+
return "int"1117+
end11091118
if mname == "__method__"11101119
return "string"11111120
end@@ -1860,7 +1869,7 @@
18601869
i = i + 118611870
end18621871
1863-
# Pass 2: top-level methods, constants (modules already collected in Pass 0)1872+
# Pass 2: top-level methods, constants, define_method18641873
i = 018651874
while i < stmts.length18661875
sid = stmts[i]@@ -1870,6 +1879,11 @@
18701879
if @nd_type[sid] == "ConstantWriteNode"18711880
collect_constant(sid)18721881
end1882+
if @nd_type[sid] == "CallNode"1883+
if @nd_name[sid] == "define_method"1884+
collect_define_method(sid)1885+
end1886+
end18731887
i = i + 118741888
end18751889
@@ -2714,6 +2728,54 @@
27142728
@meth_has_yield.push(body_has_yield(body_id))27152729
end27162730
2731+
def collect_define_method(nid)2732+
# define_method(:name) { |args| body }2733+
args_id = @nd_arguments[nid]2734+
if args_id < 02735+
return2736+
end2737+
arg_ids = get_args(args_id)2738+
if arg_ids.length < 12739+
return2740+
end2741+
mname = @nd_content[arg_ids[0]]2742+
if mname == ""2743+
mname = @nd_name[arg_ids[0]]2744+
end2745+
blk = @nd_block[nid]2746+
if blk < 02747+
return2748+
end2749+
body_id = @nd_body[blk]2750+
# Collect block params2751+
params_str = ""2752+
ptypes_str = ""2753+
bp = @nd_parameters[blk]2754+
if bp >= 02755+
inner = @nd_parameters[bp]2756+
if inner >= 02757+
reqs = parse_id_list(@nd_requireds[inner])2758+
k = 02759+
while k < reqs.length2760+
if params_str != ""2761+
params_str = params_str + ","2762+
ptypes_str = ptypes_str + ","2763+
end2764+
params_str = params_str + @nd_name[reqs[k]]2765+
ptypes_str = ptypes_str + "int"2766+
k = k + 12767+
end2768+
end2769+
end2770+
@meth_names.push(mname)2771+
@meth_param_names.push(params_str)2772+
@meth_param_types.push(ptypes_str)2773+
@meth_return_types.push("int")2774+
@meth_body_ids.push(body_id)2775+
@meth_has_defaults.push("")2776+
@meth_has_yield.push(0)2777+
end2778+
27172779
def collect_module(nid)27182780
mname = ""27192781
cp = @nd_constant_path[nid]@@ -7704,9 +7766,18 @@
77047766
@needs_string_helpers = 177057767
return "sp_int_chr(" + rc + ")"77067768
end7769+
if mname == "succ"7770+
return "((" + rc + ") + 1)"7771+
end7772+
if mname == "itself"7773+
return rc7774+
end77077775
end77087776
77097777
if recv_type == "float"7778+
if mname == "itself"7779+
return rc7780+
end77107781
if mname == "to_s"77117782
@needs_string_helpers = 177127783
return "sp_float_to_s(" + rc + ")"@@ -9326,6 +9397,11 @@
93269397
mname = @nd_name[nid]93279398
recv = @nd_receiver[nid]93289399
9400+
# define_method is handled at collection time, skip at runtime9401+
if mname == "define_method"9402+
return9403+
end9404+
93299405
if mname == "puts"93309406
if recv < 093319407
compile_puts(nid)@@ -10089,14 +10165,18 @@
1008910165
rc = compile_expr(@nd_receiver[nid])1009010166
bp1 = get_block_param(nid, 0)1009110167
bp2 = get_block_param(nid, 1)10168+
has_bp = 11009210169
if bp1 == ""10170+
has_bp = 01009310171
bp1 = "_x"1009410172
end1009510173
1009610174
if rt == "int_array"1009710175
tmp = new_temp1009810176
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_IntArray_length(" + rc + "); " + tmp + "++) {")10099-
emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp + ");")10177+
if has_bp == 110178+
emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp + ");")10179+
end1010010180
@indent = @indent + 11010110181
compile_stmts_body(@nd_body[@nd_block[nid]])1010210182
@indent = @indent - 1@@ -10105,7 +10185,9 @@
1010510185
if rt == "str_array"1010610186
tmp = new_temp1010710187
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_StrArray_length(" + rc + "); " + tmp + "++) {")10108-
emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp + ");")10188+
if has_bp == 110189+
emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp + ");")10190+
end1010910191
@indent = @indent + 11011010192
compile_stmts_body(@nd_body[@nd_block[nid]])1011110193
@indent = @indent - 1