Package exceptions

Examples of exceptions.SemanticException


  public void outLtExp(LtExp node)  {
    Type lexpType = this.mCurrentST.getExpType(node.getLExp());
    Type rexpType = this.mCurrentST.getExpType(node.getRExp());
    if (((lexpType != Type.INT) && (lexpType != Type.BYTE)) || ((rexpType != Type.INT) && (rexpType != Type.BYTE))) {
      throw new SemanticException(
          "Operands to < operator must be the same type",
          node.getRExp().getLine(),
          node.getRExp().getPos());
    }
    if ( (lexpType != Type.INT ) && (lexpType !=  Type.BYTE)){
      throw new SemanticException(
          "Operands to < operator must be Int or Byte",
          node.getLExp().getLine(),
          node.getLExp().getPos());
    }
    this.mCurrentST.setExpType(node, Type.BOOL);
View Full Code Here


  public void outArrayAssignStatement(ArrayAssignStatement node) {
    STE ste = this.mCurrentST.lookup( node.getIdLit().getLexeme() );
    this.mCurrentST.setNodeSTE( node.getIdLit(), ste);

    if((ste == null) || (!(ste instanceof VarSTE))) {
      throw new SemanticException(
          "Undeclared variable " + node.getIdLit().getLexeme(),
          node.getIdLit().getLine(),
          node.getIdLit().getPos());
    }

    VarSTE varEntry = (VarSTE) ste;
    Type arrayType = mCurrentST.getExpType(node.getExp());

    if((varEntry.getType() != Type.COLOR_ARRAY) && (varEntry.getType() != Type.INT_ARRAY)) {
      throw new SemanticException(
          "Array reference to non-array type",
          node.getIdLit().getLine(),
          node.getIdLit().getPos());
    }

    if ((mCurrentST.getExpType(node.getIndex()) != Type.INT) && (mCurrentST.getExpType(node.getIndex()) != Type.BYTE)) {
      throw new SemanticException(
          "Index expression type for array reference must be INT or BYTE",
          node.getIdLit().getLine(),
          node.getIdLit().getPos());
    }

    if( ((varEntry.getType() == Type.INT_ARRAY) && (arrayType != Type.INT) &&  (arrayType != Type.BYTE)) ||  (varEntry.getType() == Type.COLOR_ARRAY) && (arrayType != Type.COLOR) ) {
      throw new SemanticException(
          "Invalid expression type assigned to variable " + node.getIdLit().getLexeme(),
          node.getIdLit().getLine(),
          node.getIdLit().getPos());
    }
  }
View Full Code Here

    }
  }

  public void outArrayExp(ArrayExp node) {
    if ((this.mCurrentST.getExpType(node.getExp()) != Type.COLOR_ARRAY) && (mCurrentST.getExpType(node.getExp()) != Type.INT_ARRAY))
      throw new SemanticException("Array reference to non-array type", node.getExp().getLine(), node.getExp().getPos());

    if ((this.mCurrentST.getExpType(node.getIndex()) != Type.INT) && (mCurrentST.getExpType(node.getIndex()) != Type.BYTE))
      throw new SemanticException("Index expression type for array reference must be INT or BYTE", node.getIndex().getLine(), node.getIndex().getPos());

    if(mCurrentST.getExpType(node.getExp()) == Type.COLOR_ARRAY)
      mCurrentST.setExpType(node, Type.COLOR);
    else
      mCurrentST.setExpType(node, Type.INT);
View Full Code Here

      mCurrentST.setExpType(node, Type.INT);
  }

  public void outNewArrayExp(NewArrayExp node) {
    if ((mCurrentST.getExpType(node.getExp()) != Type.INT) && (mCurrentST.getExpType(node.getExp()) != Type.BYTE)) {
      throw new SemanticException(
          "Invalid operand type for new array operator",
          node.getExp().getLine(),
          node.getExp().getPos());
    }
    if(node.getType() instanceof ColorType) {
      mCurrentST.setExpType(node, Type.COLOR_ARRAY);
    }
    else if(node.getType() instanceof IntType) {
      mCurrentST.setExpType(node, Type.INT_ARRAY);
    }
    else {
      throw new SemanticException(
          "Invalid array type for new array operator",
          node.getExp().getLine(),
          node.getExp().getPos());
    }
  }
View Full Code Here

  public void outLengthExp(LengthExp node)
  {
    if ((mCurrentST.getExpType(node.getExp()) != Type.COLOR_ARRAY) && (this.mCurrentST.getExpType(node.getExp()) != Type.INT_ARRAY))
    {
      throw new SemanticException(
          "Operator length called on non-array type",
          node.getExp().getLine(),
          node.getExp().getPos());
    }
View Full Code Here

  private Type typeCheck(Node receiver, String id, List<IExp> args) {
    //check that receiver is of type Class
    Type receiverType = this.mCurrentST.getExpType(receiver);

    if((receiverType == null) || (!receiverType.isClass())) {
      throw new SemanticException("Receiver of method call must be a class type", receiver.getLine(), receiver.getPos());
    }
    //receiverInfo=lookupClass(receiverType.getClassName())
    ClassSTE classEntry = this.mCurrentST.lookupClass(receiverType.getName());
    //invocationInfo = receiverInfo.getScope().lookup(funcName);
    MethodSTE methodEntry = (MethodSTE) classEntry.getScope().lookup(id);
    mCurrentST.setNodeSTE(receiver, methodEntry);
    //if it isn’t there throw exception
    if(methodEntry == null)
      throw new SemanticException("Method " + id + " does not exist in class type " + classEntry.getName(), receiver.getLine(), receiver.getPos());
    MethodSignature sig = methodEntry.getSignature();
    int i = args.size();
    if(i != sig.numberOfArgs())
      throw new SemanticException("Method " + id + " requires exactly " + sig.numberOfArgs() + " arguments", receiver.getLine(), receiver.getPos());

    IExp[] expList = new IExp[i];
    expList = (IExp[])args.toArray(expList);
    for(i=0; i<expList.length; i++){
      Type expType = this.mCurrentST.getExpType(expList[i]);
      Type sigType = sig.getArg(i);
      if(sigType != expType)
        throw new SemanticException("Invalid argument type for method " + id, receiver.getLine(), receiver.getPos());
    }

    return sig.getReturnType();
  }
View Full Code Here

    return sig.getReturnType();
  }

  public void outMeggySetAuxLEDs(MeggySetAuxLEDs node) {
    if(mCurrentST.getExpType(node.getExp()) != Type.INT) {
      throw new SemanticException(
          "Invalid argument type for method MeggySetAuxLEDs",
          node.getLine(), node.getPos());
    }

  }
View Full Code Here

TOP

Related Classes of exceptions.SemanticException

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.