PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/TestAst/TestScope.cs

#
C# | 109 lines | 77 code | 16 blank | 16 comment | 8 complexity | 442ddfe791ca7f08f85292b7a413ea44 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 * ***************************************************************************/
 15
 16#if !CLR2
 17using System.Linq.Expressions;
 18#endif
 19
 20using System;
 21using System.Collections.Generic;
 22using System.Dynamic;
 23using Microsoft.Scripting.Ast;
 24using Microsoft.Scripting.Runtime;
 25
 26namespace TestAst {
 27    public class TestScope {
 28        private TestScope _parent;
 29        private LambdaBuilder _block;
 30        private Dictionary<string, ParameterExpression> _variables = new Dictionary<string, ParameterExpression>();
 31
 32        //////////////////////////////////////////////////////////
 33        public static TestScope Current = null;
 34        public static void PushNewScope(string name) {
 35            Current = new TestScope(name, Current);
 36        }
 37        public static void PopScope() {
 38            Current = Current.Parent;
 39        }
 40        //////////////////////////////////////////////////////////
 41
 42        public TestScope(string name, TestScope parent) {
 43            _block = Utils.Lambda(typeof(object), name);
 44            _parent = parent;
 45            Current = this;
 46        }
 47
 48        public TestScope Parent {
 49            get {
 50                return _parent;
 51            }
 52        }
 53
 54        public TestScope TopScope {
 55            get {
 56                if (_parent == null) {
 57                    return this;
 58                } else {
 59                    return _parent.TopScope;
 60                }
 61            }
 62        }
 63
 64        public LambdaBuilder Block {
 65            get {
 66                return _block;
 67            }
 68        }
 69
 70        public ParameterExpression GetOrMakeLocal(string name) {
 71            return GetOrMakeLocal(name, typeof(object));
 72        }
 73
 74        public ParameterExpression GetOrMakeLocal(string name, Type type) {
 75            ParameterExpression ret;
 76            if (_variables.TryGetValue(name, out ret)) {
 77                return ret;
 78            }
 79            ret = (ParameterExpression)_block.Variable(type, name);
 80            _variables[name] = ret;
 81            return ret;
 82        }
 83
 84        public ParameterExpression LookupName(string name) {
 85            ParameterExpression var;
 86            if (_variables.TryGetValue(name, out var)) {
 87                return var;
 88            }
 89
 90            if (_parent != null) {
 91                return _parent.LookupName(name);
 92            } else {
 93                return null;
 94            }
 95        }
 96
 97        public ParameterExpression HiddenVariable(Type type, string name) {
 98            return _block.HiddenVariable(type, name);
 99        }
100
101        public LambdaExpression FinishScope(Expression body) {
102            _block.Body = body;
103            _block.AddParameters(Expression.Parameter(typeof(Scope), "#scope"), Expression.Parameter(typeof(LanguageContext), "#context"));
104            
105            return _block.MakeLambda();
106        }
107    }
108}
109