private int setInitialContents(int axis, int[] dims, LispObject contents, int index) {

    if (dims.length == 0) {
      try {
        data[index] = coerceLispObjectToJavaByte(contents);
      } catch (ArrayIndexOutOfBoundsException e) {
        error(new LispError("Bad initial contents for array."));
        return -1;
      }
      ++index;
    } else {
      int dim = dims[0];
      if (dim != contents.length()) {
        error(new LispError("Bad initial contents for array."));
        return -1;
      }
      int[] newDims = new int[dims.length - 1];
      for (int i = 1; i < dims.length; i++) newDims[i - 1] = dims[i];
      if (contents.listp()) {
        for (int i = contents.length(); i-- > 0; ) {
          LispObject content = contents.car();
          index = setInitialContents(axis + 1, newDims, content, index);
          contents = contents.cdr();
        }
      } else {
        AbstractVector v = checkVector(contents);
        final int length = v.length();
        for (int i = 0; i < length; i++) {
          LispObject content = v.AREF(i);
          index = setInitialContents(axis + 1, newDims, content, index);
        }
      }
    }
    return index;
  }
Exemple #2
0
 @Override
 public final LispObject[] copyToArray() {
   final int length = length();
   LispObject[] array = new LispObject[length];
   LispObject rest = this;
   for (int i = 0; i < length; i++) {
     array[i] = rest.car();
     rest = rest.cdr();
   }
   return array;
 }
Exemple #3
0
 @Override
 public LispObject NTH(int index) {
   if (index < 0) type_error(Fixnum.getInstance(index), Symbol.UNSIGNED_BYTE);
   int i = 0;
   LispObject obj = this;
   while (true) {
     if (i == index) return obj.car();
     obj = obj.cdr();
     if (obj == NIL) return NIL;
     ++i;
   }
 }
Exemple #4
0
 public Cons(Cons original) {
   Cons rest = original;
   LispObject result = NIL;
   while (rest.car() != NIL) {
     result = result.push(rest.car());
     if (rest.cdr() == NIL) {
       break;
     }
     rest = (Cons) rest.cdr();
   }
   result = result.nreverse();
   this.car = result.car();
   this.cdr = result.cdr();
   ++count;
 }
Exemple #5
0
 @Override
 public LispObject printObject() {
   final LispThread thread = LispThread.currentThread();
   final LispObject printLength = Symbol.PRINT_LENGTH.symbolValue(thread);
   final int maxLength;
   if (printLength.isFixnum()) maxLength = ((Fixnum) printLength).value;
   else maxLength = Integer.MAX_VALUE;
   final LispObject printLevel = Symbol.PRINT_LEVEL.symbolValue(thread);
   final int maxLevel;
   if (printLevel.isFixnum()) maxLevel = ((Fixnum) printLevel).value;
   else maxLevel = Integer.MAX_VALUE;
   StringBuilder sb = new StringBuilder();
   if (car == Symbol.QUOTE) {
     if (cdr.isCons()) {
       // Not a dotted list.
       if (cdr.cdr() == NIL) {
         sb.append('\'');
         sb.append(cdr.car().printObject());
         return new SimpleString(sb.toString());
       }
     }
   }
   if (car == Symbol.FUNCTION) {
     if (cdr.isCons()) {
       // Not a dotted list.
       if (cdr.cdr() == NIL) {
         sb.append("#'");
         sb.append(cdr.car().printObject().toString());
         return new SimpleString(sb.toString());
       }
     }
   }
   LispObject currentPrintLevel = _CURRENT_PRINT_LEVEL_.symbolValue(thread);
   int currentLevel = Fixnum.getValue(currentPrintLevel);
   if (currentLevel < maxLevel) {
     final SpecialBindingsMark mark = thread.markSpecialBindings();
     thread.bindSpecial(_CURRENT_PRINT_LEVEL_, currentPrintLevel.incr());
     try {
       int count = 0;
       boolean truncated = false;
       sb.append('(');
       if (count < maxLength) {
         LispObject p = this;
         sb.append(p.car().printObject());
         ++count;
         while ((p = p.cdr()).isCons()) {
           sb.append(' ');
           if (count < maxLength) {
             sb.append(p.car().printObject());
             ++count;
           } else {
             truncated = true;
             break;
           }
         }
         if (!truncated && p != NIL) {
           sb.append(" . ");
           sb.append(p.printObject());
         }
       } else truncated = true;
       if (truncated) sb.append("...");
       sb.append(')');
     } finally {
       thread.resetSpecialBindings(mark);
     }
   } else sb.append('#');
   return new SimpleString(sb.toString());
 }