Example #1
0
  @Override
  public Result<IValue> fieldSelect(Field[] selectedFields) {
    int nFields = selectedFields.length;
    int fieldIndices[] = new int[nFields];
    Type baseType = this.getType();

    for (int i = 0; i < nFields; i++) {
      Field f = selectedFields[i];
      if (f.isIndex()) {
        fieldIndices[i] =
            ((IInteger) f.getFieldIndex().interpret(this.ctx.getEvaluator()).getValue()).intValue();
      } else {
        String fieldName = org.rascalmpl.interpreter.utils.Names.name(f.getFieldName());
        try {
          fieldIndices[i] = baseType.getFieldIndex(fieldName);
        } catch (UndeclaredFieldException e) {
          throw new UndeclaredFieldError(fieldName, baseType, ctx.getCurrentAST());
        }
      }

      if (fieldIndices[i] < 0 || fieldIndices[i] > baseType.getArity()) {
        throw org.rascalmpl.interpreter.utils.RuntimeExceptionFactory.indexOutOfBounds(
            ValueFactoryFactory.getValueFactory().integer(fieldIndices[i]),
            ctx.getCurrentAST(),
            ctx.getStackTrace());
      }
    }

    return this.fieldSelect(fieldIndices);
  }
Example #2
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);
  }