Commit 167cac04

167cac045044Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: Phase 5 - blocks, yield, block_given?, map, select

Add yield/block support via inline expansion at call sites. Yield-using functions get _block/_benv params; yield becomes _block(val, _benv). block_given? compiles to (_block != NULL). Add map/select block compilation returning new arrays. Fix param type inference by scanning main-level call sites.

Tests passing: bm_yield, bm_block2, bm_enumerable

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

Details

Commit
167cac045044
Parent
ff9eca606f09

spinel_codegen.rbmodifiedView file

@@ -108,6 +108,15 @@
108108 @current_method_return = ""
109109 @in_main = 0
110110 @in_loop = 0
111+ @in_yield_method = 0
112+
113+ # Yield/block tracking (parallel with meth_names / cls_meth_names)
114+ @meth_has_yield = []
115+ @cls_meth_has_yield = "".split(",")
116+
117+ # Block function accumulator (emitted before forward decls)
118+ @block_funcs = ""
119+ @block_counter = 0
111120
112121 # Feature flags
113122 @needs_gc = 0
@@ -116,6 +125,10 @@
116125 @needs_str_int_hash = 0
117126 @needs_str_str_hash = 0
118127 @needs_string_helpers = 0
128+ @needs_setjmp = 0
129+ @needs_file_io = 0
130+ @needs_mutable_str = 0
131+ @needs_rb_value = 0
119132 end
120133
121134 def join_sep(arr, sep)
@@ -1020,6 +1033,18 @@
10201033 end
10211034 return "int_array"
10221035 end
1036+ if mname == "map"
1037+ if recv >= 0
1038+ return infer_type(recv)
1039+ end
1040+ return "int_array"
1041+ end
1042+ if mname == "select"
1043+ if recv >= 0
1044+ return infer_type(recv)
1045+ end
1046+ return "int_array"
1047+ end
10231048 if mname == "[]"
10241049 if recv >= 0
10251050 rt = infer_type(recv)
@@ -1316,6 +1341,7 @@
13161341 @cls_cmeth_ptypes.push("")
13171342 @cls_cmeth_returns.push("")
13181343 @cls_cmeth_bodies.push("")
1344+ @cls_meth_has_yield.push("")
13191345 @needs_gc = 1
13201346
13211347 # Collect class body
@@ -1359,7 +1385,14 @@
13591385 params_str = collect_params_str(nid)
13601386 ptypes_str = collect_ptypes_str(nid, ci)
13611387 defaults_str = collect_defaults_str(nid)
1388+ has_y = body_has_yield(body_id)
13621389 append_cls_meth(ci, mname, params_str, ptypes_str, "int", body_id, defaults_str)
1390+ # Track yield info
1391+ if @cls_meth_has_yield[ci] != ""
1392+ @cls_meth_has_yield[ci] = @cls_meth_has_yield[ci] + ";" + has_y.to_s
1393+ else
1394+ @cls_meth_has_yield[ci] = has_y.to_s
1395+ end
13631396 return
13641397 end
13651398
@@ -1701,7 +1734,14 @@
17011734 return infer_hash_val_type(nid)
17021735 end
17031736 if t == "CallNode"
1704- if @nd_name[nid] == "new"
1737+ mname = @nd_name[nid]
1738+ if mname == "to_a"
1739+ return "int_array"
1740+ end
1741+ if mname == "split"
1742+ return "str_array"
1743+ end
1744+ if mname == "new"
17051745 r = @nd_receiver[nid]
17061746 if r >= 0
17071747 if @nd_type[r] == "ConstantReadNode"
@@ -1760,6 +1800,7 @@
17601800 @meth_return_types.push("int")
17611801 @meth_body_ids.push(body_id)
17621802 @meth_has_defaults.push(defaults_str)
1803+ @meth_has_yield.push(body_has_yield(body_id))
17631804 end
17641805
17651806 def collect_constant(nid)
@@ -1773,6 +1814,105 @@
17731814 @const_expr_ids.push(expr_id)
17741815 end
17751816
1817+ # ---- Yield detection ----
1818+ def body_has_yield(nid)
1819+ if nid < 0
1820+ return 0
1821+ end
1822+ if @nd_type[nid] == "YieldNode"
1823+ return 1
1824+ end
1825+ if @nd_type[nid] == "CallNode"
1826+ if @nd_name[nid] == "block_given?"
1827+ return 1
1828+ end
1829+ end
1830+ # Don't recurse into nested DefNode (that's a different method)
1831+ if @nd_type[nid] == "DefNode"
1832+ return 0
1833+ end
1834+ if @nd_type[nid] == "BlockNode"
1835+ # Don't look inside blocks of other calls for yield
1836+ # Actually we do - yield inside a block still belongs to enclosing method
1837+ end
1838+ # Recurse children
1839+ if @nd_body[nid] >= 0
1840+ if body_has_yield(@nd_body[nid]) == 1
1841+ return 1
1842+ end
1843+ end
1844+ stmts = parse_id_list(@nd_stmts[nid])
1845+ k = 0
1846+ while k < stmts.length
1847+ if body_has_yield(stmts[k]) == 1
1848+ return 1
1849+ end
1850+ k = k + 1
1851+ end
1852+ if @nd_expression[nid] >= 0
1853+ if body_has_yield(@nd_expression[nid]) == 1
1854+ return 1
1855+ end
1856+ end
1857+ if @nd_predicate[nid] >= 0
1858+ if body_has_yield(@nd_predicate[nid]) == 1
1859+ return 1
1860+ end
1861+ end
1862+ if @nd_subsequent[nid] >= 0
1863+ if body_has_yield(@nd_subsequent[nid]) == 1
1864+ return 1
1865+ end
1866+ end
1867+ if @nd_else_clause[nid] >= 0
1868+ if body_has_yield(@nd_else_clause[nid]) == 1
1869+ return 1
1870+ end
1871+ end
1872+ if @nd_left[nid] >= 0
1873+ if body_has_yield(@nd_left[nid]) == 1
1874+ return 1
1875+ end
1876+ end
1877+ if @nd_right[nid] >= 0
1878+ if body_has_yield(@nd_right[nid]) == 1
1879+ return 1
1880+ end
1881+ end
1882+ if @nd_block[nid] >= 0
1883+ if body_has_yield(@nd_block[nid]) == 1
1884+ return 1
1885+ end
1886+ end
1887+ conds = parse_id_list(@nd_conditions[nid])
1888+ k = 0
1889+ while k < conds.length
1890+ if body_has_yield(conds[k]) == 1
1891+ return 1
1892+ end
1893+ k = k + 1
1894+ end
1895+ args = parse_id_list(@nd_args[nid])
1896+ k = 0
1897+ while k < args.length
1898+ if body_has_yield(args[k]) == 1
1899+ return 1
1900+ end
1901+ k = k + 1
1902+ end
1903+ if @nd_arguments[nid] >= 0
1904+ if body_has_yield(@nd_arguments[nid]) == 1
1905+ return 1
1906+ end
1907+ end
1908+ if @nd_receiver[nid] >= 0
1909+ if body_has_yield(@nd_receiver[nid]) == 1
1910+ return 1
1911+ end
1912+ end
1913+ 0
1914+ end
1915+
17761916 # ---- Return type inference ----
17771917 def infer_constructor_types
17781918 # Scan AST for ClassName.new(args) calls and infer param types
@@ -2458,8 +2598,41 @@
24582598 end
24592599
24602600 # ---- Code generation ----
2601+ def infer_main_call_types
2602+ # Scan main-level code for function calls and infer param types from arguments
2603+ stmts = get_body_stmts(@root_id)
2604+ # First, figure out main local types
2605+ push_scope
2606+ lnames = "".split(",")
2607+ ltypes = "".split(",")
2608+ empty_p = "".split(",")
2609+ i = 0
2610+ while i < stmts.length
2611+ sid = stmts[i]
2612+ if @nd_type[sid] != "DefNode"
2613+ if @nd_type[sid] != "ClassNode"
2614+ if @nd_type[sid] != "ConstantWriteNode"
2615+ scan_locals(sid, lnames, ltypes, empty_p)
2616+ end
2617+ end
2618+ end
2619+ i = i + 1
2620+ end
2621+ k = 0
2622+ while k < lnames.length
2623+ declare_var(lnames[k], ltypes[k])
2624+ k = k + 1
2625+ end
2626+ # Now scan call sites to update param types
2627+ scan_new_calls(@root_id)
2628+ pop_scope
2629+ end
2630+
24612631 def compile
24622632 collect_all
2633+ infer_main_call_types
2634+ # Re-infer return types after main call type fixes
2635+ infer_all_returns
24632636 detect_features
24642637 generate_code
24652638 end
@@ -2740,10 +2913,18 @@
27402913
27412914 # ---- Forward declarations ----
27422915 def emit_forward_decls
2916+ # Emit block helper functions accumulated during collection
2917+ if @block_funcs != ""
2918+ emit_raw(@block_funcs)
2919+ end
27432920 # Top-level methods
27442921 i = 0
27452922 while i < @meth_names.length
2746- emit_raw("static " + c_type(@meth_return_types[i]) + " sp_" + sanitize_name(@meth_names[i]) + "(" + method_params_decl(i) + ");")
2923+ yp = ""
2924+ if @meth_has_yield[i] == 1
2925+ yp = yield_params_suffix(i)
2926+ end
2927+ emit_raw("static " + c_type(@meth_return_types[i]) + " sp_" + sanitize_name(@meth_names[i]) + "(" + method_params_decl(i) + yp + ");")
27472928 i = i + 1
27482929 end
27492930 # Class methods
@@ -2768,7 +2949,11 @@
27682949 if j < returns.length
27692950 rt = returns[j]
27702951 end
2771- emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + " *self" + method_with_self_params(j, all_params, all_ptypes) + ");")
2952+ yp = ""
2953+ if cls_method_has_yield(i, j) == 1
2954+ yp = yield_params_suffix_cls(i, j)
2955+ end
2956+ emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + " *self" + method_with_self_params(j, all_params, all_ptypes) + yp + ");")
27722957 end
27732958 j = j + 1
27742959 end
@@ -2791,6 +2976,29 @@
27912976 emit_raw("")
27922977 end
27932978
2979+ def yield_params_suffix(mi)
2980+ pnames = @meth_param_names[mi].split(",")
2981+ if pnames.length == 0
2982+ return "void (*_block)(mrb_int, void*), void *_benv"
2983+ end
2984+ return ", void (*_block)(mrb_int, void*), void *_benv"
2985+ end
2986+
2987+ def yield_params_suffix_cls(ci, midx)
2988+ # For class instance methods (always have self first)
2989+ return ", void (*_block)(mrb_int, void*), void *_benv"
2990+ end
2991+
2992+ def cls_method_has_yield(ci, midx)
2993+ ystr = @cls_meth_has_yield[ci].split(";")
2994+ if midx < ystr.length
2995+ if ystr[midx] == "1"
2996+ return 1
2997+ end
2998+ end
2999+ 0
3000+ end
3001+
27943002 def cls_find_method_direct(ci, mname)
27953003 mnames = @cls_meth_names[ci].split(";")
27963004 j = 0
@@ -3188,7 +3396,20 @@
31883396 @current_method_return = rt
31893397 @indent = 1
31903398
3191- emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + ") {")
3399+ midx = cls_find_method_direct(ci, mname)
3400+ if midx >= 0
3401+ if cls_method_has_yield(ci, midx) == 1
3402+ @in_yield_method = 1
3403+ else
3404+ @in_yield_method = 0
3405+ end
3406+ end
3407+
3408+ yp = ""
3409+ if @in_yield_method == 1
3410+ yp = yield_params_suffix_cls(ci, midx)
3411+ end
3412+ emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + yp + ") {")
31923413
31933414 push_scope
31943415 j = 0
@@ -3209,6 +3430,7 @@
32093430 pop_scope
32103431 @current_class_idx = -1
32113432 @current_method_name = ""
3433+ @in_yield_method = 0
32123434 @indent = 0
32133435 emit_raw("}")
32143436 emit_raw("")
@@ -3295,11 +3517,20 @@
32953517 @current_method_return = @meth_return_types[mi]
32963518 @indent = 1
32973519 @in_main = 0
3520+ if @meth_has_yield[mi] == 1
3521+ @in_yield_method = 1
3522+ else
3523+ @in_yield_method = 0
3524+ end
32983525
32993526 pnames = @meth_param_names[mi].split(",")
33003527 ptypes = @meth_param_types[mi].split(",")
33013528
3302- emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(@meth_names[mi]) + "(" + method_params_decl(mi) + ") {")
3529+ yp = ""
3530+ if @meth_has_yield[mi] == 1
3531+ yp = yield_params_suffix(mi)
3532+ end
3533+ emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(@meth_names[mi]) + "(" + method_params_decl(mi) + yp + ") {")
33033534
33043535 push_scope
33053536 j = 0
@@ -3320,6 +3551,7 @@
33203551
33213552 pop_scope
33223553 @current_method_name = ""
3554+ @in_yield_method = 0
33233555 @indent = 0
33243556 emit_raw("}")
33253557 emit_raw("")
@@ -3901,9 +4133,19 @@
39014133
39024134 # No receiver
39034135 if recv < 0
4136+ if mname == "block_given?"
4137+ if @in_yield_method == 1
4138+ return "(_block != NULL)"
4139+ end
4140+ return "0"
4141+ end
39044142 mi = find_method_idx(mname)
39054143 if mi >= 0
3906- return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + ")"
4144+ yargs = ""
4145+ if @meth_has_yield[mi] == 1
4146+ yargs = ", NULL, NULL"
4147+ end
4148+ return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"
39074149 end
39084150 if @current_class_idx >= 0
39094151 cidx = cls_find_method(@current_class_idx, mname)
@@ -4180,8 +4422,22 @@
41804422 end
41814423 end
41824424
4183- # reject as expression
4184- if mname == "reject"
4425+ # map as expression
4426+ if mname == "map"
4427+ if @nd_block[nid] >= 0
4428+ return compile_map_expr(nid)
4429+ end
4430+ end
4431+
4432+ # select as expression
4433+ if mname == "select"
4434+ if @nd_block[nid] >= 0
4435+ return compile_select_expr(nid)
4436+ end
4437+ end
4438+
4439+ # reject as expression
4440+ if mname == "reject"
41854441 if @nd_block[nid] >= 0
41864442 return compile_reject_expr(nid)
41874443 end
@@ -4599,6 +4855,10 @@
45994855 emit(" continue;")
46004856 return
46014857 end
4858+ if t == "YieldNode"
4859+ compile_yield_stmt(nid)
4860+ return
4861+ end
46024862 if t == "CallNode"
46034863 compile_call_stmt(nid)
46044864 return
@@ -4906,8 +5166,19 @@
49065166 # each with block
49075167 if mname == "each"
49085168 if @nd_block[nid] >= 0
4909- compile_each_block(nid)
4910- return
5169+ # For object types with yield-using each, use yield method call
5170+ if recv >= 0
5171+ ert = infer_type(recv)
5172+ if is_obj_type(ert) == 1
5173+ # Fall through to yield method handler below
5174+ else
5175+ compile_each_block(nid)
5176+ return
5177+ end
5178+ else
5179+ compile_each_block(nid)
5180+ return
5181+ end
49115182 end
49125183 end
49135184
@@ -4992,6 +5263,65 @@
49925263 end
49935264 end
49945265
5266+ # map with block
5267+ if mname == "map"
5268+ if @nd_block[nid] >= 0
5269+ compile_map_block(nid)
5270+ return
5271+ end
5272+ end
5273+
5274+ # select with block
5275+ if mname == "select"
5276+ if @nd_block[nid] >= 0
5277+ compile_select_block(nid)
5278+ return
5279+ end
5280+ end
5281+
5282+ # User-defined yield function called with block
5283+ if @nd_block[nid] >= 0
5284+ if recv < 0
5285+ mi = find_method_idx(mname)
5286+ if mi >= 0
5287+ if @meth_has_yield[mi] == 1
5288+ compile_yield_call_stmt(nid, mi)
5289+ return
5290+ end
5291+ end
5292+ end
5293+ # Class method with yield
5294+ if recv >= 0
5295+ rtype = infer_type(recv)
5296+ if is_obj_type(rtype) == 1
5297+ cn = rtype[4, rtype.length - 4]
5298+ cci = find_class_idx(cn)
5299+ if cci >= 0
5300+ midx = cls_find_method_direct(cci, mname)
5301+ if midx >= 0
5302+ if cls_method_has_yield(cci, midx) == 1
5303+ compile_yield_method_call_stmt(nid, cci, midx, mname)
5304+ return
5305+ end
5306+ end
5307+ # Check parent
5308+ if @cls_parents[cci] != ""
5309+ pci = find_class_idx(@cls_parents[cci])
5310+ if pci >= 0
5311+ pidx = cls_find_method_direct(pci, mname)
5312+ if pidx >= 0
5313+ if cls_method_has_yield(pci, pidx) == 1
5314+ compile_yield_method_call_stmt(nid, pci, pidx, mname)
5315+ return
5316+ end
5317+ end
5318+ end
5319+ end
5320+ end
5321+ end
5322+ end
5323+ end
5324+
49955325 # General
49965326 val = compile_expr(nid)
49975327 if val != "0"
@@ -5254,6 +5584,81 @@
52545584 @in_loop = old
52555585 end
52565586
5587+ def compile_map_expr(nid)
5588+ # Emit the map loop as side effects, return result array
5589+ compile_map_block(nid)
5590+ # The map_block stores result in a temp - we need to return it
5591+ # Actually, compile_map_block emits into current output but doesn't return the temp name.
5592+ # Let's use a different approach: inline and return temp
5593+ rt = infer_type(@nd_receiver[nid])
5594+ rc = compile_expr(@nd_receiver[nid])
5595+ bp1 = get_block_param(nid, 0)
5596+ if bp1 == ""
5597+ bp1 = "_x"
5598+ end
5599+ tmp_arr = new_temp
5600+ tmp_i = new_temp
5601+ if rt == "int_array"
5602+ @needs_int_array = 1
5603+ @needs_gc = 1
5604+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
5605+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
5606+ emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
5607+ @indent = @indent + 1
5608+ blk = @nd_block[nid]
5609+ if blk >= 0
5610+ body = @nd_body[blk]
5611+ if body >= 0
5612+ stmts = get_stmts(body)
5613+ if stmts.length > 0
5614+ last = stmts[stmts.length - 1]
5615+ val = compile_expr(last)
5616+ emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
5617+ end
5618+ end
5619+ end
5620+ @indent = @indent - 1
5621+ emit(" }")
5622+ return tmp_arr
5623+ end
5624+ "0"
5625+ end
5626+
5627+ def compile_select_expr(nid)
5628+ rt = infer_type(@nd_receiver[nid])
5629+ rc = compile_expr(@nd_receiver[nid])
5630+ bp1 = get_block_param(nid, 0)
5631+ if bp1 == ""
5632+ bp1 = "_x"
5633+ end
5634+ tmp_arr = new_temp
5635+ tmp_i = new_temp
5636+ if rt == "int_array"
5637+ @needs_int_array = 1
5638+ @needs_gc = 1
5639+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
5640+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
5641+ emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
5642+ @indent = @indent + 1
5643+ blk = @nd_block[nid]
5644+ if blk >= 0
5645+ body = @nd_body[blk]
5646+ if body >= 0
5647+ stmts = get_stmts(body)
5648+ if stmts.length > 0
5649+ last = stmts[stmts.length - 1]
5650+ cond = compile_expr(last)
5651+ emit(" if (" + cond + ") sp_IntArray_push(" + tmp_arr + ", lv_" + bp1 + ");")
5652+ end
5653+ end
5654+ end
5655+ @indent = @indent - 1
5656+ emit(" }")
5657+ return tmp_arr
5658+ end
5659+ "0"
5660+ end
5661+
52575662 def compile_reduce_expr(nid)
52585663 # Emit the reduce loop as side effects, return accumulator
52595664 compile_reduce_block(nid)
@@ -5365,57 +5770,778 @@
53655770 end
53665771 end
53675772
5368- def compile_stmts_body(nid)
5369- if nid < 0
5370- return
5773+ def compile_yield_stmt(nid)
5774+ args_id = @nd_arguments[nid]
5775+ arg_exprs = ""
5776+ if args_id >= 0
5777+ aids = get_args(args_id)
5778+ k = 0
5779+ while k < aids.length
5780+ if k > 0
5781+ arg_exprs = arg_exprs + ", "
5782+ end
5783+ arg_exprs = arg_exprs + compile_expr(aids[k])
5784+ k = k + 1
5785+ end
53715786 end
5372- stmts = get_stmts(nid)
5373- k = 0
5374- while k < stmts.length
5375- compile_stmt(stmts[k])
5376- k = k + 1
5787+ if arg_exprs != ""
5788+ emit(" if (_block) _block(" + arg_exprs + ", _benv);")
5789+ else
5790+ emit(" if (_block) _block(0, _benv);")
53775791 end
53785792 end
53795793
5380- def compile_body_return(body_id, return_type)
5381- if body_id < 0
5382- if return_type != "void"
5383- emit(" return " + c_default_val(return_type) + ";")
5384- end
5794+ def compile_yield_call_stmt(nid, mi)
5795+ # Call a yield-using top-level function with a block
5796+ # Inline the function body, replacing yield with block body
5797+ blk = @nd_block[nid]
5798+ if blk < 0
53855799 return
53865800 end
5387- stmts = get_stmts(body_id)
5388- if stmts.length == 0
5389- if return_type != "void"
5390- emit(" return " + c_default_val(return_type) + ";")
5801+
5802+ # Get block params
5803+ bp_names = "".split(",")
5804+ bp = @nd_parameters[blk]
5805+ if bp >= 0
5806+ inner = @nd_parameters[bp]
5807+ if inner >= 0
5808+ reqs = parse_id_list(@nd_requireds[inner])
5809+ k = 0
5810+ while k < reqs.length
5811+ bp_names.push(@nd_name[reqs[k]])
5812+ k = k + 1
5813+ end
53915814 end
5392- return
53935815 end
5394- # All but last
5395- i = 0
5396- while i < stmts.length - 1
5397- compile_stmt(stmts[i])
5398- i = i + 1
5816+
5817+ args_id = @nd_arguments[nid]
5818+ arg_ids = []
5819+ if args_id >= 0
5820+ arg_ids = get_args(args_id)
53995821 end
5400- last = stmts[stmts.length - 1]
5401- if @nd_type[last] == "ReturnNode"
5402- compile_return_stmt(last)
5403- return
5404- end
5405- if @nd_type[last] == "IfNode"
5406- compile_if_return(last, return_type)
5407- return
5408- end
5409- if @nd_type[last] == "CaseNode"
5410- compile_case_return(last, return_type)
5411- return
5412- end
5413- if @nd_type[last] == "WhileNode"
5414- compile_while_stmt(last)
5415- if return_type != "void"
5416- emit(" return " + c_default_val(return_type) + ";")
5417- end
5418- return
5822+
5823+ # Declare and set the function's params as new temp vars
5824+ pnames = @meth_param_names[mi].split(",")
5825+ ptypes = @meth_param_types[mi].split(",")
5826+ # Create unique temp names for function params to avoid collision
5827+ @block_counter = @block_counter + 1
5828+ suffix = "_y" + @block_counter.to_s
5829+ param_map_from = "".split(",")
5830+ param_map_to = "".split(",")
5831+ k = 0
5832+ while k < pnames.length
5833+ pt = "int"
5834+ if k < ptypes.length
5835+ pt = ptypes[k]
5836+ end
5837+ tname = pnames[k] + suffix
5838+ val = "0"
5839+ if k < arg_ids.length
5840+ val = compile_expr(arg_ids[k])
5841+ end
5842+ emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")
5843+ param_map_from.push(pnames[k])
5844+ param_map_to.push(tname)
5845+ declare_var(tname, pt)
5846+ k = k + 1
5847+ end
5848+
5849+ # Also need to declare the function's local vars
5850+ bid = @meth_body_ids[mi]
5851+ if bid >= 0
5852+ flocals_n = "".split(",")
5853+ flocals_t = "".split(",")
5854+ scan_locals(bid, flocals_n, flocals_t, pnames)
5855+ k = 0
5856+ while k < flocals_n.length
5857+ tname = flocals_n[k] + suffix
5858+ emit(" " + c_type(flocals_t[k]) + " lv_" + tname + " = " + c_default_val(flocals_t[k]) + ";")
5859+ param_map_from.push(flocals_n[k])
5860+ param_map_to.push(tname)
5861+ declare_var(tname, flocals_t[k])
5862+ k = k + 1
5863+ end
5864+ end
5865+
5866+ # Compile function body, replacing yield with block body
5867+ # and renaming function locals to temp names
5868+ if bid >= 0
5869+ stmts = get_stmts(bid)
5870+ k = 0
5871+ while k < stmts.length
5872+ compile_stmt_with_block(stmts[k], blk, bp_names, param_map_from, param_map_to)
5873+ k = k + 1
5874+ end
5875+ end
5876+ end
5877+
5878+ def compile_stmt_with_block(nid, blk, bp_names, map_from, map_to)
5879+ if nid < 0
5880+ return
5881+ end
5882+ t = @nd_type[nid]
5883+ if t == "YieldNode"
5884+ # Replace yield with the block body
5885+ args_id = @nd_arguments[nid]
5886+ if args_id >= 0
5887+ aids = get_args(args_id)
5888+ k = 0
5889+ while k < aids.length
5890+ if k < bp_names.length
5891+ emit(" lv_" + bp_names[k] + " = " + compile_expr_remap(aids[k], map_from, map_to) + ";")
5892+ end
5893+ k = k + 1
5894+ end
5895+ end
5896+ body = @nd_body[blk]
5897+ if body >= 0
5898+ stmts = get_stmts(body)
5899+ sk = 0
5900+ while sk < stmts.length
5901+ compile_stmt(stmts[sk])
5902+ sk = sk + 1
5903+ end
5904+ end
5905+ return
5906+ end
5907+ if t == "LocalVariableWriteNode"
5908+ lname = @nd_name[nid]
5909+ rname = remap_local(lname, map_from, map_to)
5910+ val = compile_expr_remap(@nd_expression[nid], map_from, map_to)
5911+ emit(" lv_" + rname + " = " + val + ";")
5912+ return
5913+ end
5914+ if t == "LocalVariableOperatorWriteNode"
5915+ lname = @nd_name[nid]
5916+ rname = remap_local(lname, map_from, map_to)
5917+ op = @nd_binop[nid]
5918+ val = compile_expr_remap(@nd_expression[nid], map_from, map_to)
5919+ if op == "+"
5920+ emit(" lv_" + rname + " += " + val + ";")
5921+ end
5922+ if op == "-"
5923+ emit(" lv_" + rname + " -= " + val + ";")
5924+ end
5925+ if op == "*"
5926+ emit(" lv_" + rname + " *= " + val + ";")
5927+ end
5928+ return
5929+ end
5930+ if t == "WhileNode"
5931+ old = @in_loop
5932+ @in_loop = 1
5933+ cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
5934+ emit(" while (" + cond + ") {")
5935+ @indent = @indent + 1
5936+ body = @nd_body[nid]
5937+ if body >= 0
5938+ stmts = get_stmts(body)
5939+ sk = 0
5940+ while sk < stmts.length
5941+ compile_stmt_with_block(stmts[sk], blk, bp_names, map_from, map_to)
5942+ sk = sk + 1
5943+ end
5944+ end
5945+ @indent = @indent - 1
5946+ emit(" }")
5947+ @in_loop = old
5948+ return
5949+ end
5950+ if t == "IfNode"
5951+ cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
5952+ emit(" if (" + cond + ") {")
5953+ @indent = @indent + 1
5954+ body = @nd_body[nid]
5955+ if body >= 0
5956+ stmts = get_stmts(body)
5957+ sk = 0
5958+ while sk < stmts.length
5959+ compile_stmt_with_block(stmts[sk], blk, bp_names, map_from, map_to)
5960+ sk = sk + 1
5961+ end
5962+ end
5963+ @indent = @indent - 1
5964+ sub = @nd_subsequent[nid]
5965+ if sub >= 0
5966+ if @nd_type[sub] == "ElseNode"
5967+ emit(" } else {")
5968+ @indent = @indent + 1
5969+ eb = @nd_body[sub]
5970+ if eb >= 0
5971+ estmts = get_stmts(eb)
5972+ sk = 0
5973+ while sk < estmts.length
5974+ compile_stmt_with_block(estmts[sk], blk, bp_names, map_from, map_to)
5975+ sk = sk + 1
5976+ end
5977+ end
5978+ @indent = @indent - 1
5979+ else
5980+ emit(" } else")
5981+ compile_stmt_with_block(sub, blk, bp_names, map_from, map_to)
5982+ return
5983+ end
5984+ end
5985+ emit(" }")
5986+ return
5987+ end
5988+ if t == "CallNode"
5989+ # Check if block_given? with remap
5990+ if @nd_name[nid] == "block_given?"
5991+ if @nd_receiver[nid] < 0
5992+ # In inlined context, block IS given, do nothing
5993+ return
5994+ end
5995+ end
5996+ # Handle nested each/times with yield inside block
5997+ if @nd_block[nid] >= 0
5998+ mname2 = @nd_name[nid]
5999+ if mname2 == "each"
6000+ # Compile the each loop, but inside the block body, recurse with yield replacement
6001+ compile_each_with_yield_inline(nid, blk, bp_names, map_from, map_to)
6002+ return
6003+ end
6004+ end
6005+ # Compile call with remapping for args
6006+ val = compile_expr_remap(nid, map_from, map_to)
6007+ if val != "0"
6008+ emit(" " + val + ";")
6009+ end
6010+ return
6011+ end
6012+ if t == "ReturnNode"
6013+ # In inlined context, return becomes a value assignment
6014+ # but for simplicity, just skip
6015+ return
6016+ end
6017+ # Default: for expression statements, use remap
6018+ val = compile_expr_remap(nid, map_from, map_to)
6019+ if val != "0"
6020+ emit(" " + val + ";")
6021+ end
6022+ end
6023+
6024+ def remap_local(name, map_from, map_to)
6025+ k = 0
6026+ while k < map_from.length
6027+ if map_from[k] == name
6028+ return map_to[k]
6029+ end
6030+ k = k + 1
6031+ end
6032+ name
6033+ end
6034+
6035+ def compile_expr_remap(nid, map_from, map_to)
6036+ if nid < 0
6037+ return "0"
6038+ end
6039+ t = @nd_type[nid]
6040+ if t == "LocalVariableReadNode"
6041+ rname = remap_local(@nd_name[nid], map_from, map_to)
6042+ return "lv_" + rname
6043+ end
6044+ if t == "InstanceVariableReadNode"
6045+ # Remap self to the _yself variable
6046+ self_name = remap_local("_self_", map_from, map_to)
6047+ return self_name + "->" + sanitize_ivar(@nd_name[nid])
6048+ end
6049+ if t == "SelfNode"
6050+ return remap_local("_self_", map_from, map_to)
6051+ end
6052+ if t == "CallNode"
6053+ if @nd_name[nid] == "block_given?"
6054+ if @nd_receiver[nid] < 0
6055+ return "1"
6056+ end
6057+ end
6058+ # For operators with remapped locals
6059+ mname = @nd_name[nid]
6060+ recv = @nd_receiver[nid]
6061+ if recv >= 0
6062+ if mname == "+"
6063+ return "(" + compile_expr_remap(recv, map_from, map_to) + " + " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6064+ end
6065+ if mname == "-"
6066+ args_id = @nd_arguments[nid]
6067+ if args_id < 0
6068+ return "(-" + compile_expr_remap(recv, map_from, map_to) + ")"
6069+ end
6070+ return "(" + compile_expr_remap(recv, map_from, map_to) + " - " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6071+ end
6072+ if mname == "*"
6073+ return "(" + compile_expr_remap(recv, map_from, map_to) + " * " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6074+ end
6075+ if mname == "/"
6076+ return "(" + compile_expr_remap(recv, map_from, map_to) + " / " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6077+ end
6078+ if mname == "%"
6079+ return "sp_imod(" + compile_expr_remap(recv, map_from, map_to) + ", " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6080+ end
6081+ if mname == "<"
6082+ return "(" + compile_expr_remap(recv, map_from, map_to) + " < " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6083+ end
6084+ if mname == ">"
6085+ return "(" + compile_expr_remap(recv, map_from, map_to) + " > " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6086+ end
6087+ if mname == "<="
6088+ return "(" + compile_expr_remap(recv, map_from, map_to) + " <= " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6089+ end
6090+ if mname == ">="
6091+ return "(" + compile_expr_remap(recv, map_from, map_to) + " >= " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6092+ end
6093+ if mname == "=="
6094+ return "(" + compile_expr_remap(recv, map_from, map_to) + " == " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6095+ end
6096+ if mname == "!="
6097+ return "(" + compile_expr_remap(recv, map_from, map_to) + " != " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
6098+ end
6099+ end
6100+ end
6101+ # Fallback: compile normally
6102+ compile_expr(nid)
6103+ end
6104+
6105+ def compile_expr_remap_arg0(nid, map_from, map_to)
6106+ args_id = @nd_arguments[nid]
6107+ if args_id >= 0
6108+ arg_ids = get_args(args_id)
6109+ if arg_ids.length > 0
6110+ return compile_expr_remap(arg_ids[0], map_from, map_to)
6111+ end
6112+ end
6113+ "0"
6114+ end
6115+
6116+ def compile_yield_method_call_stmt(nid, cci, midx, mname)
6117+ # Call a yield-using class method with a block - inline the method body
6118+ blk = @nd_block[nid]
6119+ if blk < 0
6120+ return
6121+ end
6122+ bp_names = "".split(",")
6123+ bp = @nd_parameters[blk]
6124+ if bp >= 0
6125+ inner = @nd_parameters[bp]
6126+ if inner >= 0
6127+ reqs = parse_id_list(@nd_requireds[inner])
6128+ k = 0
6129+ while k < reqs.length
6130+ bp_names.push(@nd_name[reqs[k]])
6131+ k = k + 1
6132+ end
6133+ end
6134+ end
6135+
6136+ recv = @nd_receiver[nid]
6137+ rc = compile_expr(recv)
6138+
6139+ bodies = @cls_meth_bodies[cci].split(";")
6140+ bid = -1
6141+ if midx < bodies.length
6142+ bid = bodies[midx].to_i
6143+ end
6144+
6145+ saved_ci = @current_class_idx
6146+ @current_class_idx = cci
6147+
6148+ cname = @cls_names[cci]
6149+ @block_counter = @block_counter + 1
6150+ suffix = "_y" + @block_counter.to_s
6151+
6152+ # For the method params and locals, create remapped names
6153+ all_params = @cls_meth_params[cci].split("|")
6154+ all_ptypes = @cls_meth_ptypes[cci].split("|")
6155+ pnames = "".split(",")
6156+ ptypes = "".split(",")
6157+ if midx < all_params.length
6158+ pnames = all_params[midx].split(",")
6159+ end
6160+ if midx < all_ptypes.length
6161+ ptypes = all_ptypes[midx].split(",")
6162+ end
6163+
6164+ map_from = "".split(",")
6165+ map_to = "".split(",")
6166+
6167+ # Map self to the receiver expression
6168+ map_from.push("_self_")
6169+ map_to.push(rc)
6170+
6171+ args_id = @nd_arguments[nid]
6172+ arg_ids = []
6173+ if args_id >= 0
6174+ arg_ids = get_args(args_id)
6175+ end
6176+
6177+ k = 0
6178+ while k < pnames.length
6179+ pt = "int"
6180+ if k < ptypes.length
6181+ pt = ptypes[k]
6182+ end
6183+ tname = pnames[k] + suffix
6184+ val = "0"
6185+ if k < arg_ids.length
6186+ val = compile_expr(arg_ids[k])
6187+ end
6188+ emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")
6189+ map_from.push(pnames[k])
6190+ map_to.push(tname)
6191+ declare_var(tname, pt)
6192+ k = k + 1
6193+ end
6194+
6195+ # Declare function locals
6196+ if bid >= 0
6197+ flocals_n = "".split(",")
6198+ flocals_t = "".split(",")
6199+ scan_locals(bid, flocals_n, flocals_t, pnames)
6200+ k = 0
6201+ while k < flocals_n.length
6202+ tname = flocals_n[k] + suffix
6203+ emit(" " + c_type(flocals_t[k]) + " lv_" + tname + " = " + c_default_val(flocals_t[k]) + ";")
6204+ map_from.push(flocals_n[k])
6205+ map_to.push(tname)
6206+ declare_var(tname, flocals_t[k])
6207+ k = k + 1
6208+ end
6209+ end
6210+
6211+ # Compile the method body inline with yield -> block body
6212+ if bid >= 0
6213+ stmts = get_stmts(bid)
6214+ k = 0
6215+ while k < stmts.length
6216+ compile_stmt_with_block(stmts[k], blk, bp_names, map_from, map_to)
6217+ k = k + 1
6218+ end
6219+ end
6220+
6221+ @current_class_idx = saved_ci
6222+ end
6223+
6224+ def compile_each_with_yield_inline(nid, outer_blk, outer_bp_names, map_from, map_to)
6225+ # An each call on an array inside an inlined yield function
6226+ # The each block contains yield statements that should be replaced with outer block body
6227+ recv = @nd_receiver[nid]
6228+ recv_expr = compile_expr_remap(recv, map_from, map_to)
6229+ rt = infer_type(recv)
6230+ # If recv is remapped, get the actual type
6231+ if @nd_type[recv] == "InstanceVariableReadNode"
6232+ if @current_class_idx >= 0
6233+ rt = cls_ivar_type(@current_class_idx, @nd_name[recv])
6234+ end
6235+ end
6236+
6237+ inner_bp1 = get_block_param(nid, 0)
6238+ if inner_bp1 == ""
6239+ inner_bp1 = "_ex"
6240+ end
6241+ # The inner block param might collide with outer names, so remap it
6242+ inner_bp_remapped = remap_local(inner_bp1, map_from, map_to)
6243+
6244+ old = @in_loop
6245+ @in_loop = 1
6246+ tmp = new_temp
6247+
6248+ if rt == "int_array"
6249+ emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_IntArray_length(" + recv_expr + "); " + tmp + "++) {")
6250+ emit(" lv_" + inner_bp_remapped + " = sp_IntArray_get(" + recv_expr + ", " + tmp + ");")
6251+ @indent = @indent + 1
6252+ # Compile inner block body, replacing yield with outer block body
6253+ inner_blk = @nd_block[nid]
6254+ if inner_blk >= 0
6255+ ibody = @nd_body[inner_blk]
6256+ if ibody >= 0
6257+ istmts = get_stmts(ibody)
6258+ sk = 0
6259+ while sk < istmts.length
6260+ # In the inner block, yield should be replaced with outer block body
6261+ inner_nid = istmts[sk]
6262+ if @nd_type[inner_nid] == "YieldNode"
6263+ # yield x -> set outer bp from inner bp, then run outer block body
6264+ yargs = @nd_arguments[inner_nid]
6265+ if yargs >= 0
6266+ yaids = get_args(yargs)
6267+ yk = 0
6268+ while yk < yaids.length
6269+ if yk < outer_bp_names.length
6270+ emit(" lv_" + outer_bp_names[yk] + " = " + compile_expr_remap(yaids[yk], map_from, map_to) + ";")
6271+ end
6272+ yk = yk + 1
6273+ end
6274+ end
6275+ obody = @nd_body[outer_blk]
6276+ if obody >= 0
6277+ ostmts = get_stmts(obody)
6278+ ok = 0
6279+ while ok < ostmts.length
6280+ compile_stmt(ostmts[ok])
6281+ ok = ok + 1
6282+ end
6283+ end
6284+ else
6285+ compile_stmt_with_block(inner_nid, outer_blk, outer_bp_names, map_from, map_to)
6286+ end
6287+ sk = sk + 1
6288+ end
6289+ end
6290+ end
6291+ @indent = @indent - 1
6292+ emit(" }")
6293+ end
6294+ @in_loop = old
6295+ end
6296+
6297+ def scan_captured_locals(nid, excludes, names, types)
6298+ if nid < 0
6299+ return
6300+ end
6301+ if @nd_type[nid] == "LocalVariableReadNode"
6302+ lname = @nd_name[nid]
6303+ if not_in(lname, excludes) == 1
6304+ if not_in(lname, names) == 1
6305+ names.push(lname)
6306+ vt = find_var_type(lname)
6307+ if vt == ""
6308+ vt = "int"
6309+ end
6310+ types.push(vt)
6311+ end
6312+ end
6313+ end
6314+ if @nd_type[nid] == "LocalVariableWriteNode"
6315+ lname = @nd_name[nid]
6316+ if not_in(lname, excludes) == 1
6317+ if not_in(lname, names) == 1
6318+ names.push(lname)
6319+ vt = find_var_type(lname)
6320+ if vt == ""
6321+ vt = "int"
6322+ end
6323+ types.push(vt)
6324+ end
6325+ end
6326+ end
6327+ # Recurse
6328+ if @nd_body[nid] >= 0
6329+ scan_captured_locals(@nd_body[nid], excludes, names, types)
6330+ end
6331+ stmts = parse_id_list(@nd_stmts[nid])
6332+ k = 0
6333+ while k < stmts.length
6334+ scan_captured_locals(stmts[k], excludes, names, types)
6335+ k = k + 1
6336+ end
6337+ if @nd_expression[nid] >= 0
6338+ scan_captured_locals(@nd_expression[nid], excludes, names, types)
6339+ end
6340+ if @nd_predicate[nid] >= 0
6341+ scan_captured_locals(@nd_predicate[nid], excludes, names, types)
6342+ end
6343+ if @nd_left[nid] >= 0
6344+ scan_captured_locals(@nd_left[nid], excludes, names, types)
6345+ end
6346+ if @nd_right[nid] >= 0
6347+ scan_captured_locals(@nd_right[nid], excludes, names, types)
6348+ end
6349+ if @nd_subsequent[nid] >= 0
6350+ scan_captured_locals(@nd_subsequent[nid], excludes, names, types)
6351+ end
6352+ if @nd_else_clause[nid] >= 0
6353+ scan_captured_locals(@nd_else_clause[nid], excludes, names, types)
6354+ end
6355+ if @nd_arguments[nid] >= 0
6356+ scan_captured_locals(@nd_arguments[nid], excludes, names, types)
6357+ end
6358+ args = parse_id_list(@nd_args[nid])
6359+ k = 0
6360+ while k < args.length
6361+ scan_captured_locals(args[k], excludes, names, types)
6362+ k = k + 1
6363+ end
6364+ if @nd_receiver[nid] >= 0
6365+ scan_captured_locals(@nd_receiver[nid], excludes, names, types)
6366+ end
6367+ end
6368+
6369+ def compile_map_block(nid)
6370+ @needs_int_array = 1
6371+ @needs_gc = 1
6372+ old = @in_loop
6373+ @in_loop = 1
6374+ rt = infer_type(@nd_receiver[nid])
6375+ rc = compile_expr(@nd_receiver[nid])
6376+ bp1 = get_block_param(nid, 0)
6377+ if bp1 == ""
6378+ bp1 = "_x"
6379+ end
6380+ tmp_arr = new_temp
6381+ tmp_i = new_temp
6382+ if rt == "int_array"
6383+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
6384+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
6385+ emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
6386+ @indent = @indent + 1
6387+ blk = @nd_block[nid]
6388+ if blk >= 0
6389+ body = @nd_body[blk]
6390+ if body >= 0
6391+ stmts = get_stmts(body)
6392+ if stmts.length > 0
6393+ last = stmts[stmts.length - 1]
6394+ val = compile_expr(last)
6395+ emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
6396+ end
6397+ end
6398+ end
6399+ @indent = @indent - 1
6400+ emit(" }")
6401+ end
6402+ if rt == "str_array"
6403+ @needs_str_array = 1
6404+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
6405+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
6406+ emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")
6407+ @indent = @indent + 1
6408+ blk = @nd_block[nid]
6409+ if blk >= 0
6410+ body = @nd_body[blk]
6411+ if body >= 0
6412+ stmts = get_stmts(body)
6413+ if stmts.length > 0
6414+ last = stmts[stmts.length - 1]
6415+ val = compile_expr(last)
6416+ emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");")
6417+ end
6418+ end
6419+ end
6420+ @indent = @indent - 1
6421+ emit(" }")
6422+ end
6423+ @in_loop = old
6424+ end
6425+
6426+ def compile_select_block(nid)
6427+ @needs_int_array = 1
6428+ @needs_gc = 1
6429+ old = @in_loop
6430+ @in_loop = 1
6431+ rt = infer_type(@nd_receiver[nid])
6432+ rc = compile_expr(@nd_receiver[nid])
6433+ bp1 = get_block_param(nid, 0)
6434+ if bp1 == ""
6435+ bp1 = "_x"
6436+ end
6437+ tmp_arr = new_temp
6438+ tmp_i = new_temp
6439+ if rt == "int_array"
6440+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
6441+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
6442+ emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
6443+ @indent = @indent + 1
6444+ blk = @nd_block[nid]
6445+ if blk >= 0
6446+ body = @nd_body[blk]
6447+ if body >= 0
6448+ stmts = get_stmts(body)
6449+ if stmts.length > 0
6450+ last = stmts[stmts.length - 1]
6451+ cond = compile_expr(last)
6452+ emit(" if (" + cond + ") sp_IntArray_push(" + tmp_arr + ", lv_" + bp1 + ");")
6453+ end
6454+ end
6455+ end
6456+ @indent = @indent - 1
6457+ emit(" }")
6458+ end
6459+ @in_loop = old
6460+ end
6461+
6462+ def compile_stmts_body(nid)
6463+ if nid < 0
6464+ return
6465+ end
6466+ stmts = get_stmts(nid)
6467+ k = 0
6468+ while k < stmts.length
6469+ compile_stmt(stmts[k])
6470+ k = k + 1
6471+ end
6472+ end
6473+
6474+ def compile_body_return(body_id, return_type)
6475+ if body_id < 0
6476+ if return_type != "void"
6477+ emit(" return " + c_default_val(return_type) + ";")
6478+ end
6479+ return
6480+ end
6481+ stmts = get_stmts(body_id)
6482+ if stmts.length == 0
6483+ if return_type != "void"
6484+ emit(" return " + c_default_val(return_type) + ";")
6485+ end
6486+ return
6487+ end
6488+ # All but last
6489+ i = 0
6490+ while i < stmts.length - 1
6491+ compile_stmt(stmts[i])
6492+ i = i + 1
6493+ end
6494+ last = stmts[stmts.length - 1]
6495+ if @nd_type[last] == "ReturnNode"
6496+ compile_return_stmt(last)
6497+ return
6498+ end
6499+ if @nd_type[last] == "IfNode"
6500+ compile_if_return(last, return_type)
6501+ return
6502+ end
6503+ if @nd_type[last] == "CaseNode"
6504+ compile_case_return(last, return_type)
6505+ return
6506+ end
6507+ if @nd_type[last] == "WhileNode"
6508+ compile_while_stmt(last)
6509+ if return_type != "void"
6510+ emit(" return " + c_default_val(return_type) + ";")
6511+ end
6512+ return
6513+ end
6514+ if @nd_type[last] == "YieldNode"
6515+ compile_yield_stmt(last)
6516+ if return_type != "void"
6517+ emit(" return " + c_default_val(return_type) + ";")
6518+ end
6519+ return
6520+ end
6521+ # If last statement is a CallNode with a block, handle map/select as expressions
6522+ if @nd_type[last] == "CallNode"
6523+ if @nd_block[last] >= 0
6524+ lmname = @nd_name[last]
6525+ if lmname == "map"
6526+ if return_type != "void"
6527+ val = compile_map_expr(last)
6528+ emit(" return " + val + ";")
6529+ return
6530+ end
6531+ end
6532+ if lmname == "select"
6533+ if return_type != "void"
6534+ val = compile_select_expr(last)
6535+ emit(" return " + val + ";")
6536+ return
6537+ end
6538+ end
6539+ compile_stmt(last)
6540+ if return_type != "void"
6541+ emit(" return " + c_default_val(return_type) + ";")
6542+ end
6543+ return
6544+ end
54196545 end
54206546 if return_type != "void"
54216547 emit(" return " + compile_expr(last) + ";")