Commit a03224dd

a03224dd99b3Merged

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

Description

fix: partial fixes for struct_kw, strings3, array_lit, fileops

  • Filter keyword_init: true from Struct.new field list
  • Add compile_constructor_args with keyword matching for .new calls
  • Add keyword handling in scan_new_calls for .new constructor calls
  • Add synthetic struct ivar type update from constructor params
  • Add string ljust2/rjust2 with pad char, lstrip, rstrip, dup
  • Add string[] with range, string[] with offset+length
  • Add StrArray join, include? methods

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

Details

Commit
a03224dd99b3
Parent
92871968d6da

spinel_codegen.rbmodifiedView file

@@ -2382,21 +2382,28 @@
23822382 @cls_meth_has_yield.push("")
23832383 @needs_gc = 1
23842384
2385- # Get field names from symbol args
2385+ # Get field names from symbol args (skip keyword_init hash)
23862386 args_id = @nd_arguments[call_nid]
23872387 field_names = "".split(",")
23882388 if args_id >= 0
23892389 aids = get_args(args_id)
23902390 k = 0
23912391 while k < aids.length
2392+ # Skip KeywordHashNode (keyword_init: true)
2393+ if @nd_type[aids[k]] == "KeywordHashNode"
2394+ k = k + 1
2395+ next
2396+ end
23922397 fname = @nd_content[aids[k]]
2393- field_names.push(fname)
2394- # Add ivar
2395- iname = "@" + fname
2396- add_ivar(ci, iname, "int")
2397- # Add reader/writer
2398- append_attr_reader(ci, fname)
2399- append_attr_writer(ci, fname)
2398+ if fname != ""
2399+ field_names.push(fname)
2400+ # Add ivar
2401+ iname = "@" + fname
2402+ add_ivar(ci, iname, "int")
2403+ # Add reader/writer
2404+ append_attr_reader(ci, fname)
2405+ append_attr_writer(ci, fname)
2406+ end
24002407 k = k + 1
24012408 end
24022409 end
@@ -2611,15 +2618,52 @@
26112618 if args_id >= 0
26122619 arg_ids = get_args(args_id)
26132620 all_ptypes = @cls_meth_ptypes[init_ci].split("|")
2621+ all_params = @cls_meth_params[init_ci].split("|")
26142622 if init_idx < all_ptypes.length
26152623 ptypes = all_ptypes[init_idx].split(",")
2624+ pnames = []
2625+ if init_idx < all_params.length
2626+ pnames = all_params[init_idx].split(",")
2627+ end
26162628 k = 0
26172629 while k < arg_ids.length
2618- at = infer_type(arg_ids[k])
2619- if k < ptypes.length
2620- if ptypes[k] == "int"
2621- if at != "int"
2622- ptypes[k] = at
2630+ if @nd_type[arg_ids[k]] == "KeywordHashNode"
2631+ # Handle keyword args
2632+ elems = parse_id_list(@nd_elements[arg_ids[k]])
2633+ ek = 0
2634+ while ek < elems.length
2635+ if @nd_type[elems[ek]] == "AssocNode"
2636+ key_id = @nd_key[elems[ek]]
2637+ if key_id >= 0
2638+ kname = ""
2639+ if @nd_type[key_id] == "SymbolNode"
2640+ kname = @nd_content[key_id]
2641+ end
2642+ at = infer_type(@nd_expression[elems[ek]])
2643+ pi = 0
2644+ while pi < pnames.length
2645+ if pnames[pi] == kname
2646+ if pi < ptypes.length
2647+ if ptypes[pi] == "int"
2648+ if at != "int"
2649+ ptypes[pi] = at
2650+ end
2651+ end
2652+ end
2653+ end
2654+ pi = pi + 1
2655+ end
2656+ end
2657+ end
2658+ ek = ek + 1
2659+ end
2660+ else
2661+ at = infer_type(arg_ids[k])
2662+ if k < ptypes.length
2663+ if ptypes[k] == "int"
2664+ if at != "int"
2665+ ptypes[k] = at
2666+ end
26232667 end
26242668 end
26252669 end
@@ -2729,6 +2773,40 @@
27292773 end
27302774
27312775 def update_ivar_types_from_params
2776+ # Special case: synthetic struct constructors - ivars match params directly
2777+ i = 0
2778+ while i < @cls_names.length
2779+ init_idx2 = cls_find_method_direct(i, "initialize")
2780+ if init_idx2 >= 0
2781+ bodies = @cls_meth_bodies[i].split(";")
2782+ if init_idx2 < bodies.length
2783+ if bodies[init_idx2].to_i == -2
2784+ # Synthetic struct - update ivar types from init param types
2785+ all_params = @cls_meth_params[i].split("|")
2786+ all_ptypes = @cls_meth_ptypes[i].split("|")
2787+ pnames = []
2788+ ptypes = []
2789+ if init_idx2 < all_params.length
2790+ pnames = all_params[init_idx2].split(",")
2791+ end
2792+ if init_idx2 < all_ptypes.length
2793+ ptypes = all_ptypes[init_idx2].split(",")
2794+ end
2795+ pk = 0
2796+ while pk < pnames.length
2797+ iname = "@" + pnames[pk]
2798+ if pk < ptypes.length
2799+ if ptypes[pk] != "int"
2800+ update_ivar_type(i, iname, ptypes[pk])
2801+ end
2802+ end
2803+ pk = pk + 1
2804+ end
2805+ end
2806+ end
2807+ end
2808+ i = i + 1
2809+ end
27322810 # For each class method, if it assigns @ivar = param, update ivar type from param type
27332811 i = 0
27342812 while i < @cls_names.length
@@ -5992,7 +6070,7 @@
59926070 end
59936071 ci = find_class_idx(cname)
59946072 if ci >= 0
5995- return "sp_" + cname + "_new(" + compile_call_args(nid) + ")"
6073+ return "sp_" + cname + "_new(" + compile_constructor_args(ci, nid) + ")"
59966074 end
59976075 end
59986076 end
@@ -6889,6 +6967,87 @@
68896967 result
68906968 end
68916969
6970+ def compile_constructor_args(ci, nid)
6971+ args_id = @nd_arguments[nid]
6972+ if args_id < 0
6973+ return ""
6974+ end
6975+ arg_ids = get_args(args_id)
6976+ # Check if any arg is a KeywordHashNode
6977+ has_kw = 0
6978+ ak = 0
6979+ while ak < arg_ids.length
6980+ if @nd_type[arg_ids[ak]] == "KeywordHashNode"
6981+ has_kw = 1
6982+ end
6983+ ak = ak + 1
6984+ end
6985+ if has_kw == 0
6986+ return compile_call_args(nid)
6987+ end
6988+ # Extract keyword pairs
6989+ kw_names = "".split(",")
6990+ kw_vals = "".split(",")
6991+ ak = 0
6992+ while ak < arg_ids.length
6993+ if @nd_type[arg_ids[ak]] == "KeywordHashNode"
6994+ elems = parse_id_list(@nd_elements[arg_ids[ak]])
6995+ ek = 0
6996+ while ek < elems.length
6997+ if @nd_type[elems[ek]] == "AssocNode"
6998+ key_id = @nd_key[elems[ek]]
6999+ if key_id >= 0
7000+ kname = ""
7001+ if @nd_type[key_id] == "SymbolNode"
7002+ kname = @nd_content[key_id]
7003+ end
7004+ kw_names.push(kname)
7005+ kw_vals.push(compile_expr(@nd_expression[elems[ek]]))
7006+ end
7007+ end
7008+ ek = ek + 1
7009+ end
7010+ end
7011+ ak = ak + 1
7012+ end
7013+ # Get init param names from class
7014+ init_ci = find_init_class(ci)
7015+ if init_ci < 0
7016+ return compile_call_args(nid)
7017+ end
7018+ init_idx = cls_find_method_direct(init_ci, "initialize")
7019+ if init_idx < 0
7020+ return compile_call_args(nid)
7021+ end
7022+ all_params = @cls_meth_params[init_ci].split("|")
7023+ pnames = []
7024+ if init_idx < all_params.length
7025+ pnames = all_params[init_idx].split(",")
7026+ end
7027+ # Build args in param order using keyword values
7028+ result = ""
7029+ pk = 0
7030+ while pk < pnames.length
7031+ if pk > 0
7032+ result = result + ", "
7033+ end
7034+ found = 0
7035+ ki = 0
7036+ while ki < kw_names.length
7037+ if kw_names[ki] == pnames[pk]
7038+ result = result + kw_vals[ki]
7039+ found = 1
7040+ end
7041+ ki = ki + 1
7042+ end
7043+ if found == 0
7044+ result = result + "0"
7045+ end
7046+ pk = pk + 1
7047+ end
7048+ result
7049+ end
7050+
68927051 def compile_call_args(nid)
68937052 args_id = @nd_arguments[nid]
68947053 if args_id < 0