コード例 #1
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);
    }
  }
コード例 #2
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);
    }
  }