Method: check(ILexNameToken)

1: /*******************************************************************************
2: *
3: *        Copyright (c) 2009 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ 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 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ 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. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.interpreter.runtime;
25:
26: import java.io.PrintWriter;
27:
28: import org.overture.ast.definitions.SClassDefinition;
29: import org.overture.ast.intf.lex.ILexLocation;
30: import org.overture.ast.intf.lex.ILexNameToken;
31: import org.overture.interpreter.assistant.IInterpreterAssistantFactory;
32: import org.overture.interpreter.values.Value;
33:
34: @SuppressWarnings("serial")
35: public class ClassContext extends RootContext
36: {
37:         public final SClassDefinition classdef;
38:
39:         public ClassContext(IInterpreterAssistantFactory af, ILexLocation location,
40:                         String title, Context freeVariables, Context outer,
41:                         SClassDefinition classdef)
42:         {
43:                 super(af, location, title, freeVariables, outer);
44:                 this.classdef = classdef;
45:         }
46:
47:         public ClassContext(IInterpreterAssistantFactory af, ILexLocation location,
48:                         String title, Context outer, SClassDefinition classdef)
49:         {
50:                 this(af, location, title, null, outer, classdef);
51:         }
52:
53:         /**
54:          * Check for the name in the current context and classdef, and if not present search the global context. Note that
55:          * the context chain is not followed.
56:          *
57:          * @see Context#check(ILexNameToken)
58:          */
59:
60:         @Override
61:         public Value check(ILexNameToken name)
62:         {
63:                 // A RootContext stops the name search from continuing down the
64:                 // context chain. It first checks any local context, then it
65:                 // checks the "class" context, then it goes down to the global level.
66:
67:                 Value v = get(name); // Local variables
68:
69:•                if (v != null)
70:                 {
71:                         return v;
72:                 }
73:
74:•                if (freeVariables != null)
75:                 {
76:                         v = freeVariables.get(name);
77:
78:•                        if (v != null)
79:                         {
80:                                 return v;
81:                         }
82:                 }
83:
84:                 v = assistantFactory.createSClassDefinitionAssistant().getStatic(classdef, name);
85:
86:•                if (v != null)
87:                 {
88:                         return v;
89:                 }
90:
91:                 Context g = getGlobal();
92:
93:•                if (g != this)
94:                 {
95:                         return g.check(name);
96:                 }
97:
98:                 return v;
99:         }
100:
101:         @Override
102:         public String toString()
103:         {
104:                 return super.toString(); // Self there anyway ...+ self.toString();
105:         }
106:
107:         @Override
108:         public void printStackTrace(PrintWriter out, boolean variables)
109:         {
110:                 if (outer == null) // Don't expand initial context
111:                 {
112:                         out.println("In class context of " + title);
113:                 } else
114:                 {
115:                         if (variables)
116:                         {
117:                                 out.print(this.format("\t", this));
118:                         }
119:
120:                         out.println("In class context of " + title + " " + location);
121:                         outer.printStackTrace(out, false);
122:                 }
123:         }
124: }