Source code: com/tripi/asp/ConstNode.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 /**
28 * ConstNode handles the "CONST ident = expr" expression in ASP.
29 *
30 * @author Terence Haddock
31 * @version 0.9
32 */
33 public class ConstNode extends DefaultNode
34 {
35 /** Identifier of constant expression */
36 IdentNode ident;
37
38 /** Expression the constant should be defined as. */
39 Node expr;
40
41 /** Is this constant expression private? */
42 boolean priv;
43
44 /**
45 * Constructor.
46 * @param ident identifier for constant value.
47 * @param expr Expression constant identifier should have value defined.
48 * @param priv Is this constant expression private?
49 */
50 public ConstNode(IdentNode ident, Node expr, boolean priv)
51 {
52 this.ident = ident;
53 this.expr = expr;
54 this.priv = priv;
55 }
56
57 /**
58 * Dumps the string representation of this node.
59 * @throws AspException on error
60 * @see DefaultNode#dump
61 */
62 public void dump() throws AspException
63 {
64 if (priv) System.out.print("PRIVATE ");
65 System.out.print("CONST ");
66 ident.dump();
67 System.out.print(" = ");
68 expr.dump();
69 System.out.println();
70 }
71
72 /**
73 * Executes this node. Causes the definition of the constant,
74 * wrapped in a ConstValue class to allow read-only access.
75 * @param context The current context
76 * @return result of executing this node, for this node the result is NULL
77 * @throws AspException on error
78 * @see Node#execute
79 */
80 public Object execute(AspContext context) throws AspException
81 {
82 if (context.inScope(ident))
83 throw new AspException("Variable/Const redefined");
84 Object res = expr.execute(context);
85 context.forceScope(ident);
86 context.setValue(ident, new ConstValue(res));
87 return null;
88 }
89 };
90