KickJava   Java API By Example, From Geeks To Geeks.

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


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 array[] reference expression.
41  */

42 public class ArrayTailExpr extends AbstractVarExpr {
43   protected final Expr _expr;
44
45   public ArrayTailExpr(Location location, Expr expr)
46   {
47     super(location);
48     _expr = expr;
49   }
50
51   public ArrayTailExpr(Expr expr)
52   {
53     _expr = expr;
54   }
55
56   /**
57    * Returns true for an expression that can be read (only $a[] uses this)
58    */

59   public boolean canRead()
60   {
61     return false;
62   }
63
64   /**
65    * Returns the expr.
66    */

67   public Expr getExpr()
68   {
69     return _expr;
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     return env.error("Cannot use [] as a read-value.");
82   }
83
84   /**
85    * Evaluates the expression.
86    *
87    * @param env the calling environment.
88    *
89    * @return the expression value.
90    */

91   public Value evalArg(Env env)
92   {
93     return evalRef(env);
94   }
95
96   /**
97    * Evaluates the expression.
98    *
99    * @param env the calling environment.
100    *
101    * @return the expression value.
102    */

103   public Value evalRef(Env env)
104   {
105     Value obj = _expr.evalArray(env);
106
107     if (obj instanceof ArrayValue) {
108       ArrayValue array = (ArrayValue) obj;
109       Value key = array.createTailKey();
110
111       return array.getRef(key);
112     }
113     else
114       return NullValue.NULL;
115   }
116
117   /**
118    * Evaluates the expression, setting an array if unset..
119    *
120    * @param env the calling environment.
121    *
122    * @return the expression value.
123    */

124   public Value evalArray(Env env)
125   {
126     Value obj = _expr.evalArray(env);
127
128     ArrayValue array = new ArrayValueImpl();
129
130     obj.put(array);
131
132     return array;
133   }
134
135   /**
136    * Evaluates the expression, assigning an object if unset..
137    *
138    * @param env the calling environment.
139    *
140    * @return the expression value.
141    */

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

160   public void evalAssign(Env env, Value value)
161   {
162     Value array = _expr.evalArray(env);
163
164     array.put(value);
165   }
166
167   /**
168    * Evaluates the expression.
169    *
170    * @param env the calling environment.
171    *
172    * @return the expression value.
173    */

174   public void evalUnset(Env env)
175   {
176     throw new UnsupportedOperationException JavaDoc();
177   }
178
179   public String JavaDoc toString()
180   {
181     return _expr + "[]";
182   }
183 }
184
185
Popular Tags