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:
033: import java.io.IOException;
034:
035: /**
036: * Expr is an intermediate form representing an expression.
037: */
038: class PostfixExpr extends Expr {
039: final static int PREINC = 'i';
040: final static int PREDEC = 'd';
041: final static int POSTINC = 'I';
042: final static int POSTDEC = 'D';
043:
044: private int code;
045: private FieldExpr field;
046: private IdExpr id;
047:
048: private AssignExpr cheat;
049:
050: PostfixExpr(Block block, int code, FieldExpr field) {
051: super (block);
052:
053: this .code = code;
054: this .field = field;
055:
056: if (field != null)
057: field.setUsed();
058: }
059:
060: PostfixExpr(Block block, int code, IdExpr id) {
061: super (block);
062:
063: this .code = code;
064: this .id = id;
065:
066: if (id != null)
067: id.setUsed();
068: }
069:
070: int getType() {
071: if (id == null || !id.isLocal())
072: return TYPE_NUMBER;
073: else if (id.getType() == TYPE_INTEGER)
074: return TYPE_INTEGER;
075: else if (id.getType() == TYPE_NUMBER)
076: return TYPE_NUMBER;
077: else
078: return TYPE_ES;
079: }
080:
081: void exprStatement(Function fun) throws ESException {
082: setTop();
083: fun.addExpr(this );
084: }
085:
086: void printInt32Impl() throws IOException {
087: switch (code) {
088: case PREINC:
089: if (!noValue)
090: cl.print("(");
091: cl.print(id.getId());
092: cl.print(" = ");
093: id.printInt32();
094: cl.print(" + 1");
095: if (!noValue)
096: cl.print(")");
097: return;
098: case PREDEC:
099: if (!noValue)
100: cl.print("(");
101: cl.print(id.getId());
102: cl.print(" = ");
103: id.printInt32();
104: cl.print(" - 1");
105: if (!noValue)
106: cl.print(")");
107: return;
108: case POSTINC:
109: if (!noValue) {
110: cl.print("_env._first(");
111: id.printInt32();
112: cl.print(", ");
113: }
114: cl.print(id.getId());
115: cl.print(" = ");
116: id.printInt32();
117: cl.print(" + 1");
118: if (!noValue)
119: cl.print(")");
120: return;
121: case POSTDEC:
122: if (!noValue) {
123: cl.print("_env._first(");
124: id.printInt32();
125: cl.print(", ");
126: }
127: cl.print(id.getId());
128: cl.print(" = ");
129: id.printInt32();
130: cl.print(" - 1");
131: if (!noValue)
132: cl.print(")");
133: return;
134: }
135: }
136:
137: void printNumImpl() throws IOException {
138: if (id != null && id.isLocal()) {
139: switch (code) {
140: case PREINC:
141: if (!noValue)
142: cl.print("(");
143: cl.print(id.getId());
144: cl.print(" = ");
145: id.printNum();
146: cl.print(" + 1");
147: if (!noValue)
148: cl.print(")");
149: return;
150: case PREDEC:
151: if (!noValue)
152: cl.print("(");
153: cl.print(id.getId());
154: cl.print(" = ");
155: id.printNum();
156: cl.print(" - 1");
157: if (!noValue)
158: cl.print(")");
159: return;
160: case POSTINC:
161: if (!noValue) {
162: cl.print("_env._first(");
163: id.printNum();
164: cl.print(", ");
165: }
166: cl.print(id.getId());
167: cl.print(" = ");
168: id.printNum();
169: cl.print(" + 1");
170: if (!noValue)
171: cl.print(")");
172: return;
173: case POSTDEC:
174: if (!noValue) {
175: cl.print("_env._first(");
176: id.printNum();
177: cl.print(", ");
178: }
179: cl.print(id.getId());
180: cl.print(" = ");
181: id.printNum();
182: cl.print(" - 1");
183: if (!noValue)
184: cl.print(")");
185: return;
186: }
187: }
188:
189: switch (code) {
190: case PREINC:
191: case PREDEC:
192: cl.print("_env._pre(");
193: break;
194:
195: case POSTINC:
196: case POSTDEC:
197: cl.print("_env._post(");
198: break;
199: }
200:
201: if (field != null) {
202: field.getExpr().print();
203: cl.print(", ");
204: field.getField().printStr();
205: } else if (function.isGlobalScope()) {
206: cl.print("_env.global, ");
207: printLiteral(id.getId());
208: } else {
209: printLiteral(id.getId());
210: }
211:
212: if (code == PREINC || code == POSTINC)
213: cl.print(", 1)");
214: else
215: cl.print(", -1)");
216: }
217:
218: void printImpl() throws IOException {
219: switch (code) {
220: case PREINC:
221: if (!noValue)
222: cl.print("(");
223: cl.print(id.getId());
224: cl.print(" = ESNumber.create(");
225: id.printNum();
226: cl.print(" + 1)");
227: if (!noValue)
228: cl.print(")");
229: return;
230: case PREDEC:
231: if (!noValue)
232: cl.print(")");
233: cl.print(id.getId());
234: cl.print(" = ESNumber.create(");
235: id.printNum();
236: cl.print(" - 1)");
237: if (!noValue)
238: cl.print(")");
239: return;
240: case POSTINC:
241: if (!noValue) {
242: cl.print("_env._first(");
243: id.print();
244: cl.print(", ");
245: }
246: cl.print(id.getId());
247: cl.print(" = ESNumber.create(");
248: id.printNum();
249: cl.print(" + 1)");
250: if (!noValue)
251: cl.print(")");
252: return;
253: case POSTDEC:
254: if (!noValue) {
255: cl.print("_env._first(");
256: id.print();
257: cl.print(", ");
258: }
259: cl.print(id.getId());
260: cl.print(" = ESNumber.create(");
261: id.printNum();
262: cl.print(" - 1)");
263: if (!noValue)
264: cl.print(")");
265: return;
266: }
267: return;
268: }
269: }
|