001/* ASTPtFunctionDefinitionNode represent function definitions in the 002 parse tree. 003 004 Copyright (c) 2002-2015 The Regents of the University of California. 005 All rights reserved. 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the above 009 copyright notice and the following two paragraphs appear in all copies 010 of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 028 029 Created: September 2002 030 031 */ 032package ptolemy.data.expr; 033 034import java.util.ArrayList; 035import java.util.HashMap; 036import java.util.Iterator; 037import java.util.List; 038import java.util.Map; 039 040import ptolemy.data.type.Type; 041import ptolemy.kernel.util.IllegalActionException; 042 043////////////////////////////////////////////////////////////////////////// 044//// ASTPtFunctionDefinitionNode 045 046/** 047 The parse tree created from function definitions of the form: 048 <pre> 049 function (x) x + 5 050 </pre> 051 052 which defines a function of one argument. The above is assumed to 053 have arguments declared of type general. Monomorphic type-safe 054 functions can be declared using syntax like: 055 056 <pre> 057 function (x:int) x+5 058 </pre> 059 060 This declares that the function only takes integer arguments. The 061 return type (in this case integer, since the result of adding 5 to an 062 integer is an integer) is inferred automatically. 063 064 FIXME: check argument name duplication 065 066 @author Xiaojun Liu, Steve Neuendorffer 067 @version $Id$ 068 @since Ptolemy II 0.2 069 @Pt.ProposedRating Yellow (nsmyth) 070 @Pt.AcceptedRating Red (cxh) 071 @see ptolemy.data.expr.ASTPtRootNode 072 @see ptolemy.data.expr.PtParser 073 @see ptolemy.data.Token 074 */ 075public class ASTPtFunctionDefinitionNode extends ASTPtRootNode { 076 /** Create a function definition node with an id. 077 * @param id the id. 078 */ 079 public ASTPtFunctionDefinitionNode(int id) { 080 super(id); 081 } 082 083 /** Create a function definition node with a parser and an id. 084 * @param p The parser 085 * @param id the id 086 */ 087 public ASTPtFunctionDefinitionNode(PtParser p, int id) { 088 super(p, id); 089 } 090 091 /** Clone the parse tree node by invoking the clone() method of 092 * the base class. The new node copies the list of operators (+, -) 093 * represented by this node. 094 * @return A new parse tree node. 095 * @exception CloneNotSupportedException If the superclass clone() 096 * method throws it. 097 */ 098 @Override 099 public Object clone() throws CloneNotSupportedException { 100 ASTPtFunctionDefinitionNode newNode = (ASTPtFunctionDefinitionNode) super.clone(); 101 newNode._argList = (ArrayList) _argList.clone(); 102 return newNode; 103 } 104 105 /** Return the list of argument names. 106 * @return The list of argument names. 107 */ 108 public List getArgumentNameList() { 109 return _argList; 110 } 111 112 /** Return the type of the arguments, or null if type inference 113 * has not occurred yet. 114 * @return the type of the arguments, or null if type inference 115 * has not occurred yet. 116 */ 117 public Type[] getArgumentTypes() { 118 return _argTypes; 119 } 120 121 /** Return the parse tree of the expression for this function. 122 * @return the parse tree of the expression for this function. 123 */ 124 public ASTPtRootNode getExpressionTree() { 125 // The first children are the arguments. 126 return (ASTPtRootNode) jjtGetChild(jjtGetNumChildren() - 1); 127 } 128 129 /** Return true if this node is (hierarchically) congruent to the 130 * given node, under the given renaming of bound identifiers. 131 * Derived classes should extend this method to add additional 132 * necessary congruency checks. 133 * @param node The node to compare to. 134 * @param renaming A map from String to String that gives a 135 * renaming from identifiers in this node to identifiers in the 136 * given node. 137 */ 138 @Override 139 public boolean isCongruent(ASTPtRootNode node, Map renaming) { 140 if (!(node instanceof ASTPtFunctionDefinitionNode)) { 141 return false; 142 } 143 144 ASTPtFunctionDefinitionNode functionNode = (ASTPtFunctionDefinitionNode) node; 145 146 // The number of arguments must be the same. 147 if (getArgumentNameList().size() != functionNode.getArgumentNameList() 148 .size()) { 149 return false; 150 } 151 152 Map newRenaming = new HashMap(renaming); 153 154 Iterator argNames = functionNode.getArgumentNameList().iterator(); 155 156 for (Iterator names = getArgumentNameList().iterator(); names 157 .hasNext();) { 158 String name = (String) names.next(); 159 String argName = (String) argNames.next(); 160 newRenaming.put(name, argName); 161 } 162 163 if (!super.isCongruent(node, newRenaming)) { 164 return false; 165 } 166 167 return true; 168 } 169 170 /** Close this node. 171 */ 172 @Override 173 public void jjtClose() { 174 super.jjtClose(); 175 _argList.trimToSize(); 176 } 177 178 /** Traverse this node with the given visitor. 179 */ 180 @Override 181 public void visit(ParseTreeVisitor visitor) throws IllegalActionException { 182 visitor.visitFunctionDefinitionNode(this); 183 } 184 185 protected ArrayList _argList = new ArrayList(); 186 187 protected Type[] _argTypes = null; 188}