001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es.parser;
030:
031: import com.caucho.es.ESBase;
032: import com.caucho.es.ESBoolean;
033: import com.caucho.es.ESException;
034: import com.caucho.es.ESWrapperException;
035:
036: import java.io.IOException;
037:
038: /**
039: * Expr is an intermediate form representing an expression.
040: */
041: class BooleanBinaryExpr extends BinaryExpr {
042: private BooleanBinaryExpr(Block block, Expr left, Expr right, int op) {
043: super (block, left, right, op);
044: }
045:
046: static Expr create(Block block, Expr left, Expr right, int op)
047: throws ESException {
048: if (!(left instanceof LiteralExpr)
049: || !(right instanceof LiteralExpr))
050: return new BooleanBinaryExpr(block, left, right, op);
051:
052: ESBase lvalue = ((LiteralExpr) left).getLiteral();
053: ESBase rvalue = ((LiteralExpr) right).getLiteral();
054: boolean value;
055:
056: try {
057: switch (op) {
058: case '<':
059: value = lvalue.lessThan(rvalue, false);
060: break;
061:
062: case '>':
063: value = rvalue.lessThan(lvalue, false);
064: break;
065:
066: case Lexer.LEQ:
067: value = rvalue.lessThan(lvalue, true);
068: break;
069:
070: case Lexer.GEQ:
071: value = lvalue.lessThan(rvalue, true);
072: break;
073:
074: case Lexer.EQ:
075: value = lvalue.ecmaEquals(rvalue);
076: break;
077:
078: case Lexer.NEQ:
079: value = !lvalue.ecmaEquals(rvalue);
080: break;
081:
082: case Lexer.STRICT_EQ:
083: value = lvalue.equals(rvalue);
084: break;
085:
086: case Lexer.STRICT_NEQ:
087: value = !lvalue.equals(rvalue);
088: break;
089:
090: default:
091: throw new RuntimeException("foo");
092: }
093: } catch (Throwable e) {
094: throw new ESWrapperException(e);
095: }
096:
097: return new LiteralExpr(block, ESBoolean.create(value));
098: }
099:
100: int getType() {
101: return TYPE_BOOLEAN;
102: }
103:
104: void printBooleanImpl() throws IOException {
105: switch (op) {
106: case '<':
107: if (left.getType() == TYPE_INTEGER
108: && right.getType() == TYPE_INTEGER) {
109: cl.print("(");
110: left.printInt32();
111: cl.print("<");
112: right.printInt32();
113: cl.print(")");
114: } else if (left.isNumeric() || right.isNumeric()) {
115: cl.print("(");
116: left.printNum();
117: cl.print("<");
118: right.printNum();
119: cl.print(")");
120: } else {
121: left.print();
122: cl.print(".lessThan(");
123: right.print();
124: cl.print(", false)");
125: }
126: break;
127:
128: case '>':
129: if (left.isNumeric() || right.isNumeric()) {
130: cl.print("(");
131: left.printNum();
132: cl.print(">");
133: right.printNum();
134: cl.print(")");
135: } else {
136: left.print();
137: cl.print(".greaterThan(");
138: right.print();
139: cl.print(", false)");
140: }
141: break;
142:
143: case Lexer.LEQ:
144: if (left.isNumeric() || right.isNumeric()) {
145: cl.print("(");
146: left.printNum();
147: cl.print("<=");
148: right.printNum();
149: cl.print(")");
150: } else {
151: left.print();
152: cl.print(".greaterThan(");
153: right.print();
154: cl.print(", true)");
155: }
156: break;
157:
158: case Lexer.GEQ:
159: if (left.isNumeric() || right.isNumeric()) {
160: cl.print("(");
161: left.printNum();
162: cl.print(">=");
163: right.printNum();
164: cl.print(")");
165: } else {
166: left.print();
167: cl.print(".lessThan(");
168: right.print();
169: cl.print(", true)");
170: }
171: break;
172:
173: case Lexer.EQ:
174: if (left.isNumeric() && right.isNumeric()) {
175: cl.print("(");
176: left.printNum();
177: cl.print("==");
178: right.printNum();
179: cl.print(")");
180: } else {
181: left.print();
182: cl.print(".ecmaEquals(");
183: right.print();
184: cl.print(")");
185: }
186: break;
187:
188: case Lexer.NEQ:
189: if (left.isNumeric() && right.isNumeric()) {
190: cl.print("(");
191: left.printNum();
192: cl.print("!=");
193: right.printNum();
194: cl.print(")");
195: } else {
196: cl.print("!");
197: left.print();
198: cl.print(".ecmaEquals(");
199: right.print();
200: cl.print(")");
201: }
202: break;
203:
204: case Lexer.STRICT_EQ:
205: if (left.isNumeric() && right.isNumeric()) {
206: cl.print("(");
207: left.printNum();
208: cl.print("==");
209: right.printNum();
210: cl.print(")");
211: } else {
212: left.print();
213: cl.print(".equals(");
214: right.print();
215: cl.print(")");
216: }
217: break;
218:
219: case Lexer.STRICT_NEQ:
220: if (left.isNumeric() && right.isNumeric()) {
221: cl.print("(");
222: left.printNum();
223: cl.print("!=");
224: right.printNum();
225: cl.print(")");
226: } else {
227: cl.print("!");
228: left.print();
229: cl.print(".equals(");
230: right.print();
231: cl.print(")");
232: }
233: break;
234:
235: default:
236: throw new IOException("foo");
237: }
238: }
239: }
|