001/* ASTPtUnaryNode represent the unary operator(!, -) nodes in the parse tree
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.data.expr;
029
030import java.util.Map;
031
032import ptolemy.kernel.util.IllegalActionException;
033
034///////////////////////////////////////////////////////////////////
035//// ASTPtUnaryNode
036
037/**
038 The parse tree created from the expression string consists of a
039 hierarchy of node objects. This class represents unary operator(!, -, ~)
040 nodes in the parse tree.
041
042 @author Neil Smyth
043 @version $Id$
044 @since Ptolemy II 0.2
045 @Pt.ProposedRating Yellow (nsmyth)
046 @Pt.AcceptedRating Red (cxh)
047 @see ptolemy.data.expr.ASTPtRootNode
048 @see ptolemy.data.expr.PtParser
049 @see ptolemy.data.Token
050 */
051public class ASTPtUnaryNode extends ASTPtRootNode {
052    ///////////////////////////////////////////////////////////////////
053    ////                         public methods                    ////
054    public ASTPtUnaryNode(int id) {
055        super(id);
056    }
057
058    public ASTPtUnaryNode(PtParser p, int id) {
059        super(p, id);
060    }
061
062    /** Return the token that represents the operation of this node.
063     *  @return the token that represents the operation of this node.
064     */
065    public Token getOperator() {
066        return _lexicalToken;
067    }
068
069    /** Return true if this node represents the bitwise negation of its
070     *  child.
071     *  @return true if this node represents the bitwise negation of its
072     *  child.
073     */
074    public boolean isBitwiseNot() {
075        return _isBitwiseNot;
076    }
077
078    /** Return true if this node is (hierarchically) congruent to the
079     *  given node, under the given renaming of bound identifiers.
080     *  Derived classes should extend this method to add additional
081     *  necessary congruency checks.
082     *  @param node The node to compare to.
083     *  @param renaming A map from String to String that gives a
084     *  renaming from identifiers in this node to identifiers in the
085     *  given node.
086     *  @return True if this node is congruent.
087     */
088    @Override
089    public boolean isCongruent(ASTPtRootNode node, Map renaming) {
090        if (!super.isCongruent(node, renaming)) {
091            return false;
092        }
093
094        if (_lexicalToken.kind != ((ASTPtUnaryNode) node)._lexicalToken.kind) {
095            return false;
096        }
097
098        return true;
099    }
100
101    /** Return true if this node represents the additive inverse of its
102     *  child.
103     *  @return true if this node represents the additive inverse of its
104     *  child.
105     */
106    public boolean isMinus() {
107        return _isMinus;
108    }
109
110    /** Return true if this node represents the boolean negation of its
111     *  child.
112     *  @return true if this node represents the boolean negation of
113     *  its child.
114     */
115    public boolean isNot() {
116        return _isNot;
117    }
118
119    /** Traverse this node with the given visitor.
120     */
121    @Override
122    public void visit(ParseTreeVisitor visitor) throws IllegalActionException {
123        visitor.visitUnaryNode(this);
124    }
125
126    ///////////////////////////////////////////////////////////////////
127    ////                         protected variables               ////
128    protected boolean _isMinus = false;
129
130    protected boolean _isNot = false;
131
132    protected boolean _isBitwiseNot = false;
133
134    protected Token _lexicalToken = null;
135}