Beispiel #1
0
  @Override
  public int compareTo(FunctionSignature o) {
    int cmpVal = name.compareTo(o.name);

    if (cmpVal != 0) {
      return cmpVal;
    }

    cmpVal = functionType.name().compareTo(o.functionType.name());

    if (cmpVal != 0) {
      return cmpVal;
    }

    cmpVal = returnType.getType().name().compareTo(o.returnType.getType().name());

    if (cmpVal != 0) {
      return cmpVal;
    }

    for (int i = 0; i < Math.min(paramTypes.length, o.paramTypes.length); i++) {
      cmpVal = paramTypes[i].getType().name().compareTo(o.paramTypes[i].getType().name());

      if (cmpVal != 0) {
        return cmpVal;
      }
    }

    return o.paramTypes.length - paramTypes.length;
  }
Beispiel #2
0
  /**
   * set the values from an Array
   *
   * @param arr value of Attribute
   */
  protected void setValues(Array arr) {
    if (arr == null) {
      dataType = DataType.STRING;
      return;
    }

    if (DataType.getType(arr.getElementType()) == null)
      throw new IllegalArgumentException("Cant set Attribute with type " + arr.getElementType());

    if (arr.getElementType() == char.class) { // turn CHAR into STRING
      ArrayChar carr = (ArrayChar) arr;
      if (carr.getRank() == 1) { // common case
        svalue = carr.getString();
        this.nelems = 1;
        this.dataType = DataType.STRING;
        return;
      }
      // otherwise its an array of Strings
      arr = carr.make1DStringArray();
    }

    // this should be a utility somewhere
    if (arr.getElementType() == ByteBuffer.class) { // turn OPAQUE into BYTE
      int totalLen = 0;
      arr.resetLocalIterator();
      while (arr.hasNext()) {
        ByteBuffer bb = (ByteBuffer) arr.next();
        totalLen += bb.limit();
      }
      byte[] ba = new byte[totalLen];
      int pos = 0;
      arr.resetLocalIterator();
      while (arr.hasNext()) {
        ByteBuffer bb = (ByteBuffer) arr.next();
        System.arraycopy(bb.array(), 0, ba, pos, bb.limit());
        pos += bb.limit();
      }
      arr = Array.factory(DataType.BYTE, new int[] {totalLen}, ba);
    }

    if (arr.getRank() > 1) arr = arr.reshape(new int[] {(int) arr.getSize()}); // make sure 1D

    this.values = arr;
    this.nelems = (int) arr.getSize();
    this.dataType = DataType.getType(arr.getElementType());
    hashCode = 0;
  }
