void print(slot_t item, slot_t *type) { slot_t tag = *type; switch(tag) { case 0: case 1: case 2: case 3: case 4: internal_print(&item,type); break; default: internal_print((slot_t*)item,type); } printf("\n"); }
void internal_print(slot_t *item, slot_t *type) { // NOTE: this function is not working properly yet. slot_t tag = *type; switch(tag) { case VOID_TAG: // void break; case BOOL_TAG: // bool if(*item == 0) { printf("false"); } else { printf("true"); } break; case CHAR_TAG: { // char char tmp[2]; tmp[0] = *item; tmp[1] = '\0'; printf("%s",tmp); break; } case INT_TAG: // int printf("%d",*item); break; case REAL_TAG: // real printf("%g",*item); break; case STRING_TAG: // string printf("%s",item); break; case RECORD_TAG: { int i; // record printf("{"); slot_t nfields = *(++type); for(i=0;i!=nfields;++i) { if(i != 0) { printf(","); } slot_t fieldNameSize = *(++type); printf("%s:",++type); type = ((void *)type) + fieldNameSize + 1; internal_print(item,type); item = ((void *)item) + widthof(type); // FIXME: this is clearly broken here, because we don't increment type correctly. } printf("}"); } } }
/* Implementation of PyObject_Print with recursion checking */ static int internal_print(PyObject *op, FILE *fp, int flags, int nesting) { int ret = 0; if (nesting > 10) { PyErr_SetString(PyExc_RuntimeError, "print recursion"); return -1; } if (PyErr_CheckSignals()) return -1; #ifdef USE_STACKCHECK if (PyOS_CheckStack()) { PyErr_SetString(PyExc_MemoryError, "stack overflow"); return -1; } #endif clearerr(fp); /* Clear any previous error condition */ if (op == NULL) { fprintf(fp, "<nil>"); } else { if (op->ob_refcnt <= 0) /* XXX(twouters) cast refcount to long until %zd is universally available */ fprintf(fp, "<refcnt %ld at %p>", (long)op->ob_refcnt, op); else if (op->ob_type->tp_print == NULL) { PyObject *s; if (flags & Py_PRINT_RAW) s = PyObject_Str(op); else s = PyObject_Repr(op); if (s == NULL) ret = -1; else { ret = internal_print(s, fp, Py_PRINT_RAW, nesting+1); } Py_XDECREF(s); } else ret = (*op->ob_type->tp_print)(op, fp, flags); } if (ret == 0) { if (ferror(fp)) { PyErr_SetFromErrno(PyExc_IOError); clearerr(fp); ret = -1; } } return ret; }
int internal_log(lua_State *L, const char *name, const char *cr, const char *cn, const char *cs, const char *cnil, const char *cb, const char *co) { const int n = lua_gettop(L); fprintf(stdout, "[console:%s]", name); int i = 0; for (i = 1; i <= n; i++) { fprintf(stdout, " "); internal_print(L, i, cr, cn, cs, cnil, cb, co, false); } fprintf(stdout, "\n"); return 0; }
// Internal ------------------------------------------------------------------- // ---------------------------------------------------------------------------- void internal_print(lua_State *L, const int i, const char *cr, const char *cn, const char *cs, const char *cnil, const char *cb, const char *co, const bool wrapString) { unsigned int length = 0; unsigned int count = 0; unsigned int index = 0; bool initial = true; if (lua_isnumber(L, i)) { fprintf(stdout, "%s%s%s", cn, lua_tolstring(L, i, &length), cr); } else if (lua_isstring(L, i)) { if (wrapString) { fprintf(stdout, "%s'%s'%s", co, lua_tolstring(L, i, &length), cr); } else { fprintf(stdout, "%s%s%s", cs, lua_tolstring(L, i, &length), cr); } } else if (lua_isnil(L, i)) { fprintf(stdout, "%snil%s", cnil, cr); } else if (lua_isboolean(L, i)) { if (lua_toboolean(L, i)) { fprintf(stdout, "%strue%s", cb, cr); } else { fprintf(stdout, "%sfalse%s", cb, cr); } } else if (lua_istable(L, i)) { lua_len(L, i); count = lua_tointeger(L, -1); fprintf(stdout, "%s{%s ", co, cr); lua_pushnil(L); while(lua_next(L, i) != 0) { if (!initial) { fprintf(stdout, ", "); } else { initial = false; } if (lua_isnumber(L, -2) && lua_isstring(L, -2)) { fprintf(stdout, "%s%d%s = ", cn, ++index, cr); } else { fprintf(stdout, "%s%s%s = ", cs, lua_tolstring(L, -2, &length), cr); } internal_print(L, lua_gettop(L), cr, cn, cs, cnil, cb, co, true); lua_pop(L, 1); } fprintf(stdout, " %s}(#%s%d%s)%s", co, cn, count, co, cr); lua_pop(L, 1); } }
int PyObject_Print(PyObject *op, FILE *fp, int flags) { return internal_print(op, fp, flags, 0); }
void print() { char buffer[MAX_TRIE_DEPTH + 1]; buffer[MAX_TRIE_DEPTH] = '\0'; internal_print(buffer + MAX_TRIE_DEPTH, 0); }