001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.tree;
022:
023: import EDU.purdue.cs.bloat.editor.*;
024:
025: /**
026: * ArithExpr represents a binary arithmetic expression. It consists of two
027: * operands and an operator.
028: */
029: public class ArithExpr extends Expr {
030: char operation; // Arithmetic operator
031:
032: Expr left; // Expression on left-hand side of operation
033:
034: Expr right; // Expression on right-hand side of operation
035:
036: // Operators...
037: public static final char ADD = '+';
038:
039: public static final char SUB = '-';
040:
041: public static final char DIV = '/';
042:
043: public static final char MUL = '*';
044:
045: public static final char REM = '%';
046:
047: public static final char AND = '&';
048:
049: public static final char IOR = '|';
050:
051: public static final char XOR = '^';
052:
053: public static final char CMP = '?';
054:
055: public static final char CMPL = '<';
056:
057: public static final char CMPG = '>';
058:
059: /**
060: * Constructor.
061: *
062: * @param operation
063: * Arithmetic operation that this expression performs.
064: * @param left
065: * Left-hand argument to operation.
066: * @param right
067: * Right-hand argument to operation.
068: * @param type
069: * The type of this expression.
070: */
071: public ArithExpr(final char operation, final Expr left,
072: final Expr right, final Type type) {
073: super (type);
074: this .operation = operation;
075: this .left = left;
076: this .right = right;
077:
078: left.setParent(this );
079: right.setParent(this );
080: }
081:
082: public int operation() {
083: return operation;
084: }
085:
086: public Expr left() {
087: return left;
088: }
089:
090: public Expr right() {
091: return right;
092: }
093:
094: public void visitForceChildren(final TreeVisitor visitor) {
095: if (visitor.reverse()) {
096: right.visit(visitor);
097: left.visit(visitor);
098: } else {
099: left.visit(visitor);
100: right.visit(visitor);
101: }
102: }
103:
104: public void visit(final TreeVisitor visitor) {
105: visitor.visitArithExpr(this );
106: }
107:
108: public int exprHashCode() {
109: return 1 + operation ^ left.exprHashCode()
110: ^ right.exprHashCode();
111: }
112:
113: /**
114: * Compare this arithmetic expression to another Expression.
115: *
116: * @return True, if both expressions have the same contents.
117: */
118: public boolean equalsExpr(final Expr other) {
119: return (other != null) && (other instanceof ArithExpr)
120: && (((ArithExpr) other).operation == operation)
121: && ((ArithExpr) other).left.equalsExpr(left)
122: && ((ArithExpr) other).right.equalsExpr(right);
123: }
124:
125: public Object clone() {
126: return copyInto(new ArithExpr(operation, (Expr) left.clone(),
127: (Expr) right.clone(), type));
128: }
129: }
|