Example #1
0
  private static void printDimList(RAbstractListVector s, PrintContext printCtx)
      throws IOException {
    final PrintParameters pp = printCtx.parameters();

    int ns = s.getLength();
    String[] t = new String[ns];
    for (int i = 0; i < ns; i++) {
      Object tmp = RRuntime.asAbstractVector(s.getDataAtAsObject(i));
      final String pbuf;
      if (tmp == null || tmp == RNull.instance) {
        pbuf = RRuntime.NULL;
      } else if (tmp instanceof RAbstractLogicalVector) {
        RAbstractLogicalVector lv = (RAbstractLogicalVector) tmp;
        if (lv.getLength() == 1) {
          FormatMetrics fm = LogicalVectorPrinter.formatLogicalVector(lv, 0, 1, pp.getNaWidth());
          pbuf =
              snprintf(
                  115, "%s", LogicalVectorPrinter.encodeLogical(lv.getDataAt(0), fm.maxWidth, pp));
        } else {
          pbuf = snprintf(115, "Logical,%d", lv.getLength());
        }
      } else if (tmp instanceof RAbstractIntVector) {
        RAbstractIntVector iv = (RAbstractIntVector) tmp;
        if (printCtx.printerNode().inherits(iv, RRuntime.CLASS_FACTOR)) {
          /* factors are stored as integers */
          pbuf = snprintf(115, "factor,%d", iv.getLength());
        } else {
          if (iv.getLength() == 1) {
            FormatMetrics fm = IntegerVectorPrinter.formatIntVector(iv, 0, 1, pp.getNaWidth());
            pbuf =
                snprintf(
                    115,
                    "%s",
                    IntegerVectorPrinter.encodeInteger(iv.getDataAt(0), fm.maxWidth, pp));
          } else {
            pbuf = snprintf(115, "Integer,%d", iv.getLength());
          }
        }
      } else if (tmp instanceof RAbstractDoubleVector) {
        RAbstractDoubleVector dv = (RAbstractDoubleVector) tmp;
        if (dv.getLength() == 1) {
          DoubleVectorMetrics fm = DoubleVectorPrinter.formatDoubleVector(dv, 0, 1, 0, pp);
          pbuf = snprintf(115, "%s", DoubleVectorPrinter.encodeReal(dv.getDataAt(0), fm, pp));
        } else {
          pbuf = snprintf(115, "Numeric,%d", dv.getLength());
        }
      } else if (tmp instanceof RAbstractComplexVector) {
        RAbstractComplexVector cv = (RAbstractComplexVector) tmp;
        if (cv.getLength() == 1) {
          RComplex x = cv.getDataAt(0);
          if (RRuntime.isNA(x.getRealPart()) || RRuntime.isNA(x.getImaginaryPart())) {
            /* formatReal(NA) --> w=R_print.na_width, d=0, e=0 */
            pbuf =
                snprintf(
                    115,
                    "%s",
                    DoubleVectorPrinter.encodeReal(
                        RRuntime.DOUBLE_NA, pp.getNaWidth(), 0, 0, '.', pp));
          } else {
            ComplexVectorMetrics cvm = ComplexVectorPrinter.formatComplexVector(x, 0, 1, 0, pp);
            pbuf = snprintf(115, "%s", ComplexVectorPrinter.encodeComplex(x, cvm, pp));
          }
        } else {
          pbuf = snprintf(115, "Complex,%d", cv.getLength());
        }
      } else if (tmp instanceof RAbstractStringVector) {
        RAbstractStringVector sv = (RAbstractStringVector) tmp;
        if (sv.getLength() == 1) {
          String ctmp = sv.getDataAt(0);
          int len = ctmp.length();
          if (len < 100) {
            pbuf = snprintf(115, "\"%s\"", ctmp);
          } else {
            pbuf = snprintf(101, "\"%s", ctmp) + "\" [truncated]";
          }
        } else {
          pbuf = snprintf(115, "Character,%d", sv.getLength());
        }
      } else if (tmp instanceof RAbstractRawVector) {
        pbuf = snprintf(115, "Raw,%d", ((RAbstractRawVector) (tmp)).getLength());
      } else if (tmp instanceof RAbstractListVector) {
        pbuf = snprintf(115, "List,%d", ((RAbstractListVector) (tmp)).getLength());
      } else if (tmp instanceof RLanguage) {
        pbuf = snprintf(115, "Expression");
      } else {
        pbuf = snprintf(115, "?");
      }

      t[i] = pbuf;
    }

    RStringVector tt = RDataFactory.createStringVector(t, true, s.getDimensions());
    Object dimNames = s.getAttr(RRuntime.DIMNAMES_ATTR_KEY);
    tt.setAttr(RRuntime.DIMNAMES_ATTR_KEY, dimNames);

    PrintContext cc = printCtx.cloneContext();
    cc.parameters().setQuote(false);
    StringVectorPrinter.INSTANCE.print(tt, cc);
  }
