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
emit(" return 0;") end def compile_call_stmt(nid) mname = @nd_name[nid] recv = @nd_receiver[nid] return end # IO: puts, print, printf if compile_io_call_stmt(nid, mname, recv) == 1 return end # File.open with block if compile_file_open_call_stmt(nid, mname, recv) == 1 return end # Mutating operations: []=, delete, <<, replace, clear, push, reverse!, sort! if compile_mutating_call_stmt(nid, mname, recv) == 1 return end # Block iteration: each, times, upto, downto, loop, reduce, inject, reject if compile_block_iteration_stmt(nid, mname, recv) == 1 return end # Control flow: raise, system, trap, catch, throw, File ops, exit if compile_control_call_stmt(nid, mname, recv) == 1 return end # attr_writer, map, select, yield method calls if compile_writer_and_block_call_stmt(nid, mname, recv) == 1 return end # General val = compile_expr(nid) if val != "0" emit(" " + val + ";") end end def compile_io_call_stmt(nid, mname, recv) if mname == "puts" if recv < 0 compile_puts(nid) return return 1 end if recv >= 0 if @nd_type[recv] == "GlobalVariableReadNode" if @nd_name[recv] == "$stderr" compile_stderr_puts(nid) return return 1 end end end if mname == "print" if recv < 0 compile_print(nid) return return 1 end end if mname == "printf" k = k + 1 end emit(" printf(" + fmt_expr + rest_args + ");") return return 1 end end end end 0 end def compile_file_open_call_stmt(nid, mname, recv) # File.open with block if mname == "open" if recv >= 0 end emit(" fclose(" + ftmp + ");") emit(" } }") return return 1 end end end end end 0 end def compile_mutating_call_stmt(nid, mname, recv) # []= if mname == "[]=" if recv >= 0 compile_bracket_assign(nid) return return 1 end end rc = compile_expr(recv) if rt == "str_int_hash" emit(" sp_StrIntHash_delete(" + rc + ", " + compile_arg0(nid) + ");") return return 1 end if rt == "str_str_hash" emit(" sp_StrStrHash_delete(" + rc + ", " + compile_arg0(nid) + ");") return return 1 end end end # If receiver is a local variable, reassign if @nd_type[recv] == "LocalVariableReadNode" emit(" lv_" + @nd_name[recv] + " = sp_str_concat(lv_" + @nd_name[recv] + ", " + val + ");") return return 1 end if @nd_type[recv] == "InstanceVariableReadNode" emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = sp_str_concat(self->" + sanitize_ivar(@nd_name[recv]) + ", " + val + ");") return return 1 end end end val = compile_arg0(nid) if @nd_type[recv] == "LocalVariableReadNode" emit(" lv_" + @nd_name[recv] + " = " + val + ";") return return 1 end if @nd_type[recv] == "InstanceVariableReadNode" emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = " + val + ";") return return 1 end end end if rt == "string" if @nd_type[recv] == "LocalVariableReadNode" emit(" lv_" + @nd_name[recv] + " = \"\";") return return 1 end if @nd_type[recv] == "InstanceVariableReadNode" emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = \"\";") return return 1 end end end end end emit(" sp_IntArray_push(" + rc + ", " + av + ");") return return 1 end if rt == "str_array" emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");") return return 1 end end end rc = compile_expr(recv) if rt == "int_array" emit(" sp_IntArray_reverse_bang(" + rc + ");") return return 1 end end end rc = compile_expr(recv) if rt == "int_array" emit(" sp_IntArray_sort_bang(" + rc + ");") return return 1 end end end 0 end def compile_block_iteration_stmt(nid, mname, recv) # each with block if mname == "each" if @nd_block[nid] >= 0 # Fall through to yield method handler below else compile_each_block(nid) return return 1 end else compile_each_block(nid) return return 1 end end end if mname == "times" if @nd_block[nid] >= 0 compile_times_block(nid) return return 1 end end if mname == "upto" if @nd_block[nid] >= 0 compile_upto_block(nid) return return 1 end end if mname == "downto" if @nd_block[nid] >= 0 compile_downto_block(nid) return return 1 end end if mname == "reduce" if @nd_block[nid] >= 0 compile_reduce_block(nid) return return 1 end end if mname == "inject" if @nd_block[nid] >= 0 compile_reduce_block(nid) return return 1 end end if mname == "reject" if @nd_block[nid] >= 0 compile_reject_block(nid) return return 1 end end @indent = @indent - 1 emit(" }") @in_loop = old return return 1 end end 0 end def compile_control_call_stmt(nid, mname, recv) # raise if mname == "raise" if recv < 0 else emit(" sp_raise(\"RuntimeError\");") end return return 1 end end @needs_file_io = 1 emit(" fflush(stdout);") emit(" sp_last_status = system(" + compile_arg0(nid) + ");") return return 1 end end # trap (just ignore) if mname == "trap" if recv < 0 return return 1 end end if recv < 0 if @nd_block[nid] >= 0 compile_catch_stmt(nid) return return 1 end end end if mname == "throw" if recv < 0 compile_throw_stmt(nid) return return 1 end end a1 = compile_expr(arg_ids[1]) end emit(" sp_file_write(" + a0 + ", " + a1 + ");") return return 1 end if mname == "delete" @needs_file_io = 1 emit(" sp_file_delete(" + compile_arg0(nid) + ");") return return 1 end end end if recv < 0 val = compile_arg0(nid) emit(" exit(" + val + ");") return return 1 end end 0 end def compile_writer_and_block_call_stmt(nid, mname, recv) # attr_writer: obj.x = val if recv >= 0 if mname.length > 1 if is_obj_type(rt) == 1 rc = compile_expr(recv) emit(" " + rc + "->" + bname + " = " + compile_arg0(nid) + ";") return return 1 end end end if mname == "map" if @nd_block[nid] >= 0 compile_map_block(nid) return return 1 end end if mname == "select" if @nd_block[nid] >= 0 compile_select_block(nid) return return 1 end end if mi >= 0 if @meth_has_yield[mi] == 1 compile_yield_call_stmt(nid, mi) return return 1 end end end if midx >= 0 if cls_method_has_yield(cci, midx) == 1 compile_yield_method_call_stmt(nid, cci, midx, mname) return return 1 end end # Check parent if pidx >= 0 if cls_method_has_yield(pci, pidx) == 1 compile_yield_method_call_stmt(nid, pci, pidx, mname) return return 1 end end end end end end # General val = compile_expr(nid) if val != "0" emit(" " + val + ";") end 0 end def compile_file_block_stmt(nid, ftmp, bp) if nid < 0 return