KickJava   Java API By Example, From Geeks To Geeks.

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


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

38 class PostfixExpr extends Expr {
39   final static int PREINC = 'i';
40   final static int PREDEC = 'd';
41   final static int POSTINC = 'I';
42   final static int POSTDEC = 'D';
43   
44   private int code;
45   private FieldExpr field;
46   private IdExpr id;
47
48   private AssignExpr cheat;
49
50   PostfixExpr(Block block, int code, FieldExpr field)
51   {
52     super(block);
53     
54     this.code = code;
55     this.field = field;
56
57     if (field != null)
58       field.setUsed();
59   }
60
61   PostfixExpr(Block block, int code, IdExpr id)
62   {
63     super(block);
64     
65     this.code = code;
66     this.id = id;
67     
68     if (id != null)
69       id.setUsed();
70   }
71
72   int getType()
73   {
74     if (id == null || ! id.isLocal())
75       return TYPE_NUMBER;
76     else if (id.getType() == TYPE_INTEGER)
77       return TYPE_INTEGER;
78     else if (id.getType() == TYPE_NUMBER)
79       return TYPE_NUMBER;
80     else
81       return TYPE_ES;
82   }
83
84   void exprStatement(Function fun) throws ESException
85   {
86     setTop();
87     fun.addExpr(this);
88   }
89
90   void printInt32Impl() throws IOException JavaDoc
91   {
92     switch (code) {
93     case PREINC:
94       if (! noValue)
95         cl.print("(");
96       cl.print(id.getId());
97       cl.print(" = ");
98       id.printInt32();
99       cl.print(" + 1");
100       if (! noValue)
101         cl.print(")");
102       return;
103     case PREDEC:
104       if (! noValue)
105         cl.print("(");
106       cl.print(id.getId());
107       cl.print(" = ");
108       id.printInt32();
109       cl.print(" - 1");
110       if (! noValue)
111         cl.print(")");
112       return;
113     case POSTINC:
114       if (! noValue) {
115         cl.print("_env._first(");
116         id.printInt32();
117         cl.print(", ");
118       }
119       cl.print(id.getId());
120       cl.print(" = ");
121       id.printInt32();
122       cl.print(" + 1");
123       if (! noValue)
124         cl.print(")");
125       return;
126     case POSTDEC:
127       if (! noValue) {
128         cl.print("_env._first(");
129         id.printInt32();
130         cl.print(", ");
131       }
132       cl.print(id.getId());
133       cl.print(" = ");
134       id.printInt32();
135       cl.print(" - 1");
136       if (! noValue)
137         cl.print(")");
138       return;
139     }
140   }
141
142   void printNumImpl() throws IOException JavaDoc
143   {
144     if (id != null && id.isLocal()) {
145       switch (code) {
146       case PREINC:
147         if (! noValue)
148           cl.print("(");
149         cl.print(id.getId());
150         cl.print(" = ");
151         id.printNum();
152         cl.print(" + 1");
153         if (! noValue)
154           cl.print(")");
155         return;
156       case PREDEC:
157         if (! noValue)
158           cl.print("(");
159         cl.print(id.getId());
160         cl.print(" = ");
161         id.printNum();
162         cl.print(" - 1");
163         if (! noValue)
164           cl.print(")");
165         return;
166       case POSTINC:
167         if (! noValue) {
168           cl.print("_env._first(");
169           id.printNum();
170           cl.print(", ");
171         }
172         cl.print(id.getId());
173         cl.print(" = ");
174         id.printNum();
175         cl.print(" + 1");
176         if (! noValue)
177           cl.print(")");
178         return;
179       case POSTDEC:
180         if (! noValue) {
181           cl.print("_env._first(");
182           id.printNum();
183           cl.print(", ");
184         }
185         cl.print(id.getId());
186         cl.print(" = ");
187         id.printNum();
188         cl.print(" - 1");
189         if (! noValue)
190           cl.print(")");
191         return;
192       }
193     }
194
195     switch (code) {
196     case PREINC:
197     case PREDEC:
198       cl.print("_env._pre(");
199       break;
200
201     case POSTINC:
202     case POSTDEC:
203       cl.print("_env._post(");
204       break;
205     }
206
207     if (field != null) {
208       field.getExpr().print();
209       cl.print(", ");
210       field.getField().printStr();
211     } else if (function.isGlobalScope()) {
212       cl.print("_env.global, ");
213       printLiteral(id.getId());
214     } else {
215       printLiteral(id.getId());
216     }
217
218     if (code == PREINC || code == POSTINC)
219       cl.print(", 1)");
220     else
221       cl.print(", -1)");
222   }
223
224   void printImpl() throws IOException JavaDoc
225   {
226     switch (code) {
227     case PREINC:
228       if (! noValue)
229         cl.print("(");
230       cl.print(id.getId());
231       cl.print(" = ESNumber.create(");
232       id.printNum();
233       cl.print(" + 1)");
234       if (! noValue)
235         cl.print(")");
236       return;
237     case PREDEC:
238       if (! noValue)
239         cl.print(")");
240       cl.print(id.getId());
241       cl.print(" = ESNumber.create(");
242       id.printNum();
243       cl.print(" - 1)");
244       if (! noValue)
245         cl.print(")");
246       return;
247     case POSTINC:
248       if (! noValue) {
249         cl.print("_env._first(");
250         id.print();
251         cl.print(", ");
252       }
253       cl.print(id.getId());
254       cl.print(" = ESNumber.create(");
255       id.printNum();
256       cl.print(" + 1)");
257       if (! noValue)
258         cl.print(")");
259       return;
260     case POSTDEC:
261       if (! noValue) {
262         cl.print("_env._first(");
263         id.print();
264         cl.print(", ");
265       }
266       cl.print(id.getId());
267       cl.print(" = ESNumber.create(");
268       id.printNum();
269       cl.print(" - 1)");
270       if (! noValue)
271         cl.print(")");
272       return;
273     }
274     return;
275   }
276 }
277
Popular Tags