KickJava   Java API By Example, From Geeks To Geeks.

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


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.Env;
34 import com.caucho.quercus.env.Value;
35
36 /**
37  * Represents a PHP array reference expression.
38  */

39 public class ArrayGetExpr extends AbstractVarExpr {
40   protected final Expr _expr;
41   protected final Expr _index;
42
43   public ArrayGetExpr(Location location, Expr expr, Expr index)
44   {
45     super(location);
46     _expr = expr;
47     _index = index;
48   }
49
50   public ArrayGetExpr(Expr expr, Expr index)
51   {
52     _expr = expr;
53     _index = index;
54   }
55
56   /**
57    * Returns the expr.
58    */

59   public Expr getExpr()
60   {
61     return _expr;
62   }
63
64   /**
65    * Returns the index.
66    */

67   public Expr getIndex()
68   {
69     return _index;
70   }
71
72   /**
73    * Evaluates the expression.
74    *
75    * @param env the calling environment.
76    *
77    * @return the expression value.
78    */

79   public Value eval(Env env)
80   {
81     Value array = _expr.eval(env);
82
83     Value index = _index.eval(env);
84
85     return array.get(index);
86   }
87
88   /**
89    * Evaluates the expression as a copyable result.
90    *
91    * @param env the calling environment.
92    *
93    * @return the expression value.
94    */

95   public Value evalCopy(Env env)
96   {
97     Value array = _expr.eval(env);
98
99     Value index = _index.eval(env);
100
101     return array.get(index).copy();
102   }
103
104   /**
105    * Evaluates the expression, creating an array if the value is unset..
106    *
107    * @param env the calling environment.
108    *
109    * @return the expression value.
110    */

111   public Value evalArray(Env env)
112   {
113     Value array = _expr.evalArray(env);
114
115     Value index = _index.eval(env);
116
117     return array.getArray(index);
118   }
119
120   /**
121    * Evaluates the expression, marking as dirty.
122    *
123    * @param env the calling environment.
124    *
125    * @return the expression value.
126    */

127   public Value evalDirty(Env env)
128   {
129     Value array = _expr.eval(env);
130
131     Value index = _index.eval(env);
132
133     return array.getDirty(index);
134   }
135
136   /**
137    * Evaluates the expression, creating an object if the value is unset.
138    *
139    * @param env the calling environment.
140    *
141    * @return the expression value.
142    */

143   public Value evalObject(Env env)
144   {
145     Value array = _expr.evalArray(env);
146
147     Value index = _index.eval(env);
148
149     return array.getObject(env, index);
150   }
151
152   /**
153    * Evaluates the expression.
154    *
155    * @param env the calling environment.
156    *
157    * @return the expression value.
158    */

159   public Value evalArg(Env env)
160   {
161     Value value = _expr.evalArg(env); // php/0d2t
162

163     return value.getArg(_index.eval(env));
164   }
165
166   /**
167    * Evaluates the expression.
168    *
169    * @param env the calling environment.
170    *
171    * @return the expression value.
172    */

173   public Value evalRef(Env env)
174   {
175     Value value = _expr.evalArray(env);
176
177     return value.getRef(_index.eval(env));
178   }
179
180   /**
181    * Evaluates the expression.
182    *
183    * @param env the calling environment.
184    *
185    * @return the expression value.
186    */

187   public void evalAssign(Env env, Value value)
188   {
189     Value array = _expr.evalArray(env);
190
191     Value index = _index.eval(env);
192
193     array.put(index, value);
194   }
195
196   /**
197    * Evaluates the expression.
198    *
199    * @param env the calling environment.
200    *
201    * @return the expression value.
202    */

203   public void evalUnset(Env env)
204   {
205     Value array = _expr.evalDirty(env);
206
207     Value index = _index.eval(env);
208
209     array.remove(index);
210   }
211
212   public String JavaDoc toString()
213   {
214     return _expr + "[" + _index + "]";
215   }
216 }
217
218
Popular Tags