Implement C++ pretty-printer
This commit is contained in:
@@ -1,50 +1,6 @@
|
||||
#include "table.hpp"
|
||||
#include "source.hpp"
|
||||
|
||||
void table_findtables_i(LuaStack &LS0, LuaSlot root, LuaSlot tabcount) {
|
||||
lua_State *L = LS0.state();
|
||||
LuaVar key, val, tab, count;
|
||||
LuaStack LS(L, key, val, tab, count);
|
||||
|
||||
LS.newtable(tabcount);
|
||||
int top = lua_gettop(L);
|
||||
if (LS.istable(root)) {
|
||||
lua_pushvalue(L, root.index());
|
||||
}
|
||||
while (lua_gettop(L) > top) {
|
||||
lua_checkstack(L, 10);
|
||||
lua_replace(L, tab.index());
|
||||
LS.rawget(count, tabcount, tab);
|
||||
if (LS.isnil(count)) {
|
||||
LS.rawset(tabcount, tab, 1);
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(tab, key, val)) {
|
||||
if (LS.istable(key)) {
|
||||
lua_pushvalue(L, key.index());
|
||||
}
|
||||
if (LS.istable(val)) {
|
||||
lua_pushvalue(L, val.index());
|
||||
}
|
||||
}
|
||||
LS.getmetatable(val, tab);
|
||||
if (LS.istable(val)) {
|
||||
lua_pushvalue(L, val.index());
|
||||
}
|
||||
} else {
|
||||
LS.rawset(tabcount, tab, LS.ckint(count) + 1);
|
||||
}
|
||||
}
|
||||
LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(table_findtables, "c") {
|
||||
LuaArg root;
|
||||
LuaRet tabcount;
|
||||
LuaStack LS(L, root, tabcount);
|
||||
table_findtables_i(LS, root, tabcount);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(table_getregistry, "f") {
|
||||
LuaArg key;
|
||||
LuaRet result;
|
||||
@@ -575,28 +531,13 @@ static void auxsort (lua_State *L, int tab, int l, int u) {
|
||||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
static int table_getpairs(lua_State *L, bool sort, bool *unsortable) {
|
||||
lua_checkstack(L, 40);
|
||||
LuaArg tab;
|
||||
bool table_getpairs(LuaStack &LS0, LuaSlot tab, LuaSlot pairs, bool sort) {
|
||||
lua_State *L = LS0.state();
|
||||
LuaVar key, value;
|
||||
LuaRet pairs;
|
||||
LuaStack LS(L, tab, key, value, pairs);
|
||||
if (unsortable != nullptr) {
|
||||
*unsortable = false;
|
||||
}
|
||||
LS.checktable(tab);
|
||||
// Count the total number of pairs.
|
||||
// TODO: add lua_npairs to make this faster.
|
||||
lua_pushnil(L);
|
||||
int total = 0;
|
||||
while (lua_next(L, tab.index()) != 0) {
|
||||
int ktype = lua_type(L, -2);
|
||||
if (ktype == LUA_TNUMBER || ktype == LUA_TSTRING || ktype == LUA_TBOOLEAN) {
|
||||
total += 1;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
LuaStack LS(L, key, value);
|
||||
bool sorted = true;
|
||||
// Create the table, store the initial 1.
|
||||
int total = lua_nkeys(L, tab.index());
|
||||
LS.createtable(pairs, total * 2 + 1, 0);
|
||||
LS.rawseti(pairs, 1, 1);
|
||||
// Transfer the pairs into the sequence.
|
||||
@@ -604,21 +545,18 @@ static int table_getpairs(lua_State *L, bool sort, bool *unsortable) {
|
||||
int offset = 2;
|
||||
while (lua_next(L, tab.index()) != 0) {
|
||||
int ktype = lua_type(L, -2);
|
||||
if (ktype == LUA_TNUMBER || ktype == LUA_TSTRING || ktype == LUA_TBOOLEAN) {
|
||||
lua_pushvalue(L, -2); // K V K
|
||||
lua_rawseti(L, pairs.index(), offset++);
|
||||
lua_rawseti(L, pairs.index(), offset++);
|
||||
} else {
|
||||
if (unsortable != nullptr) {
|
||||
*unsortable = true;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
if (ktype != LUA_TNUMBER && ktype != LUA_TSTRING && ktype != LUA_TBOOLEAN) {
|
||||
sorted = false;
|
||||
}
|
||||
lua_pushvalue(L, -2); // K V K
|
||||
lua_rawseti(L, pairs.index(), offset++);
|
||||
lua_rawseti(L, pairs.index(), offset++);
|
||||
}
|
||||
if (sort) {
|
||||
auxsort(L, pairs.index(), 1, total);
|
||||
}
|
||||
return LS.result();
|
||||
LS.result();
|
||||
return sorted;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
@@ -652,13 +590,20 @@ LuaDefine(table_sortedpairs, "c") {
|
||||
LuaArg tab;
|
||||
LuaRet closure, rtab, key;
|
||||
LuaStack LS(L, tab, closure, rtab, key);
|
||||
lua_pushvalue(L, tab.index());
|
||||
bool unsortable;
|
||||
table_getpairs(L, true, &unsortable);
|
||||
if (unsortable) {
|
||||
bool sorted = table_getpairs(LS, tab, rtab, true);
|
||||
if (!sorted) {
|
||||
luaL_error(L, "Cannot iterate over a table with unsortable keys");
|
||||
}
|
||||
lua_replace(L, rtab.index());
|
||||
LS.set(closure, lfn_table_nextsortedpair);
|
||||
LS.set(key, LuaNil);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(table_semisortedpairs, "c") {
|
||||
LuaArg tab;
|
||||
LuaRet closure, rtab, key;
|
||||
LuaStack LS(L, tab, closure, rtab, key);
|
||||
table_getpairs(LS, tab, rtab, true);
|
||||
LS.set(closure, lfn_table_nextsortedpair);
|
||||
LS.set(key, LuaNil);
|
||||
return LS.result();
|
||||
|
||||
Reference in New Issue
Block a user