/// <summary>
        /// Allocates a new expression. This call fails if the scope was already compiled
        /// </summary>
        /// <param name="scope">A valid identifier for namespace sub-path like "mycode.test" no leading or trailing "."</param>
        /// <param name="expression">A C# expression to compile</param>
        /// <param name="referencedAssemblies"> An enumerable of assemblies that compiler should reference while building scope assembly i.e. "MyCompany.dll"</param>
        /// <param name="usings">Extra usings i.e. "System.IO", "MyCode.Routines" etc.</param>
        public CompilingExpressionEvaluator(string scope,
                                            string expression,
                                            IEnumerable <string> referencedAssemblies = null,
                                            IEnumerable <string> usings = null)
        {
            if (string.IsNullOrWhiteSpace(scope) ||
                string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException(StringConsts.ARGUMENT_ERROR + "CompilingExpressionEvaluator(scope,expression)");
            }

            m_Scope      = scope.Trim();
            m_Expression = expression;

            _ScopeData sdata = null;

            lock (_G.s_Scopes)
            {
                if (!_G.s_Scopes.TryGetValue(scope, out sdata))
                {
                    sdata = new _ScopeData();
                    _G.s_Scopes.Add(scope, sdata);
                }

                if (sdata.Assembly != null)
                {
                    throw new NFXException(StringConsts.CAN_NOT_CREATE_MORE_SCOPE_EXPRESSIONS_ERROR + scope);
                }

                if (sdata.CompileException != null)
                {
                    throw new NFXException(StringConsts.CAN_NOT_ADD_FAILED_SCOPE_COMPILE_ERROR + scope, sdata.CompileException);
                }

                sdata.m_Expressions.Add(this);

                if (referencedAssemblies != null)
                {
                    foreach (var ass in referencedAssemblies)
                    {
                        if (!sdata.m_ReferencedAssemblies.Contains(ass))
                        {
                            sdata.m_ReferencedAssemblies.Add(ass);
                        }
                    }
                }

                if (usings != null)
                {
                    foreach (var use in usings)
                    {
                        if (!sdata.m_Usings.Contains(use))
                        {
                            sdata.m_Usings.Add(use);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Indicates whether this scope has already been compiled and no more epressions can be allocated in it
        /// </summary>
        public static bool IsScopeAlreadyCompiled(string scope)
        {
            _ScopeData sdata = null;

            lock (_G.s_Scopes)
            {
                if (_G.s_Scopes.TryGetValue(scope, out sdata))
                {
                    if (sdata.Assembly != null || sdata.CompileException != null)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        private void doCompile(_ScopeData sdata)
        {
            CSharpCodeProvider comp = new CSharpCodeProvider();

            CompilerParameters cp = new CompilerParameters();

            foreach (var ass in sdata.m_ReferencedAssemblies)
            {
                cp.ReferencedAssemblies.Add(ass);
            }
            cp.GenerateExecutable = false;
            cp.GenerateInMemory   = true;


            var ns = "NFX.Parsing.CompilingExpressionEvaluator." + m_Scope;

            StringBuilder code = new StringBuilder();

            foreach (var use in sdata.m_Usings)
            {
                code.AppendLine("using " + use + ";");
            }

            code.AppendLine("namespace " + ns + " { ");
            code.AppendLine("  public static class Evaluator { ");

            for (var i = 0; i < sdata.m_Expressions.Count; i++)
            {
                var expr = sdata.m_Expressions[i];

                code.AppendLine(
                    string.Format(" public static {0} DoEval_{1}({2} ctx, {3} arg)",
                                  tname(typeof(TResult)),
                                  i,
                                  tname(typeof(TContext)),
                                  tname(typeof(TArg))
                                  )
                    );

                code.AppendLine("  { ");

                code.AppendLine("       return (" + expr.m_Expression + ");");

                code.AppendLine("  } ");
                code.AppendLine();
            }  //for

            code.AppendLine("}");  //class
            code.AppendLine("}");  //namespace

            CompilerResults cr = comp.CompileAssemblyFromSource(cp, code.ToString());

            if (cr.Errors.HasErrors)
            {
                StringBuilder error = new StringBuilder();
                error.Append("Error Compiling Expression: ");
                foreach (CompilerError err in cr.Errors)
                {
                    error.AppendFormat("{0}\n", err.ErrorText);
                }

                sdata.CompileException = new NFXException(StringConsts.EXPRESSION_SCOPE_COMPILE_ERROR + error.ToString());
                return;
            }

            sdata.Assembly = cr.CompiledAssembly;

            for (var i = 0; i < sdata.m_Expressions.Count; i++)
            {
                var expr   = sdata.m_Expressions[i];
                var type   = sdata.Assembly.GetType(ns + ".Evaluator");
                var method = type.GetMethod("DoEval_" + i.ToString());

                expr.m_EvalMethod = method;
            }
        }
Ejemplo n.º 4
0
        private void doCompile(_ScopeData sdata)
        {
           #warning Implement using PAL
            //CSharpCodeProvider comp = new CSharpCodeProvider();

            // CompilerParameters cp = new CompilerParameters();
            //   foreach(var ass in sdata.m_ReferencedAssemblies)
            //      cp.ReferencedAssemblies.Add(ass);
            //   cp.GenerateExecutable = false;
            //   cp.GenerateInMemory = true;


            // var ns = "Azos.Text.CompilingExpressionEvaluator."+m_Scope;

            // StringBuilder code = new StringBuilder();
            //   foreach(var use in sdata.m_Usings)
            //     code.AppendLine("using " + use + ";");

            //   code.AppendLine("namespace "+ns+" { ");
            //   code.AppendLine("  public static class Evaluator { ");

            //   for(var i=0; i<sdata.m_Expressions.Count; i++)
            //   {
            //      var expr = sdata.m_Expressions[i];

            //           code.AppendLine(
            //             string.Format(" public static {0} DoEval_{1}({2} ctx, {3} arg)",
            //                tname(typeof(TResult)),
            //                i,
            //                tname(typeof(TContext)),
            //                tname(typeof(TArg))
            //             )
            //           );

            //           code.AppendLine("  { ");

            //           code.AppendLine("       return (" + expr.m_Expression + ");");

            //           code.AppendLine("  } ");
            //           code.AppendLine();
            //   }//for

            //   code.AppendLine("}");//class
            //   code.AppendLine("}");//namespace

            // CompilerResults cr = comp.CompileAssemblyFromSource(cp, code.ToString());
            //   if (cr.Errors.HasErrors)
            //   {
            //     StringBuilder error = new StringBuilder();
            //     error.Append("Error Compiling Expression: ");
            //     foreach (CompilerError err in cr.Errors)
            //     {
            //       error.AppendFormat("{0}\n", err.ErrorText);
            //     }

            //     sdata.CompileException =  new AzosException(StringConsts.EXPRESSION_SCOPE_COMPILE_ERROR + error.ToString());
            //     return;
            //   }

            // sdata.Assembly = cr.CompiledAssembly;

            // for(var i=0; i<sdata.m_Expressions.Count; i++)
            // {
            //    var expr = sdata.m_Expressions[i];
            //    var type = sdata.Assembly.GetType(ns+".Evaluator");
            //    var method = type.GetMethod("DoEval_"+i.ToString());

            //    expr.m_EvalMethod = method;
            // }
        }