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.ESException;
032: import com.caucho.es.ESString;
033:
034: import java.io.IOException;
035:
036: /**
037: * Expr is an intermediate form representing an expression.
038: */
039: class PlusExpr extends Expr {
040: Expr left;
041: Expr right;
042: int op;
043:
044: private PlusExpr(Block block, Expr left, Expr right) {
045: super (block);
046:
047: this .left = left;
048: this .right = right;
049: }
050:
051: /**
052: * When possible, reorder the arguments to avoid allocating an extra
053: * char buffer.
054: *
055: * x + ("b" + y) -> (x + "b") + y
056: */
057: static Expr create(Block block, Expr left, Expr right)
058: throws ESException {
059: if (left instanceof LiteralExpr
060: && right instanceof LiteralExpr
061: && (left.getType() == TYPE_STRING || right.getType() == TYPE_STRING)) {
062: LiteralExpr leftLit = (LiteralExpr) left;
063: LiteralExpr rightLit = (LiteralExpr) right;
064:
065: String string = leftLit.getLiteral().toString()
066: + rightLit.getLiteral();
067:
068: return new LiteralExpr(block, ESString.create(string));
069: }
070:
071: if (!(right instanceof PlusExpr)
072: || right.getType() != TYPE_STRING)
073: return new PlusExpr(block, left, right);
074:
075: PlusExpr pright = (PlusExpr) right;
076:
077: if (pright.left.getType() == TYPE_STRING)
078: return create(block, create(block, left, pright.left),
079: pright.right);
080: else
081: return new PlusExpr(block, left, right);
082: }
083:
084: int getType() {
085: int ltype = left.getType();
086: int rtype = right.getType();
087:
088: if (ltype == TYPE_INTEGER && rtype == TYPE_INTEGER)
089: return TYPE_INTEGER;
090: else if (ltype == TYPE_STRING || rtype == TYPE_STRING)
091: return TYPE_STRING;
092: else if (left.isNumeric() && right.isNumeric()) {
093: return TYPE_NUMBER;
094: } else
095: return TYPE_ES;
096: }
097:
098: void exprStatement(Function fun) throws ESException {
099: left.exprStatement(fun);
100: right.exprStatement(fun);
101: }
102:
103: void printInt32Impl() throws IOException {
104: cl.print("(");
105: left.printInt32();
106: cl.print("+");
107: right.printInt32();
108: cl.print(")");
109: }
110:
111: void printNumImpl() throws IOException {
112: cl.print("(");
113: left.printNum();
114: cl.print("+");
115: right.printNum();
116: cl.print(")");
117: }
118:
119: void printStringImpl() throws IOException {
120: printCharBufferAppend();
121: cl.print(".close()");
122: }
123:
124: void printCharBufferAppend() throws IOException {
125: if (left instanceof PlusExpr && left.getType() == TYPE_STRING) {
126: ((PlusExpr) left).printCharBufferAppend();
127: } else {
128: cl.print("CharBuffer.allocate()");
129: cl.print(".append(");
130: left.printJavaString();
131: cl.print(")");
132: }
133:
134: cl.print(".append(");
135: right.printJavaString();
136: cl.print(")");
137: }
138:
139: void printImpl() throws IOException {
140: left.print();
141: cl.print(".plus(");
142: right.print();
143: cl.print(")");
144: }
145: }
|