コード例 #1
0
ファイル: ConstantExpression.cs プロジェクト: SueDou/python
        public override MSAst.Expression Reduce()
        {
            UnicodeWrapper wrapper;

            if (_value == Ellipsis.Value)
            {
                return(EllipsisExpr);
            }
            else if (_value is bool)
            {
                if ((bool)_value)
                {
                    return(TrueExpr);
                }
                else
                {
                    return(FalseExpr);
                }
            }
            else if ((wrapper = _value as UnicodeWrapper) != null)
            {
                return(GlobalParent.Constant(wrapper.Value));
            }

            return(GlobalParent.Constant(_value));
        }
コード例 #2
0
        public override MSAst.Expression Reduce()
        {
            UnicodeWrapper wrapper;

            if (_value == Ellipsis.Value)
            {
                return(Ast.Property(
                           null,
                           typeof(PythonOps).GetProperty("Ellipsis")
                           ));
            }
            else if (_value is bool)
            {
                if ((bool)_value)
                {
                    return(Ast.Field(null, typeof(ScriptingRuntimeHelpers).GetField("True")));
                }
                else
                {
                    return(Ast.Field(null, typeof(ScriptingRuntimeHelpers).GetField("False")));
                }
            }
            else if ((wrapper = _value as UnicodeWrapper) != null)
            {
                return(GlobalParent.Constant(wrapper.Value));
            }

            return(GlobalParent.Constant(_value));
        }
コード例 #3
0
        internal override void RewriteBody(MSAst.ExpressionVisitor visitor)
        {
            _dlrBody = null;    // clear the cached body if we've been reduced

            MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
            FuncCodeExpr = funcCode;

            Body = new RewrittenBodyStatement(Body, visitor.Visit(Body));
        }
コード例 #4
0
        public override MSAst.Expression Reduce()
        {
            if (_value == Ellipsis.Value)
            {
                return(EllipsisExpr);
            }
            else if (_value is bool)
            {
                return((bool)_value ? TrueExpr : FalseExpr);
            }

            return(GlobalParent.Constant(_value));
        }
