private void button1_Click(object sender, EventArgs e) { StringWriter writer = new StringWriter(); Dictionary<string,object> locals = new Dictionary<string, object>(); locals.Add("output", writer); try { CompiledCode functionsCode = eng.Compile(functions.Text); scriptCompiledCode = eng.Compile(script.Text); functionsCode.Execute(mod1); scriptCompiledCode.Execute(mod1, locals); } catch(Exception ex) { output.Text += Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine; } output.Text += writer.GetStringBuilder().ToString(); output.Text += Environment.NewLine; output.Text += Environment.NewLine; eng.Shutdown(); }
public void AddSnippet(CompiledCode compiledCode) { snippets.Add(compiledCode); }
public static CompiledCode GenerateSnippet(CompilerContext context, Statement body, string name, bool printExprStmts, bool enableDebugging) { GlobalSuite gs = Ast.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; if (enableDebugging) { tg = MakeDebuggableSnippetType(name, context.SourceFile); cg = tg.DefineUserHiddenMethod(MethodAttributes.Public | MethodAttributes.Static, "Initialize", typeof(object), new Type[] { typeof(ModuleScope) }); cg.typeGen = tg; cg.ModuleSlot = tg.moduleSlot; } else { cg = snippetAssembly.DefineDynamicMethod(name, typeof(object), new Type[] { typeof(ModuleScope) }); 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(SymbolTable.Underscore); } cg.ContextSlot.EmitGet(cg); cg.EmitFieldGet(typeof(ModuleScope), "__module__"); cg.ModuleSlot.EmitSet(cg); if (context.TrueDivision) { cg.ContextSlot.EmitGet(cg); cg.EmitInt(1); cg.EmitCall(typeof(ICallerContext), "set_TrueDivision"); } Slot dummySlot = null; // Emit a try/catch block for TraceBack support, except for simple return statements if (!(body is ReturnStatement)) { // Try block may yield, but we are not interested in the isBlockYielded value // hence push a dummySlot to pass the Assertion. dummySlot = cg.GetLocalTmp(typeof(object)); cg.EmitTraceBackTryBlockStart(dummySlot); cg.FuncOrClassName = "<module>"; cg.EmitSetTraceBackUpdateStatus(false); } gs.Emit(cg); if (!(body is ReturnStatement)) { // free up the dummySlot cg.FreeLocalTmp(dummySlot); cg.EmitTraceBackFaultBlock(); cg.EmitPosition(Location.None, Location.None); cg.EmitReturn(null); } cg.Finish(); if (tg != null) tg.FinishType(); CompiledCode compiledCode = new CompiledCode(name, (CompiledCodeDelegate)cg.CreateDelegate(typeof(CompiledCodeDelegate)), staticData); return compiledCode; }
internal FunctionCode(CompiledCode code, IronPython.Modules.Builtin.CompileFlags compilerFlags) : this(code) { if ((compilerFlags & IronPython.Modules.Builtin.CompileFlags.CO_FUTURE_DIVISION) != 0) flags |= FuncCodeFlags.FutureDivision; }
public FunctionCode(CompiledCode code) { this.compiledCode = code; }