PageRenderTime 36ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Tools/IronStudio/IronPythonToolsCore/PyAnalysis/Interpreter/InterpreterScope.cs

#
C# | 89 lines | 64 code | 12 blank | 13 comment | 7 complexity | e4d6dbeb5a6da7b13e39d48f91d9c26c MD5 | raw file
 1/* ****************************************************************************
 2 *
 3 * Copyright (c) Microsoft Corporation. 
 4 *
 5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 6 * copy of the license can be found in the License.html file at the root of this distribution. If 
 7 * you cannot locate the Apache License, Version 2.0, please send an email to 
 8 * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 9 * by the terms of the Apache License, Version 2.0.
10 *
11 * You must not remove this notice, or any other, from this software.
12 *
13 * ***************************************************************************/
14
15using System.Collections.Generic;
16using IronPython.Compiler.Ast;
17using Microsoft.PyAnalysis.Values;
18
19namespace Microsoft.PyAnalysis.Interpreter {
20    abstract class InterpreterScope {
21        private readonly Namespace _ns;
22        private Dictionary<string, VariableDef> _variables = new Dictionary<string, VariableDef>();
23
24        public InterpreterScope(Namespace ns) {
25            _ns = ns;
26        }
27
28        public abstract string Name {
29            get;
30        }
31
32        public void SetVariable(Node node, AnalysisUnit unit, string name, IEnumerable<Namespace> value, bool addRef = true) {
33            var variable = CreateVariable(node, unit, name, false);
34            
35            variable.AddTypes(node, unit, value);
36            if (addRef) {
37                variable.AddAssignment(node, unit);
38            }
39        }
40
41        public VariableDef GetVariable(Node node, AnalysisUnit unit, string name, bool addRef = true) {
42            VariableDef res;
43            if (_variables.TryGetValue(name, out res)) {
44                if (addRef) {
45                    res.AddReference(node, unit);
46                }
47                return res;
48            }
49            return null;
50        }
51
52        public VariableDef CreateVariable(Node node, AnalysisUnit unit, string name, bool addRef = true) {
53            var res = GetVariable(node, unit, name, addRef);
54            if (res == null) {
55                _variables[name] = res = new VariableDef();
56                if (addRef) {
57                    res.AddReference(node, unit);
58                }
59            }
60            return res;
61        }
62
63        protected VariableDef CreateVariableWorker(Node node, AnalysisUnit unit, string name) {
64            VariableDef res;
65            if (!_variables.TryGetValue(name, out res)) {
66                _variables[name] = res = new VariableDef();
67            }
68            return res;
69        }
70
71        public IDictionary<string, VariableDef> Variables {
72            get {
73                return _variables;
74            }
75        }
76
77        public virtual bool VisibleToChildren {
78            get {
79                return true;
80            }
81        }
82
83        public Namespace Namespace {
84            get {
85                return _ns;
86            }
87        }
88    }
89}