Пример #1
0
  private void readStringPool(int size) throws IOException {
    final String[] myStringPool = new String[size];

    for (int i = 0; i != size; ++i) {
      int length = input.read_uv();
      try {
        byte[] data = new byte[length];
        input.read(data);
        String str = new String(data, 0, length, "UTF-8");
        myStringPool[i] = str;
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 Charset not supported?");
      }
    }
    stringPool = myStringPool;
  }
Пример #2
0
  private void readConstantPool(int size) throws IOException {
    final Constant[] myConstantPool = new Constant[size];

    for (int i = 0; i != size; ++i) {
      int code = input.read_uv();
      Constant constant;

      switch (code) {
        case WyilFileWriter.CONSTANT_Null:
          constant = Constant.V_NULL;
          break;
        case WyilFileWriter.CONSTANT_False:
          constant = Constant.V_BOOL(false);
          break;
        case WyilFileWriter.CONSTANT_True:
          constant = Constant.V_BOOL(true);
          break;
        case WyilFileWriter.CONSTANT_Byte:
          {
            byte val = (byte) input.read_u8();
            constant = Constant.V_BYTE(val);
            break;
          }
        case WyilFileWriter.CONSTANT_Char:
          {
            char val = (char) input.read_uv();
            constant = Constant.V_CHAR(val);
            break;
          }
        case WyilFileWriter.CONSTANT_Int:
          {
            int len = input.read_uv();
            byte[] bytes = new byte[len];
            input.read(bytes);
            BigInteger bi = new BigInteger(bytes);
            constant = Constant.V_INTEGER(bi);
            break;
          }
        case WyilFileWriter.CONSTANT_Real:
          {
            int len = input.read_uv();
            byte[] bytes = new byte[len];
            input.read(bytes);
            BigInteger num = new BigInteger(bytes);
            len = input.read_uv();
            bytes = new byte[len];
            input.read(bytes);
            BigInteger den = new BigInteger(bytes);
            BigRational br = new BigRational(num, den);
            constant = Constant.V_RATIONAL(br);
            break;
          }
        case WyilFileWriter.CONSTANT_String:
          {
            int index = input.read_uv();
            constant = Constant.V_STRING(stringPool[index]);
            break;
          }
        case WyilFileWriter.CONSTANT_List:
          {
            int len = input.read_uv();
            ArrayList<Constant> values = new ArrayList<Constant>();
            for (int j = 0; j != len; ++j) {
              int index = input.read_uv();
              values.add(myConstantPool[index]);
            }
            constant = Constant.V_LIST(values);
            break;
          }
        case WyilFileWriter.CONSTANT_Set:
          {
            int len = input.read_uv();
            ArrayList<Constant> values = new ArrayList<Constant>();
            for (int j = 0; j != len; ++j) {
              int index = input.read_uv();
              values.add(myConstantPool[index]);
            }
            constant = Constant.V_SET(values);
            break;
          }
        case WyilFileWriter.CONSTANT_Tuple:
          {
            int len = input.read_uv();
            ArrayList<Constant> values = new ArrayList<Constant>();
            for (int j = 0; j != len; ++j) {
              int index = input.read_uv();
              values.add(myConstantPool[index]);
            }
            constant = Constant.V_TUPLE(values);
            break;
          }
        case WyilFileWriter.CONSTANT_Map:
          {
            int len = input.read_uv();
            HashSet<Pair<Constant, Constant>> values = new HashSet<Pair<Constant, Constant>>();
            for (int j = 0; j != len; ++j) {
              int keyIndex = input.read_uv();
              int valIndex = input.read_uv();
              Constant key = myConstantPool[keyIndex];
              Constant val = myConstantPool[valIndex];
              values.add(new Pair<Constant, Constant>(key, val));
            }
            constant = Constant.V_MAP(values);
            break;
          }
        case WyilFileWriter.CONSTANT_Record:
          {
            int len = input.read_uv();
            HashMap<String, Constant> tvs = new HashMap<String, Constant>();
            for (int j = 0; j != len; ++j) {
              int fieldIndex = input.read_uv();
              int constantIndex = input.read_uv();
              String str = stringPool[fieldIndex];
              tvs.put(str, myConstantPool[constantIndex]);
            }
            constant = Constant.V_RECORD(tvs);
            break;
          }
        case WyilFileWriter.CONSTANT_Function:
        case WyilFileWriter.CONSTANT_Method:
          {
            int typeIndex = input.read_uv();
            int nameIndex = input.read_uv();
            Type.FunctionOrMethod t = (Type.FunctionOrMethod) typePool[typeIndex];
            NameID name = namePool[nameIndex];
            constant = Constant.V_LAMBDA(name, t);
            break;
          }
        default:
          throw new RuntimeException("Unknown constant encountered in WhileyDefine: " + code);
      }
      myConstantPool[i] = constant;
    }

    constantPool = myConstantPool;
  }