コード例 #1
0
 @Override
 public DataType visitFuncdef(@NotNull PythonParser.FuncdefContext ctx) {
   String name = ctx.NAME().getText();
   scope.setMethodName(name);
   String[] argumentNames = visitParameters(ctx.parameters());
   Integer numberParms = argumentNames.length;
   //        System.out.println(name + numberParms);
   scope.declareFunction(name, numberParms);
   scope.addFunctionSuite(name, numberParms, ctx.suite());
   scope.addFunctionArgumentsNames(name, numberParms, argumentNames);
   //        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, name, "()V", null, null);//TODO to
   // function call
   //        visitSuite(ctx.suite());
   //        mv.visitMaxs(0, 0);
   //        mv.visitEnd();
   //        scope.setMethodName(name);
   //        scope.refreshLocalVariables();
   return null;
 }
コード例 #2
0
  @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());
  }