KickJava   Java API By Example, From Geeks To Geeks.

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


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.NullValue;
35 import com.caucho.quercus.env.Value;
36 import com.caucho.quercus.parser.QuercusParser;
37 import com.caucho.quercus.program.AbstractFunction;
38 import com.caucho.util.L10N;
39
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * Represents a PHP function expression.
44  */

45 public class FunctionExpr extends Expr {
46   private static final L10N L = new L10N(FunctionExpr.class);
47   
48   protected final String JavaDoc _name;
49   protected final Expr []_args;
50
51   public FunctionExpr(Location location, String JavaDoc name, ArrayList JavaDoc<Expr> args)
52   {
53     // quercus/120o
54
super(location);
55     _name = name.intern();
56
57     _args = new Expr[args.size()];
58     args.toArray(_args);
59   }
60
61   public FunctionExpr(Location location, String JavaDoc name, Expr []args)
62   {
63     // quercus/120o
64
super(location);
65     _name = name.intern();
66
67     _args = args;
68   }
69
70   public FunctionExpr(String JavaDoc name, ArrayList JavaDoc<Expr> args)
71   {
72     this(Location.UNKNOWN, name, args);
73   }
74
75   public FunctionExpr(String JavaDoc name, Expr []args)
76   {
77     this(Location.UNKNOWN, name, args);
78   }
79
80   /**
81    * Returns the name.
82    */

83   public String JavaDoc getName()
84   {
85     return _name;
86   }
87   
88   /**
89    * Returns the location if known.
90    */

91   public String JavaDoc getFunctionLocation()
92   {
93     return " [" + _name + "]";
94   }
95
96   /**
97    * Returns the reference of the value.
98    * @param location
99    */

100   @Override JavaDoc
101   public Expr createRef(QuercusParser parser)
102   {
103     return parser.getFactory().createRef(this);
104   }
105
106   /**
107    * Returns the copy of the value.
108    * @param location
109    */

110   @Override JavaDoc
111   public Expr createCopy(ExprFactory factory)
112   {
113     return this;
114   }
115   
116   /**
117    * Evaluates the expression.
118    *
119    * @param env the calling environment.
120    *
121    * @return the expression value.
122    */

123   public Value eval(Env env)
124   {
125     return evalImpl(env, false, false);
126   }
127   
128   /**
129    * Evaluates the expression.
130    *
131    * @param env the calling environment.
132    *
133    * @return the expression value.
134    */

135   public Value evalRef(Env env)
136   {
137     return evalImpl(env, true, false);
138   }
139   
140   /**
141    * Evaluates the expression.
142    *
143    * @param env the calling environment.
144    *
145    * @return the expression value.
146    */

147   public Value evalCopy(Env env)
148   {
149     return evalImpl(env, false, true);
150   }
151   
152   /**
153    * Evaluates the expression.
154    *
155    * @param env the calling environment.
156    *
157    * @return the expression value.
158    */

159   private Value evalImpl(Env env, boolean isRef, boolean isCopy)
160   {
161     AbstractFunction fun = env.findFunction(_name);
162
163     if (fun == null) {
164       env.error(getLocationLine(), L.l("'{0}' is an unknown function.", _name));
165
166       return NullValue.NULL;
167     }
168
169     Value []args = fun.evalArguments(env, this, _args);
170
171     env.pushCall(this, NullValue.NULL);
172     try {
173       env.checkTimeout();
174     
175       if (isRef)
176     return fun.callRef(env, args);
177       else if (isCopy)
178     return fun.callCopy(env, args);
179       else
180     return fun.call(env, args);
181     } finally {
182       env.popCall();
183     }
184   }
185   
186   public String JavaDoc toString()
187   {
188     return _name + "()";
189   }
190 }
191
192
Popular Tags