Exemplo n.º 1
0
 @Subroutine(value = "format")
 public static LispString format(LispString formatString, @Optional LispObject... objects) {
   checkFormatCharacters(formatString.getData());
   try {
     Object[] data = new Object[objects.length];
     for (int i = 0; i != objects.length; i++) {
       LispObject object = objects[i];
       data[i] =
           object instanceof LispNumber
               ? ((LispNumber) object).getData()
               : (object instanceof LispString
                   ? ((LispString) object).getData()
                   : object.toString());
     }
     return new LispString(String.format(formatString.getData(), data));
   } catch (MissingFormatArgumentException e1) {
     throw new LispException(JelispBundle.message("few.args.for.format.string"));
   } catch (UnknownFormatConversionException e2) {
     char c = e2.getMessage().charAt(e2.getMessage().length() - 2);
     if (c < 'A') throw new LispException(JelispBundle.message("unexpected.format.string.end"));
     throw new InvalidFormatOperationException(c);
   } catch (IllegalFormatConversionException e3) {
     throw new LispException(JelispBundle.message("format.arg.types.mismatch"));
   }
 }
Exemplo n.º 2
0
 @Subroutine("message")
 public static LispString message(@Optional LispObject... args) {
   if (args.length == 0) throw new WrongNumberOfArgumentsException("message", 0);
   LispObject formatObj = args[0];
   if (formatObj instanceof LispString) {
     LispObject[] a = new LispObject[args.length - 1];
     System.arraycopy(args, 1, a, 0, args.length - 1);
     myCurrentMessage = format((LispString) formatObj, a);
   } else if (formatObj.equals(LispSymbol.ourNil)) {
     myCurrentMessage = new LispString(LispSymbol.ourNil.toString());
   } else {
     throw new WrongTypeArgumentException("stringp", formatObj);
   }
   GlobalEnvironment.echo(myCurrentMessage.getData(), GlobalEnvironment.MessageType.OUTPUT);
   return myCurrentMessage;
 }
Exemplo n.º 3
0
 @Subroutine("read-from-string")
 public static LispList readFromString(
     LispString string,
     @Optional @Nullable LispObject start,
     @Optional @Nullable LispObject finish) {
   int begin = getInt(start, 0);
   int end = getInt(finish, string.size());
   try {
     String code = string.getData().substring(begin, end);
     ForwardParser forwardParser = new ForwardParser();
     LispObject read = Core.thisOrNil(forwardParser.parseLine(code));
     return LispList.cons(read, new LispInteger(begin + forwardParser.getCurrentIndex()));
   } catch (StringIndexOutOfBoundsException e) {
     throw new ArgumentOutOfRange(string, begin, end);
   }
 }