PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Compiler/RunnableScriptCode.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 108 lines | 77 code | 17 blank | 14 comment | 7 complexity | 2d2495c8d52b7f7056fdea98cc8e98c5 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. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.Threading;
  19. using Microsoft.Scripting;
  20. using Microsoft.Scripting.Runtime;
  21. using Microsoft.Scripting.Utils;
  22. using IronPython.Runtime;
  23. using IronPython.Runtime.Operations;
  24. namespace IronPython.Compiler {
  25. abstract class RunnableScriptCode : ScriptCode {
  26. private FunctionCode _code;
  27. private readonly Compiler.Ast.PythonAst _ast;
  28. public RunnableScriptCode(Compiler.Ast.PythonAst ast)
  29. : base(ast.SourceUnit) {
  30. _ast = ast;
  31. }
  32. public override object Run() {
  33. return base.Run();
  34. }
  35. public override object Run(Scope scope) {
  36. throw new NotImplementedException();
  37. }
  38. protected static CodeContext/*!*/ CreateTopLevelCodeContext(PythonDictionary/*!*/ dict, LanguageContext/*!*/ context) {
  39. ModuleContext modContext = new ModuleContext(dict, (PythonContext)context);
  40. return modContext.GlobalContext;
  41. }
  42. protected static CodeContext GetContextForScope(Scope scope, SourceUnit sourceUnit) {
  43. CodeContext ctx;
  44. var ext = scope.GetExtension(sourceUnit.LanguageContext.ContextId) as PythonScopeExtension;
  45. if (ext == null) {
  46. ext = sourceUnit.LanguageContext.EnsureScopeExtension(scope) as PythonScopeExtension;
  47. }
  48. ctx = ext.ModuleContext.GlobalContext;
  49. return ctx;
  50. }
  51. protected FunctionCode EnsureFunctionCode(Delegate/*!*/ dlg) {
  52. Debug.Assert(dlg != null);
  53. if (_code == null) {
  54. Interlocked.CompareExchange(
  55. ref _code,
  56. new FunctionCode(
  57. (PythonContext)SourceUnit.LanguageContext,
  58. dlg,
  59. _ast,
  60. _ast.GetDocumentation(_ast)
  61. ),
  62. null
  63. );
  64. }
  65. return _code;
  66. }
  67. public Compiler.Ast.PythonAst Ast {
  68. get {
  69. return _ast;
  70. }
  71. }
  72. public FunctionCode Code {
  73. get {
  74. return _code;
  75. }
  76. }
  77. public abstract FunctionCode GetFunctionCode();
  78. protected void PushFrame(CodeContext context, Delegate code) {
  79. if (((PythonContext)SourceUnit.LanguageContext).PythonOptions.Frames) {
  80. EnsureFunctionCode(code);
  81. PythonOps.PushFrame(context, _code);
  82. }
  83. }
  84. protected void PopFrame() {
  85. if (((PythonContext)SourceUnit.LanguageContext).PythonOptions.Frames) {
  86. List<FunctionStack> stack = PythonOps.GetFunctionStack();
  87. stack.RemoveAt(stack.Count - 1);
  88. }
  89. }
  90. }
  91. }