Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/tripi/asp/SubDefinitionNode.java


1   /**
2    * ArrowHead ASP Server 
3    * This is a source file for the ArrowHead ASP Server - an 100% Java
4    * VBScript interpreter and ASP server.
5    *
6    * For more information, see http://www.tripi.com/arrowhead
7    *
8    * Copyright (C) 2002  Terence Haddock
9    *
10   * This program is free software; you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation; either version 2 of the License, or
13   * (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   *
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23   *
24   */
25  package com.tripi.asp;
26  
27  import java.util.Hashtable;
28  import java.util.Vector;
29  
30  import org.apache.log4j.Category;
31  
32  /**
33   * SubDefinitionCode contains the parsed representation of a subroutine
34   * definition, also includes function definition syntax.
35   *
36   * @author Terence Haddock
37   */
38  public class SubDefinitionNode implements FunctionNode
39  {
40      /** Debugging category */
41      static final Category DBG = Category.getInstance(SubDefinitionNode.class);
42  
43      /** Name of the subroutine/function */
44      IdentNode        ident;
45  
46      /** Parameter list to the function */
47      VarListNode        identlist;
48  
49      /** Code within the function */
50      BlockNode        block;
51  
52      /** <b>true</b> if this is a function, <b>false</b> otherwise */
53      boolean            isFunction;
54  
55      /**
56       * Constructor.
57       * @param ident Name of subroutine
58       * @param identlist Name of parameters to subroutine
59       * @param block Code within the subroutine
60       * @param isFunction is this a function?
61       */
62      public SubDefinitionNode(IdentNode ident, VarListNode identlist,
63                  BlockNode block, boolean isFunction)
64      {
65          this.ident = ident;
66          this.identlist = identlist;
67          this.block = block;
68          this.isFunction = isFunction;
69      }
70  
71      /**
72       * Get the identifier of this subroutine.
73       * @return identifier of this subroutine.
74       */
75      public IdentNode getIdent()
76      {
77          return ident;
78      }
79  
80      /**
81       * Get the list of parameters for this subroutine.
82       * @return list of parameters for this subroutine.
83       */
84      public VarListNode getIdentList()
85      {
86          return identlist;
87      }
88  
89      /**
90       * Get the block of this subroutine.
91       * @return block of this subroutine.
92       */
93      public BlockNode getBlock()
94      {
95          return block;
96      }
97  
98      /**
99       * Is this a function?
100      * @return <b>true</b> if this is a function, <b>false</b> otherwise.
101      */
102     public boolean isFunction()
103     {
104         return isFunction;
105     }
106 
107     /**
108      * Dumps the string representation of this subroutine definition.
109      * @see DefaultNode#dump
110      */
111     public void dump() throws AspException
112     {
113         if (isFunction) {
114             System.out.print("FUNCTION ");
115         } else {
116             System.out.print("SUB ");
117         }
118         ident.dump();
119         System.out.print(" (");
120         identlist.dump();
121         System.out.println(")");
122         block.dump();
123         if (isFunction) {
124             System.out.println("END FUNCTION");
125         } else {
126             System.out.println("END SUB");
127         }
128     }
129 
130     /**
131      * Prepares this subroutine, it effectively defines this subroutine
132      * within the global scope.
133      * @param context AspContext for this function call
134      */
135     public void prepare(AspContext scope) throws AspException
136     {
137         /* Here is the purpose of prepare, to set the scope of a
138          * subroutine definition before code is executed which
139          * may reference it. */
140         scope.setValue(ident, this);
141         /* We do not prepare the block itself for execution, as this would
142            bring the block's define statements into global scope, a big oops */
143     }
144 
145     /**
146      * Executes this subroutine, the subroutine definition itself has no
147      * effect when executed.
148      * @param context AspContext under which to execute this code.
149      * @return always null
150      */
151     public Object execute(AspContext scope) throws AspException
152     {
153         return null;
154     }
155 
156     /**
157      * This function executes the subroutine/function itself.
158      * @param param List of parameters
159      * @param context Context under which to execute this code.
160      * @return return value of function, null if this is a subroutine
161      * @see FunctionNode#execute(VarListNode, AspContext)
162      */
163     public Object execute(VarListNode param, AspContext scope) throws AspException
164     {
165         /* Check parameter count */
166         if (param.size() != identlist.size()) {
167             throw new AspException("Function called with invalid arguments");
168         }
169         /* Get the values of the parameters */
170         Vector value = (Vector)param.execute(scope);
171         /* Create a new scope */
172         Hashtable newScope = new Hashtable();
173         /* If this is a function node, we need to prepare the scope for
174            the function name. */
175         if (isFunction)
176         {
177             newScope.put(ident, "");
178         }
179         /* Set the variables */
180         for (int i = 0; i < value.size(); i++)
181         {
182             Object iVal = Types.dereference(value.get(i));
183             Object iParam = identlist.get(i);
184             /* If this is a pass by reference, get the actual ident */
185             if (iParam instanceof ByRefNode) {
186                 iParam = ((ByRefNode)iParam).getIdent();
187             }
188             IdentNode ident = (IdentNode)iParam;
189             if (iVal == null) iVal = Constants.nullNode;
190             newScope.put(ident, iVal);
191         }
192         /* Execute the block within the new scope */
193         scope.pushSubroutineScope(newScope);
194         try {
195             /* Prepare the scope of the variables */
196             block.prepare(scope);
197             block.execute(scope);
198         } catch (AspExitFunctionException ex) {
199             if (!isFunction) throw ex;
200         } catch (AspExitSubException ex) {
201             if (isFunction) throw ex;
202         } finally {
203             scope.popSubroutineScope();
204         }
205         /* Check for any pass by reference values */
206         for (int i = 0; i < value.size(); i++)
207         {
208             Object iVal = param.get(i);
209             /* The param needs to be an IdentNode */
210             if (iVal instanceof IdentNode) {
211                 Object iParam = identlist.get(i);
212                 /* The parameter has to be a pass by reference */
213                 if (iParam instanceof ByRefNode) {
214                     iParam = ((ByRefNode)iParam).getIdent();
215                     /* Get the value */
216                     Object newVal = Types.dereference(newScope.get((IdentNode)iParam));
217                     /* Set the value in the return scope */
218                     scope.setValue((IdentNode)iVal, newVal);
219                 }
220             }
221         }
222         /* If this is a function, we need to set the return value */
223         if (isFunction) {
224             return newScope.get(ident);
225         } else {
226             return null;
227         }
228     }
229 };
230