Implement printf, dprintf, string.format

This commit is contained in:
2026-02-17 19:57:58 -05:00
parent a987754b38
commit ff81d79b4a
7 changed files with 363 additions and 121 deletions

View File

@@ -857,57 +857,6 @@ LuaDefine(math_randomstate, "seed",
LuaSandboxBuiltin(math_randomseed, "", "");
LuaDefine(pprint, "obj1, obj2, ...",
"|Pretty-print the specified objects."
"|"
"|See also: pprintx, which has a lot more options."
"|This function uses the default options: pretty print indented,"
"|start at indentation level zero, and always expand the"
"|top-level table."
"|") {
World *w = World::fetch_global_pointer(L);
std::ostream *ostream = w->lthread_print_stream();
LuaExtraArgs extra;
LuaDefStack LS(L, extra);
for (int i = 0; i < extra.size(); i++) {
pprint(LS, extra[i], PrettyPrintOptions(), ostream);
(*ostream) << std::endl;
}
return LS.result();
}
LuaDefine(pprintx, "options",
"|Pretty-print the specified object, with options"
"|"
"|Options is a table with these fields:"
"|"
"| value - the object to pretty-print"
"| indent - if false, suppress newlines and indentation (default: true)"
"| level - base level of indentation (default: zero)"
"| expand - if true, force expansion of top-level table (default: true)"
"|"
"|About the expand flag: normally, when you print a class, it just "
"|prints '<class name>', and when you print a tangible, it just"
"|prints '<tangible id>'. But sometimes, you want to see the details."
"|The expand flag forces it to expand the top-level table, even if the"
"|top-level table is a tangible or class."
"|") {
World *w = World::fetch_global_pointer(L);
std::ostream *ostream = w->lthread_print_stream();
LuaArg loptions;
LuaVar value;
LuaDefStack LS(L, loptions, value);
PrettyPrintOptions options;
LuaKeywordParser kp(LS, loptions);
options.parse(kp);
if (!kp.optional(value, "value")) {
LS.set(value, LuaNil);
}
kp.final_check_throw();
pprint(LS, value, options, ostream);
return LS.result();
}
LuaDefine(print, "obj1, obj2, ...",
"|Print object or objects.") {
World *w = World::fetch_global_pointer(L);
@@ -938,6 +887,88 @@ LuaDefine(dprint, "obj1, obj2, ...",
return 0;
}
#define PRINTF_DOCS \
"|" \
"|Arguments: format, arg1, arg2, arg3..." \
"|" \
"|Numeric types (argument must be a number):" \
"| %d, %i -- signed decimal integer" \
"| %o -- unsigned octal" \
"| %u -- unsigned decimal" \
"| %x, %X -- unsigned hexadecimal (lower/upper)" \
"| %e, %E -- scientific notation (lower/upper)" \
"| %f -- decimal floating point" \
"| %g, %G -- shortest of %e/%f (lower/upper)" \
"| %c -- character (integer converted to character)" \
"|" \
"|Print any lua value:" \
"| %s -- Print unquoted" \
"| %q -- Print quoted" \
"|" \
"|Pretty-print any lua value, showing contents of tables:" \
"| %p -- pretty print" \
"| %lp -- pretty print, all on one line" \
"| %P -- pretty print, force table expansion" \
"| %lP -- pretty print, force table expansion, all on one line" \
"|" \
"|Other:" \
"| %% -- literal percent sign" \
"|"
LuaDefine(printf, "fmt, ...",
"|Print a formatted string to the console."
"|"
PRINTF_DOCS) {
World *w = World::fetch_global_pointer(L);
std::ostream *ostream = w->lthread_print_stream();
LuaArg lfmt;
LuaExtraArgs extra;
LuaDefStack LS(L, lfmt, extra);
eng::string fmtstr = LS.ckstring(lfmt, "format string");
eng::string err = format(LS, fmtstr, extra, ostream);
if (!err.empty()) {
luaL_error(L, "%s", err.c_str());
}
(*ostream) << std::endl;
return 0;
}
LuaDefine(dprintf, "fmt, ...",
"|Print a formatted string to the debug log."
"|"
PRINTF_DOCS) {
LuaArg lfmt;
LuaExtraArgs extra;
LuaDefStack LS(L, lfmt, extra);
eng::string fmtstr = LS.ckstring(lfmt, "format string");
std::ostringstream oss;
eng::string err = format(LS, fmtstr, extra, &oss);
if (!err.empty()) {
luaL_error(L, "%s", err.c_str());
}
oss << std::endl;
util::dprintview(oss.str());
return 0;
}
LuaDefine(string_format, "fmt, ...",
"|Format a string using printf-style format specifiers."
"|"
PRINTF_DOCS) {
LuaArg lfmt;
LuaRet result;
LuaExtraArgs extra;
LuaDefStack LS(L, lfmt, result, extra);
eng::string fmtstr = LS.ckstring(lfmt, "format string");
util::ostringstream oss;
eng::string err = format(LS, fmtstr, extra, &oss);
if (!err.empty()) {
luaL_error(L, "%s", err.c_str());
}
LS.set(result, oss.view());
return LS.result();
}
LuaDefine(doc, "function",
"|Print documentation for specified function.") {
World *w = World::fetch_global_pointer(L);