Beispiel #3
0
 /**
  * Create a scalar numeric-valued Attribute.
  *
  * @param name name of Attribute
  * @param val value of Attribute
  */
 public Attribute(String name, Number val) {
   this.name = name;
   int[] shape = new int[1];
   shape[0] = 1;
   DataType dt = DataType.getType(val.getClass());
   Array vala = Array.factory(dt.getPrimitiveClassType(), shape);
   Index ima = vala.getIndex();
   vala.setDouble(ima.set0(0), val.doubleValue());
   setValues(vala);
 }
  protected Array convertEnums(Array values) {
    DataType dt = DataType.getType(values.getElementType());
    if (!dt.isNumeric()) System.out.println("HEY");

    Array result = Array.factory(DataType.STRING, values.getShape());
    IndexIterator ii = result.getIndexIterator();
    values.resetLocalIterator();
    while (values.hasNext()) {
      String sval = lookupEnumString(values.nextInt());
      ii.setObjectNext(sval);
    }
    return result;
  }
 @Override
 public DataType visitPrint_stmt(@NotNull PythonParser.Print_stmtContext ctx) {
   mv.visitFieldInsn(
       GETSTATIC,
       Type.getInternalName(System.class),
       "out",
       Type.getDescriptor(PrintStream.class));
   DataType type = visitTest(ctx.test());
   mv.visitMethodInsn(
       INVOKEVIRTUAL,
       Type.getInternalName(PrintStream.class),
       "println",
       Type.getMethodDescriptor(Type.VOID_TYPE, type.getType()),
       false);
   return null;
 }
 private static ColumnDecl buildDecl(DataType dt, List<AttributeValue> attrs) {
   StringBuilder buf = new StringBuilder();
   List<AttributeValue> attrApplied = new ArrayList<AttributeValue>();
   ResizableArray<AttributeValue> leftBind = new ResizableArray<AttributeValue>();
   if (attrs != null) {
     for (AttributeValue p : attrs) {
       if (p.canApply(dt, attrApplied)) {
         if (p.getAttribute().getType().getOrder() > -1)
           leftBind.set(p.getAttribute().getType().getOrder(), p);
         attrApplied.add(p);
       }
     }
   }
   buf.append(dt.getType(leftBind));
   for (AttributeValue av : attrApplied) {
     if (av.isOrdered()) continue;
     buf.append(" ").append(av.get());
   }
   return new ColumnDecl(dt, attrApplied, buf.toString());
 }
  @Override
  public DataType visitAtom(@NotNull PythonParser.AtomContext ctx) {
    //        System.out.println("Zz" + ctx.NAME() + ctx.STRING());
    if (ctx.test() != null) { // 1
      return visitTest(ctx.test());
    } else if (ctx.STRING().size() > 0) { // 7,8,9
      String ss = "";
      for (TerminalNode s : ctx.STRING()) {
        ss += s.getText();
      }
      mv.visitLdcInsn(ss.length());
      mv.visitIntInsn(NEWARRAY, T_CHAR);
      int tempIndex = 0;
      for (int i = 0; i < ss.length(); i++) {
        if (!("\"".equals(ss.substring(i, i + 1))) && !("\'".equals(ss.substring(i, i + 1)))) {
          mv.visitInsn(DUP);
          mv.visitLdcInsn(tempIndex);
          mv.visitLdcInsn(ss.charAt(i));
          mv.visitInsn(CASTORE);
          tempIndex++;
        }
      }
      if (ctx.LBRACK() != null) {
        if (Integer.parseInt(ctx.INT().getText()) > tempIndex - 1) {
          throw new CompileException(
              String.format(
                  "Index out of bounds exception for string %s with index %s",
                  ss, Integer.parseInt(ctx.INT().getText())));
        }
        mv.visitLdcInsn(Integer.parseInt(ctx.INT().getText()));
        mv.visitInsn(CALOAD);
        mv.visitMethodInsn(
            INVOKESTATIC,
            Type.getType(String.class).getInternalName(),
            "valueOf",
            "(C)Ljava/lang/String;",
            false);
        return new StringType(1);
      } else {
        mv.visitMethodInsn(
            INVOKESTATIC,
            Type.getType(String.class).getInternalName(),
            "valueOf",
            "([C)Ljava/lang/String;",
            false);
        return new StringType(ss.length());
      }
    } else if (!ctx.NAME().isEmpty()) { // 3,4,5
      DataType type = null;
      //            System.out.println("GG" + ctx.getText() + "GG");
      String varName = ctx.NAME().get(0).getText();
      if (scope.isLocalVariable(varName)) {
        if (ctx.LBRACK() != null) { // 4,5
          type = scope.getLocalVariableType(varName);
          if (!(type instanceof StringType)) {
            throw new CompileException(String.format("Trying to take index not from string!!"));
          }
          mv.visitVarInsn(type.isPrimitive() ? ILOAD : ALOAD, scope.getLocalVariableIndex(varName));
          if (ctx.INT() != null) {
            mv.visitMethodInsn(
                INVOKEVIRTUAL,
                Type.getType(String.class).getInternalName(),
                "toCharArray",
                "()[C",
                false);
            if (Integer.parseInt(ctx.INT().getText()) > type.getSize()) {
              throw new CompileException(String.format("Index out of bounds exception!"));
            }
            mv.visitLdcInsn(Integer.parseInt(ctx.INT().getText()));
            mv.visitInsn(CALOAD);
            mv.visitMethodInsn(
                INVOKESTATIC,
                Type.getType(String.class).getInternalName(),
                "valueOf",
                "(C)Ljava/lang/String;",
                false);
            return new StringType(1);
          } else if (ctx.NAME(1) != null) {
            varName = ctx.NAME(1).getText();
            type = scope.getLocalVariableType(ctx.NAME(1).getText());
            if (!type.isInteger()) {
              throw new CompileException(
                  String.format("Trying to take index from string not with int!"));
            } else {
              Label okLabel = new Label();
              mv.visitMethodInsn(
                  INVOKEVIRTUAL,
                  Type.getType(String.class).getInternalName(),
                  "toCharArray",
                  "()[C",
                  false);
              mv.visitVarInsn(ILOAD, scope.getLocalVariableIndex(ctx.NAME(1).getText()));
              mv.visitInsn(DUP);
              mv.visitLdcInsn(type.getSize());
              mv.visitInsn(SWAP);
              mv.visitJumpInsn(IF_ICMPGT, okLabel);
              mv.visitLabel(okLabel);
              mv.visitInsn(CALOAD);
              mv.visitMethodInsn(
                  INVOKESTATIC,
                  Type.getType(String.class).getInternalName(),
                  "valueOf",
                  "(C)Ljava/lang/String;",
                  false);
              // TODO out of bounds a[b]
              //                            mv.visitLdcInsn("Out of bounds!");
              //                            mv.visitMethodInsn(INVOKESPECIAL,
              //                                    "java/lang/CompilerException", "<init>",
              // "(Ljava/lang/String;)V", false);
              return new StringType(1);
            }

          } else {
            throw new CompileException(String.format("Unidentified operation"));
          }
        } else { // 3
          type = scope.getLocalVariableType(varName);
          mv.visitVarInsn(type.isPrimitive() ? ILOAD : ALOAD, scope.getLocalVariableIndex(varName));
        }
      } else if (scope.isGlobalVariable(varName)) {
        if (ctx.LBRACK() != null) { // 4,5
          type = scope.getGlobalVariableType(varName);
          if (!(type instanceof StringType)) {
            throw new CompileException(String.format("Trying to take index not from string!!"));
          }
          mv.visitFieldInsn(
              GETSTATIC, scope.getClassName(), varName, type.getType().getDescriptor());
          if (ctx.INT() != null) {
            mv.visitMethodInsn(
                INVOKEVIRTUAL,
                Type.getType(String.class).getInternalName(),
                "toCharArray",
                "()[C",
                false);
            if (Integer.parseInt(ctx.INT().getText()) > type.getSize()) {
              throw new CompileException(String.format("Index out of bounds exception!"));
            }
            mv.visitLdcInsn(Integer.parseInt(ctx.INT().getText()));
            mv.visitInsn(CALOAD);
            mv.visitMethodInsn(
                INVOKESTATIC,
                Type.getType(String.class).getInternalName(),
                "valueOf",
                "(C)Ljava/lang/String;",
                false);
            return new StringType(1);
          } else if (ctx.NAME(1) != null) {
            varName = ctx.NAME(1).getText();
            type = scope.getLocalVariableType(ctx.NAME(1).getText());
            if (!type.isInteger()) {
              throw new CompileException(
                  String.format("Trying to take index from string not with int!"));
            } else {
              Label okLabel = new Label();
              mv.visitMethodInsn(
                  INVOKEVIRTUAL,
                  Type.getType(String.class).getInternalName(),
                  "toCharArray",
                  "()[C",
                  false);
              mv.visitVarInsn(ILOAD, scope.getLocalVariableIndex(ctx.NAME(1).getText()));
              mv.visitInsn(DUP);
              mv.visitLdcInsn(type.getSize());
              mv.visitInsn(SWAP);
              mv.visitJumpInsn(IF_ICMPGT, okLabel);
              mv.visitLabel(okLabel);
              mv.visitInsn(CALOAD);
              mv.visitMethodInsn(
                  INVOKESTATIC,
                  Type.getType(String.class).getInternalName(),
                  "valueOf",
                  "(C)Ljava/lang/String;",
                  false);
              // TODO out of bounds a[b]
              //                            mv.visitLdcInsn("Out of bounds!");
              //                            mv.visitMethodInsn(INVOKESPECIAL,
              //                                    "java/lang/CompilerException", "<init>",
              // "(Ljava/lang/String;)V", false);
              return new StringType(1);
            }

          } else {
            throw new CompileException(String.format("Unidentified operation"));
          }
        } else { // 3
          type = scope.getGlobalVariableType(varName);
          mv.visitFieldInsn(
              GETSTATIC, scope.getClassName(), varName, type.getType().getDescriptor());
        }
      }
      return type;
      //			} else if (scope.isFunction)
    } else if (ctx.INT() != null) { // 6
      mv.visitLdcInsn(Integer.parseInt(ctx.INT().getText()));
      return PrimitiveType.INTEGER;
    } else if (ctx.BOOL() != null) { // 2
      mv.visitInsn("False".equals(ctx.BOOL().getText()) ? ICONST_0 : ICONST_1);
      return PrimitiveType.BOOLEAN;
    } else {
      throw new CompileException(String.format("Undefined thing to work with! %s", ctx.getText()));
    }
  }
  @Override
  public DataType visitPower(@NotNull PythonParser.PowerContext ctx) {
    //        System.out.println("called!");

    if (ctx.trailer() != null)
      if (ctx.atom().NAME() != null) {
        String name = ctx.atom().NAME(0).getText();
        Integer argumentGiven = 0;
        String returnSignatue = "V";
        String argSignature = "";
        String totalSinature = "";

        if (ctx.trailer().arglist() != null) {
          argumentGiven = ctx.trailer().arglist().argument().size();
        }
        if (!scope.isFunctionDeclared(name))
          throw new CompileException(String.format("Function %s is not defined.", name));
        else {
          Integer argumentsCount = scope.functionArgumentCount(name);
          if (!argumentsCount.equals(argumentGiven)) {
            throw new CompileException(
                String.format(
                    "Function %s takes exactly %s arguments (%s given)",
                    name, argumentsCount, argumentGiven));
          } else {
            if (argumentGiven == 0) {
              argSignature = "";
            } else {
              int i = 0;
              String[] argumentsNames = scope.getFunctionArgumentsNames(name, argumentGiven);
              for (PythonParser.ArgumentContext argctx : ctx.trailer().arglist().argument()) {
                DataType argType = visitArgument(argctx);
                argSignature += argType.getType().getDescriptor();
                // System.out.println("Хуйня тут" + argType + argumentsNames[i]);
                scope.addLocalVariable(argumentsNames[i], argType);
                //                                int opcode =
                // scope.getLocalVariableType(argumentsNames[i]).isPrimitive() ? ISTORE : ASTORE;
                //                                mv.visitVarInsn(opcode,
                // scope.getLocalVariableIndex(argumentsNames[i]));
                i++;
              }
            }

            totalSinature = "(" + argSignature + ")" + returnSignatue;
            System.out.println("ACTION!" + totalSinature);
            MethodVisitor mvSave = mv;
            mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, name, totalSinature, null, null);
            visitSuite(scope.getFunctionSuite(name, argumentGiven));
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
            scope.setMethodName(name);
            scope.refreshLocalVariables();
            mv = mvSave;
            mv.visitMethodInsn(INVOKESTATIC, scope.getClassName(), name, totalSinature, false);
          }
        }

        //                //TODO function with () call {a()} + default values
        //                DataType returnType = scope.getFunctionReturnType(name, argumentType);
        //                mv.visitMethodInsn(INVOKESTATIC, scope.getClassName(), name,
        // Utils.getFunctionDescriptor(returnType, argumentType), false);
        //                System.out.println("called!");
        return null;

      } else {
        throw new CompileException(
            String.format("%s object is not callable!", ctx.atom().getText()));
      }
    return visitAtom(ctx.atom());
  }
 @Override
 public DataType visitExpr_stmt(@NotNull PythonParser.Expr_stmtContext ctx) {
   if (ctx.power() != null) {
     if (ctx.power().atom().NAME() != null && ctx.power().atom().STRING().size() == 0) {
       String var = ctx.power().atom().NAME().get(0).getText();
       if (ctx.power().atom().LBRACK() != null) {
         if (scope.isLocalVariable(var)) {
           if (!(scope.getLocalVariableType(var) instanceof StringType)) {
             throw new CompileException(String.format("Trying to take index not from string!"));
           }
         } else if (scope.isGlobalVariable(var)) {
           if (!(scope.getGlobalVariableType(var) instanceof StringType)) {
             throw new CompileException(String.format("Trying to take index not from string!"));
           }
         }
         throw new CompileException(
             String.format("Assigning to element of String is not implemented yet!"));
         //                    if (ctx.power().atom().INT() != null) {
         //
         //                    } else {
         //
         //                    }
       } else {
         if (ctx.test() != null) {
           if (ctx.power().trailer() != null) {
             throw new CompileException(String.format("Can't assign to function!"));
           }
           DataType etype = visitTest(ctx.test());
           if (scope.isLocalVariable(var)) {
             //                System.out.println("LOCAL" + var + "e" + etype +  "e" +
             // scope.getLocalVariableType(var) );
             if (etype.equals(scope.getLocalVariableType(var))) {
               //                    System.out.println("Type not changing!");
               int opcode = scope.getLocalVariableType(var).isPrimitive() ? ISTORE : ASTORE;
               mv.visitVarInsn(opcode, scope.getLocalVariableIndex(var));
             } else {
               //                    System.out.println("Type changing!");
               scope.changeLocalVariableType(var, etype);
               int opcode = etype.isPrimitive() ? ISTORE : ASTORE;
               mv.visitVarInsn(opcode, scope.getLocalVariableIndex(var));
             }
           } else if (scope.isGlobalVariable(var)) {
             DataType vtype = scope.getGlobalVariableType(var);
             mv.visitFieldInsn(
                 PUTSTATIC, scope.getClassName(), var, vtype.getType().getDescriptor());
             //                        } else if (scope.isFunctionDeclared(var)) {    TODO
           } else {
             scope.addLocalVariable(var, etype);
             int opcode = scope.getLocalVariableType(var).isPrimitive() ? ISTORE : ASTORE;
             mv.visitVarInsn(opcode, scope.getLocalVariableIndex(var));
             // throw new CompileException(String.format("Variable %s not found in context %s.",
             // var, ctx.getText()));
           }
           return etype;
         } else if (ctx.power().trailer() != null) { // is function
           visitPower(ctx.power());
           //                        if
           // (!scope.isFunctionDeclared(ctx.power().atom().NAME(0).getText(),
           // ctx.power().trailer().arglist().argument().size()))
           //                            throw new CompileException(String.format("Function %s not
           // defined.", var));
           //                        else
           //                            System.out.println("ACTION!");
         } else if (scope.isLocalVariable(var)) {
           return scope.getLocalVariableType(var);
         } else if (scope.isGlobalVariable(var)) {
           return scope.getGlobalVariableType(var);
         } else throw new CompileException(String.format("Variable %s not defined.", var));
       }
     } else {
       throw new CompileException(String.format("can't assign to operator"));
     }
   } else {
     System.out.println("Something bad happened!");
   }
   return null;
 }