コード例 #5
0
        /// <summary>
        /// Returns an expression which creates the function object.
        /// </summary>
        internal MSAst.Expression MakeFunctionExpression()
        {
            List <MSAst.Expression> defaults = new List <MSAst.Expression>(0);

            foreach (var param in _parameters)
            {
                if (param.DefaultValue != null)
                {
                    defaults.Add(AstUtils.Convert(param.DefaultValue, typeof(object)));
                }
            }

            MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
            FuncCodeExpr = funcCode;

            MSAst.Expression ret;
            if (EmitDebugFunction())
            {
                LightLambdaExpression code = CreateFunctionLambda();

                // we need to compile all of the debuggable code together at once otherwise mdbg gets confused.  If we're
                // in tracing mode we'll still compile things one off though just to keep things simple.  The code will still
                // be debuggable but naive debuggers like mdbg will have more issues.
                ret = Ast.Call(
                    AstMethods.MakeFunctionDebug,                                                   // method
                    Parent.LocalContext,                                                            // 1. Emit CodeContext
                    FuncCodeExpr,                                                                   // 2. FunctionCode
                    ((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue(),     // 3. module name
                    defaults.Count == 0 ?                                                           // 4. default values
                    AstUtils.Constant(null, typeof(object[])) :
                    (MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults),
                    IsGenerator ?
                    (MSAst.Expression) new PythonGeneratorExpression(code, GlobalParent.PyContext.Options.CompilationThreshold) :
                    (MSAst.Expression)code
                    );
            }
            else
            {
                ret = Ast.Call(
                    AstMethods.MakeFunction,                                                        // method
                    Parent.LocalContext,                                                            // 1. Emit CodeContext
                    FuncCodeExpr,                                                                   // 2. FunctionCode
                    ((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue(),     // 3. module name
                    defaults.Count == 0 ?                                                           // 4. default values
                    AstUtils.Constant(null, typeof(object[])) :
                    (MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults)
                    );
            }

            return(AddDecorators(ret, _decorators));
        }
コード例 #6
0
        public override MSAst.Expression Reduce()
        {
            var codeObj  = GetOrMakeFunctionCode();
            var funcCode = GlobalParent.Constant(codeObj);

            FuncCodeExpr = funcCode;

            MSAst.Expression lambda;
            if (EmitDebugSymbols)
            {
                lambda = GetLambda();
            }
            else
            {
                lambda = NullLambda;
                ThreadPool.QueueUserWorkItem((x) => {
                    // class defs are almost always run, so start
                    // compiling the code now so it might be ready
                    // when we actually go and execute it
                    codeObj.UpdateDelegate(PyContext, true);
                });
            }

            MSAst.Expression classDef = Ast.Call(
                AstMethods.MakeClass,
                funcCode,
                lambda,
                Parent.LocalContext,
                AstUtils.Constant(_name),
                Ast.NewArrayInit(
                    typeof(object),
                    ToObjectArray(_bases)
                    ),
                Metaclass is null ? AstUtils.Constant(null, typeof(object)) : AstUtils.Convert(Metaclass, typeof(object)),
                AstUtils.Constant(FindSelfNames())
                );

            classDef = AddDecorators(classDef, Decorators);

            return(GlobalParent.AddDebugInfoAndVoid(
                       AssignValue(Parent.GetVariableExpression(PythonVariable), classDef),
                       new SourceSpan(
                           GlobalParent.IndexToLocation(StartIndex),
                           GlobalParent.IndexToLocation(HeaderIndex)
                           )
                       ));
        }
コード例 #7
0
        void IInstructionProvider.AddInstructions(LightCompiler compiler)
        {
            if (Decorators != null)
            {
                // decorators aren't supported, skip using the optimized instruction.
                compiler.Compile(Reduce());
                return;
            }

            // currently needed so we can later compile
            MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
            FuncCodeExpr = funcCode;

            var variable = Parent.GetVariableExpression(PythonVariable);

            CompileAssignment(compiler, variable, CreateFunctionInstructions);
        }
コード例 #8
0
        /// <summary>
        /// Returns an expression which creates the function object.
        /// </summary>
        internal MSAst.Expression MakeFunctionExpression()
        {
            var defaults    = new List <MSAst.Expression>();
            var kwdefaults  = new List <MSAst.Expression>();
            var annotations = new List <MSAst.Expression>();

            if (ReturnAnnotation != null)
            {
                // value needs to come before key in the array
                annotations.Add(AstUtils.Convert(ReturnAnnotation, typeof(object)));
                annotations.Add(Ast.Constant("return", typeof(string)));
            }

            foreach (var param in _parameters)
            {
                if (param.Kind == ParameterKind.Normal && param.DefaultValue != null)
                {
                    defaults.Add(AstUtils.Convert(param.DefaultValue, typeof(object)));
                }

                if (param.Kind == ParameterKind.KeywordOnly && param.DefaultValue != null)
                {
                    // value needs to come before key in the array
                    kwdefaults.Add(AstUtils.Convert(param.DefaultValue, typeof(object)));
                    kwdefaults.Add(Ast.Constant(param.Name, typeof(string)));
                }

                if (param.Annotation != null)
                {
                    // value needs to come before key in the array
                    annotations.Add(AstUtils.Convert(param.Annotation, typeof(object)));
                    annotations.Add(Ast.Constant(param.Name, typeof(string)));
                }
            }

            MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
            FuncCodeExpr = funcCode;

            MSAst.Expression ret;
            if (EmitDebugFunction())
            {
                LightLambdaExpression code = CreateFunctionLambda();

                // we need to compile all of the debuggable code together at once otherwise mdbg gets confused.  If we're
                // in tracing mode we'll still compile things one off though just to keep things simple.  The code will still
                // be debuggable but naive debuggers like mdbg will have more issues.
                ret = Ast.Call(
                    AstMethods.MakeFunctionDebug,                                                   // method
                    Parent.LocalContext,                                                            // 1. Emit CodeContext
                    FuncCodeExpr,                                                                   // 2. FunctionCode
                    ((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue(),     // 3. module name
                    defaults.Count == 0 ?                                                           // 4. default values
                    AstUtils.Constant(null, typeof(object[])) :
                    (MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults),
                    kwdefaults.Count == 0 ? AstUtils.Constant(null, typeof(PythonDictionary)) :
                    (MSAst.Expression)Ast.Call(                                                     // 5. kwdefaults
                        AstMethods.MakeDictFromItems,
                        Ast.NewArrayInit(
                            typeof(object),
                            kwdefaults
                            )
                        ),
                    annotations.Count == 0 ? AstUtils.Constant(null, typeof(PythonDictionary)) :
                    (MSAst.Expression)Ast.Call(                                                     // 6. annotations
                        AstMethods.MakeDictFromItems,
                        Ast.NewArrayInit(
                            typeof(object),
                            annotations
                            )
                        ),
                    IsGenerator ?
                    (MSAst.Expression) new PythonGeneratorExpression(code, GlobalParent.PyContext.Options.CompilationThreshold) :
                    (MSAst.Expression)code
                    );
            }
            else
            {
                ret = Ast.Call(
                    AstMethods.MakeFunction,                                                        // method
                    Parent.LocalContext,                                                            // 1. Emit CodeContext
                    FuncCodeExpr,                                                                   // 2. FunctionCode
                    ((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue(),     // 3. module name
                    defaults.Count == 0 ?                                                           // 4. default values
                    AstUtils.Constant(null, typeof(object[])) :
                    (MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults),
                    kwdefaults.Count == 0 ? AstUtils.Constant(null, typeof(PythonDictionary)) :
                    (MSAst.Expression)Ast.Call(                                                     // 5. kwdefaults
                        AstMethods.MakeDictFromItems,
                        Ast.NewArrayInit(
                            typeof(object),
                            kwdefaults
                            )
                        ),
                    annotations.Count == 0 ? AstUtils.Constant(null, typeof(PythonDictionary)) :
                    (MSAst.Expression)Ast.Call(                                                     // 6. annotations
                        AstMethods.MakeDictFromItems,
                        Ast.NewArrayInit(
                            typeof(object),
                            annotations
                            )
                        )
                    );
            }

            return(AddDecorators(ret, Decorators));
        }