Example #1
0
  @Override
  @SuppressWarnings("unchecked")
  public <U extends IValue, V extends IValue> Result<U> subscript(Result<?>[] subscripts) {
    if (subscripts.length > 1) {
      throw new UnsupportedSubscriptArityError(getType(), subscripts.length, ctx.getCurrentAST());
    }
    Result<IValue> subsBase = (Result<IValue>) subscripts[0];
    if (subsBase == null)
      /*
       * Wild card not allowed as tuple subscript
       */
      throw new UnsupportedSubscriptError(type, null, ctx.getCurrentAST());
    if (!subsBase.getType().isIntegerType()) {
      throw new UnsupportedSubscriptError(
          getTypeFactory().integerType(), subsBase.getType(), ctx.getCurrentAST());
    }
    IInteger index = (IInteger) subsBase.getValue();
    if (index.intValue() >= getValue().arity()) {
      throw RuntimeExceptionFactory.indexOutOfBounds(
          index, ctx.getCurrentAST(), ctx.getStackTrace());
    }

    Type elementType = getType().getFieldType(index.intValue());
    IValue element = getValue().get(index.intValue());
    return makeResult(elementType, element, ctx);
  }
Example #2
0
 public IValue precision(INumber x) {
   if (x.getType().isIntegerType()) {
     IInteger k = (IInteger) x;
     return values.integer(k.toReal().precision());
   }
   if (x.getType().isRationalType()) {
     IRational k = (IRational) x;
     return values.integer(k.toReal().precision());
   }
   return values.integer(((IReal) x).precision());
 }
Example #3
0
 public IValue scale(INumber x) {
   try {
     if (x.getType().isIntegerType()) {
       IInteger k = (IInteger) x;
       return values.integer(k.toReal().scale());
     }
     if (x.getType().isRationalType()) {
       IRational k = (IRational) x;
       return values.integer(k.toReal().scale());
     }
     return values.integer(((IReal) x).scale());
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
Example #4
0
 public void arbSeed(IInteger seed) {
   random.setSeed(seed.intValue());
 }
Example #5
0
 public IValue arbInt(IInteger limit)
       // @doc{arbInt -- return an arbitrary integer value in the interval [0, limit).}
     {
   // TODO allow big ints
   return values.integer(random.nextInt(limit.intValue()));
 }
Example #6
0
 public IValue toReal(IInteger n)
       // @doc{toReal -- convert a rational value to a integer value.}
     {
   return values.real(n.intValue());
 }
Example #7
0
 public IValue setPrecision(IInteger precision) {
   return values.integer(values.setPrecision(precision.intValue()));
 }
Example #8
0
 public IValue precision(INumber x, IInteger precision) {
   return values.real(x.toString(), precision.intValue());
 }