Improvements to the pretty-printer

This commit is contained in:
2021-11-26 12:28:59 -05:00
parent 63bca62edd
commit c02109699e
7 changed files with 75 additions and 52 deletions

View File

@@ -37,9 +37,16 @@ static bool string_quote(LuaStack &LS, LuaSlot val, std::ostream *os) {
case LUA_TSTRING:
util::quote_string(LS.ckstring(val), os);
return true;
case LUA_TNUMBER:
(*os) << LS.ckinteger(val);
case LUA_TNUMBER: {
double value = LS.cknumber(val);
int64_t ivalue = int64_t(value);
if (double(ivalue) == value) {
(*os) << ivalue;
} else {
(*os) << value;
}
return true;
}
case LUA_TBOOLEAN:
(*os) << (LS.ckboolean(val) ? "true" : "false");
return true;
@@ -140,29 +147,48 @@ static void pprint_r(Inspector &insp, int level, LuaSlot root) {
return;
}
// If the table ID is greater than zero, then we've already
// printed it. Just print the table ID.
// Determine the table's ID, allocating an ID if necessary.
LS.rawget(idv, insp.ids, root);
int id = LS.ckint(idv);
if (id > 0) {
if (lua_nkeys(insp.L, root.index())==0) {
(*insp.stream) << "<table " << id << ">{}";
} else {
(*insp.stream) << "<table " << id << ">{...}";
bool new_id = false;
if (id < 0) {
new_id = true;
id = insp.nextid++;
LS.rawset(insp.ids, root, id);
}
// Print the table's name, if any.
bool is_class = false;
bool is_tangible = false;
std::string cname = LS.classname(root);
if (cname != "") {
is_class = true;
(*insp.stream) << "<class " << cname << ">";
} else {
int64_t tid = LS.tanid(root);
if (tid > 0) {
is_tangible = true;
(*insp.stream) << "<tangible " << tid << ">";
} else if (id > 0) {
(*insp.stream) << "<table " << id << ">";
}
}
// If this is a class, and we're not at the top level, truncate.
if ((is_class || is_tangible) && (level > 0)) {
LS.result();
return;
}
// Allocate an ID for the table, if necessary.
if (id < 0) {
id = insp.nextid++;
LS.rawset(insp.ids, root, id);
}
// Print the table ID if greater than zero.
if (id > 0) {
(*insp.stream) << "<table " << id << ">";
// If this is a table we've already printed, truncate it.
if ((id > 0) && (!new_id)) {
if (lua_nkeys(insp.L, root.index())==0) {
(*insp.stream) << "{}";
} else {
(*insp.stream) << "{...}";
}
LS.result();
return;
}
// State variables.
@@ -212,12 +238,6 @@ static void pprint_r(Inspector &insp, int level, LuaSlot root) {
needcomma = true;
tabify(insp, level + 1);
(*insp.stream) << "<meta> = ";
// if isclass(mt) then
// self:puts('<class ')
// self:puts(rawget(mt, "__class"))
// self:puts('>')
// end
pprint_r(insp, level + 1, val);
}