Package weasel.interpreter

Examples of weasel.interpreter.WeaselGenericClass


    }
    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);
View Full Code Here


    }
  }

  public WeaselGenericClass getGenericClass(WeaselGenericClass parentClass) throws WeaselCompilerException {
    try{
      return new WeaselGenericClass(parentClass.getBaseClass().getInterpreter().getWeaselClass(classByteName), generic.getGenericClasses(parentClass));
    }catch(WeaselNativeException e){}
    WeaselGenericClass wgc = parentClass.getGenericClass(realClassName);
    if(wgc==null){
      throw new WeaselCompilerException(token.line, "Class %s not found", className);
    }
    WeaselClass wc = wgc.getBaseClass();
    int i=0;
    while(classByteName.charAt(i++)=='['){
      wc = new WeaselClass(wc.getInterpreter(), wc, "["+wc.getByteName(), null);
    }
    return new WeaselGenericClass(wc, wgc.getGenerics());
  }
View Full Code Here

    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{
View Full Code Here

        methodBodys[id] = new WeaselMethodBodyCompilerV2(method, this, new ArrayList<WeaselToken>(), new ArrayList<String>(), new ArrayList<Integer>(), compiler);
      }
     
    }
   
    genericClass = new WeaselGenericClass(this);
  }
View Full Code Here

    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);
    }
    return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(compiler.baseTypes.voidClass));
  }
View Full Code Here

public class WeaselKeyWordCompilerReturn extends WeaselKeyWordCompiler {

  @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

  private WeaselInstructionList instructionList = new WeaselInstructionList();
 
  public WeaselCompilerReturnConstant(WeaselCompiler compiler, int line, Object constant) throws WeaselCompilerException{
    this.constant = constant;
    if(constant instanceof Boolean){
      returnType = new WeaselGenericClass(compiler.baseTypes.booleanClass);
      instructionList.add(line, new WeaselInstructionLoadConstBoolean((Boolean)constant));
    }else if(constant instanceof Character){
      returnType = new WeaselGenericClass(compiler.baseTypes.charClass);
      instructionList.add(line, new WeaselInstructionLoadConstChar((Character)constant));
    }else if(constant instanceof Byte){
      returnType = new WeaselGenericClass(compiler.baseTypes.byteClass);
      instructionList.add(line, new WeaselInstructionLoadConstByte((Byte)constant));
    }else if(constant instanceof Short){
      returnType = new WeaselGenericClass(compiler.baseTypes.shortClass);
      instructionList.add(line, new WeaselInstructionLoadConstShort((Short)constant));
    }else if(constant instanceof Integer){
      returnType = new WeaselGenericClass(compiler.baseTypes.intClass);
      instructionList.add(line, new WeaselInstructionLoadConstInteger((Integer)constant));
    }else if(constant instanceof Long){
      returnType = new WeaselGenericClass(compiler.baseTypes.longClass);
      instructionList.add(line, new WeaselInstructionLoadConstLong((Long)constant));
    }else if(constant instanceof Float){
      returnType = new WeaselGenericClass(compiler.baseTypes.floatClass);
      instructionList.add(line, new WeaselInstructionLoadConstFloat((Float)constant));
    }else if(constant instanceof Double){
      returnType = new WeaselGenericClass(compiler.baseTypes.doubleClass);
      instructionList.add(line, new WeaselInstructionLoadConstDouble((Double)constant));
    }else if(constant instanceof String){
      returnType = new WeaselGenericClass(compiler.baseTypes.getStringClass());
      instructionList.add(line, new WeaselInstructionLoadConstString((String)constant));
    }else if(constant==null){
      returnType = null;
      instructionList.add(line, new WeaselInstructionLoadNull());
    }else{
View Full Code Here

    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);
    }
    return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(compiler.baseTypes.voidClass));
  }
View Full Code Here

          throw new WeaselCompilerException(instructions.getLine(), "Types %s and %s are not compatible", returnType, weaselGenericClass);
        }
      }
      return instructions;
    }
    WeaselGenericClass returnWrapper;
    WeaselGenericClass expectWrapper;
    if(returnType.getBaseClass().isPrimitive()){
      returnWrapper = new WeaselGenericClass(compiler.getWeaselClass(WeaselPrimitive.getWrapper(returnType.getBaseClass())));
    }else{
      returnWrapper = returnType;
    }
    if(weaselGenericClass.getBaseClass().isPrimitive()){
      expectWrapper = new WeaselGenericClass(compiler.getWeaselClass(WeaselPrimitive.getWrapper(weaselGenericClass.getBaseClass())));
    }else{
      expectWrapper = weaselGenericClass;
    }
   
    if(returnType.getBaseClass().isPrimitive()){
      instructions.addWithoutLine(new WeaselInstructionCast(returnWrapper.getBaseClass().getByteName()));
    }
   
    if(!returnWrapper.getBaseClass().isInterface() && !expectWrapper.getBaseClass().isInterface()){
      if(returnWrapper.canCastTo(expectWrapper) && !expectWrapper.canCastTo(returnWrapper)){
        throw new WeaselCompilerException(instructions.getLine(), "Types %s and %s are not compatible", returnType, weaselGenericClass);
      }
    }
   
    instructions.addWithoutLine(new WeaselInstructionCast(expectWrapper.getBaseClass().getRealName()));
   
    if(weaselGenericClass.getBaseClass().isPrimitive()){
      instructions.addWithoutLine(new WeaselInstructionCastPrimitive(WeaselPrimitive.getPrimitiveID(weaselGenericClass.getBaseClass())));
    }
    returnType = weaselGenericClass;
View Full Code Here

    WeaselTree tree1 = WeaselTree.parse(iterator, WeaselTokenType.CLOSEBRACKET);
    if(tree1==null){
      throw new WeaselCompilerException(t.line, "Expect boolean value in while");
    }
    WeaselCompilerReturn wcr;
    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), t.line, instructions, true);
    WeaselInstruction ifJump = new WeaselInstructionJump(startJump);
    instructions.add(t.line, new WeaselInstructionIf(ifJump));
    instructions.add(t.line, 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.addFirst(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);
    }
    return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(compiler.baseTypes.voidClass));
  }
View Full Code Here

TOP

Related Classes of weasel.interpreter.WeaselGenericClass

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.