KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > expr > VarVarExpr


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.expr;
31
32 import com.caucho.quercus.Location;
33 import com.caucho.quercus.env.ArrayValue;
34 import com.caucho.quercus.env.ArrayValueImpl;
35 import com.caucho.quercus.env.Env;
36 import com.caucho.quercus.env.NullValue;
37 import com.caucho.quercus.env.Value;
38
39 /**
40  * Represents a PHP variable expression.
41  */

42 public class VarVarExpr extends AbstractVarExpr {
43   private static final NullValue NULL = NullValue.create();
44
45   protected final Expr _var;
46
47   public VarVarExpr(Location location, Expr var)
48   {
49     super(location);
50     _var = var;
51   }
52
53   public VarVarExpr(Expr var)
54   {
55     _var = var;
56   }
57
58   public Expr getExpr()
59   {
60     return _var;
61   }
62
63   /**
64    * Evaluates the expression.
65    *
66    * @param env the calling environment.
67    *
68    * @return the expression value.
69    */

70   public Value eval(Env env)
71   {
72     String JavaDoc varName = _var.evalString(env).intern();
73
74     Value value = env.getValue(varName);
75
76     if (value != null)
77       return value;
78     else
79       return NULL;
80   }
81
82   /**
83    * Evaluates the expression, returning a copy as necessary.
84    *
85    * @param env the calling environment.
86    *
87    * @return the expression value.
88    */

89   public Value evalCopy(Env env)
90   {
91     String JavaDoc varName = _var.evalString(env).intern();
92
93     Value value = env.getValue(varName);
94
95     if (value != null)
96       return value.copy();
97     else
98       return NULL;
99   }
100
101   /**
102    * Evaluates the expression.
103    *
104    * @param env the calling environment.
105    *
106    * @return the expression value.
107    */

108   public void evalAssign(Env env, Value value)
109   {
110     String JavaDoc varName = _var.evalString(env).intern();
111
112     env.setVar(varName, value);
113   }
114
115   /**
116    * Evaluates the expression.
117    *
118    * @param env the calling environment.
119    *
120    * @return the expression value.
121    */

122   public void evalUnset(Env env)
123   {
124     String JavaDoc varName = _var.evalString(env).intern();
125
126     env.unsetVar(varName);
127   }
128
129   /**
130    * Evaluates the expression.
131    *
132    * @param env the calling environment.
133    *
134    * @return the expression value.
135    */

136   public Value evalRef(Env env)
137   {
138     String JavaDoc varName = _var.evalString(env).intern();
139
140     Value value = env.getVar(varName);
141
142     if (value != null)
143       return value;
144     else
145       return NULL;
146   }
147
148   /**
149    * Evaluates the expression.
150    *
151    * @param env the calling environment.
152    *
153    * @return the expression value.
154    */

155   public Value evalArg(Env env)
156   {
157     String JavaDoc varName = _var.evalString(env).intern();
158
159     Value value = env.getVar(varName);
160
161     if (value != null)
162       return value;
163     else
164       return NULL;
165   }
166
167   /**
168    * Evaluates the expression, converting to an array if necessary.
169    *
170    * @param env the calling environment.
171    *
172    * @return the expression value.
173    */

174   public Value evalArray(Env env)
175   {
176     String JavaDoc varName = _var.evalString(env).intern();
177
178     Value value = env.getVar(varName);
179
180     if (value != null)
181       return value.getArray();
182     else {
183       ArrayValue array = new ArrayValueImpl();
184
185       env.setVar(varName, array);
186
187       return array;
188     }
189   }
190
191   public String JavaDoc toString()
192   {
193     return "$" + _var;
194   }
195 }
196
197
Popular Tags