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 end
17731773 if mname == "%"
1774- # String#% returns "string" only when the RHS is a str_array
1775- # (sprintf-style format). Other RHS types fall through to sp_imod
1776- # in compile_operator_expr, which yields int.
1774+ # String#% returns "string" when the LHS is a string (and the RHS
1775+ # is a str_array or a single primitive value). Otherwise the
1776+ # operator is integer modulo.
17771777 if recv >= 0
17781778 rt = infer_type(recv)
17791779 if rt == "string" || rt == "mutable_str"
17801780 args_id = @nd_arguments[nid]
17811781 if args_id >= 0
17821782 aargs = get_args(args_id)
1783- if aargs.length > 0 && infer_type(aargs[0]) == "str_array"
1784- return "string"
1783+ if aargs.length > 0
1784+ at = infer_type(aargs[0])
1785+ if at == "str_array"
1786+ return "string"
1787+ end
1788+ if rt == "string"
1789+ return "string"
1790+ end
17851791 end
17861792 end
17871793 end
@@ -13940,6 +13946,62 @@
1394013946 end
1394113947 end
1394213948 end
13949+ if lt == "string"
13950+ # Ruby's `"fmt" % val` — single-value form. Arg is cast to match
13951+ # the conversion: (double) for %f, (long long) for integer
13952+ # conversions. Ruby's `%d` is mapped to C's `%lld` when the
13953+ # 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 time
13958+ # (done byte-by-byte to avoid pulling in the regex engine — the
13959+ # 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+ end
13965+ rewritten = ""
13966+ fi = 0
13967+ while fi < lit.length
13968+ if lit[fi] == "%"
13969+ # Copy "%" + any flags/width + final spec.
13970+ rewritten = rewritten + "%"
13971+ fi = fi + 1
13972+ while fi < lit.length
13973+ c = lit[fi]
13974+ if c == "-" || c == "+" || c == " " || c == "#" || c == "0" || (c >= "1" && c <= "9") || c == "."
13975+ rewritten = rewritten + c
13976+ fi = fi + 1
13977+ else
13978+ break
13979+ end
13980+ end
13981+ if fi < lit.length
13982+ c = lit[fi]
13983+ if c == "d" || c == "i"
13984+ rewritten = rewritten + "lld"
13985+ else
13986+ rewritten = rewritten + c
13987+ end
13988+ fi = fi + 1
13989+ end
13990+ else
13991+ rewritten = rewritten + lit[fi]
13992+ fi = fi + 1
13993+ end
13994+ end
13995+ fmt_c = c_string_literal(rewritten)
13996+ end
13997+ 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+ else
14002+ return "sp_sprintf(" + fmt_c + ", (long long)" + compile_expr(arg0) + ")"
14003+ end
14004+ end
1394314005 return "sp_imod(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
1394414006 end
1394514007 if mname == "<"