Beispiel #10
0
  private boolean compareData(String name, Array data1, Array data2, double tol, boolean justOne) {
    boolean ok = true;
    if (data1.getSize() != data2.getSize()) {
      f.format(" DIFF %s: size %d !== %d%n", name, data1.getSize(), data2.getSize());
      ok = false;
    }

    if (data1.getElementType() != data2.getElementType()) {
      f.format(
          " DIFF %s: element type %s !== %s%n",
          name, data1.getElementType(), data2.getElementType());
      ok = false;
    }

    if (!ok) return false;

    DataType dt = DataType.getType(data1.getElementType());

    IndexIterator iter1 = data1.getIndexIterator();
    IndexIterator iter2 = data2.getIndexIterator();

    if (dt == DataType.DOUBLE) {
      while (iter1.hasNext() && iter2.hasNext()) {
        double v1 = iter1.getDoubleNext();
        double v2 = iter2.getDoubleNext();
        if (!Double.isNaN(v1) || !Double.isNaN(v2))
          if (!closeEnough(v1, v2, tol)) {
            f.format(
                " DIFF %s: %f != %f count=%s diff = %f pdiff = %f %n",
                name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2));
            ok = false;
            if (justOne) break;
          }
      }
    } else if (dt == DataType.FLOAT) {
      while (iter1.hasNext() && iter2.hasNext()) {
        float v1 = iter1.getFloatNext();
        float v2 = iter2.getFloatNext();
        if (!Float.isNaN(v1) || !Float.isNaN(v2))
          if (!closeEnough(v1, v2, (float) tol)) {
            f.format(
                " DIFF %s: %f != %f count=%s diff = %f pdiff = %f %n",
                name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2));
            ok = false;
            if (justOne) break;
          }
      }
    } else if (dt == DataType.INT) {
      while (iter1.hasNext() && iter2.hasNext()) {
        int v1 = iter1.getIntNext();
        int v2 = iter2.getIntNext();
        if (v1 != v2) {
          f.format(
              " DIFF %s: %d != %d count=%s diff = %f pdiff = %f %n",
              name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2));
          ok = false;
          if (justOne) break;
        }
      }
    } else if (dt == DataType.SHORT) {
      while (iter1.hasNext() && iter2.hasNext()) {
        short v1 = iter1.getShortNext();
        short v2 = iter2.getShortNext();
        if (v1 != v2) {
          f.format(
              " DIFF %s: %d != %d count=%s diff = %f pdiff = %f %n",
              name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2));
          ok = false;
          if (justOne) break;
        }
      }
    } else if (dt == DataType.BYTE) {
      while (iter1.hasNext() && iter2.hasNext()) {
        byte v1 = iter1.getByteNext();
        byte v2 = iter2.getByteNext();
        if (v1 != v2) {
          f.format(
              " DIFF %s: %d != %d count=%s diff = %f pdiff = %f %n",
              name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2));
          ok = false;
          if (justOne) break;
        }
      }
    } else if (dt == DataType.STRUCTURE) {
      while (iter1.hasNext() && iter2.hasNext()) {
        compareStructureData(
            (StructureData) iter1.next(), (StructureData) iter2.next(), tol, justOne);
      }
    }

    return ok;
  }