示例#1
0
        public static LambdaExpression Parse(
            string s, 
            IScope GlobalScope,
            IEnumerable<Assembly> RequireASM = null,
            IEnumerable<Type> RequireType = null)
        {
            var l = new LiParser(
                    Regex.Replace(s, ";(.*)((\r\n)|(\r)|(\n))", "\r\n")
                    .Replace("\r\n", "\r")
            );
            
            l.Included = RequireASM != null ? RequireASM.ToList() : new List<Assembly>();
            l.IncludedType = RequireType != null ? RequireType.ToList() : new List<Type>();
            /*l.DefineFunc = D;
            l.CompileFunc = C;
            l.GetVar = G;
            l.SetVar = S;
            l.CreateVar = CR;*/
            GlobalScope.Parent = new ClrScope((RequireType ?? new List<Type>()).Concat((RequireASM ?? new List<Assembly>()).SelectMany(x => x.ExportedTypes)));
            ExpressionTypeBeam.InitializeParameterEngine(GlobalScope);
            

            List<Expression> Output = new List<Expression>();
            l.DefineGlobal();
            l.DeclareAllFunctions();
            l.LiCompileFunction();

            ExpressionTypeBeam.PushScope();
            l.Script = Regex.Replace(string.Join("\r", l.Script), "func(.*?)endfunc", "", RegexOptions.IgnoreCase | RegexOptions.Singleline).Split('\r');
            if (l.Script.Length > 0)
                l.GotoLine(0);

            Expression ex;

            var cmd = AutExpression.VariableAccess("CmdLine", false, Expression.Constant(1, typeof(int)), typeof(string)).Generator();
            l.ConsumeWS();
            if (l.ScriptLine != "" && !l.ScriptLine.StartsWith(";") && !l.EOF && !l.EOL)
            {
                ex = l.ParseBoolean();
                if (ex is VarAutExpression) ex = (ex as VarAutExpression).Generator();
                Output.Add(ex);
            }
            while (!l.EOF)
            {
                try
                {
                    l.NextLine();
                }
                catch { break; }
                l.ConsumeWS();
                if (l.ScriptLine == "" || l.ScriptLine.StartsWith(";") || l.EOF || l.EOL) continue;
                
                ex = l.ParseBoolean();
                if (ex is VarAutExpression) ex = (ex as VarAutExpression).Generator();
                Output.Add(ex);
            }
            if (Output.Count(x => x != null) <= 0) return null;
            var SC = ExpressionTypeBeam.PopScope();
            BlockExpression e = Expression.Block(SC.Where(x => x.Name != "CmdLine" || x.Type != typeof(string[])), Output.ToArray().Where(x => x != null));
            
            return Expression.Lambda<Action<string[]>>(e, SC.First(x => x.Name == "CmdLine" && x.Type == typeof(string[])));
        }
示例#2
0
        public static LambdaExpression Parse(
            string s,
            IScope GlobalScope,
            IEnumerable <Assembly> RequireASM = null,
            IEnumerable <Type> RequireType    = null)
        {
            var l = new LiParser(
                Regex.Replace(s, ";(.*)((\r\n)|(\r)|(\n))", "\r\n")
                .Replace("\r\n", "\r")
                );

            l.Included = RequireASM != null?RequireASM.ToList() : new List <Assembly>();

            l.IncludedType = RequireType != null?RequireType.ToList() : new List <Type>();

            /*l.DefineFunc = D;
            *  l.CompileFunc = C;
            *  l.GetVar = G;
            *  l.SetVar = S;
            *  l.CreateVar = CR;*/
            GlobalScope.Parent = new ClrScope((RequireType ?? new List <Type>()).Concat((RequireASM ?? new List <Assembly>()).SelectMany(x => x.ExportedTypes)));
            ExpressionTypeBeam.InitializeParameterEngine(GlobalScope);


            List <Expression> Output = new List <Expression>();

            l.DefineGlobal();
            l.DeclareAllFunctions();
            l.LiCompileFunction();

            ExpressionTypeBeam.PushScope();
            l.Script = Regex.Replace(string.Join("\r", l.Script), "func(.*?)endfunc", "", RegexOptions.IgnoreCase | RegexOptions.Singleline).Split('\r');
            if (l.Script.Length > 0)
            {
                l.GotoLine(0);
            }

            Expression ex;

            var cmd = AutExpression.VariableAccess("CmdLine", false, Expression.Constant(1, typeof(int)), typeof(string)).Generator();

            l.ConsumeWS();
            if (l.ScriptLine != "" && !l.ScriptLine.StartsWith(";") && !l.EOF && !l.EOL)
            {
                ex = l.ParseBoolean();
                if (ex is VarAutExpression)
                {
                    ex = (ex as VarAutExpression).Generator();
                }
                Output.Add(ex);
            }
            while (!l.EOF)
            {
                try
                {
                    l.NextLine();
                }
                catch { break; }
                l.ConsumeWS();
                if (l.ScriptLine == "" || l.ScriptLine.StartsWith(";") || l.EOF || l.EOL)
                {
                    continue;
                }

                ex = l.ParseBoolean();
                if (ex is VarAutExpression)
                {
                    ex = (ex as VarAutExpression).Generator();
                }
                Output.Add(ex);
            }
            if (Output.Count(x => x != null) <= 0)
            {
                return(null);
            }
            var             SC = ExpressionTypeBeam.PopScope();
            BlockExpression e  = Expression.Block(SC.Where(x => x.Name != "CmdLine" || x.Type != typeof(string[])), Output.ToArray().Where(x => x != null));

            return(Expression.Lambda <Action <string[]> >(e, SC.First(x => x.Name == "CmdLine" && x.Type == typeof(string[]))));
        }