Commit e5e4e991

e5e4e991d525Merged

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

Description

fix: support << operator on arrays and improve push-based type inference

  • Add << as alias for push in scan_locals array type detection, so arr = []; arr << "hello" correctly upgrades int_array to str_array
  • Add array << codegen in compile_mutating_call_stmt for int/str/float and ptr arrays, generating sp_*Array_push calls instead of C bitshift

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

Details

Commit
e5e4e991d525
Parent
b634f4416e86

spinel_codegen.rbmodifiedView file

@@ -8958,9 +8958,9 @@
89588958 end
89598959 end
89608960 end
8961- # Detect object array push: arr.push(ClassName.new(...))
8961+ # Detect array element type from push/<<: arr.push(x) or arr << x
89628962 if @nd_type[nid] == "CallNode"
8963- if @nd_name[nid] == "push"
8963+ if @nd_name[nid] == "push" || @nd_name[nid] == "<<"
89648964 recv = @nd_receiver[nid]
89658965 if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"
89668966 arr_name = @nd_name[recv]
@@ -14143,6 +14143,33 @@
1414314143 end
1414414144 end
1414514145
14146+ # << on array (same as push)
14147+ if mname == "<<"
14148+ if recv >= 0
14149+ rt = infer_type(recv)
14150+ if rt == "int_array"
14151+ rc = compile_expr(recv)
14152+ emit(" sp_IntArray_push(" + rc + ", " + compile_arg0(nid) + ");")
14153+ return 1
14154+ end
14155+ if rt == "str_array"
14156+ rc = compile_expr(recv)
14157+ emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");")
14158+ return 1
14159+ end
14160+ if rt == "float_array"
14161+ rc = compile_expr(recv)
14162+ emit(" sp_FloatArray_push(" + rc + ", " + compile_arg0(nid) + ");")
14163+ return 1
14164+ end
14165+ if is_ptr_array_type(rt) == 1
14166+ rc = compile_expr(recv)
14167+ emit(" sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + ");")
14168+ return 1
14169+ end
14170+ end
14171+ end
14172+
1414614173 # replace on string (mutating reassign)
1414714174 if mname == "replace"
1414814175 if recv >= 0