PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Compiler/CollectableCompilationMode.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 86 lines | 52 code | 13 blank | 21 comment | 0 complexity | 9ec923ba2d82e4ee48824828f15f6649 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. 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 Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !CLR2
  16. using MSAst = System.Linq.Expressions;
  17. #else
  18. using MSAst = Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Collections.ObjectModel;
  23. using System.Runtime.CompilerServices;
  24. using Microsoft.Scripting;
  25. using Microsoft.Scripting.Ast;
  26. using Microsoft.Scripting.Runtime;
  27. using Microsoft.Scripting.Utils;
  28. using IronPython.Runtime;
  29. using AstUtils = Microsoft.Scripting.Ast.Utils;
  30. namespace IronPython.Compiler.Ast {
  31. using Ast = MSAst.Expression;
  32. /// <summary>
  33. /// A global allocator that puts all of the globals into an array access. The array is an
  34. /// array of PythonGlobal objects. We then just close over the array for any inner functions.
  35. ///
  36. /// Once compiled a RuntimeScriptCode is produced which is closed over the entire execution
  37. /// environment.
  38. /// </summary>
  39. class CollectableCompilationMode : CompilationMode {
  40. public override MSAst.LambdaExpression ReduceAst(PythonAst instance, string name) {
  41. return Ast.Lambda<Func<FunctionCode, object>>(
  42. Ast.Block(
  43. new[] { PythonAst._globalArray, PythonAst._globalContext },
  44. Ast.Assign(PythonAst._globalArray, instance.GlobalArrayInstance),
  45. Ast.Assign(PythonAst._globalContext, Ast.Constant(instance.ModuleContext.GlobalContext)),
  46. AstUtils.Convert(instance.ReduceWorker(), typeof(object))
  47. ),
  48. name,
  49. new[] { PythonAst._functionCode }
  50. );
  51. }
  52. public override void PrepareScope(PythonAst ast, ReadOnlyCollectionBuilder<MSAst.ParameterExpression> locals, List<MSAst.Expression> init) {
  53. locals.Add(PythonAst._globalArray);
  54. init.Add(Ast.Assign(PythonAst._globalArray, ast._arrayExpression));
  55. }
  56. public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global) {
  57. Assert.NotNull(global);
  58. return new PythonGlobalVariableExpression(
  59. Ast.ArrayIndex(
  60. PythonAst._globalArray,
  61. Ast.Constant(arrayIndex)
  62. ),
  63. variable,
  64. global
  65. );
  66. }
  67. public override Type DelegateType {
  68. get {
  69. return typeof(MSAst.Expression<Func<FunctionCode, object>>);
  70. }
  71. }
  72. }
  73. }