PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

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

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