Skip to content
Snippets Groups Projects
Commit db7cd2ae authored by Michael Haupt's avatar Michael Haupt
Browse files

refactor list pretty-printing, support deeper nesting levels

parent 032bd561
No related branches found
No related tags found
No related merge requests found
...@@ -351,33 +351,11 @@ public abstract class PrettyPrinterNode extends RNode { ...@@ -351,33 +351,11 @@ public abstract class PrettyPrinterNode extends RNode {
@SlowPath @SlowPath
@Specialization(order = 1000, guards = "!isMatrix") @Specialization(order = 1000, guards = "!isMatrix")
public String prettyPrint(VirtualFrame frame, RList operand) { public String prettyPrint(VirtualFrame frame, RList operand) {
int length = operand.getLength(); return prettyPrintList0(frame, operand, null);
if (length == 0) {
return "list()";
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
if (isPrintingAttributes() && operand.elementNamePrefix != null) {
sb.append(operand.elementNamePrefix);
}
Object name = operand.getNameAt(i);
sb.append(name).append('\n');
Object value = operand.getDataAt(i);
sb.append(printSingleListValue(frame, value, name)).append("\n\n");
}
String result = sb.deleteCharAt(sb.length() - 1).toString();
Map<String, Object> attributes = operand.getAttributes();
if (attributes != null) {
result = result + printAttributes(frame, operand, attributes);
}
return result;
}
} }
// TODO refactor: too much code reuse
// FIXME support nesting levels >1 // FIXME support nesting levels >1
public String prettyPrintNestedList(VirtualFrame frame, RList operand, Object listName) { private String prettyPrintList0(VirtualFrame frame, RList operand, Object listName) {
int length = operand.getLength(); int length = operand.getLength();
if (length == 0) { if (length == 0) {
return "list()"; return "list()";
...@@ -386,10 +364,11 @@ public abstract class PrettyPrinterNode extends RNode { ...@@ -386,10 +364,11 @@ public abstract class PrettyPrinterNode extends RNode {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (isPrintingAttributes() && operand.elementNamePrefix != null) { if (isPrintingAttributes() && operand.elementNamePrefix != null) {
sb.append(operand.elementNamePrefix); sb.append(operand.elementNamePrefix);
} else {
sb.append(listName);
} }
Object name = operand.getNameAt(i); Object name = operand.getNameAt(i);
if (listName != null) {
name = new StringBuilder(listName.toString()).append(name).toString();
}
sb.append(name).append('\n'); sb.append(name).append('\n');
Object value = operand.getDataAt(i); Object value = operand.getDataAt(i);
sb.append(printSingleListValue(frame, value, name)).append("\n\n"); sb.append(printSingleListValue(frame, value, name)).append("\n\n");
...@@ -400,7 +379,6 @@ public abstract class PrettyPrinterNode extends RNode { ...@@ -400,7 +379,6 @@ public abstract class PrettyPrinterNode extends RNode {
result = result + printAttributes(frame, operand, attributes); result = result + printAttributes(frame, operand, attributes);
} }
return result; return result;
} }
} }
...@@ -486,7 +464,7 @@ public abstract class PrettyPrinterNode extends RNode { ...@@ -486,7 +464,7 @@ public abstract class PrettyPrinterNode extends RNode {
} }
if (RTYPES.isRList(argumentsValue0)) { if (RTYPES.isRList(argumentsValue0)) {
RList argumentsValue0Cast = RTYPES.asRList(argumentsValue0); RList argumentsValue0Cast = RTYPES.asRList(argumentsValue0);
return prettyPrintNestedList(frame, argumentsValue0Cast, listElementName); return prettyPrintList0(frame, argumentsValue0Cast, listElementName);
} }
if (RTYPES.isRInvisible(argumentsValue0)) { if (RTYPES.isRInvisible(argumentsValue0)) {
RInvisible argumentsValue0Cast = RTYPES.asRInvisible(argumentsValue0); RInvisible argumentsValue0Cast = RTYPES.asRInvisible(argumentsValue0);
......
...@@ -4926,6 +4926,39 @@ character(0) ...@@ -4926,6 +4926,39 @@ character(0)
[1] 3 [1] 3
   
   
##com.oracle.truffle.r.test.simple.TestSimpleBuiltins.testPrint
#{ print(list(list(list(1,2),list(3)),list(list(4),list(5,6)))) }
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[1] 1
[[1]][[1]][[2]]
[1] 2
[[1]][[2]]
[[1]][[2]][[1]]
[1] 3
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[1] 4
[[2]][[2]]
[[2]][[2]][[1]]
[1] 5
[[2]][[2]][[2]]
[1] 6
##com.oracle.truffle.r.test.simple.TestSimpleBuiltins.testPrint ##com.oracle.truffle.r.test.simple.TestSimpleBuiltins.testPrint
#{ x<-c("11", "7", "2222", "7", "33"); names(x)<-1:5; print(x) } #{ x<-c("11", "7", "2222", "7", "33"); names(x)<-1:5; print(x) }
1 2 3 4 5 1 2 3 4 5
...@@ -2267,6 +2267,7 @@ public class TestSimpleBuiltins extends TestBase { ...@@ -2267,6 +2267,7 @@ public class TestSimpleBuiltins extends TestBase {
assertEval("{ x<-c(11, 7, 2222, 7, 33); print(x) }"); assertEval("{ x<-c(11, 7, 2222, 7, 33); print(x) }");
assertEval("{ x<-c(\"11\", \"7\", \"2222\", \"7\", \"33\"); names(x)<-1:5; print(x) }"); assertEval("{ x<-c(\"11\", \"7\", \"2222\", \"7\", \"33\"); names(x)<-1:5; print(x) }");
assertEval("{ x<-c(11, 7, 2222, 7, 33); names(x)<-1:5; print(x) }"); assertEval("{ x<-c(11, 7, 2222, 7, 33); names(x)<-1:5; print(x) }");
assertEval("{ print(list(list(list(1,2),list(3)),list(list(4),list(5,6)))) }");
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment