Package weasel.compiler

Examples of weasel.compiler.WeaselInstructionList


    return s + "}";
  }

  public WeaselInstructionList compile(WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelper, WeaselGenericClass arrayClass) throws WeaselCompilerException {
    arrayClass = new WeaselGenericClass(arrayClass.getBaseClass().getArrayClass(), arrayClass.getGenerics());
    WeaselInstructionList instructions = new WeaselInstructionList();
    instructions.add(token.line, new WeaselInstructionLoadConstInteger(indexes.size()));
    instructions.add(token.line, new WeaselInstructionNewArray(arrayClass.getBaseClass().getByteName(), 1));
    for(int i=0; i<indexes.size(); i++){
      Object o = indexes.get(i);
      if(o instanceof WeaselArrayInit){
        instructions.addAll(((WeaselArrayInit)o).compile(compiler, compilerHelper, arrayClass));
      }else{
        WeaselCompilerReturn wcr = ((WeaselTree)o).compile(compiler, compilerHelper, null, arrayClass, null, false);
        if(!wcr.getReturnType().canCastTo(arrayClass))
          throw new WeaselCompilerException(token.line, "Can't cast %s to %s", wcr.getReturnType(), arrayClass);
        instructions.addAll(wcr.getInstructions());
      }
      instructions.add(token.line, new WeaselInstructionPush(2));
      instructions.add(token.line, new WeaselInstructionLoadConstInteger(i));
      instructions.add(token.line, new WeaselInstructionWriteIndex(WeaselPrimitive.getPrimitiveID(arrayClass.getBaseClass())));
    }
    return instructions;
  }
View Full Code Here


    return new WeaselTreeAddResult(wtl);
  }
 
  @Override
  public WeaselCompilerReturn compile(WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelper, WeaselGenericClass write, WeaselGenericClass expect, WeaselGenericClass elementParent, boolean isVariable) throws WeaselCompilerException {
    WeaselInstructionList instructions = new WeaselInstructionList();
    if(newClass!=null){
     
      WeaselClass weaselClass = compiler.getWeaselClass(WeaselClass.mapClassNames(newClass));
      //TODO
      WeaselGenericClass genericClass = new WeaselGenericClass(weaselClass);
     
      if(arraySize==null){
       
        instructions.add(token.line, new WeaselInstructionNew(weaselClass.getByteName()));
       
        instructions.add(token.line, new WeaselInstructionPush(1));
       
        instructions.add(token.line, new WeaselInstructionInvoke(weaselClass.getRealName()+".<preInit>()"));
       
        List<WeaselGenericMethod2> methods = genericClass.getGenericMethods("<init>", true);
       
        instructions.add(token.line, new WeaselInstructionPush(1));
       
        WeaselParameterCompileReturn wcr = WeaselTree.compileParamList(token.line, newClass, compiler, compilerHelper, func, methods);
       
        instructions.addAll(wcr.instructions);
       
        instructions.add(token.line, new WeaselInstructionInvoke(wcr.method.getMethod().getMethod().getClassNameAndDesk()));
       
      }else{
       
        String className = weaselClass.getByteName();
        for(int i=0; i<arraySize.size(); i++){
          className = "["+className;
        }
       
        genericClass = new WeaselGenericClass(compiler.getWeaselClass(className), genericClass.getGenerics());
       
        if(arrayInit==null){
         
          className = weaselClass.getByteName();
         
          int sizes = 0;
          for(WeaselTree tree:arraySize){
            if(tree==null){
              className = "["+className;
            }else{
              WeaselCompilerReturn wcr = tree.compile(compiler, compilerHelper, null, new WeaselGenericClass(compiler.baseTypes.intClass), null, false);
              instructions.addAll(wcr.getInstructions(compiler, compiler.baseTypes.intClass));
              sizes++;
            }
          }
         
          instructions.add(token.line, new WeaselInstructionNewArray(className, sizes));
         
        }else{
          instructions.addAll(arrayInit.compile(compiler, compilerHelper, genericClass));
        }
       
      }
     
      return new WeaselCompilerReturnInstructionList(instructions, genericClass);
    }else if(isFunc){
      List<WeaselGenericMethod2> methods;
      if(elementParent==null){
        methods = compilerHelper.getGenericMethods((String)token.param);
      }else{
        compiler.compileEasy(elementParent.getBaseClass());
        methods = elementParent.getGenericMethods((String)token.param, isVariable);
      }
      if(methods.isEmpty()){
        throw new WeaselCompilerException(token.line, "Method not found %s", token);
      }
     
      WeaselParameterCompileReturn wcr = WeaselTree.compileParamList(token.line, (String)token.param, compiler, compilerHelper, func, methods);
     
      if(elementParent==null){
        if(!WeaselModifier.isStatic(wcr.method.getMethod().getMethod().getModifier())){
          instructions.add(token.line, new WeaselInstructionLoadVariable(compilerHelper.getVariable("this").pos));
        }
      }
     
      instructions.addAll(wcr.instructions);
     
      if(elementParent==null){
        if(WeaselModifier.isStatic(wcr.method.getMethod().getMethod().getModifier())){
          instructions.add(token.line, new WeaselInstructionInvokeStatic(wcr.method.getMethod().getMethod().getNameAndDesk()));
        }else{
          instructions.add(token.line, new WeaselInstructionInvoke(wcr.method.getMethod().getMethod().getClassNameAndDesk()));
        }
      }else{
        if(isVariable){
          instructions.add(token.line, new WeaselInstructionInvoke(wcr.method.getMethod().getMethod().getClassNameAndDesk()));
        }else{
          instructions.add(token.line, new WeaselInstructionInvokeStatic(wcr.method.getMethod().getMethod().getNameAndDesk()));
        }
      }
     
      return new WeaselCompilerReturnInstructionList(instructions, wcr.method.getGenericReturn());
    }else if(isIndex){
      WeaselGenericClass arrayClass;
      String variable = (String)token.param;
      if(elementParent==null){
        WeaselVariableInfo wvi = compilerHelper.getVariable(variable);
        if(wvi==null){
          WeaselGenericField wf = compilerHelper.getGenericField(variable);
          if(wf==null){
            WeaselClass weaselClass;
            try{
              weaselClass = compiler.getWeaselClass(WeaselClass.mapClassNames(variable));
            }catch(WeaselNativeException e){
              throw new WeaselCompilerException(token.line, "Variable not declared bevore %s", variable);
            }
            return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(weaselClass), true);
          }
          if(WeaselModifier.isStatic(compilerHelper.getCompilingMethod().getMethod().getMethod().getModifier())){
            if(!WeaselModifier.isStatic(wf.getField().getModifier())){
              throw new WeaselCompilerException(token.line, "Variable %s is not static", variable);
            }
          }
          if(WeaselModifier.isStatic(wf.getField().getModifier())){
            instructions.add(token.line, new WeaselInstructionReadStaticField(wf.getField().getDesk()));
          }else{
            wvi = compilerHelper.getVariable("this");
            instructions.add(token.line, new WeaselInstructionReadFieldOf(wvi.pos, wf.getField().getDesk()));
          }
          arrayClass = wf.getGenericType();
        }else{
          arrayClass = wvi.type;
          instructions.add(token.line, new WeaselInstructionLoadVariable(wvi.pos));
        }
      }else{
        WeaselGenericField field = elementParent.getGenericField((String)token.param);
        if(field==null)
          throw new WeaselCompilerException(token.line, "Variable %s not declared in %s", token, elementParent);
        arrayClass = field.getGenericType();
        instructions.add(token.line, new WeaselInstructionReadField(field.getField().getDesk()));
      }
      if(!arrayClass.getBaseClass().isArray()){
        throw new WeaselCompilerException(token.line, "%s is not an array", arrayClass);
      }
      WeaselCompilerReturn wcr = func.compile(compiler, compilerHelper, null, new WeaselGenericClass(compiler.baseTypes.intClass), null, false);
      instructions.addAll(wcr.getInstructions(compiler, compiler.baseTypes.intClass));
      if(write==null){
        instructions.add(token.line, new WeaselInstructionReadIndex(WeaselPrimitive.getPrimitiveID(arrayClass.getBaseClass().getArrayClass())));
      }else{
        instructions.add(token.line, new WeaselInstructionPlaceHolder());
        WeaselTree.autoCast(compiler, write, new WeaselGenericClass(arrayClass.getBaseClass().getArrayClass(), arrayClass.getGenerics()), token.line, instructions, true);
        instructions.add(token.line, new WeaselInstructionWriteIndex(WeaselPrimitive.getPrimitiveID(arrayClass.getBaseClass().getArrayClass())));
      }
      return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(arrayClass.getBaseClass().getArrayClass(), arrayClass.getGenerics()));
    }else if(token==null){
      return tree.compile(compiler, compilerHelper, write, expect, elementParent, isVariable);
    }else{
      switch(token.tokenType){
      case BOOL:
      case DOUBLE:
      case INTEGER:
      case NULL:
      case STRING:
        if(write!=null){
          throw new WeaselCompilerException(token.line, "Can't write %s to constant %s", write, token);
        }
        return new WeaselCompilerReturnConstant(compiler, token.line, token.param);
      case KEYWORD:
        if(token.param!=WeaselKeyWord.THIS)
          throw new WeaselCompilerException(token.line, "Expect ident but got %s", token);
      case IDENT:
        String variable = token.toString();
        if(elementParent==null){
          WeaselVariableInfo wvi = compilerHelper.getVariable(variable);
          if(wvi==null){
            WeaselGenericField wf = compilerHelper.getGenericField(variable);
            if(wf==null){
              WeaselClass weaselClass;
              try{
                weaselClass = compiler.getWeaselClass(WeaselClass.mapClassNames(variable));
              }catch(WeaselNativeException e){
                throw new WeaselCompilerException(token.line, "Variable not declared bevore %s", variable);
              }
              return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(weaselClass), true);
            }
            if(WeaselModifier.isStatic(compilerHelper.getCompilingMethod().getMethod().getMethod().getModifier())){
              if(!WeaselModifier.isStatic(wf.getField().getModifier())){
                throw new WeaselCompilerException(token.line, "Variable %s is not static", variable);
              }
            }
            if(WeaselModifier.isStatic(wf.getField().getModifier())){
              if(write==null){
                instructions.add(token.line, new WeaselInstructionReadStaticField(wf.getField().getDesk()));
              }else{
                instructions.add(token.line, new WeaselInstructionPlaceHolder());
                WeaselTree.autoCast(compiler, write, wf.getGenericType(), token.line, instructions, true);
                instructions.add(token.line, new WeaselInstructionWriteStaticField(wf.getField().getDesk()));
              }
            }else{
              wvi = compilerHelper.getVariable("this");
              if(write==null){
                instructions.add(token.line, new WeaselInstructionReadFieldOf(wvi.pos, wf.getField().getDesk()));
              }else{
                instructions.add(token.line, new WeaselInstructionPlaceHolder());
                WeaselTree.autoCast(compiler, write, wf.getGenericType(), token.line, instructions, true);
                instructions.add(token.line, new WeaselInstructionWriteFieldOf(wvi.pos, wf.getField().getDesk()));
              }
            }
            return new WeaselCompilerReturnInstructionList(instructions, wf.getGenericType());
          }else{
            if(write==null){
              instructions.add(token.line, new WeaselInstructionLoadVariable(wvi.pos));
            }else{
              instructions.add(token.line, new WeaselInstructionPlaceHolder());
              WeaselTree.autoCast(compiler, write, wvi.type, token.line, instructions, true);
              instructions.add(token.line, new WeaselInstructionSaveVariable(wvi.pos));
            }
            return new WeaselCompilerReturnInstructionList(instructions, wvi.type);
          }
        }else{
          WeaselGenericField wf = elementParent.getGenericField(variable);
          if(wf==null){
            WeaselClass weaselClass;
            try{
              weaselClass = compiler.getWeaselClass(WeaselClass.mapClassNames(variable));
            }catch(WeaselNativeException e){
              throw new WeaselCompilerException(token.line, "Variable not declared bevore %s", variable);
            }
            if(isVariable){
              throw new WeaselCompilerException(token.line, "Can't get class form variable", variable);
            }
            return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(weaselClass), true);
          }
          if(isVariable){
            if(write==null){
              instructions.add(token.line, new WeaselInstructionReadField(wf.getField().getDesk()));
            }else{
              instructions.add(token.line, new WeaselInstructionPlaceHolder());
              WeaselTree.autoCast(compiler, write, wf.getGenericType(), token.line, instructions, true);
              instructions.add(token.line, new WeaselInstructionWriteField(wf.getField().getDesk()));
            }
          }else{
            if(!WeaselModifier.isStatic(wf.getField().getModifier()))
              throw new WeaselCompilerException(token.line, "Filed %s isn't static", wf);
            if(write==null){
              instructions.add(token.line, new WeaselInstructionReadStaticField(wf.getField().getDesk()));
            }else{
              instructions.add(token.line, new WeaselInstructionPlaceHolder());
              WeaselTree.autoCast(compiler, write, wf.getGenericType(), token.line, instructions, true);
              instructions.add(token.line, new WeaselInstructionWriteStaticField(wf.getField().getDesk()));
            }
          }
          return new WeaselCompilerReturnInstructionList(instructions, wf.getGenericType());
        }
      case CHAR:
View Full Code Here

    WeaselGenericClass elementParent, boolean isVariable, int i) throws WeaselCompilerException {
    if(i==-1 || i==operators.size())
      return level.get(0).compile(compiler, compilerHelper, null, expect, elementParent, isVariable);
    WeaselToken operator = operators.get(i);
    Properties oper = (Properties)operator.param;
    WeaselInstructionList instructions;
    WeaselCompilerReturn wcr;
    WeaselGenericClass ret;
    if(oper==WeaselOperator.INSTANCEOF){
      WeaselInstanceofToken wit = (WeaselInstanceofToken) operator;
      WeaselClass wc = compiler.getWeaselClass("O"+wit.className+";");
      WeaselGenericClass wgc = new WeaselGenericClass(wc);
      wcr = compileOperator(compiler, compilerHelper, write, wgc, elementParent, isVariable, i-1);
      if(wcr.getReturnType().canCastTo(wgc)){
        instructions = new WeaselInstructionList();
        instructions.add(operator.line, new WeaselInstructionLoadConstBoolean(true));
      }else{
        if(wcr.getReturnType().getBaseClass().isPrimitive())
          throw new WeaselCompilerException(operator.line, "can't use implements for primitives");
        instructions = wcr.getInstructions();
        instructions.add(operator.line, new WeaselInstructionInstanceof(wc.getByteName()));
      }
      ret = new WeaselGenericClass(compiler.baseTypes.booleanClass);
    }else if(oper==WeaselOperator.CAST){
      WeaselCastToken wct = (WeaselCastToken) operator;
      WeaselClass wc = compiler.getWeaselClass(WeaselClass.mapClassNames(wct.className));
      ret = new WeaselGenericClass(wc);
      wcr = compileOperator(compiler, compilerHelper, write, ret, elementParent, isVariable, i+1);
      instructions = wcr.getInstructions();
      if(wc.isPrimitive()){
        instructions.add(operator.line, new WeaselInstructionCastPrimitive(WeaselPrimitive.getPrimitiveID(wc)));
      }else{
        instructions.add(operator.line, new WeaselInstructionCast(wc.getByteName()));
      }
    }else if(oper==WeaselOperator.MINUS_PREFIX || oper==WeaselOperator.PLUS_PREFIX){
      wcr = compileOperator(compiler, compilerHelper, null, null, null, false, i+1);
      instructions = wcr.getInstructions();
      ret = wcr.getReturnType();
      int primitiveID = WeaselPrimitive.getPrimitiveID(ret.getBaseClass());
      if(oper==WeaselOperator.MINUS_PREFIX || oper==WeaselOperator.PLUS_PREFIX){
        if(primitiveID!=WeaselPrimitive.CHAR && primitiveID!=WeaselPrimitive.BYTE && primitiveID!=WeaselPrimitive.SHORT
            && primitiveID!=WeaselPrimitive.INT && primitiveID!=WeaselPrimitive.LONG
             && primitiveID!=WeaselPrimitive.FLOAT && primitiveID!=WeaselPrimitive.DOUBLE){
          throw new WeaselCompilerException(operator.line, "Operator %s is only usable with char, byte, short, int, long, float, double, not with %s", oper, ret);
        }
      }
      if(oper==WeaselOperator.MINUS_PREFIX){
        instructions.add(operator.line, new WeaselInstructionNeg(primitiveID));
      }
    }else{
      throw new WeaselCompilerException(operator.line, "Unknown operator %s", operator);
    }
    return new WeaselCompilerReturnInstructionList(instructions, ret);
View Full Code Here

      return level.get(0).compile(compiler, compilerHelper, write, expect, elementParent, isVariable);
    if(i==operators.size())
      return level.get(operators.size()).compile(compiler, compilerHelper, write, expect, elementParent, isVariable);
    WeaselToken operator = operators.get(i);
    Properties oper = (Properties)operator.param;
    WeaselInstructionList instructions = new WeaselInstructionList();
    WeaselCompilerReturn wcr;
    WeaselGenericClass ret;
    WeaselGenericClass wgc;
    if(oper==WeaselOperator.COMMA){
      wcr = level.get(i).compile(compiler, compilerHelper, null, expect, null, false);
      instructions.addAll(wcr.getInstructions());
      ret = wcr.getReturnType();
      wcr = compileInfixOperator(compiler, compilerHelper, null, new WeaselGenericClass(compiler.baseTypes.voidClass), null, false, i+1);
      instructions.addAll(wcr.getInstructions());
      if(wcr.getReturnType().getBaseClass()!=compiler.baseTypes.voidClass){
        instructions.add(operator.line, new WeaselInstructionPop());
      }
    }else if(oper==WeaselOperator.ASSIGN){
      WeaselCompilerReturn wcr2 = compileInfixOperator(compiler, compilerHelper, null, expect, null, false, i+1);
      wcr = level.get(i).compile(compiler, compilerHelper, wcr2.getReturnType(), expect, null, false);
      instructions.addAll(wcr.getInstructions());
      instructions.replacePlaceHolderWith(wcr2.getInstructions());
      ret = wcr.getReturnType();
    }else if(oper==WeaselOperator.LOGICAL_OR || oper==WeaselOperator.LOGICAL_AND || oper==WeaselOperator.BITWISE_OR || oper==WeaselOperator.BITWISE_AND
         || oper==WeaselOperator.BITWISE_XOR || oper==WeaselOperator.LESS || oper==WeaselOperator.GREATER
         || oper==WeaselOperator.LESS_EQUAL || oper==WeaselOperator.GREATER_EQUAL
         || oper==WeaselOperator.PLUS || oper==WeaselOperator.MINUS || oper==WeaselOperator.TIMES || oper==WeaselOperator.DIVIDE
         || oper==WeaselOperator.REMAINDER){
      wcr = compileInfixOperator(compiler, compilerHelper, null, expect, null, false, i-1);
      instructions.addAll(wcr.getInstructions());
      wgc = wcr.getReturnType();
      wcr = level.get(i+1).compile(compiler, compilerHelper, null, expect, null, false);
      ret = wcr.getReturnType();
      wgc = WeaselTree.autoCast(compiler, wgc, ret, operator.line, instructions, false);
      instructions.addAll(wcr.getInstructions());
      ret = WeaselTree.autoCast(compiler, ret, wgc, operator.line, instructions, true);
      ret = WeaselGenericClass.getSmallestSame(wgc, ret);
      int primitiveID = WeaselPrimitive.getPrimitiveID(ret.getBaseClass());
      if(oper==WeaselOperator.LOGICAL_OR||oper==WeaselOperator.LOGICAL_AND){
        if(primitiveID!=WeaselPrimitive.BOOLEAN){
          throw new WeaselCompilerException(operator.line, "Operator %s is only usable with boolean, not with %s %s", oper, wgc, ret);
        }
      }else if(oper==WeaselOperator.BITWISE_OR || oper==WeaselOperator.BITWISE_AND || oper==WeaselOperator.BITWISE_XOR){
        if(primitiveID!=WeaselPrimitive.BOOLEAN && primitiveID!=WeaselPrimitive.CHAR && primitiveID!=WeaselPrimitive.BYTE &&
            primitiveID!=WeaselPrimitive.SHORT && primitiveID!=WeaselPrimitive.INT && primitiveID!=WeaselPrimitive.LONG){
          throw new WeaselCompilerException(operator.line, "Operator %s is only usable with boolean, char, byte, short, int, long, not with %s %s", oper, wgc, ret);
        }
      }else if(oper==WeaselOperator.LESS || oper==WeaselOperator.GREATER || oper==WeaselOperator.LESS_EQUAL
           || oper==WeaselOperator.GREATER_EQUAL){
        if(primitiveID!=WeaselPrimitive.CHAR && primitiveID!=WeaselPrimitive.BYTE && primitiveID!=WeaselPrimitive.SHORT
            && primitiveID!=WeaselPrimitive.INT && primitiveID!=WeaselPrimitive.LONG
             && primitiveID!=WeaselPrimitive.FLOAT && primitiveID!=WeaselPrimitive.DOUBLE){
          throw new WeaselCompilerException(operator.line, "Operator %s is only usable with char, byte, short, int, long, float, double, not with %s %s", oper, wgc, ret);
        }
        ret = new WeaselGenericClass(compiler.baseTypes.booleanClass);
      }else if(oper==WeaselOperator.PLUS || oper==WeaselOperator.MINUS || oper==WeaselOperator.TIMES || oper==WeaselOperator.DIVIDE
           || oper==WeaselOperator.REMAINDER){
        if(primitiveID!=WeaselPrimitive.CHAR && primitiveID!=WeaselPrimitive.BYTE && primitiveID!=WeaselPrimitive.SHORT
            && primitiveID!=WeaselPrimitive.INT && primitiveID!=WeaselPrimitive.LONG
             && primitiveID!=WeaselPrimitive.FLOAT && primitiveID!=WeaselPrimitive.DOUBLE){
          throw new WeaselCompilerException(operator.line, "Operator %s is only usable with char, byte, short, int, long, float, double, not with %s %s", oper, wgc, ret);
        }
      }
      if(oper==WeaselOperator.LOGICAL_OR){
        instructions.add(operator.line, new WeaselInstructionLogicalOr(primitiveID));
      }else if(oper==WeaselOperator.LOGICAL_AND){
        instructions.add(operator.line, new WeaselInstructionLogicalAnd(primitiveID));
      }else if(oper==WeaselOperator.BITWISE_OR){
        instructions.add(operator.line, new WeaselInstructionBitwiseOr(primitiveID));
      }else if(oper==WeaselOperator.BITWISE_AND){
        instructions.add(operator.line, new WeaselInstructionBitwiseAnd(primitiveID));
      }else if(oper==WeaselOperator.BITWISE_XOR){
        instructions.add(operator.line, new WeaselInstructionBitwiseXor(primitiveID));
      }else if(oper==WeaselOperator.LESS){
        instructions.add(operator.line, new WeaselInstructionLess(primitiveID));
      }else if(oper==WeaselOperator.GREATER){
        instructions.add(operator.line, new WeaselInstructionGreater(primitiveID));
      }else if(oper==WeaselOperator.LESS_EQUAL){
        instructions.add(operator.line, new WeaselInstructionLessEqual(primitiveID));
      }else if(oper==WeaselOperator.GREATER_EQUAL){
        instructions.add(operator.line, new WeaselInstructionGreaterEqual(primitiveID));
      }else if(oper==WeaselOperator.PLUS){
        instructions.add(operator.line, new WeaselInstructionAdd(primitiveID));
      }else if(oper==WeaselOperator.MINUS){
        instructions.add(operator.line, new WeaselInstructionSub(primitiveID));
      }else if(oper==WeaselOperator.TIMES){
        instructions.add(operator.line, new WeaselInstructionMul(primitiveID));
      }else if(oper==WeaselOperator.DIVIDE){
        instructions.add(operator.line, new WeaselInstructionDiv(primitiveID));
      }else if(oper==WeaselOperator.REMAINDER){
        instructions.add(operator.line, new WeaselInstructionMod(primitiveID));
      }
    }else if(oper==WeaselOperator.RSHIFT || oper==WeaselOperator.LSHIFT){
      wcr = compileInfixOperator(compiler, compilerHelper, null, expect, null, false, i-1);
      instructions.addAll(wcr.getInstructions());
      ret = wcr.getReturnType();
      wcr = level.get(i+1).compile(compiler, compilerHelper, null, expect, null, false);
      wgc = wcr.getReturnType();
      instructions.addAll(wcr.getInstructions());
      WeaselTree.autoCast(compiler, wgc, new WeaselGenericClass(compiler.baseTypes.intClass), operator.line, instructions, true);
      int primitiveID = WeaselPrimitive.getPrimitiveID(ret.getBaseClass());
      if(oper==WeaselOperator.RSHIFT||oper==WeaselOperator.LSHIFT){
        if(primitiveID!=WeaselPrimitive.CHAR && primitiveID!=WeaselPrimitive.BYTE && primitiveID!=WeaselPrimitive.SHORT
            && primitiveID!=WeaselPrimitive.INT && primitiveID!=WeaselPrimitive.LONG){
          throw new WeaselCompilerException(operator.line, "Operator %s is only usable with char, byte, short, int, long, not with %s", oper, ret);
        }
      }
      if(oper==WeaselOperator.RSHIFT){
        instructions.add(operator.line, new WeaselInstructionRShift(primitiveID));
      }else if(oper==WeaselOperator.LSHIFT){
        instructions.add(operator.line, new WeaselInstructionLShift(primitiveID));
      }
    }else if(oper==WeaselOperator.VERY_SAME || oper==WeaselOperator.NOT_VERY_SAME
           || oper==WeaselOperator.EQUAL || oper==WeaselOperator.NOT_EQUAL){
      wcr = compileInfixOperator(compiler, compilerHelper, null, expect, null, false, i-1);
      instructions.addAll(wcr.getInstructions());
      wgc = wcr.getReturnType();
      wcr = level.get(i+1).compile(compiler, compilerHelper, null, expect, null, false);
      ret = wcr.getReturnType();
      if(wgc.getBaseClass().isPrimitive()||ret.getBaseClass().isPrimitive())
        wgc = WeaselTree.autoCast(compiler, wgc, ret, operator.line, instructions, false);
      instructions.addAll(wcr.getInstructions());
      if(wgc.getBaseClass().isPrimitive()||ret.getBaseClass().isPrimitive())
        ret = WeaselTree.autoCast(compiler, ret, wgc, operator.line, instructions, true);
      int primitiveID = WeaselPrimitive.getPrimitiveID(ret.getBaseClass());
      if(oper==WeaselOperator.VERY_SAME){
        instructions.add(operator.line, new WeaselInstructionVerySame(primitiveID));
      }else if(oper==WeaselOperator.NOT_VERY_SAME){
        instructions.add(operator.line, new WeaselInstructionNotVerySame(primitiveID));
      }else if(oper==WeaselOperator.EQUAL){
        instructions.add(operator.line, new WeaselInstructionEqual(primitiveID));
      }else if(oper==WeaselOperator.NOT_EQUAL){
        instructions.add(operator.line, new WeaselInstructionNotEqual(primitiveID));
      }
      ret = new WeaselGenericClass(compiler.baseTypes.booleanClass);
    }else if(oper==WeaselOperator.ELEMENT){
      wcr = compileInfixOperator(compiler, compilerHelper, null, null, null, false, i-1);
      instructions.addAll(wcr.getInstructions());
      wgc = wcr.getReturnType();
      wcr = level.get(i+1).compile(compiler, compilerHelper, write, expect, wgc, !wcr.isClassAccess());
      ret = wcr.getReturnType();
      instructions.addAll(wcr.getInstructions());
    }else{
      throw new WeaselCompilerException(operator.line, "Unknown operator %s", operator);
    }
    return new WeaselCompilerReturnInstructionList(instructions, ret);
  }
View Full Code Here

  @Override
  public WeaselCompilerReturn compile(WeaselToken token, WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelpher, ListIterator<WeaselToken> iterator) throws WeaselCompilerException {
    expect(iterator.next(), WeaselTokenType.OPENBRACKET);
    compilerHelpher.openBlock(true);
    WeaselInstructionList instructions = WeaselTree.parseAndCompileWhithVarDec(compiler, compilerHelpher, iterator);
    WeaselTree tree2 = WeaselTree.parse(iterator, WeaselTokenType.SEMICOLON);
    if(tree2==null){
      throw new WeaselCompilerException(token.line, "Expect boolean value in secound part of for");
    }
    WeaselTree tree3 = WeaselTree.parse(iterator, WeaselTokenType.CLOSEBRACKET);
   
    WeaselToken t = iterator.next();
    WeaselCompilerReturn wcr;
    if(instructions == null){
      instructions = new WeaselInstructionList();
      instructions.add(token.line, new WeaselInstructionJumperDummy());
    }
    WeaselInstruction startJump = instructions.getLast();
    wcr = tree2.compile(compiler, compilerHelpher, null, new WeaselGenericClass(compiler.baseTypes.voidClass), null, false);
    instructions.addAll(wcr.getInstructions());
    WeaselTree.autoCast(compiler, wcr.getReturnType(), new WeaselGenericClass(compiler.baseTypes.booleanClass), token.line, instructions, true);
    WeaselInstructionIf ifI;
    instructions.add(token.line, ifI = new WeaselInstructionIf());
    if(t.tokenType==WeaselTokenType.OPENBLOCK){
      t = iterator.next();
      while(t.tokenType!=WeaselTokenType.CLOSEBLOCK){
        iterator.previous();
        instructions.addAll(WeaselTree.parseAndCompile(compiler, compilerHelpher, iterator));
        t = iterator.next();
      }
    }else{
      iterator.previous();
      instructions.addAll(WeaselTree.parseAndCompile(compiler, compilerHelpher, iterator));
    }
    WeaselInstruction continueJump = instructions.getLast();
    if(tree3!=null){
      wcr = tree3.compile(compiler, compilerHelpher, null, new WeaselGenericClass(compiler.baseTypes.voidClass), null, false);
      instructions = wcr.getInstructions();
      if(wcr.getReturnType().getBaseClass()!=compiler.baseTypes.voidClass)
        instructions.add(t.line, new WeaselInstructionPop());
    }
    WeaselInstruction ifJump = new WeaselInstructionJump(startJump);
    instructions.add(token.line, ifJump);
    ifI.setTarget(ifJump);
    WeaselBlockInfo wbi = compilerHelpher.closeBlock();
    int pops = wbi.varsToPop();
    if(pops==1){
      instructions.add(t.line, new WeaselInstructionPop());
    }else if(pops>1){
      instructions.add(t.line, new WeaselInstructionPops(pops));
    }
    if(pops>=1){
      instructions.add(token.line, new WeaselInstructionReservate(pops));
    }
    WeaselInstruction breakJump = instructions.getLast();
    for(WeaselInstructionJump breakI:wbi.breaks){
      breakI.setTarget(breakJump);
    }
    for(WeaselInstructionJump continueI:wbi.continues){
      continueI.setTarget(continueJump);
View Full Code Here

      s = (Integer)t.param;
      if(s<=0)
        throw new WeaselCompilerException(t.line, "Negatives and 0 are not permitted");
      t = iterator.next();
    }
    WeaselInstructionList instructionList = new WeaselInstructionList();
    compilerHelpher.addClosingsAndFrees(s, instructionList, true);
    WeaselInstructionJump breakJump;
    instructionList.add(token.line, breakJump = new WeaselInstructionJump());
    compilerHelpher.addBreak(s, breakJump);
    expect(t, WeaselTokenType.SEMICOLON);
    return null;
  }
View Full Code Here

  @Override
  public WeaselCompilerReturn compile(WeaselToken token, WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelpher, ListIterator<WeaselToken> iterator) throws WeaselCompilerException {
    WeaselGenericMethod2 method = compilerHelpher.getCompilingMethod();
    WeaselGenericClass retClass = method.getGenericReturn();
    WeaselInstructionList instructions;
    if(retClass.getBaseClass()==compiler.baseTypes.voidClass){
      expect(iterator.next(), WeaselTokenType.SEMICOLON);
      instructions = new WeaselInstructionList();
      instructions.add(token.line, new WeaselInstructionReturnNull(compilerHelpher.getVarCount()));
    }else{
      WeaselTree tree = WeaselTree.parse(iterator, WeaselTokenType.SEMICOLON);
      if(tree==null){
        throw new WeaselCompilerException(token.line, "return need to return %s", retClass);
      }
      WeaselCompilerReturn wcr = tree.compile(compiler, compilerHelpher, null, retClass, null, false);
      instructions = wcr.getInstructions();
      WeaselTree.autoCast(compiler, wcr.getReturnType(), retClass, token.line, instructions, true);
      instructions.add(token.line, new WeaselInstructionReturn(compilerHelpher.getVarCount()));
    }
    return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(compiler.baseTypes.voidClass));
  }
View Full Code Here

    }
    for(int i=0; i<paramNames.size(); i++){
      WeaselGenericMethod2 genericMethod = classCompiler.genericClass.getGenericMethod(method.getNameAndDesk(), null);
      block.newVar(paramModifier.get(i), paramNames.get(i), genericMethod.getGenericParams()[i]);
    }
    WeaselInstructionList instructions = new WeaselInstructionList();
    ListIterator<WeaselToken> iterator = methodTokens.listIterator();
    if(method.getName().equals("<preInit>")){
      if(method.getParentClass().getSuperClass()!=null){
        instructions.add(0, new WeaselInstructionLoadVariable(getVariable("this").pos));
        instructions.add(0, new WeaselInstructionInvoke(method.getParentClass().getSuperClass().getRealName()+".<preInit>()"));
      }
    }else if(method.getName().equals("<init>")){
      boolean auto = true;
      if(iterator.hasNext()){
        WeaselToken token = iterator.next();
        if(token!=null && token.tokenType==WeaselTokenType.KEYWORD){
          if(iterator.next().tokenType==WeaselTokenType.OPENBRACKET){
            try{
              if(token.param==WeaselKeyWord.THIS){
                superCaller = false;
                auto = false;
                WeaselTree tree = WeaselTree.parse(iterator, WeaselTokenType.CLOSEBRACKET);
                List<WeaselGenericMethod2> methods = classCompiler.genericClass.getGenericMethodsOfThis("<init>", true);
                WeaselParameterCompileReturn wpcr = WeaselTree.compileParamList(token.line, "<init>", compiler, this, tree, methods);
                instructions.addAll(wpcr.instructions);
                instructions.add(token.line, new WeaselInstructionInvoke(wpcr.method.getMethod().getMethod().getClassNameAndDesk()));
                token = iterator.next();
                if(token.tokenType!=WeaselTokenType.SEMICOLON)
                  throw new WeaselCompilerException(token.line, "Expect ; but got %s", token);
              }else if(token.param==WeaselKeyWord.SUPER){
                superCaller = true;
                auto = false;
                WeaselTree tree = WeaselTree.parse(iterator, WeaselTokenType.CLOSEBRACKET);
                List<WeaselGenericMethod2> methods = classCompiler.genericClass.getGenericSuperClass().getGenericMethodsOfThis("<init>", true);
                WeaselParameterCompileReturn wpcr = WeaselTree.compileParamList(token.line, "<init>", compiler, this, tree, methods);
                instructions.addAll(wpcr.instructions);
                instructions.add(token.line, new WeaselInstructionInvoke(wpcr.method.getMethod().getMethod().getClassNameAndDesk()));
                token = iterator.next();
                if(token.tokenType!=WeaselTokenType.SEMICOLON)
                  throw new WeaselCompilerException(token.line, "Expect ; but got %s", token);
              }else{
                iterator.previous();
                iterator.previous();
              }
            }catch(WeaselCompilerException e){
              compiler.addWeaselCompilerMessage(new WeaselCompilerMessage(MessageType.ERROR, e.getLine(), parentClass.getFileName(), e.getMessage()));
              iterator.previous();
              token = iterator.next();
              while(token.tokenType!=WeaselTokenType.SEMICOLON && iterator.hasNext()){
                token = iterator.next();
              }
            }
          }else{
            iterator.previous();
            iterator.previous();
          }
        }else{
          iterator.previous();
        }
      }
      if(auto){
        superCaller = true;
        if(classCompiler.genericClass.getGenericSuperClass()!=null){
          WeaselGenericMethod2 method = classCompiler.genericClass.getGenericSuperClass().getGenericMethodOfThis("<init>()", new WeaselGenericClass[0]);
          if(method==null){
            compiler.addWeaselCompilerMessage(
                new WeaselCompilerMessage(MessageType.ERROR, 0, parentClass.getFileName(),
                    String.format("No default constructor in %s found", classCompiler.genericClass.getGenericSuperClass())));
          }else{
            instructions.add(0, new WeaselInstructionInvoke(method.getMethod().getMethod().getClassNameAndDesk()));
          }
        }
      }
    }
    while(iterator.hasNext()){
      try{
        instructions.addAll(WeaselTree.parseAndCompile(compiler, this, iterator));
      }catch(WeaselCompilerException e){
        compiler.addWeaselCompilerMessage(new WeaselCompilerMessage(MessageType.ERROR, e.getLine(), parentClass.getFileName(), e.getMessage()));
        iterator.previous();
        WeaselToken token = iterator.next();
        while(token.tokenType!=WeaselTokenType.SEMICOLON && iterator.hasNext()){
          token = iterator.next();
        }
      }
    }
    instructions.add(0, new WeaselInstructionReturn(block.varsToPop()));
    System.out.println(instructions);
    this.instructions = instructions.getInstructions();
  }
View Full Code Here

      s = (Integer)t.param;
      if(s<=0)
        throw new WeaselCompilerException(t.line, "Negatives and 0 are not permitted");
      t = iterator.next();
    }
    WeaselInstructionList instructionList = new WeaselInstructionList();
    compilerHelpher.addClosingsAndFrees(s, instructionList, false);
    WeaselInstructionJump continueJump;
    instructionList.add(token.line, continueJump = new WeaselInstructionJump());
    compilerHelpher.addContinue(s, continueJump);
    expect(t, WeaselTokenType.SEMICOLON);
    return null;
  }
View Full Code Here

    if(tree1==null){
      throw new WeaselCompilerException(token.line, "Expect boolean value in while");
    }
    WeaselToken t = iterator.next();
    WeaselCompilerReturn wcr;
    WeaselInstructionList instructions = new WeaselInstructionList();
    WeaselInstruction continueJump = new WeaselInstructionJumperDummy();
    instructions.add(token.line, continueJump);
    wcr = tree1.compile(compiler, compilerHelpher, null, new WeaselGenericClass(compiler.baseTypes.booleanClass), null, false);
    instructions.addAll(wcr.getInstructions());
    WeaselTree.autoCast(compiler, wcr.getReturnType(), new WeaselGenericClass(compiler.baseTypes.booleanClass), token.line, instructions, true);
    WeaselInstructionIf ifI;
    instructions.add(token.line, ifI = new WeaselInstructionIf());
    if(t.tokenType==WeaselTokenType.OPENBLOCK){
      t = iterator.next();
      while(t.tokenType!=WeaselTokenType.CLOSEBLOCK){
        iterator.previous();
        instructions.addAll(WeaselTree.parseAndCompile(compiler, compilerHelpher, iterator));
        t = iterator.next();
      }
    }else{
      iterator.previous();
      instructions.addAll(WeaselTree.parseAndCompile(compiler, compilerHelpher, iterator));
    }
    WeaselInstruction ifJump = new WeaselInstructionJump(continueJump);
    instructions.add(token.line, ifJump);
    ifI.setTarget(ifJump);
    WeaselBlockInfo wbi = compilerHelpher.closeBlock();
    int pops = wbi.varsToPop();
    if(pops==1){
      instructions.add(token.line, new WeaselInstructionPop());
    }else if(pops>1){
      instructions.add(token.line, new WeaselInstructionPops(pops));
    }
    if(pops>=1){
      instructions.add(t.line, new WeaselInstructionReservate(pops));
    }
    WeaselInstruction breakJump = instructions.getLast();
    for(WeaselInstructionJump breakI:wbi.breaks){
      breakI.setTarget(breakJump);
    }
    for(WeaselInstructionJump continueI:wbi.continues){
      continueI.setTarget(continueJump);
View Full Code Here

TOP

Related Classes of weasel.compiler.WeaselInstructionList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.