KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > parser > BooleanBinaryExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es.parser;
30
31 import com.caucho.es.ESBase;
32 import com.caucho.es.ESBoolean;
33 import com.caucho.es.ESException;
34 import com.caucho.es.ESWrapperException;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Expr is an intermediate form representing an expression.
40  */

41 class BooleanBinaryExpr extends BinaryExpr {
42   private BooleanBinaryExpr(Block block, Expr left, Expr right, int op)
43   {
44     super(block, left, right, op);
45   }
46
47   static Expr create(Block block, Expr left, Expr right, int op)
48     throws ESException
49   {
50     if (! (left instanceof LiteralExpr) || ! (right instanceof LiteralExpr))
51       return new BooleanBinaryExpr(block, left, right, op);
52       
53     ESBase lvalue = ((LiteralExpr) left).getLiteral();
54     ESBase rvalue = ((LiteralExpr) right).getLiteral();
55     boolean value;
56
57     try {
58     switch (op) {
59     case '<':
60       value = lvalue.lessThan(rvalue, false);
61       break;
62
63     case '>':
64       value = rvalue.lessThan(lvalue, false);
65       break;
66
67     case Lexer.LEQ:
68       value = rvalue.lessThan(lvalue, true);
69       break;
70
71     case Lexer.GEQ:
72       value = lvalue.lessThan(rvalue, true);
73       break;
74
75     case Lexer.EQ:
76       value = lvalue.ecmaEquals(rvalue);
77       break;
78
79     case Lexer.NEQ:
80       value = ! lvalue.ecmaEquals(rvalue);
81       break;
82
83     case Lexer.STRICT_EQ:
84       value = lvalue.equals(rvalue);
85       break;
86
87     case Lexer.STRICT_NEQ:
88       value = ! lvalue.equals(rvalue);
89       break;
90
91     default:
92       throw new RuntimeException JavaDoc("foo");
93     }
94     } catch (Throwable JavaDoc e) {
95       throw new ESWrapperException(e);
96     }
97
98     return new LiteralExpr(block, ESBoolean.create(value));
99   }
100
101   int getType()
102   {
103     return TYPE_BOOLEAN;
104   }
105
106   void printBooleanImpl() throws IOException JavaDoc
107   {
108     switch (op) {
109     case '<':
110       if (left.getType() == TYPE_INTEGER && right.getType() == TYPE_INTEGER) {
111         cl.print("(");
112         left.printInt32();
113         cl.print("<");
114         right.printInt32();
115         cl.print(")");
116       } else if (left.isNumeric() || right.isNumeric()) {
117         cl.print("(");
118         left.printNum();
119         cl.print("<");
120         right.printNum();
121         cl.print(")");
122       } else {
123         left.print();
124         cl.print(".lessThan(");
125         right.print();
126         cl.print(", false)");
127       }
128       break;
129
130     case '>':
131       if (left.isNumeric() || right.isNumeric()) {
132         cl.print("(");
133         left.printNum();
134         cl.print(">");
135         right.printNum();
136         cl.print(")");
137       } else {
138         left.print();
139         cl.print(".greaterThan(");
140         right.print();
141         cl.print(", false)");
142       }
143       break;
144
145     case Lexer.LEQ:
146       if (left.isNumeric() || right.isNumeric()) {
147         cl.print("(");
148         left.printNum();
149         cl.print("<=");
150         right.printNum();
151         cl.print(")");
152       } else {
153         left.print();
154         cl.print(".greaterThan(");
155         right.print();
156         cl.print(", true)");
157       }
158       break;
159
160     case Lexer.GEQ:
161       if (left.isNumeric() || right.isNumeric()) {
162         cl.print("(");
163         left.printNum();
164         cl.print(">=");
165         right.printNum();
166         cl.print(")");
167       } else {
168         left.print();
169         cl.print(".lessThan(");
170         right.print();
171         cl.print(", true)");
172       }
173       break;
174
175     case Lexer.EQ:
176       if (left.isNumeric() && right.isNumeric()) {
177         cl.print("(");
178         left.printNum();
179         cl.print("==");
180         right.printNum();
181         cl.print(")");
182       } else {
183         left.print();
184         cl.print(".ecmaEquals(");
185         right.print();
186         cl.print(")");
187       }
188       break;
189
190     case Lexer.NEQ:
191       if (left.isNumeric() && right.isNumeric()) {
192         cl.print("(");
193         left.printNum();
194         cl.print("!=");
195         right.printNum();
196         cl.print(")");
197       } else {
198         cl.print("!");
199         left.print();
200         cl.print(".ecmaEquals(");
201         right.print();
202         cl.print(")");
203       }
204       break;
205
206     case Lexer.STRICT_EQ:
207       if (left.isNumeric() && right.isNumeric()) {
208         cl.print("(");
209         left.printNum();
210         cl.print("==");
211         right.printNum();
212         cl.print(")");
213       } else {
214         left.print();
215         cl.print(".equals(");
216         right.print();
217         cl.print(")");
218       }
219       break;
220
221     case Lexer.STRICT_NEQ:
222       if (left.isNumeric() && right.isNumeric()) {
223         cl.print("(");
224         left.printNum();
225         cl.print("!=");
226         right.printNum();
227         cl.print(")");
228       } else {
229         cl.print("!");
230         left.print();
231         cl.print(".equals(");
232         right.print();
233         cl.print(")");
234       }
235       break;
236
237     default:
238       throw new IOException JavaDoc("foo");
239     }
240   }
241 }
242
243
244
Popular Tags