Example #2
0
  @TruffleBoundary
  static void printNoDimList(RAbstractContainer s, PrintContext printCtx) throws IOException {
    final PrintParameters pp = printCtx.parameters();
    final PrintWriter out = printCtx.output();

    final StringBuilder tagbuf = printCtx.getOrCreateTagBuffer();
    // save the original length so that we can restore the original value
    int taglen = tagbuf.length();

    int ns = s.getLength();

    RAbstractStringVector names;
    names = Utils.castTo(RRuntime.asAbstractVector(s.getNames(dummyAttrProfiles)));

    if (ns > 0) {
      int npr = (ns <= pp.getMax() + 1) ? ns : pp.getMax();
      /* '...max +1' ==> will omit at least 2 ==> plural in msg below */
      for (int i = 0; i < npr; i++) {
        if (i > 0) {
          out.println();
        }
        String ss = names == null ? null : Utils.<String>getDataAt(names, i);
        if (ss != null && !ss.isEmpty()) {
          /*
           * Bug for L <- list(`a\\b` = 1, `a\\c` = 2) : const char *ss =
           * translateChar(STRING_ELT(names, i));
           */
          if (taglen + ss.length() > TAGBUFLEN) {
            if (taglen <= TAGBUFLEN) {
              tagbuf.append("$...");
            }
          } else {
            /*
             * we need to distinguish character NA from "NA", which is a valid (if
             * non-syntactic) name
             */
            if (ss == RRuntime.STRING_NA) {
              tagbuf.append("$<NA>");
            } else if (RDeparse.isValidName(ss)) {
              tagbuf.append(String.format("$%s", ss));
            } else {
              tagbuf.append(String.format("$`%s`", ss));
            }
          }

        } else {
          if (taglen + indexWidth(i) > TAGBUFLEN) {
            if (taglen <= TAGBUFLEN) {
              tagbuf.append("$...");
            }
          } else {
            tagbuf.append(String.format("[[%d]]", i + 1));
          }
        }

        out.println(tagbuf);
        Object si = s.getDataAtAsObject(i);
        if (si instanceof RAttributable && ((RAttributable) si).isObject()) {
          RContext.getEngine().printResult(si);
        } else {
          ValuePrinters.INSTANCE.print(si, printCtx);
          ValuePrinters.printNewLine(printCtx);
        }
        tagbuf.setLength(taglen); // reset tag buffer to the original value
      }

      if (npr < ns) {
        out.printf("\n [ reached getOption(\"max.print\") -- omitted %d entries ]", ns - npr);
      }

    } else {
      /* ns = length(s) == 0 */

      /* Formal classes are represented as empty lists */
      String className = null;
      if (printCtx.printerNode().isObject(s) && printCtx.printerNode().isMethodDispatchOn()) {
        RAbstractStringVector klass =
            Utils.castTo(RRuntime.asAbstractVector(s.getAttr(RRuntime.CLASS_ATTR_KEY)));
        if (klass != null && klass.getLength() == 1) {
          String ss = klass.getDataAt(0);
          String str = snprintf(200, ".__C__%s", ss);
          Frame frame = com.oracle.truffle.r.runtime.Utils.getActualCurrentFrame();
          if (ReadVariableNode.lookupAny(str, frame, false) != null) {
            className = ss;
          }
        }
      }
      if (className != null) {
        out.printf("An object of class \"%s\"", className);
      } else {
        if (names != null) {
          out.print("named ");
        }
        out.print("list()");
      }
    }
  }