KickJava   Java API By Example, From Geeks To Geeks.

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


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.ESException;
32 import com.caucho.es.ESString;
33
34 import java.io.IOException JavaDoc;
35
36 /**
37  * Expr is an intermediate form representing an expression.
38  */

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   /**
53    * When possible, reorder the arguments to avoid allocating an extra
54    * char buffer.
55    *
56    * x + ("b" + y) -> (x + "b") + y
57    */

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 JavaDoc 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 JavaDoc
107   {
108     cl.print("(");
109     left.printInt32();
110     cl.print("+");
111     right.printInt32();
112     cl.print(")");
113   }
114
115   void printNumImpl() throws IOException JavaDoc
116   {
117     cl.print("(");
118     left.printNum();
119     cl.print("+");
120     right.printNum();
121     cl.print(")");
122   }
123
124   void printStringImpl() throws IOException JavaDoc
125   {
126     printCharBufferAppend();
127     cl.print(".close()");
128   }
129
130   void printCharBufferAppend() throws IOException JavaDoc
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 JavaDoc
148   {
149     left.print();
150     cl.print(".plus(");
151     right.print();
152     cl.print(")");
153   }
154 }
155
Popular Tags