Source code: com/tripi/asp/DefineIdentArrayNode.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.List;
28 import org.apache.log4j.Category;
29
30 /**
31 * This node implements the defining of an array, DIM ident(length)
32 *
33 * @author Terence Haddock
34 * @version 0.9
35 */
36 public class DefineIdentArrayNode extends DefaultNode
37 {
38 /** Debugging class */
39 private static Category DBG =
40 Category.getInstance(DefineIdentArrayNode.class);
41
42 /** Name of this identifier */
43 IdentNode ident;
44
45 /** List of dimension sizes */
46 VarListNode dimensions;
47
48 /**
49 * Constructor.
50 *
51 * @param ident Name of this array
52 * @param dimensions Array of dimensions
53 */
54 public DefineIdentArrayNode(IdentNode ident, VarListNode dimensions)
55 {
56 this.ident = ident;
57 this.dimensions = dimensions;
58 }
59
60 /**
61 * Get the ident this define array is defining.
62 *
63 * @return identifier
64 */
65 public IdentNode getIdent()
66 {
67 return ident;
68 }
69
70 /**
71 * Get the list of dimensions this define array is defining
72 *
73 * @return dimension list
74 */
75 public VarListNode getDimensionList()
76 {
77 return dimensions;
78 }
79
80 /**
81 * Dumps this node.
82 * @throws AspException if an error occurs
83 * @see Node#dump()
84 */
85 public void dump() throws AspException
86 {
87 System.out.print("DIM ");
88 ident.dump();
89 System.out.print("(");
90 dimensions.dump();
91 System.out.println(")");
92 }
93
94 /**
95 * Executes this node, defining the ident.
96 *
97 * @param context AspContext under which to evaluate this expression.
98 * @throws AspException if an error occurs.
99 * @return always null
100 * @see Node#execute(AspContext)
101 */
102 public Object execute(AspContext context) throws AspException
103 {
104 /* Check if this variable is already in scope */
105 if (context.inDirectScope(ident)) {
106 throw new AspRedefineIdentException(ident.toString());
107 }
108
109 context.forceScope(ident);
110
111 if (dimensions.size() > 0) {
112 List dimList = (List)dimensions.execute(context);
113 ArrayNode an = createArray(dimList, 0);
114 context.setValue(ident, an);
115 }
116 return null;
117 }
118
119 /**
120 * This function handles the actual creation of an array.
121 * Internal function.
122 *
123 * @param vl List of dimensions.
124 * @param index Index of this array's creation, used for recursive
125 * calls to create multi-dimensional arrays.
126 * @return Array created.
127 */
128 private ArrayNode createArray(List vl, int index) {
129 Integer size = (Integer)vl.get(index);
130
131 if (DBG.isDebugEnabled())
132 DBG.debug("createArray(vl,"+index+") = " + size);
133
134 ArrayNode array = new ArrayNode(size.intValue()+1);
135 if (index < vl.size() - 1) {
136 for (int i = 0; i < size.intValue()+1; i++) {
137 ArrayNode an = createArray(vl, index+1);
138 array._setValue(i, an);
139 }
140 }
141 return array;
142 }
143 };
144