Commit 275b1615

275b16152569Merged

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

Description

refactor: split compile_call_stmt into 6 focused submethods

Break up the 493-line compile_call_stmt into a dispatcher (~50 lines) and 6 submethods returning 1 (handled) or 0 (not handled):

compile_io_call_stmt (puts, print, printf) compile_file_open_call_stmt (File.open with block) compile_mutating_call_stmt ([]=, delete, <<, replace, clear, push, sort!, reverse!) compile_block_iteration_stmt (each, times, upto, downto, loop, reduce, reject) compile_control_call_stmt (raise, system, trap, catch, throw, File ops, exit) compile_writer_and_block_call_stmt (attr_writer, map, select, yield calls)

Verified: 57/57 tests pass, bootstrap gen2==gen3, fib=5702887.

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

Details

Commit
275b16152569
Parent
6d53252cbe87

spinel_codegen.rbmodifiedView file

@@ -11120,6 +11120,7 @@
1112011120 emit(" return 0;")
1112111121 end
1112211122
11123+
1112311124 def compile_call_stmt(nid)
1112411125 mname = @nd_name[nid]
1112511126 recv = @nd_receiver[nid]
@@ -11129,16 +11130,54 @@
1112911130 return
1113011131 end
1113111132
11133+ # IO: puts, print, printf
11134+ if compile_io_call_stmt(nid, mname, recv) == 1
11135+ return
11136+ end
11137+
11138+ # File.open with block
11139+ if compile_file_open_call_stmt(nid, mname, recv) == 1
11140+ return
11141+ end
11142+
11143+ # Mutating operations: []=, delete, <<, replace, clear, push, reverse!, sort!
11144+ if compile_mutating_call_stmt(nid, mname, recv) == 1
11145+ return
11146+ end
11147+
11148+ # Block iteration: each, times, upto, downto, loop, reduce, inject, reject
11149+ if compile_block_iteration_stmt(nid, mname, recv) == 1
11150+ return
11151+ end
11152+
11153+ # Control flow: raise, system, trap, catch, throw, File ops, exit
11154+ if compile_control_call_stmt(nid, mname, recv) == 1
11155+ return
11156+ end
11157+
11158+ # attr_writer, map, select, yield method calls
11159+ if compile_writer_and_block_call_stmt(nid, mname, recv) == 1
11160+ return
11161+ end
11162+
11163+ # General
11164+ val = compile_expr(nid)
11165+ if val != "0"
11166+ emit(" " + val + ";")
11167+ end
11168+ end
11169+
11170+ def compile_io_call_stmt(nid, mname, recv)
1113211171 if mname == "puts"
1113311172 if recv < 0
1113411173 compile_puts(nid)
11135- return
11174+ return 1
1113611175 end
1113711176 if recv >= 0
1113811177 if @nd_type[recv] == "GlobalVariableReadNode"
1113911178 if @nd_name[recv] == "$stderr"
1114011179 compile_stderr_puts(nid)
11141- return
11180+ return 1
1114211181 end
1114311182 end
1114411183 end
@@ -11146,7 +11185,7 @@
1114611185 if mname == "print"
1114711186 if recv < 0
1114811187 compile_print(nid)
11149- return
11188+ return 1
1115011189 end
1115111190 end
1115211191 if mname == "printf"
@@ -11169,12 +11208,15 @@
1116911208 k = k + 1
1117011209 end
1117111210 emit(" printf(" + fmt_expr + rest_args + ");")
11172- return
11211+ return 1
1117311212 end
1117411213 end
1117511214 end
1117611215 end
11216+ 0
11217+ end
1117711218
11219+ def compile_file_open_call_stmt(nid, mname, recv)
1117811220 # File.open with block
1117911221 if mname == "open"
1118011222 if recv >= 0
@@ -11211,18 +11253,21 @@
1121111253 end
1121211254 emit(" fclose(" + ftmp + ");")
1121311255 emit(" } }")
11214- return
11256+ return 1
1121511257 end
1121611258 end
1121711259 end
1121811260 end
1121911261 end
11262+ 0
11263+ end
1122011264
11265+ def compile_mutating_call_stmt(nid, mname, recv)
1122111266 # []=
1122211267 if mname == "[]="
1122311268 if recv >= 0
1122411269 compile_bracket_assign(nid)
11225- return
11270+ return 1
1122611271 end
1122711272 end
1122811273
@@ -11233,11 +11278,11 @@
1123311278 rc = compile_expr(recv)
1123411279 if rt == "str_int_hash"
1123511280 emit(" sp_StrIntHash_delete(" + rc + ", " + compile_arg0(nid) + ");")
11236- return
11281+ return 1
1123711282 end
1123811283 if rt == "str_str_hash"
1123911284 emit(" sp_StrStrHash_delete(" + rc + ", " + compile_arg0(nid) + ");")
11240- return
11285+ return 1
1124111286 end
1124211287 end
1124311288 end
@@ -11253,11 +11298,11 @@
1125311298 # If receiver is a local variable, reassign
1125411299 if @nd_type[recv] == "LocalVariableReadNode"
1125511300 emit(" lv_" + @nd_name[recv] + " = sp_str_concat(lv_" + @nd_name[recv] + ", " + val + ");")
11256- return
11301+ return 1
1125711302 end
1125811303 if @nd_type[recv] == "InstanceVariableReadNode"
1125911304 emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = sp_str_concat(self->" + sanitize_ivar(@nd_name[recv]) + ", " + val + ");")
11260- return
11305+ return 1
1126111306 end
1126211307 end
1126311308 end
@@ -11271,11 +11316,11 @@
1127111316 val = compile_arg0(nid)
1127211317 if @nd_type[recv] == "LocalVariableReadNode"
1127311318 emit(" lv_" + @nd_name[recv] + " = " + val + ";")
11274- return
11319+ return 1
1127511320 end
1127611321 if @nd_type[recv] == "InstanceVariableReadNode"
1127711322 emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = " + val + ";")
11278- return
11323+ return 1
1127911324 end
1128011325 end
1128111326 end
@@ -11288,11 +11333,11 @@
1128811333 if rt == "string"
1128911334 if @nd_type[recv] == "LocalVariableReadNode"
1129011335 emit(" lv_" + @nd_name[recv] + " = \"\";")
11291- return
11336+ return 1
1129211337 end
1129311338 if @nd_type[recv] == "InstanceVariableReadNode"
1129411339 emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = \"\";")
11295- return
11340+ return 1
1129611341 end
1129711342 end
1129811343 end
@@ -11320,11 +11365,11 @@
1132011365 end
1132111366 end
1132211367 emit(" sp_IntArray_push(" + rc + ", " + av + ");")
11323- return
11368+ return 1
1132411369 end
1132511370 if rt == "str_array"
1132611371 emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");")
11327- return
11372+ return 1
1132811373 end
1132911374 end
1133011375 end
@@ -11336,7 +11381,7 @@
1133611381 rc = compile_expr(recv)
1133711382 if rt == "int_array"
1133811383 emit(" sp_IntArray_reverse_bang(" + rc + ");")
11339- return
11384+ return 1
1134011385 end
1134111386 end
1134211387 end
@@ -11346,11 +11391,14 @@
1134611391 rc = compile_expr(recv)
1134711392 if rt == "int_array"
1134811393 emit(" sp_IntArray_sort_bang(" + rc + ");")
11349- return
11394+ return 1
1135011395 end
1135111396 end
1135211397 end
11398+ 0
11399+ end
1135311400
11401+ def compile_block_iteration_stmt(nid, mname, recv)
1135411402 # each with block
1135511403 if mname == "each"
1135611404 if @nd_block[nid] >= 0
@@ -11361,11 +11409,11 @@
1136111409 # Fall through to yield method handler below
1136211410 else
1136311411 compile_each_block(nid)
11364- return
11412+ return 1
1136511413 end
1136611414 else
1136711415 compile_each_block(nid)
11368- return
11416+ return 1
1136911417 end
1137011418 end
1137111419 end
@@ -11374,19 +11422,19 @@
1137411422 if mname == "times"
1137511423 if @nd_block[nid] >= 0
1137611424 compile_times_block(nid)
11377- return
11425+ return 1
1137811426 end
1137911427 end
1138011428 if mname == "upto"
1138111429 if @nd_block[nid] >= 0
1138211430 compile_upto_block(nid)
11383- return
11431+ return 1
1138411432 end
1138511433 end
1138611434 if mname == "downto"
1138711435 if @nd_block[nid] >= 0
1138811436 compile_downto_block(nid)
11389- return
11437+ return 1
1139011438 end
1139111439 end
1139211440
@@ -11394,13 +11442,13 @@
1139411442 if mname == "reduce"
1139511443 if @nd_block[nid] >= 0
1139611444 compile_reduce_block(nid)
11397- return
11445+ return 1
1139811446 end
1139911447 end
1140011448 if mname == "inject"
1140111449 if @nd_block[nid] >= 0
1140211450 compile_reduce_block(nid)
11403- return
11451+ return 1
1140411452 end
1140511453 end
1140611454
@@ -11408,7 +11456,7 @@
1140811456 if mname == "reject"
1140911457 if @nd_block[nid] >= 0
1141011458 compile_reject_block(nid)
11411- return
11459+ return 1
1141211460 end
1141311461 end
1141411462
@@ -11423,10 +11471,13 @@
1142311471 @indent = @indent - 1
1142411472 emit(" }")
1142511473 @in_loop = old
11426- return
11474+ return 1
1142711475 end
1142811476 end
11477+ 0
11478+ end
1142911479
11480+ def compile_control_call_stmt(nid, mname, recv)
1143011481 # raise
1143111482 if mname == "raise"
1143211483 if recv < 0
@@ -11456,7 +11507,7 @@
1145611507 else
1145711508 emit(" sp_raise(\"RuntimeError\");")
1145811509 end
11459- return
11510+ return 1
1146011511 end
1146111512 end
1146211513
@@ -11466,14 +11517,14 @@
1146611517 @needs_file_io = 1
1146711518 emit(" fflush(stdout);")
1146811519 emit(" sp_last_status = system(" + compile_arg0(nid) + ");")
11469- return
11520+ return 1
1147011521 end
1147111522 end
1147211523
1147311524 # trap (just ignore)
1147411525 if mname == "trap"
1147511526 if recv < 0
11476- return
11527+ return 1
1147711528 end
1147811529 end
1147911530
@@ -11482,14 +11533,14 @@
1148211533 if recv < 0
1148311534 if @nd_block[nid] >= 0
1148411535 compile_catch_stmt(nid)
11485- return
11536+ return 1
1148611537 end
1148711538 end
1148811539 end
1148911540 if mname == "throw"
1149011541 if recv < 0
1149111542 compile_throw_stmt(nid)
11492- return
11543+ return 1
1149311544 end
1149411545 end
1149511546
@@ -11514,12 +11565,12 @@
1151411565 a1 = compile_expr(arg_ids[1])
1151511566 end
1151611567 emit(" sp_file_write(" + a0 + ", " + a1 + ");")
11517- return
11568+ return 1
1151811569 end
1151911570 if mname == "delete"
1152011571 @needs_file_io = 1
1152111572 emit(" sp_file_delete(" + compile_arg0(nid) + ");")
11522- return
11573+ return 1
1152311574 end
1152411575 end
1152511576 end
@@ -11530,10 +11581,13 @@
1153011581 if recv < 0
1153111582 val = compile_arg0(nid)
1153211583 emit(" exit(" + val + ");")
11533- return
11584+ return 1
1153411585 end
1153511586 end
11587+ 0
11588+ end
1153611589
11590+ def compile_writer_and_block_call_stmt(nid, mname, recv)
1153711591 # attr_writer: obj.x = val
1153811592 if recv >= 0
1153911593 if mname.length > 1
@@ -11543,7 +11597,7 @@
1154311597 if is_obj_type(rt) == 1
1154411598 rc = compile_expr(recv)
1154511599 emit(" " + rc + "->" + bname + " = " + compile_arg0(nid) + ";")
11546- return
11600+ return 1
1154711601 end
1154811602 end
1154911603 end
@@ -11553,7 +11607,7 @@
1155311607 if mname == "map"
1155411608 if @nd_block[nid] >= 0
1155511609 compile_map_block(nid)
11556- return
11610+ return 1
1155711611 end
1155811612 end
1155911613
@@ -11561,7 +11615,7 @@
1156111615 if mname == "select"
1156211616 if @nd_block[nid] >= 0
1156311617 compile_select_block(nid)
11564- return
11618+ return 1
1156511619 end
1156611620 end
1156711621
@@ -11572,7 +11626,7 @@
1157211626 if mi >= 0
1157311627 if @meth_has_yield[mi] == 1
1157411628 compile_yield_call_stmt(nid, mi)
11575- return
11629+ return 1
1157611630 end
1157711631 end
1157811632 end
@@ -11587,7 +11641,7 @@
1158711641 if midx >= 0
1158811642 if cls_method_has_yield(cci, midx) == 1
1158911643 compile_yield_method_call_stmt(nid, cci, midx, mname)
11590- return
11644+ return 1
1159111645 end
1159211646 end
1159311647 # Check parent
@@ -11598,7 +11652,7 @@
1159811652 if pidx >= 0
1159911653 if cls_method_has_yield(pci, pidx) == 1
1160011654 compile_yield_method_call_stmt(nid, pci, pidx, mname)
11601- return
11655+ return 1
1160211656 end
1160311657 end
1160411658 end
@@ -11607,14 +11661,10 @@
1160711661 end
1160811662 end
1160911663 end
11610-
11611- # General
11612- val = compile_expr(nid)
11613- if val != "0"
11614- emit(" " + val + ";")
11615- end
11664+ 0
1161611665 end
1161711666
11667+
1161811668 def compile_file_block_stmt(nid, ftmp, bp)
1161911669 if nid < 0
1162011670 return