1 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 ; 37 38 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 ("foo"); 93 } 94 } catch (Throwable 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 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 ("foo"); 239 } 240 } 241 } 242 243 244 | Popular Tags |