Package weasel.compiler

Examples of weasel.compiler.WeaselInstructionList


  @Override
  public WeaselCompilerReturn compile(WeaselToken token, WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelpher, ListIterator<WeaselToken> iterator) throws WeaselCompilerException {
    compilerHelpher.openBlock(true);
    WeaselToken t = iterator.next();
    WeaselInstructionList instructions = new WeaselInstructionList();
    WeaselInstructionJumperDummy startJump = new WeaselInstructionJumperDummy();
    instructions.add(token.line, startJump);
    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));
    }
    t = iterator.next();
    if(t.tokenType!=WeaselTokenType.KEYWORD || t.param!=WeaselKeyWord.WHILE){
      throw new WeaselCompilerException(t.line, "expect while but got %s", t);
    }
    WeaselInstruction continueJump = instructions.getLast();
    expect(t = iterator.next(), WeaselTokenType.OPENBRACKET);
    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);
View Full Code Here


    expect(iterator.next(), WeaselTokenType.OPENBRACKET);
    WeaselTree tree = WeaselTree.parse(iterator, WeaselTokenType.CLOSEBRACKET);
    if(tree==null)
      throw new WeaselCompilerException(token.line, "Condition need to be a boolean value");
    WeaselCompilerReturn wcr = tree.compile(compiler, compilerHelpher, null, new WeaselGenericClass(compiler.baseTypes.booleanClass), null, false);
    WeaselInstructionList instructions = wcr.getInstructions();
    WeaselTree.autoCast(compiler, wcr.getReturnType(), new WeaselGenericClass(compiler.baseTypes.booleanClass), token.line, instructions, true);
    WeaselInstructionJump j1;
    instructions.add(token.line, j1 = new WeaselInstructionIf());
    instructions.addAll(compileBlock(compiler, compilerHelpher, iterator));
    WeaselToken t = iterator.next();
    if(t.tokenType==WeaselTokenType.KEYWORD && t.param == WeaselKeyWord.ELSE){
      WeaselInstructionJump j2;
      instructions.add(t.line, j2 = new WeaselInstructionJump());
      j1.setTarget(j2);
      instructions.addAll(compileBlock(compiler, compilerHelpher, iterator));
      j2.setTarget(instructions.getLast());
    }else{
      iterator.previous();
      j1.setTarget(instructions.getLast());
    }
    return new WeaselCompilerReturnInstructionList(instructions, new WeaselGenericClass(compiler.baseTypes.voidClass));
  }
View Full Code Here

  }

  private WeaselInstructionList compileBlock(WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelpher, ListIterator<WeaselToken> iterator) throws WeaselCompilerException{
    compilerHelpher.openBlock(false);
    WeaselToken token = iterator.next();
    WeaselInstructionList instructions;
    if(token.tokenType==WeaselTokenType.OPENBLOCK){
      instructions = new WeaselInstructionList();
      token = iterator.next();
      while(token.tokenType!=WeaselTokenType.CLOSEBLOCK){
        iterator.previous();
        instructions.addAll(WeaselTree.parseAndCompile(compiler, compilerHelpher, iterator));
        token = iterator.next();
      }
    }else{
      iterator.previous();
      instructions =  WeaselTree.parseAndCompile(compiler, compilerHelpher, iterator);
    }
    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(token.line, new WeaselInstructionReservate(pops));
    }
    return instructions;
  }
View Full Code Here

    int modifier = getModifier(modifiers, WeaselModifier.FINAL);
    WeaselClassCompiler wcc = compilerHelper.getClassCompiler();
    WeaselGenericClassInfo wgci = wcc.readGenericClass(token, iterator);
    WeaselGenericClass wgc = wgci.getGenericClass(wcc.getGenericClass());
    iterator.previous();
    WeaselInstructionList instructions = new WeaselInstructionList();
    do{
      iterator.next();
      instructions.addAll(compileVar(modifier, wgc, compiler, compilerHelper, iterator));
    }while(iterator.previous().tokenType==WeaselTokenType.COMMA);
    WeaselCompiler.expect(iterator.next(), WeaselTokenType.SEMICOLON);
    return instructions;
  }
View Full Code Here

    }
    wgc = new WeaselGenericClass(compiler.getWeaselClass(className), wgc.getGenerics());
    WeaselVariableInfo wvi = compilerHelper.newVar(modifier, varName, wgc);
    if(token.tokenType==WeaselTokenType.OPERATOR && token.param == WeaselOperator.ASSIGN){
      WeaselCompilerReturn wcr = parse(iterator, WeaselTokenType.SEMICOLON, WeaselTokenType.COMMA).compile(compiler, compilerHelper, null, wgc, null, false);
      WeaselInstructionList instructions = wcr.getInstructions(compiler, wgc);
      instructions.add(token.line, new WeaselInstructionSaveVariable(wvi.pos));
      instructions.add(token.line, new WeaselInstructionPop());
      compilerHelper.writeVar(wvi);
      return instructions;
    }else{
      return new WeaselInstructionList();
    }
  }
View Full Code Here

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

  @Override
  public WeaselCompilerReturn compile(WeaselCompiler compiler, WeaselKeyWordCompilerHelper compilerHelper, WeaselGenericClass write, WeaselGenericClass expect, WeaselGenericClass elementParent, boolean isVariable) throws WeaselCompilerException {
    if(write!=null){
      throw new WeaselCompilerException(token.line, "Can't write any value to Condition");
    }
    WeaselInstructionList instructions;
    WeaselCompilerReturn wcr = condition.compile(compiler, compilerHelper, null, new WeaselGenericClass(compiler.baseTypes.booleanClass), null, false);
    instructions = wcr.getInstructions(compiler, compiler.baseTypes.booleanClass);
    WeaselInstructionJump j1;
    WeaselInstructionJump j2;
    instructions.add(token.line, j1 = new WeaselInstructionIf());
    wcr = tree1.compile(compiler, compilerHelper, null, expect, elementParent, isVariable);
    WeaselGenericClass wc = wcr.getReturnType();
    instructions.addAll(wcr.getInstructions());
    wcr = tree2.compile(compiler, compilerHelper, null, expect, elementParent, isVariable);
    if(wc.getBaseClass()==compiler.baseTypes.voidClass || wcr.getReturnType().getBaseClass()==compiler.baseTypes.voidClass){
      throw new WeaselCompilerException(token.line, "Can't return void");
    }
    WeaselGenericClass wc2 = wcr.getReturnType();
    wc = WeaselTree.autoCast(compiler, wc, wc2, token.line, instructions, false);
    instructions.add(token.line, j2 = new WeaselInstructionJump());
    j1.setTarget(j2);
    instructions.addAll(wcr.getInstructions());
    wc2 = WeaselTree.autoCast(compiler, wc2, wc, token.line, instructions, true);
    j2.setTarget(instructions.getLast());
    return new WeaselCompilerReturnInstructionList(instructions, WeaselGenericClass.getSmallestSame(wc, wc2));
  }
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.