Commit 667be996
667be99671bfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #89 from mame/feat/string-format
feat(codegen): support single-value "fmt" % value String#%
Details
- Commit
- 667be99671bf
- Parent
- 2c82182b70bb
- Merge parent
- b3836955a63f
spinel_codegen.rbmodifiedView file
@@ -1771,17 +1771,23 @@
17711771
return "int"17721772
end17731773
if mname == "%"1774-
# String#% returns "string" only when the RHS is a str_array1775-
# (sprintf-style format). Other RHS types fall through to sp_imod1776-
# in compile_operator_expr, which yields int.1774+
# String#% returns "string" when the LHS is a string (and the RHS1775+
# is a str_array or a single primitive value). Otherwise the1776+
# operator is integer modulo.17771777
if recv >= 017781778
rt = infer_type(recv)17791779
if rt == "string" || rt == "mutable_str"17801780
args_id = @nd_arguments[nid]17811781
if args_id >= 017821782
aargs = get_args(args_id)1783-
if aargs.length > 0 && infer_type(aargs[0]) == "str_array"1784-
return "string"1783+
if aargs.length > 01784+
at = infer_type(aargs[0])1785+
if at == "str_array"1786+
return "string"1787+
end1788+
if rt == "string"1789+
return "string"1790+
end17851791
end17861792
end17871793
end@@ -13940,6 +13946,62 @@
1394013946
end1394113947
end1394213948
end13949+
if lt == "string"13950+
# Ruby's `"fmt" % val` — single-value form. Arg is cast to match13951+
# the conversion: (double) for %f, (long long) for integer13952+
# conversions. Ruby's `%d` is mapped to C's `%lld` when the13953+
# format is a string literal — otherwise pass through.13954+
arg0 = get_args(@nd_arguments[nid])[0]13955+
at = infer_type(arg0)13956+
fmt_c = compile_expr(recv)13957+
# Literal-format optimization: rewrite %d → %lld at compile time13958+
# (done byte-by-byte to avoid pulling in the regex engine — the13959+
# self-hosted bootstrap step links without libspinel_rt.a).13960+
if @nd_type[recv] == "StringNode"13961+
lit = @nd_unescaped[recv]13962+
if lit == ""13963+
lit = @nd_content[recv]13964+
end13965+
rewritten = ""13966+
fi = 013967+
while fi < lit.length13968+
if lit[fi] == "%"13969+
# Copy "%" + any flags/width + final spec.13970+
rewritten = rewritten + "%"13971+
fi = fi + 113972+
while fi < lit.length13973+
c = lit[fi]13974+
if c == "-" || c == "+" || c == " " || c == "#" || c == "0" || (c >= "1" && c <= "9") || c == "."13975+
rewritten = rewritten + c13976+
fi = fi + 113977+
else13978+
break13979+
end13980+
end13981+
if fi < lit.length13982+
c = lit[fi]13983+
if c == "d" || c == "i"13984+
rewritten = rewritten + "lld"13985+
else13986+
rewritten = rewritten + c13987+
end13988+
fi = fi + 113989+
end13990+
else13991+
rewritten = rewritten + lit[fi]13992+
fi = fi + 113993+
end13994+
end13995+
fmt_c = c_string_literal(rewritten)13996+
end13997+
if at == "float"13998+
return "sp_sprintf(" + fmt_c + ", (double)" + compile_expr(arg0) + ")"13999+
elsif at == "string"14000+
return "sp_sprintf(" + fmt_c + ", " + compile_expr(arg0) + ")"14001+
else14002+
return "sp_sprintf(" + fmt_c + ", (long long)" + compile_expr(arg0) + ")"14003+
end14004+
end1394314005
return "sp_imod(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"1394414006
end1394514007
if mname == "<"test/string_format_single.rbaddedView file
Loading diff…