PageRenderTime 182ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Compiler/ToDiskCompilationMode.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 84 lines | 57 code | 13 blank | 14 comment | 0 complexity | 00a0cbd409bb2259dcecd0d875f22734 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 Microsoft.Scripting;
  23. using Microsoft.Scripting.Ast;
  24. using Microsoft.Scripting.Runtime;
  25. using Microsoft.Scripting.Utils;
  26. using IronPython.Runtime;
  27. using IronPython.Runtime.Operations;
  28. using AstUtils = Microsoft.Scripting.Ast.Utils;
  29. namespace IronPython.Compiler.Ast {
  30. using Ast = MSAst.Expression;
  31. class ToDiskCompilationMode : CollectableCompilationMode {
  32. public override MSAst.Expression GetConstant(object value) {
  33. return AstUtils.Constant(value);
  34. }
  35. public override void PrepareScope(PythonAst ast, System.Runtime.CompilerServices.ReadOnlyCollectionBuilder<MSAst.ParameterExpression> locals, List<MSAst.Expression> init) {
  36. locals.Add(PythonAst._globalArray);
  37. init.Add(
  38. Ast.Assign(
  39. PythonAst._globalArray,
  40. Ast.Call(
  41. typeof(PythonOps).GetMethod("GetGlobalArrayFromContext"),
  42. PythonAst._globalContext
  43. )
  44. )
  45. );
  46. }
  47. public override MSAst.LambdaExpression ReduceAst(PythonAst instance, string name) {
  48. return Ast.Lambda<Func<CodeContext, FunctionCode, object>>(
  49. Ast.Block(
  50. new[] { PythonAst._globalArray },
  51. Ast.Assign(
  52. PythonAst._globalArray,
  53. Ast.Call(
  54. null,
  55. typeof(PythonOps).GetMethod("GetGlobalArrayFromContext"),
  56. IronPython.Compiler.Ast.PythonAst._globalContext
  57. )
  58. ),
  59. AstUtils.Convert(instance.ReduceWorker(), typeof(object))
  60. ),
  61. name,
  62. PythonAst._arrayFuncParams
  63. );
  64. }
  65. public override ScriptCode MakeScriptCode(PythonAst ast) {
  66. PythonCompilerOptions pco = ast.CompilerContext.Options as PythonCompilerOptions;
  67. var code = (MSAst.Expression<Func<CodeContext/*!*/, FunctionCode/*!*/, object>>)ast.Reduce();
  68. return new PythonSavableScriptCode(code, ast.SourceUnit, ast.GetNames(), pco.ModuleName);
  69. }
  70. }
  71. }