Commit 7c2b76ca authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #774 from corona10/recursive

Recursive printing
parents ff6fe32f b784a5cf
......@@ -29,23 +29,41 @@ namespace pyston {
Box* dictRepr(BoxedDict* self) {
std::vector<char> chars;
chars.push_back('{');
bool first = true;
for (const auto& p : self->d) {
if (!first) {
chars.push_back(',');
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
throwCAPIException();
chars.push_back('{');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back('}');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
try {
chars.push_back('{');
bool first = true;
for (const auto& p : self->d) {
if (!first) {
chars.push_back(',');
chars.push_back(' ');
}
first = false;
BoxedString* k = static_cast<BoxedString*>(repr(p.first));
BoxedString* v = static_cast<BoxedString*>(repr(p.second));
chars.insert(chars.end(), k->s().begin(), k->s().end());
chars.push_back(':');
chars.push_back(' ');
chars.insert(chars.end(), v->s().begin(), v->s().end());
}
first = false;
BoxedString* k = static_cast<BoxedString*>(repr(p.first));
BoxedString* v = static_cast<BoxedString*>(repr(p.second));
chars.insert(chars.end(), k->s().begin(), k->s().end());
chars.push_back(':');
chars.push_back(' ');
chars.insert(chars.end(), v->s().begin(), v->s().end());
chars.push_back('}');
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)self);
throw e;
}
chars.push_back('}');
Py_ReprLeave((PyObject*)self);
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
......
......@@ -17,8 +17,6 @@
#include <algorithm>
#include <cstring>
#include "llvm/Support/raw_ostream.h"
#include "capi/types.h"
#include "core/ast.h"
#include "core/common.h"
......@@ -63,36 +61,40 @@ extern "C" PyObject* PyList_AsTuple(PyObject* v) noexcept {
}
extern "C" Box* listRepr(BoxedList* self) {
// TODO highly inefficient with all the string copying
std::string O("");
llvm::raw_string_ostream os(O);
std::vector<char> chars;
int status = Py_ReprEnter((PyObject*)self);
// Implementing Recursive Print of list in same way from Cpython
int recursive = Py_ReprEnter((PyObject*)self);
if (recursive != 0) {
if (recursive < 0)
return boxString(os.str());
if (status != 0) {
if (status < 0)
throwCAPIException();
os << "[...]";
return boxString(os.str());
chars.push_back('[');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back(']');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
try {
chars.push_back('[');
for (int i = 0; i < self->size; i++) {
os << '[';
for (int i = 0; i < self->size; i++) {
if (i > 0)
os << ", ";
Box* r = self->elts->elts[i]->reprICAsString();
assert(r->cls == str_cls);
BoxedString* s = static_cast<BoxedString*>(r);
os << s->s();
if (i > 0) {
chars.push_back(',');
chars.push_back(' ');
}
Box* r = self->elts->elts[i]->reprICAsString();
assert(r->cls == str_cls);
BoxedString* s = static_cast<BoxedString*>(r);
chars.insert(chars.end(), s->s().begin(), s->s().end());
}
chars.push_back(']');
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)self);
throw e;
}
os << ']';
Py_ReprLeave((PyObject*)self);
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
extern "C" Box* listNonzero(BoxedList* self) {
......
......@@ -14,8 +14,6 @@
#include "runtime/set.h"
#include <llvm/Support/raw_ostream.h>
#include "gc/collector.h"
#include "runtime/objmodel.h"
......@@ -96,29 +94,52 @@ Box* setNew(Box* _cls, Box* container) {
}
static Box* _setRepr(BoxedSet* self, const char* type_name) {
std::string O("");
llvm::raw_string_ostream os(O);
std::vector<char> chars;
int status = Py_ReprEnter((PyObject*)self);
if (status != 0) {
if (status < 0)
return boxString(os.str());
throwCAPIException();
std::string ty = std::string(type_name);
chars.insert(chars.end(), ty.begin(), ty.end());
chars.push_back('(');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back(')');
os << type_name << "(...)";
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
os << type_name << "([";
bool first = true;
for (Box* elt : self->s) {
if (!first) {
os << ", ";
try {
std::string ty = std::string(type_name);
chars.insert(chars.end(), ty.begin(), ty.end());
chars.push_back('(');
chars.push_back('[');
bool first = true;
for (Box* elt : self->s) {
if (!first) {
chars.push_back(',');
chars.push_back(' ');
}
BoxedString* str = static_cast<BoxedString*>(repr(elt));
chars.insert(chars.end(), str->s().begin(), str->s().end());
first = false;
}
os << static_cast<BoxedString*>(repr(elt))->s();
first = false;
chars.push_back(']');
chars.push_back(')');
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)self);
throw e;
}
os << "])";
Py_ReprLeave((PyObject*)self);
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
Box* setRepr(BoxedSet* self) {
......
......@@ -16,8 +16,6 @@
#include <algorithm>
#include "llvm/Support/raw_ostream.h"
#include "capi/typeobject.h"
#include "core/ast.h"
#include "core/common.h"
......@@ -210,42 +208,52 @@ extern "C" Py_ssize_t PyTuple_Size(PyObject* op) noexcept {
}
Box* tupleRepr(BoxedTuple* t) {
assert(isSubclass(t->cls, tuple_cls));
assert(isSubclass(t->cls, tuple_cls));
int n;
std::string O("");
llvm::raw_string_ostream os(O);
std::vector<char> chars;
int status = Py_ReprEnter((PyObject*)t);
n = t->size();
if (n == 0) {
os << "()";
return boxString(os.str());
chars.push_back('(');
chars.push_back(')');
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
int status = Py_ReprEnter((PyObject*)t);
if (status != 0) {
if (status < 0)
return boxString(os.str());
throwCAPIException();
chars.push_back('(');
chars.push_back('.');
chars.push_back('.');
chars.push_back('.');
chars.push_back(')');
os << "(...)";
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
os << "(";
try {
chars.push_back('(');
for (int i = 0; i < n; i++) {
if (i) {
chars.push_back(',');
chars.push_back(' ');
}
BoxedString* elt_repr = static_cast<BoxedString*>(repr(t->elts[i]));
chars.insert(chars.end(), elt_repr->s().begin(), elt_repr->s().end());
}
for (int i = 0; i < n; i++) {
if (i)
os << ", ";
if (n == 1)
chars.push_back(',');
BoxedString* elt_repr = static_cast<BoxedString*>(repr(t->elts[i]));
os << elt_repr->s();
chars.push_back(')');
} catch (ExcInfo e) {
Py_ReprLeave((PyObject*)t);
throw e;
}
if (n == 1)
os << ",";
os << ")";
Py_ReprLeave((PyObject*)t);
return boxString(os.str());
return boxString(llvm::StringRef(&chars[0], chars.size()));
}
Box* tupleNonzero(BoxedTuple* self) {
......
......@@ -230,3 +230,9 @@ while True:
except StopIteration:
break
print sorted(l)
#recursive printing test
d = dict()
d['one'] = '1'
d['two'] = d
print d
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment