Package weasel.interpreter

Examples of weasel.interpreter.WeaselGenericMethod2


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


    }else{
      block = new WeaselBlockInfo(false, -paramNames.size());
      block.newVar(0, "this", classCompiler.genericClass);
    }
    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()){
View Full Code Here

  }
 
  public static WeaselParameterCompileReturn compileParamList(int line, String methodName, WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelper, WeaselTree func, List<WeaselGenericMethod2> methods) throws WeaselCompilerException {
    WeaselCompilerReturn wcr;
    WeaselInstructionList instructions = new WeaselInstructionList();
    WeaselGenericMethod2 method;
    if(func instanceof WeaselTreeLevel){
      WeaselTreeLevel treeLevel = (WeaselTreeLevel) func;
      if(treeLevel.levelPriority == 0){
        int param=treeLevel.level.size();
        Iterator<WeaselGenericMethod2> iterator = methods.iterator();
        while(iterator.hasNext()){
          if(iterator.next().getGenericParams().length!=param)
            iterator.remove();
        }
        WeaselGenericClass expect = null;
        if(methods.isEmpty())
          throw new WeaselCompilerException(line, "No method %s found with %s params", param);
        List<WeaselGenericClass> params = new ArrayList<WeaselGenericClass>();
        for(int i=0; i<param; i++){
          if(methods.size()==1)
            expect = methods.get(0).getGenericParams()[i];
          wcr = treeLevel.level.get(i).compile(compiler, compilerHelper, null, expect, null, false);
          instructions.addAll(wcr.getInstructions());
          params.add(wcr.getReturnType());
        }
        iterator = methods.iterator();
        while(iterator.hasNext()){
          WeaselGenericMethod2 m=iterator.next();
          for(int i=0; i<param; i++){
            if(params.get(i)!=null && !params.get(i).canCastTo(m.getGenericParams()[i]))
              iterator.remove();
          }
        }
        if(methods.isEmpty()){
          throw new WeaselCompilerException(line, "No method %s found with for params %s", methodName, params);
        }
        if(methods.size()==1){
          method = methods.get(0);
        }else{
          throw new WeaselCompilerException(line, "Not supported now");
        }
        return new WeaselParameterCompileReturn(instructions, method);
      }
    }
    if(func==null){
      method = null;
      for(WeaselGenericMethod2 m:methods){
        if(m.getGenericParams().length==0){
          method = m;
          break;
        }
      }
      if(method==null){
        throw new WeaselCompilerException(line, "No method %s found with no params", methodName);
      }
    }else{
      Iterator<WeaselGenericMethod2> iterator = methods.iterator();
      while(iterator.hasNext()){
        if(iterator.next().getGenericParams().length!=1)
          iterator.remove();
      }
      WeaselGenericClass expect = null;
      if(methods.isEmpty())
        throw new WeaselCompilerException(line, "No method %s found with one param", methodName);
      if(methods.size()==1)
        expect = methods.get(0).getGenericParams()[0];
      wcr = func.compile(compiler, compilerHelper, null, expect, null, false);
      instructions.addAll(wcr.getInstructions());
      iterator = methods.iterator();
      while(iterator.hasNext()){
        WeaselGenericMethod2 m=iterator.next();
        if(!wcr.getReturnType().canCastTo(m.getGenericParams()[0]))
          iterator.remove();
      }
      if(methods.isEmpty()){
        throw new WeaselCompilerException(line, "No method %s found with for param %s", methodName, wcr.getReturnType());
      }
View Full Code Here

TOP

Related Classes of weasel.interpreter.WeaselGenericMethod2

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.