public ExecuteExpression <T> CreateExpressionDelegate <T>(string expression, object functionClass)
        {
            ExecuteExpression <T> expressionDelegate = null;
            DynamicMethodState    methodState;

            IExpressionCompiler compiler;

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            AppDomainSetup loSetup = new AppDomainSetup();

            loSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            AppDomain loAppDomain = AppDomain.CreateDomain("CompilerDomain", null, loSetup);

            compiler = (IExpressionCompiler)loAppDomain.CreateInstanceFromAndUnwrap("ArcherFusion.ExpressionCompiler.dll", "ArcherFusion.ExpressionCompiler.CSharpExpressionCompiler");

            try
            {
                methodState = compiler.CompileExpression(expression, functionClass.GetType(), typeof(T));
            }
            catch (CompileException e)
            {
                StringBuilder exceptionMessage = new StringBuilder();

                foreach (CompilerError error in e.CompileErrors)
                {
                    exceptionMessage.Append("Error# ").Append(error.ErrorNumber);
                    exceptionMessage.Append(", column ").Append(error.Column);
                    exceptionMessage.Append(", ").Append(error.ErrorText);
                    exceptionMessage.Append(Environment.NewLine);
                }

                throw new ApplicationException(exceptionMessage.ToString());
            }
            finally
            {
                AppDomain.Unload(loAppDomain);
            }

            stopWatch.Stop();

            if (methodState != null && methodState.CodeBytes != null)
            {
                expressionDelegate = CreateExpressionDelegate <T>(methodState, functionClass);
            }

            return(expressionDelegate);
        }
        /// <summary>
        /// Compiles an expression and returns a delegate to the compiled code.
        /// </summary>
        /// <typeparam name="R">The return type of the expression</typeparam>
        /// <typeparam name="C">The type of the function class</typeparam>
        /// <param name="expression">Expression to evaluate</param>
        /// <returns>ExecuteExpression&lt;R, C&gt; - a delegate that calls the compiled expression</returns>
        public ExecuteExpression <R, C> CreateExpressionDelegate <R, C>(string expression)
        {
            ExecuteExpression <R, C> expressionDelegate = null;

            //create the compiled expression
            var methodState = CreateExpressionMethodState <R, C>(expression);

            if (methodState != null && methodState.CodeBytes != null)
            {
                //get a dynamic method delegate from the method state
                expressionDelegate = CreateExpressionDelegate <R, C>(methodState);
            }

            return(expressionDelegate);
        }
Example #3
0
        /// <summary>
        /// Compiles a DynamicMethodState and returns a delegate.
        /// </summary>
        /// <typeparam name="R">The return type of the expression</typeparam>
        /// <typeparam name="C">The type of the function class</typeparam>
        /// <param name="methodState">The serialized version of a method on the functionClass</param>
        /// <returns>EvalExpression&lt;R, C&gt; - a delegate that calls the compiled expression</returns>
        internal EvalExpression <R, C> GetDelegate <R, C>(DynamicMethodState methodState)
        {
            ExecuteExpression <R, C> methodDelegate = null;

            //get delegate factory
            var delegateFactory = new ExpressionDelegateFactory();

            if (methodState != null && methodState.CodeBytes != null)
            {
                //get delegate from factory
                methodDelegate = delegateFactory.CreateExpressionDelegate <R, C>(methodState);
            }

            //return an eval delegate based on the delegate from the factory
            return(new EvalExpression <R, C>(methodDelegate));
        }
        /// <summary>
        /// Compiles a DynamicMethodState and returns a delegate.
        /// </summary>
        /// <typeparam name="R">The return type of the expression</typeparam>
        /// <typeparam name="C">The type of the function class</typeparam>
        /// <param name="methodState">The serialized version of a method on the functionClass</param>
        /// <returns>CompiledCode&lt;R, C&gt; - a delegate that calls the compiled expression</returns>
        public CompiledCode <R, C> GetDelegate <R, C>(DynamicMethodState methodState)
        {
            ExecuteExpression <R, C> methodDelegate = null;

            //get delegate factory
            IExpressionDelegateFactory delegateFactory = new ExpressionDelegateFactory(m_language);

            if (methodState != null && methodState.codeBytes != null)
            {
                //get delegate from factory
                methodDelegate = delegateFactory.CreateExpressionDelegate <R, C>(methodState);
            }

            //return an eval delegate based on the delegate from the factory
            return(new CompiledCode <R, C>(methodDelegate));
        }
Example #5
0
        public override void EmitIL(_ATHProgram program, Colour expressionColour, ILGenerator ilGenerator, Dictionary <string, ImportHandle> importHandles, Dictionary <Tuple <string, Colour>, ImportHandle> objects)
        {
            var startLabel = ilGenerator.DefineLabel();
            var endLabel   = ilGenerator.DefineLabel();

            ilGenerator.MarkLabel(startLabel);

            if (Target == "THIS")
            {
                program.EmitIsTHISAlive(ilGenerator, TargetColour ?? expressionColour);

                if (Not)
                {
                    ilGenerator.Emit(OpCodes.Brtrue, endLabel);
                }
                else
                {
                    ilGenerator.Emit(OpCodes.Brfalse, endLabel);
                }
            }
            else
            {
                var target = Tuple.Create(Target, TargetColour ?? expressionColour);
                objects[target].EmitIsAlive(program, ilGenerator, target);
                if (Not)
                {
                    ilGenerator.Emit(OpCodes.Brtrue, endLabel);
                }
                else
                {
                    ilGenerator.Emit(OpCodes.Brfalse, endLabel);
                }
            }

            foreach (var expression in _loopExpressions)
            {
                program.EmitDieIfKilled(ilGenerator, expressionColour);

                expression.EmitIL(program, expressionColour, ilGenerator, importHandles, objects);
            }

#if !NODELAY
            var threadSleep = ((Action <int>)Thread.Sleep).Method;

            ilGenerator.Emit(OpCodes.Ldc_I4, 100);
            ilGenerator.EmitCall(OpCodes.Call, threadSleep, null);
#endif

            ilGenerator.Emit(OpCodes.Br, startLabel);

            ilGenerator.MarkLabel(endLabel);

            if (ExecuteExpression != null)
            {
                if (ExecuteExpression is NULLExpression)
                {
                    // Empty.
                }
                else
                {
                    program.EmitDieIfKilled(ilGenerator, expressionColour);

                    ExecuteExpression.EmitIL(program, expressionColour, ilGenerator, importHandles, objects);
                }
            }
            else
            {
                var getFullPath  = ((Func <string, string>)Path.GetFullPath).Method;
                var startProcess = ((Func <string, Process>)Process.Start).Method;

                ilGenerator.Emit(OpCodes.Ldstr, ExecuteCommand);
                ilGenerator.EmitCall(OpCodes.Call, getFullPath, null);
                ilGenerator.EmitCall(OpCodes.Call, startProcess, null);
                ilGenerator.Emit(OpCodes.Pop);
            }
        }