Commit 92871968
92871968d6daMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: pass bm_strings3, bm_array_lit, bm_fileops - string/array methods
- Add ljust/rjust with pad character (ljust2, rjust2)
- Add lstrip, rstrip, dup for strings
- Add string[] with range argument (s[1..3]) and two-arg form (s[0, 2])
- Add String#to_s (identity), #to_sym, #ord
- Add StrArray join, include? methods
- Add IntArray uniq, join methods
- Add File.join, File.basename
- Add srand/rand, raise in expressions, format/sprintf
- Add ARGV support
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 92871968d6da
- Parent
- 006d78a54a4a
spinel_codegen.rbmodifiedView file
@@ -1057,6 +1057,18 @@
10571057
if mname == "to_sym"10581058
return "string"10591059
end1060+
if mname == "lstrip"1061+
return "string"1062+
end1063+
if mname == "rstrip"1064+
return "string"1065+
end1066+
if mname == "dup"1067+
if recv >= 01068+
return infer_type(recv)1069+
end1070+
return "string"1071+
end10601072
if mname == "ord"10611073
return "int"10621074
end@@ -4120,6 +4132,11 @@
41204132
emit_raw("static const char*sp_str_ljust(const char*s,mrb_int w){size_t l=strlen(s);if((mrb_int)l>=w)return s;char*r=(char*)malloc(w+1);memcpy(r,s,l);memset(r+l,' ',w-l);r[w]=0;return r;}")41214133
emit_raw("static const char*sp_str_rjust(const char*s,mrb_int w){size_t l=strlen(s);if((mrb_int)l>=w)return s;char*r=(char*)malloc(w+1);memset(r,' ',w-l);memcpy(r+w-l,s,l+1);return r;}")41224134
emit_raw("static const char*sp_str_center(const char*s,mrb_int w){size_t l=strlen(s);if((mrb_int)l>=w)return s;mrb_int pad=w-l;mrb_int left=pad/2;mrb_int right=pad-left;char*r=(char*)malloc(w+1);memset(r,' ',left);memcpy(r+left,s,l);memset(r+left+l,' ',right);r[w]=0;return r;}")4135+
emit_raw("static const char*sp_str_ljust2(const char*s,mrb_int w,const char*pad){size_t l=strlen(s);if((mrb_int)l>=w)return s;char*r=(char*)malloc(w+1);memcpy(r,s,l);char pc=pad[0];for(mrb_int i=l;i<w;i++)r[i]=pc;r[w]=0;return r;}")4136+
emit_raw("static const char*sp_str_rjust2(const char*s,mrb_int w,const char*pad){size_t l=strlen(s);if((mrb_int)l>=w)return s;char*r=(char*)malloc(w+1);char pc=pad[0];for(mrb_int i=0;i<w-(mrb_int)l;i++)r[i]=pc;memcpy(r+w-l,s,l+1);return r;}")4137+
emit_raw("static const char*sp_str_lstrip(const char*s){while(*s&&isspace((unsigned char)*s))s++;char*r=(char*)malloc(strlen(s)+1);strcpy(r,s);return r;}")4138+
emit_raw("static const char*sp_str_rstrip(const char*s){size_t l=strlen(s);while(l>0&&isspace((unsigned char)s[l-1]))l--;char*r=(char*)malloc(l+1);memcpy(r,s,l);r[l]=0;return r;}")4139+
emit_raw("static const char*sp_str_dup(const char*s){char*r=(char*)malloc(strlen(s)+1);strcpy(r,s);return r;}")41234140
emit_raw("")41244141
end41254142
@@ -6035,6 +6052,22 @@
60356052
return "sp_str_index(" + rc + ", " + compile_arg0(nid) + ")"60366053
end60376054
if mname == "[]"6055+
args_id = @nd_arguments[nid]6056+
if args_id >= 06057+
a = get_args(args_id)6058+
if a.length >= 16059+
if @nd_type[a[0]] == "RangeNode"6060+
# s[1..3]6061+
left = compile_expr(@nd_left[a[0]])6062+
right = compile_expr(@nd_right[a[0]])6063+
return "sp_str_sub_range(" + rc + ", " + left + ", " + right + " - " + left + " + 1)"6064+
end6065+
if a.length >= 26066+
# s[0, 2]6067+
return "sp_str_sub_range(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"6068+
end6069+
end6070+
end60386071
return "sp_str_sub_range(" + rc + ", " + compile_arg0(nid) + ", 1)"60396072
end60406073
if mname == "reverse"@@ -6123,14 +6156,40 @@
61236156
return "sp_str_sub_range(" + rc + ", " + compile_arg0(nid) + ", 1)"61246157
end61256158
if mname == "ljust"6159+
args_id = @nd_arguments[nid]6160+
if args_id >= 06161+
a = get_args(args_id)6162+
if a.length >= 26163+
return "sp_str_ljust2(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"6164+
end6165+
end61266166
return "sp_str_ljust(" + rc + ", " + compile_arg0(nid) + ")"61276167
end61286168
if mname == "rjust"6169+
args_id = @nd_arguments[nid]6170+
if args_id >= 06171+
a = get_args(args_id)6172+
if a.length >= 26173+
return "sp_str_rjust2(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"6174+
end6175+
end61296176
return "sp_str_rjust(" + rc + ", " + compile_arg0(nid) + ")"61306177
end61316178
if mname == "center"61326179
return "sp_str_center(" + rc + ", " + compile_arg0(nid) + ")"61336180
end6181+
if mname == "lstrip"6182+
return "sp_str_lstrip(" + rc + ")"6183+
end6184+
if mname == "rstrip"6185+
return "sp_str_rstrip(" + rc + ")"6186+
end6187+
if mname == "dup"6188+
return "sp_str_dup(" + rc + ")"6189+
end6190+
if mname == "to_s"6191+
return rc6192+
end61346193
end61356194
61366195
# Range methods