void dumpsyms(Stab *st, int indent) { size_t i, n; void **k; /* decls */ k = htkeys(st->dcl, &n); for (i = 0; i < n; i++) { dumptypes(getdcl(st, k[i]), indent); } free(k); /* union constructors */ k = htkeys(st->uc, &n); for (i = 0; i < n; i++) dumpucon(getucon(st, k[i]), indent + 1); /* sub-namespaces */ k = htkeys(st->ns, &n); for (i = 0; i < n; i++) { printindent(indent + 1); printf("namespace %s:\n", (char*)k[i]); dumpsyms(getns_str(st, k[i]), indent + 2); } free(k); }
void dumpucon(Ucon *uc, int indent) { printindent(indent); printf("`"); if (uc->name->name.ns) printf("%s.", uc->name->name.ns); printf("%s\n", uc->name->name.name); }
void printfile(be_node *n, int indent) { int i = 0; if (n -> type == BE_INT) { printindent(indent); printf("[Integer: %lld]\n", n -> val.i); } else if (n -> type == BE_STR) { printindent(indent); printf("[String: \'%s\']\n", n -> val.s); } else if (n -> type == BE_DICT) { be_dict * d = n -> val.d; putchar('\n'); printindent(indent); printf("[Dictionary:]\n"); for (i = 0; ; ++i) { if (d[i].val) { printindent(indent+2); printf("[Key: \'%s\'], ", d[i].key); printfile(d[i].val, indent+2); } else { break; } } printindent(indent); printf("[Dictionary End]\n"); } else if (n -> type == BE_LIST) { be_node ** l = n -> val.l; putchar('\n'); printindent(indent); printf("[List:]\n"); for (i = 0; ; ++i) { if (l[i]) { printfile(l[i], indent+2); } else { break; } } printindent(indent); printf("[List End]\n"); } else { assert(0); } }
void printwords(Word **w, int nw) { int i, j, n, col, nsp; /* one output line per loop */ for(i=0; i<nw; ){ /* if it's a blank line, print it */ if(w[i]->indent == -1){ Bputc(&bout, '\n'); if(++i == nw) /* out of words */ break; } /* emit leading indent */ col = extraindent+w[i]->indent; printindent(col); /* emit words until overflow; always emit at least one word */ for(n=0;; n++){ Bprint(&bout, "%s", w[i]->text); col += utflen(w[i]->text); if(++i == nw) break; /* out of words */ if(w[i]->indent != w[i-1]->indent) break; /* indent change */ nsp = nspaceafter(w[i-1]->text); if(col+nsp+utflen(w[i]->text) > extraindent+length) break; /* fold line */ if(!join && w[i]->bol) break; for(j=0; j<nsp; j++) Bputc(&bout, ' '); /* emit space; another word will follow */ col += nsp; } /* emit newline */ Bputc(&bout, '\n'); } }
/* Top-level delegating persist function */ static void persist(PersistInfo *pi) { /* perms reftbl ... obj */ lua_checkstack(pi->L, 2); /* If the object has already been written, write a reference to it */ lua_pushvalue(pi->L, -1); /* perms reftbl ... obj obj */ lua_rawget(pi->L, 2); /* perms reftbl ... obj ref? */ if(!lua_isnil(pi->L, -1)) { /* perms reftbl ... obj ref */ int zero = 0; int ref = (int)lua_touserdata(pi->L, -1); pi->writer(pi->L, &zero, sizeof(int), pi->ud); pi->writer(pi->L, &ref, sizeof(int), pi->ud); lua_pop(pi->L, 1); /* perms reftbl ... obj ref */ #ifdef PLUTO_DEBUG printindent(pi->level); printf("0 %d\n", ref); #endif return; } /* perms reftbl ... obj nil */ lua_pop(pi->L, 1); /* perms reftbl ... obj */ /* If the object is nil, write the pseudoreference 0 */ if(lua_isnil(pi->L, -1)) { int zero = 0; /* firsttime */ pi->writer(pi->L, &zero, sizeof(int), pi->ud); /* ref */ pi->writer(pi->L, &zero, sizeof(int), pi->ud); #ifdef PLUTO_DEBUG printindent(pi->level); printf("0 0\n"); #endif return; } { /* indicate that it's the first time */ int one = 1; pi->writer(pi->L, &one, sizeof(int), pi->ud); } lua_pushvalue(pi->L, -1); /* perms reftbl ... obj obj */ lua_pushlightuserdata(pi->L, (void*)(++(pi->counter))); /* perms reftbl ... obj obj ref */ lua_rawset(pi->L, 2); /* perms reftbl ... obj */ pi->writer(pi->L, &pi->counter, sizeof(int), pi->ud); /* At this point, we'll give the permanents table a chance to play. */ { lua_pushvalue(pi->L, -1); /* perms reftbl ... obj obj */ lua_gettable(pi->L, 1); /* perms reftbl ... obj permkey? */ if(!lua_isnil(pi->L, -1)) { /* perms reftbl ... obj permkey */ int type = PLUTO_TPERMANENT; #ifdef PLUTO_DEBUG printindent(pi->level); printf("1 %d PERM\n", pi->counter); pi->level++; #endif pi->writer(pi->L, &type, sizeof(int), pi->ud); persist(pi); lua_pop(pi->L, 1); /* perms reftbl ... obj */ #ifdef PLUTO_DEBUG pi->level--; #endif return; } else { /* perms reftbl ... obj nil */ lua_pop(pi->L, 1); /* perms reftbl ... obj */ } /* perms reftbl ... obj */ } { int type = lua_type(pi->L, -1); pi->writer(pi->L, &type, sizeof(int), pi->ud); #ifdef PLUTO_DEBUG printindent(pi->level); printf("1 %d %d\n", pi->counter, type); pi->level++; #endif } switch(lua_type(pi->L, -1)) { case LUA_TBOOLEAN: persistboolean(pi); break; case LUA_TLIGHTUSERDATA: persistlightuserdata(pi); break; case LUA_TNUMBER: persistnumber(pi); break; case LUA_TSTRING: persiststring(pi); break; case LUA_TTABLE: persisttable(pi); break; case LUA_TFUNCTION: persistfunction(pi); break; case LUA_TTHREAD: persistthread(pi); break; case LUA_TPROTO: persistproto(pi); break; case LUA_TUPVAL: persistupval(pi); break; case LUA_TUSERDATA: persistuserdata(pi); break; default: lua_assert(0); } #ifdef PLUTO_DEBUG pi->level--; #endif }
static void unpersist(UnpersistInfo *upi) { /* perms reftbl ... */ int firstTime; int stacksize = lua_gettop(upi->L); /* DEBUG */ luaZ_read(&upi->zio, &firstTime, sizeof(int)); if(firstTime) { int ref; int type; luaZ_read(&upi->zio, &ref, sizeof(int)); lua_assert(!inreftable(upi->L, ref)); luaZ_read(&upi->zio, &type, sizeof(int)); #ifdef PLUTO_DEBUG printindent(upi->level); printf("1 %d %d\n", ref, type); upi->level++; #endif switch(type) { case LUA_TBOOLEAN: unpersistboolean(upi); break; case LUA_TLIGHTUSERDATA: unpersistlightuserdata(upi); break; case LUA_TNUMBER: unpersistnumber(upi); break; case LUA_TSTRING: unpersiststring(upi); break; case LUA_TTABLE: unpersisttable(ref, upi); break; case LUA_TFUNCTION: unpersistfunction(ref, upi); break; case LUA_TTHREAD: unpersistthread(ref, upi); break; case LUA_TPROTO: unpersistproto(ref, upi); break; case LUA_TUPVAL: unpersistupval(ref, upi); break; case LUA_TUSERDATA: unpersistuserdata(ref, upi); break; case PLUTO_TPERMANENT: unpersistpermanent(ref, upi); break; default: lua_assert(0); } /* perms reftbl ... obj */ lua_assert(lua_type(upi->L, -1) == type || type == PLUTO_TPERMANENT || /* Remember, upvalues get a special dispensation, as * described in boxupval */ (lua_type(upi->L, -1) == LUA_TFUNCTION && type == LUA_TUPVAL)); registerobject(ref, upi); /* perms reftbl ... obj */ #ifdef PLUTO_DEBUG upi->level--; #endif } else { int ref; luaZ_read(&upi->zio, &ref, sizeof(int)); #ifdef PLUTO_DEBUG printindent(upi->level); printf("0 %d\n", ref); #endif if(ref == 0) { lua_pushnil(upi->L); /* perms reftbl ... nil */ } else { lua_pushlightuserdata(upi->L, (void*)ref); /* perms reftbl ... ref */ lua_gettable(upi->L, 2); /* perms reftbl ... obj? */ lua_assert(!lua_isnil(upi->L, -1)); } /* perms reftbl ... obj/nil */ } /* perms reftbl ... obj/nil */ lua_assert(lua_gettop(upi->L) == stacksize + 1); lua_setgcthreshold(upi->L, 0); lua_setgcthreshold(upi->L, 0); }
static void dumptypes(Node *n, int indent) { size_t i; char *ty; if (!n) return; switch (n->type) { case Nfile: for (i = 0; i < n->file.nuses; i++) dumptypes(n->file.uses[i], indent); for (i = 0; i < n->file.nstmts; i++) dumptypes(n->file.stmts[i], indent); break; case Ndecl: printindent(indent); if (n->decl.isconst) printf("const "); else printf("var "); ty = tystr(n->decl.type); printf("%s : %s\n", namestr(n->decl.name), ty); free(ty); dumptypes(n->decl.init, indent + 1); break; case Nblock: for (i = 0; i < n->block.nstmts; i++) dumptypes(n->block.stmts[i], indent); break; case Nifstmt: dumptypes(n->ifstmt.cond, indent); dumptypes(n->ifstmt.iftrue, indent); dumptypes(n->ifstmt.iffalse, indent); break; case Nloopstmt: dumptypes(n->loopstmt.init, indent); dumptypes(n->loopstmt.cond, indent); dumptypes(n->loopstmt.step, indent); dumptypes(n->loopstmt.body, indent); break; case Niterstmt: dumptypes(n->iterstmt.elt, indent); dumptypes(n->iterstmt.seq, indent); dumptypes(n->iterstmt.body, indent); break; case Nmatchstmt: dumptypes(n->matchstmt.val, indent); for (i = 0; i < n->matchstmt.nmatches; i++) dumptypes(n->matchstmt.matches[i], indent); break; case Nmatch: dumptypes(n->match.pat, indent); dumptypes(n->match.block, indent); break; case Nuse: printindent(indent); if (n->use.islocal) printf("Use \"%s\"\n", n->use.name); else printf("Use %s\n", n->use.name); break; case Nexpr: dumptypes(n->expr.idx, indent); for (i = 0; i < n->expr.nargs; i++) dumptypes(n->expr.args[i], indent); break; case Nlit: switch (n->lit.littype) { case Lfunc: dumptypes(n->lit.fnval, indent); break; default: break; } break; case Nfunc: printindent(indent); printf("Args:\n"); for (i = 0; i < n->func.nargs; i++) dumptypes(n->func.args[i], indent+1); printindent(indent); printf("Body:\n"); dumptypes(n->func.body, indent + 1); break; case Ntrait: die("Traits not fully implemented\n"); break; case Nname: break; case Nnone: die("Nnone not a real node type!"); break; } }