KickJava   Java API By Example, From Geeks To Geeks.

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


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 AssignExpr extends Expr {
39   private Expr lhs;
40   private IdExpr var;
41   private Expr field;
42   private Expr rhs;
43
44   AssignExpr(Block block, IdExpr var, Expr rhs)
45   {
46     super(block);
47     
48     this.var = var;
49     this.rhs = rhs;
50     rhs.getType();
51     if (! (rhs instanceof LiteralExpr || rhs instanceof IdExpr))
52       var.setUsed();
53   }
54
55   AssignExpr(Block block, Expr lhs, Expr field, Expr rhs)
56   {
57     super(block);
58     
59     this.lhs = lhs;
60     this.field = field;
61     this.rhs = rhs;
62     rhs.getType();
63     lhs.setUsed();
64   }
65
66   void exprStatement(Function fun) throws ESException
67   {
68     isTop = true;
69     noValue = true;
70
71     fun.addExpr(this);
72   }
73
74   /**
75    * The assignment operator
76    */

77   void printImpl()
78     throws IOException JavaDoc
79   {
80     cl.setLine(getFilename(), getLine());
81
82     if (var != null && (var.isJavaLocal() || var.isJavaGlobal())) {
83       if (! isTop && ! noValue) {
84         switch (var.getType()) {
85         case TYPE_NUMBER:
86         case TYPE_INTEGER:
87           cl.print("ESNumber.create(");
88           break;
89         
90         case TYPE_BOOLEAN:
91           cl.print("ESBoolean.create(");
92           break;
93         
94         default:
95           cl.print("(");
96           break;
97         }
98       }
99       else if (noValue && ! var.isUsed() &&
100                (rhs instanceof LiteralExpr || rhs instanceof IdExpr))
101         return;
102       else if (! noValue)
103         cl.print("(");
104       
105       cl.print(var.getId());
106       cl.print(" = ");
107       switch (var.getType()) {
108       case TYPE_NUMBER:
109         rhs.printNum();
110         break;
111         
112       case TYPE_INTEGER:
113         rhs.printInt32();
114         break;
115         
116       case TYPE_BOOLEAN:
117         rhs.printBoolean();
118         break;
119         
120       case TYPE_JAVA:
121         if (var.getType() == TYPE_JAVA &&
122             ! var.getJavaClass().isAssignableFrom(rhs.getJavaClass()))
123           cl.print("(" + var.getJavaClass().getName() + ") ");
124         rhs.printJava();
125         break;
126         
127       default:
128         rhs.print();
129         break;
130       }
131
132       if (! noValue)
133         cl.print(")");
134     }
135     else if (noValue && var != null) {
136       if (var.isGlobalScope())
137         cl.print("_env.global.setProperty(");
138       // XXX: should expand to anything without a side-effect
139
else if (! var.isUsed() && var.isLocal() &&
140                (rhs instanceof LiteralExpr || rhs instanceof IdExpr))
141         return;
142       else if (var.getVar().hasInit())
143         cl.print("_arg.setProperty(");
144       else
145         cl.print("_env.setScopeProperty(");
146
147       printLiteral(var.getId());
148       cl.print(", ");
149       rhs.print();
150       cl.print(")");
151     }
152     else if (var != null) {
153       if (var.isGlobalScope())
154         cl.print("_env.setGlobalProperty(");
155       else if (! var.isUsed() && var.isLocal() &&
156                (rhs instanceof LiteralExpr || rhs instanceof IdExpr))
157         return;
158       else if (var.getVar().hasInit())
159         cl.print("_arg.setProperty(");
160       else
161         cl.print("_env.setScopeProperty(");
162
163       printLiteral(var.getId());
164       cl.print(", ");
165       rhs.print();
166       cl.print(")");
167     }
168     else if (noValue) {
169       lhs.print();
170       cl.print(".setProperty(");
171       if (field.getType() == TYPE_INTEGER)
172         field.printInt32();
173       else
174         field.printStr();
175       cl.print(", ");
176       rhs.print();
177       cl.print(")");
178     }
179     else {
180       cl.print("_env.setProperty(");
181       lhs.print();
182       cl.print(", ");
183       field.printStr();
184       cl.print(", ");
185       rhs.print();
186       cl.print(")");
187     }
188   }
189 }
190
Popular Tags