Пример #1
0
  /**
   * @param tokenArray
   * @param vectorLength
   * @param farReceivers
   * @param i
   * @throws IllegalActionException
   */
  private void putAtFarReceivers(Token[] tokenArray, int vectorLength, Receiver[] farReceivers)
      throws IllegalActionException {
    for (int j = 0; j < farReceivers.length; j++) {
      TypedIOPort farPort = (TypedIOPort) farReceivers[j].getContainer();
      Type farType = farPort.getType();

      boolean needConversion = false;
      for (int k = 0; k < tokenArray.length; k++) {
        if (!farType.equals(tokenArray[k].getType())) {
          needConversion = true;
        }
      }

      if (!needConversion) {
        // Good, no conversion necessary.
        farReceivers[j].putArray(tokenArray, vectorLength);
      } else {
        // Note: This is very bad for performance!
        // For better efficiency, make sure
        // all ports have the same type.
        for (int k = 0; k < vectorLength; k++) {
          Token newToken = convertTokenForFarPort(tokenArray[k], farPort);
          farReceivers[j].put(newToken);
        }
      }
    }
  }
 /**
  * Set the type of the given node to be the type of the child of the given node.
  *
  * @param node The specified node.
  * @exception IllegalActionException If an inference error occurs.
  */
 public void visitUnaryNode(ASTPtUnaryNode node) throws IllegalActionException {
   Type[] childTypes = _inferAllChildren(node);
   Type baseType = childTypes[0];
   if (node.isMinus()) {
     _setType(node, baseType.zero().subtract(baseType));
   } else {
     _setType(node, baseType);
   }
 }
  /**
   * Set the type of the given node to be the least upper bound type of the types of the node's
   * children.
   *
   * @param node The specified node.
   * @exception IllegalActionException If an inference error occurs.
   */
  public void visitSumNode(ASTPtSumNode node) throws IllegalActionException {
    Type[] childTypes = _inferAllChildren(node);

    List lexicalTokenList = node.getLexicalTokenList();
    int numChildren = node.jjtGetNumChildren();

    Type resultType = childTypes[0];
    for (int i = 1; i < numChildren; i++) {
      Token operator = (Token) lexicalTokenList.get(i - 1);
      Type nextType = childTypes[i];
      if (operator.kind == PtParserConstants.PLUS) {
        resultType = resultType.add(nextType);
      } else if (operator.kind == PtParserConstants.MINUS) {
        resultType = resultType.subtract(nextType);
      } else {
        _assert(false, node, "Invalid operation");
      }
    }
    _setType(node, resultType);
  }
  /**
   * Set the type of the given node to be the least upper bound type of the types of the node's
   * children.
   *
   * @param node The specified node.
   * @exception IllegalActionException If an inference error occurs.
   */
  public void visitProductNode(ASTPtProductNode node) throws IllegalActionException {
    Type[] childTypes = _inferAllChildren(node);

    List lexicalTokenList = node.getLexicalTokenList();
    int numChildren = node.jjtGetNumChildren();

    Type resultType = childTypes[0];
    for (int i = 1; i < numChildren; i++) {
      Token operator = (Token) lexicalTokenList.get(i - 1);
      Type nextType = childTypes[i];
      if (operator.kind == PtParserConstants.MULTIPLY) {
        resultType = resultType.multiply(nextType);
      } else if (operator.kind == PtParserConstants.DIVIDE) {
        resultType = resultType.divide(nextType);
      } else if (operator.kind == PtParserConstants.MODULO) {
        resultType = resultType.modulo(nextType);
      } else {
        _assert(false, node, "Invalid operation");
      }
    }
    _setType(node, resultType);
  }
Пример #5
0
  /**
   * Construct an ArrayToken that contains the tokens from the children of the specified node.
   *
   * @param node The specified node.
   * @exception IllegalActionException If an evaluation error occurs.
   */
  public void visitArrayConstructNode(ASTPtArrayConstructNode node) throws IllegalActionException {
    if (node.isConstant() && node.isEvaluated()) {
      _evaluatedChildToken = node.getToken();
      return;
    }

    ptolemy.data.Token[] tokens = _evaluateAllChildren(node);

    if (tokens.length == 0) {
      _evaluatedChildToken = ArrayToken.NIL;
      node.setToken(_evaluatedChildToken);
      return;
    }

    int numChildren = node.jjtGetNumChildren();

    // Convert up to LUB.
    ptolemy.data.type.Type elementType = tokens[0].getType();

    for (int i = 0; i < numChildren; i++) {
      Type valueType = tokens[i].getType();

      if (!elementType.equals(valueType)) {
        elementType = TypeLattice.leastUpperBound(elementType, valueType);
      }
    }

    for (int i = 0; i < numChildren; i++) {
      tokens[i] = elementType.convert(tokens[i]);
    }

    _evaluatedChildToken = (new ArrayToken(elementType, tokens));

    if (node.isConstant()) {
      node.setToken(_evaluatedChildToken);
    }
  }
Пример #6
0
  /**
   * Evaluate the first child, and depending on its (boolean) result, evaluate either the second or
   * the third child. The result of that evaluation becomes the result of the specified node.
   *
   * @param node The specified node.
   * @exception IllegalActionException If an evaluation error occurs.
   */
  public void visitFunctionalIfNode(ASTPtFunctionalIfNode node) throws IllegalActionException {
    if (node.isConstant() && node.isEvaluated()) {
      _evaluatedChildToken = node.getToken();
      return;
    }

    int numChildren = node.jjtGetNumChildren();

    if (numChildren != 3) {
      // A functional-if node MUST have three children in the parse
      // tree.
      throw new InternalErrorException(
          "PtParser error: a functional-if node does not have "
              + "three children in the parse tree.");
    }

    // evaluate the first sub-expression
    _evaluateChild(node, 0);

    ptolemy.data.Token test = _evaluatedChildToken;

    if (!(test instanceof BooleanToken)) {
      throw new IllegalActionException(
          "Functional-if must branch on a boolean, but instead was "
              + test.toString()
              + " an instance of "
              + test.getClass().getName());
    }

    boolean value = ((BooleanToken) test).booleanValue();

    // Choose the correct sub-expression to evaluate,
    // and type check the other.
    if (_typeInference == null) {
      _typeInference = new ParseTreeTypeInference();
    }

    ASTPtRootNode tokenChild;
    ASTPtRootNode typeChild;

    if (value) {
      tokenChild = (ASTPtRootNode) node.jjtGetChild(1);
      typeChild = (ASTPtRootNode) node.jjtGetChild(2);
    } else {
      tokenChild = (ASTPtRootNode) node.jjtGetChild(2);
      typeChild = (ASTPtRootNode) node.jjtGetChild(1);
    }

    tokenChild.visit(this);

    ptolemy.data.Token token = _evaluatedChildToken;
    Type type = _typeInference.inferTypes(typeChild, _scope);

    Type conversionType = (Type) TypeLattice.lattice().leastUpperBound(type, token.getType());

    token = conversionType.convert(token);
    _evaluatedChildToken = (token);

    if (node.isConstant()) {
      node.setToken(_evaluatedChildToken);
    }
  }