1 28 29 package com.caucho.es.parser; 30 31 import com.caucho.es.ESException; 32 import com.caucho.es.ESString; 33 34 import java.io.IOException ; 35 36 39 class PlusExpr extends Expr { 40 Expr left; 41 Expr right; 42 int op; 43 44 private PlusExpr(Block block, Expr left, Expr right) 45 { 46 super(block); 47 48 this.left = left; 49 this.right = right; 50 } 51 52 58 static Expr create(Block block, Expr left, Expr right) 59 throws ESException 60 { 61 if (left instanceof LiteralExpr && 62 right instanceof LiteralExpr && 63 (left.getType() == TYPE_STRING || right.getType() == TYPE_STRING)) { 64 LiteralExpr leftLit = (LiteralExpr) left; 65 LiteralExpr rightLit = (LiteralExpr) right; 66 67 String string = leftLit.getLiteral().toString() + rightLit.getLiteral(); 68 69 return new LiteralExpr(block, ESString.create(string)); 70 } 71 72 if (! (right instanceof PlusExpr) || 73 right.getType() != TYPE_STRING) 74 return new PlusExpr(block, left, right); 75 76 PlusExpr pright = (PlusExpr) right; 77 78 if (pright.left.getType() == TYPE_STRING) 79 return create(block, create(block, left, pright.left), pright.right); 80 else 81 return new PlusExpr(block, left, right); 82 } 83 84 int getType() 85 { 86 int ltype = left.getType(); 87 int rtype = right.getType(); 88 89 if (ltype == TYPE_INTEGER && rtype == TYPE_INTEGER) 90 return TYPE_INTEGER; 91 else if (ltype == TYPE_STRING || rtype == TYPE_STRING) 92 return TYPE_STRING; 93 else if (left.isNumeric() && right.isNumeric()) { 94 return TYPE_NUMBER; 95 } 96 else 97 return TYPE_ES; 98 } 99 100 void exprStatement(Function fun) throws ESException 101 { 102 left.exprStatement(fun); 103 right.exprStatement(fun); 104 } 105 106 void printInt32Impl() throws IOException 107 { 108 cl.print("("); 109 left.printInt32(); 110 cl.print("+"); 111 right.printInt32(); 112 cl.print(")"); 113 } 114 115 void printNumImpl() throws IOException 116 { 117 cl.print("("); 118 left.printNum(); 119 cl.print("+"); 120 right.printNum(); 121 cl.print(")"); 122 } 123 124 void printStringImpl() throws IOException 125 { 126 printCharBufferAppend(); 127 cl.print(".close()"); 128 } 129 130 void printCharBufferAppend() throws IOException 131 { 132 if (left instanceof PlusExpr && left.getType() == TYPE_STRING) { 133 ((PlusExpr) left).printCharBufferAppend(); 134 } 135 else { 136 cl.print("CharBuffer.allocate()"); 137 cl.print(".append("); 138 left.printJavaString(); 139 cl.print(")"); 140 } 141 142 cl.print(".append("); 143 right.printJavaString(); 144 cl.print(")"); 145 } 146 147 void printImpl() throws IOException 148 { 149 left.print(); 150 cl.print(".plus("); 151 right.print(); 152 cl.print(")"); 153 } 154 } 155 | Popular Tags |