예제 #1
0
        public bool DoOneInteractive(Frame topFrame)
        {
            bool continueInteraction;
            Stmt s = ReadStatement(out continueInteraction);

            if (continueInteraction == false)
            {
                return(false);
            }

            //  's' is null when we parse a line composed only of a NEWLINE (interactive_input grammar);
            //  we don't generate anything when 's' is null
            if (s != null)
            {
                FrameCode code = OutputGenerator.GenerateSnippet(context, s, true);

                if (ExecWrapper != null)
                {
                    CallTarget0 t = delegate() {
                        try { code.Run(topFrame); } catch (Exception e) { DumpException(e); }
                        return(null);
                    };
                    object callable = new Function0(topFrame.__module__, "wrapper", t, new string[0], new object[0]);
                    Ops.Call(ExecWrapper, callable);
                }
                else
                {
                    code.Run(topFrame);
                }
            }

            return(true);
        }
예제 #2
0
        public static FrameCode GenerateSnippet(CompilerContext context, Stmt body, string name, bool printExprStmts)
        {
            GlobalSuite gs = Binder.Bind(body, context);

            if (name.Length == 0)
            {
                name = "<empty>";                   // The empty string isn't a legal method name
            }
            CodeGen       cg;
            List <object> staticData = null;
            TypeGen       tg         = null;

            cg                     = snippetAssembly.DefineDynamicMethod(name, typeof(object), new Type[] { typeof(Frame) });
            staticData             = new List <object>();
            cg.staticData          = staticData;
            cg.doNotCacheConstants = true;
            cg.ModuleSlot          = cg.GetLocalTmp(typeof(PythonModule));
            cg.ContextSlot         = cg.GetArgumentSlot(0);
            cg.Names               = CodeGen.CreateFrameNamespace(cg.ContextSlot);
            cg.Context             = context;
            cg.printExprStmts      = printExprStmts;
            if (printExprStmts)
            {
                cg.Names.EnsureLocalSlot(Name.Make("_"));
            }

            cg.ContextSlot.EmitGet(cg);
            cg.EmitFieldGet(typeof(Frame), "__module__");
            cg.ModuleSlot.EmitSet(cg);

            if (context.TrueDivision)
            {
                cg.ContextSlot.EmitGet(cg);
                cg.EmitInt(1);
                cg.EmitCall(typeof(ICallerContext), "set_TrueDivision");
            }

            gs.Emit(cg);

            if (!(body is ReturnStmt))
            {
                cg.EmitPosition(Location.None, Location.None);
                cg.EmitReturn(null);
            }

            if (tg != null)
            {
                tg.FinishType();
            }

            FrameCode frameCode = new FrameCode(name,
                                                (FrameCodeDelegate)cg.CreateDelegate(typeof(FrameCodeDelegate)),
                                                staticData);

            return(frameCode);
        }
예제 #3
0
        public void ExecuteFile(string fileName)
        {
            Parser p = Parser.FromFile(engineContext.SystemState, context.CopyWithNewSourceFile(fileName));
            Stmt   s = p.ParseFileInput();

            Frame     topFrame = new Frame(engineContext.Module);
            FrameCode code     = OutputGenerator.GenerateSnippet(p.CompilerContext, s, false);

            code.Run(topFrame);
        }
예제 #4
0
        public int Execute(string text)
        {
            Parser p = Parser.FromString(engineContext.SystemState, context, text);
            Stmt   s = p.ParseFileInput();

            FrameCode code = OutputGenerator.GenerateSnippet(context, s, true);

            try {
                code.Run(new Frame(engineContext.Module));
            } catch (PythonSystemExit e) {
                return(e.GetExitCode(engineContext));
            }
            return(0);
        }
예제 #5
0
        public void Execute(object code)
        {
            FrameCode fc = code as FrameCode;

            if (fc != null)
            {
                fc.Run(new Frame(engineContext.Module));
            }
            else if (code is string)
            {
                Execute((string)code);
            }
            else
            {
                throw new ArgumentException("code object must be string or have been generated via PythonEngine.Compile");
            }
        }
예제 #6
0
        public static object ExecFile(ICallerContext context, object filename, object globals, object locals)
        {
            PythonModule mod = context.Module;

            if (globals == null)
            {
                globals = mod.__dict__;
            }
            if (locals == null)
            {
                locals = globals;
            }

            string fname = Converter.ConvertToString(filename);
            IDictionary <object, object> g = globals as IDictionary <object, object>;

            if (g == null)
            {
                throw Ops.TypeError("execfile: arg 2 must be dictionary");
            }

            CompilerContext cc = context.CreateCompilerContext().CopyWithNewSourceFile(fname);
            Parser          p;

            try {
                p = Parser.FromFile(context.SystemState, cc);
            } catch (UnauthorizedAccessException x) {
                throw Ops.IOError(x.Message);
            }
            Stmt s = p.ParseFileInput();

            IDictionary <object, object> l = locals as IDictionary <object, object>;

            if (l == null)
            {
                throw Ops.TypeError("execfile: arg 3 must be dictionary");
            }

            Frame     topFrame = new Frame(mod, g, l);
            FrameCode code     = OutputGenerator.GenerateSnippet(cc, s, false);

            code.Run(topFrame);
            return(null);
        }
예제 #7
0
        public static object Eval(ICallerContext context, string expression, IDictionary <object, object> globals, object locals)
        {
            if (locals != null && PythonOperator.IsMappingType(context, locals) == Ops.FALSE)
            {
                throw Ops.TypeError("locals must be mapping");
            }

            CompilerContext cc = context.CreateCompilerContext();
            Parser          p  = Parser.FromString(context.SystemState, cc, expression.TrimStart(' ', '\t'));
            Expr            e  = p.ParseTestListAsExpression();

            PythonModule mod = new PythonModule("<eval>", globals, context.SystemState, null, context.ContextFlags);

            if (Options.FastEval)  //!!! experimenting with a HUGE (>100x) performance boost to simple evals
            {
                return(e.Evaluate(new NameEnv(mod, locals)));
            }
            else
            {
                Stmt      s  = new ReturnStmt(e);
                FrameCode fc = OutputGenerator.GenerateSnippet(cc, s, false);
                return(fc.Run(new Frame(mod, globals, locals)));
            }
        }
예제 #8
0
 public void AddSnippet(FrameCode fc)
 {
     snippets.Add(fc);
 }