Program: 74142
Project: 74142
Buggy source:
/** 
 * Apply a function to the children of the specified node. This also handles indexing into matrices and arrays, which look like function calls. In the simplest cases, if the function is being applied to an expression that evaluated to a FunctionToken, an ArrayToken, or a MatrixToken, then the function application is simply applied to the available arguments. More complex is if the function is being applied to an expression that does not evaluate as above, resulting in three cases:  Of primary interest is a function node that represents the invocation of a Java method registered with the expression parser.  This method uses the reflection mechanism in the CachedMethod class to find the correct method, based on the types of the arguments and invoke it.  See that class for information about how method arguments are matched. A second case is the eval() function, which is handled specially in this method.  The argument to the function is evaluated, and the parsed as a string using the expression parser.  The result is then evaluated *in this evaluator*. This has the effect that any identifiers are evaluated in the same scope as the original expression. A third case is the matlab() function, which is also handled specially in this method, allowing the evaluation of expressions in matlab if matlab is installed.  The format of the function is covered in  {@link ptolemy.data.expr.MatlabUtilities#evaluate(String,Set,ParserScope)}.
 * @param node The specified node.
 * @exception IllegalActionException If an parse error occurs.
 */
public void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node) throws IllegalActionException {
  String result="String_Node_Str";
  ptolemy.data.Token value=null;
  Type type=null;
  String functionName=node.getFunctionName();
  if ((functionName != null) && (_scope != null)) {
    value=_scope.get(node.getFunctionName());
    type=_scope.getType(node.getFunctionName());
  }
  if (functionName != null) {
    int index=functionName.indexOf("String_Node_Str");
    if (index > 0) {
      String label=value.toString();
      if (label.startsWith("String_Node_Str")) {
        label=label.substring(7,label.length() - 1);
        int position=label.indexOf("String_Node_Str");
        result+=label.substring(0,position + 1);
        _evaluateChild(node,1);
        result+=_childCode + label.substring(position + 2);
        _childCode=result;
        return;
      }
    }
    String cFunction=(String)cFunctionMap.get(functionName);
    if (cFunction != null) {
      functionName=cFunction;
    }
  }
  int argCount=node.jjtGetNumChildren() - 1;
  if ((value != null) || (functionName == null)) {
    if (value == null) {
      value=_evaluateChild(node,0);
    }
    if (type instanceof ArrayType) {
      if (argCount == 1) {
        _evaluateArrayIndex(node,value,type);
      }
 else {
        throw new IllegalActionException("String_Node_Str" + "String_Node_Str" + node.getFunctionName());
      }
    }
 else     if (type instanceof MatrixType) {
      if (argCount == 2) {
      }
 else {
        throw new IllegalActionException("String_Node_Str" + "String_Node_Str" + node.getFunctionName());
      }
    }
 else     if (type instanceof FunctionType) {
      FunctionToken function=(FunctionToken)value;
      if (function.getNumberOfArguments() != argCount) {
        throw new IllegalActionException("String_Node_Str" + "String_Node_Str" + value.toString());
      }
    }
 else {
      throw new IllegalActionException("String_Node_Str" + value.getType() + "String_Node_Str"+ value.toString()+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
    }
    return;
  }
  result+=functionName + "String_Node_Str";
  for (int i=0; i < argCount; i++) {
    if (i != 0) {
      result+="String_Node_Str";
    }
    _evaluateChild(node,i + 1);
    result+=_specializeArgument(functionName,i,((ASTPtRootNode)node.jjtGetChild(i + 1)).getType(),_childCode);
  }
}

Fixed version (ground truth):
/** 
 * Apply a function to the children of the specified node. This also handles indexing into matrices and arrays, which look like function calls. In the simplest cases, if the function is being applied to an expression that evaluated to a FunctionToken, an ArrayToken, or a MatrixToken, then the function application is simply applied to the available arguments. More complex is if the function is being applied to an expression that does not evaluate as above, resulting in three cases:  Of primary interest is a function node that represents the invocation of a Java method registered with the expression parser.  This method uses the reflection mechanism in the CachedMethod class to find the correct method, based on the types of the arguments and invoke it.  See that class for information about how method arguments are matched. A second case is the eval() function, which is handled specially in this method.  The argument to the function is evaluated, and the parsed as a string using the expression parser.  The result is then evaluated *in this evaluator*. This has the effect that any identifiers are evaluated in the same scope as the original expression. A third case is the matlab() function, which is also handled specially in this method, allowing the evaluation of expressions in matlab if matlab is installed.  The format of the function is covered in  {@link ptolemy.data.expr.MatlabUtilities#evaluate(String,Set,ParserScope)}.
 * @param node The specified node.
 * @exception IllegalActionException If an parse error occurs.
 */
public void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node) throws IllegalActionException {
  String result="String_Node_Str";
  ptolemy.data.Token value=null;
  Type type=null;
  String functionName=node.getFunctionName();
  if ((functionName != null) && (_scope != null)) {
    value=_scope.get(node.getFunctionName());
    type=_scope.getType(node.getFunctionName());
  }
  if (functionName != null) {
    int index=functionName.indexOf("String_Node_Str");
    if (index > 0) {
      String label=value.toString();
      if (label.startsWith("String_Node_Str")) {
        label=label.substring(7,label.length() - 1);
        int position=label.indexOf("String_Node_Str");
        result+=label.substring(0,position + 1);
        _evaluateChild(node,1);
        result+=_childCode + label.substring(position + 2);
        _childCode=result;
        return;
      }
    }
    String cFunction=(String)cFunctionMap.get(functionName);
    if (cFunction != null) {
      functionName=cFunction;
    }
  }
  int argCount=node.jjtGetNumChildren() - 1;
  if ((value != null) || (functionName == null)) {
    if (value == null) {
      value=_evaluateChild(node,0);
    }
    if (type instanceof ArrayType) {
      if (argCount == 1) {
        _evaluateArrayIndex(node,value,type);
      }
 else {
        throw new IllegalActionException("String_Node_Str" + "String_Node_Str" + node.getFunctionName());
      }
    }
 else     if (type instanceof MatrixType) {
      if (argCount == 2) {
      }
 else {
        throw new IllegalActionException("String_Node_Str" + "String_Node_Str" + node.getFunctionName());
      }
    }
 else     if (type instanceof FunctionType) {
      FunctionToken function=(FunctionToken)value;
      if (function.getNumberOfArguments() != argCount) {
        throw new IllegalActionException("String_Node_Str" + "String_Node_Str" + value.toString());
      }
    }
 else {
      throw new IllegalActionException("String_Node_Str" + value.getType() + "String_Node_Str"+ value.toString()+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
    }
    return;
  }
  result+=functionName + "String_Node_Str";
  for (int i=0; i < argCount; i++) {
    if (i != 0) {
      result+="String_Node_Str";
    }
    _evaluateChild(node,i + 1);
    result+=_specializeArgument(functionName,i,((ASTPtRootNode)node.jjtGetChild(i + 1)).getType(),_childCode);
  }
  _childCode=result + "String_Node_Str";
}