/** * 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); }
/** * Apply a sum operator to the children of the specified node. * * @param node The specified node. * @exception IllegalActionException If an evaluation error occurs. */ public void visitSumNode(ASTPtSumNode node) throws IllegalActionException { if (node.isConstant() && node.isEvaluated()) { _evaluatedChildToken = node.getToken(); return; } ptolemy.data.Token[] tokens = _evaluateAllChildren(node); List lexicalTokenList = node.getLexicalTokenList(); int numChildren = node.jjtGetNumChildren(); _assert(numChildren > 0, node, "The number of child nodes must be greater than zero"); _assert( numChildren == (lexicalTokenList.size() + 1), node, "The number of child nodes is " + "not equal to number of operators plus one"); ptolemy.data.Token result = tokens[0]; for (int i = 1; i < numChildren; i++) { Token operator = (Token) lexicalTokenList.get(i - 1); ptolemy.data.Token nextToken = tokens[i]; if (operator.kind == PtParserConstants.PLUS) { result = result.add(nextToken); } else if (operator.kind == PtParserConstants.MINUS) { result = result.subtract(nextToken); } else { _assert(false, node, "Invalid operation"); } } _evaluatedChildToken = (result); if (node.isConstant()) { node.setToken(_evaluatedChildToken); } }