Пример #1
0
        private Expression ParseKeywordOrFunc(string Keyword, Type Desired = null)
        {
            //Expression Element;
            Keyword = Keyword.ToUpper();
            switch (Keyword)
            {
            case "TRUE": return(Expression.Constant(true, typeof(bool)));

            case "FALSE": return(Expression.Constant(false, typeof(bool)));

            case "IF": return(ParseKeyword_IF(Keyword));

            case "WHILE": return(ParseKeyword_WHILE(Keyword));

            case "FOR": return(ParseKeyword_FOR(Keyword));

            case "RETURN": return(ParseKeyword_RETURN(Keyword));

            case "DO": return(ParseKeyword_DO(Keyword));

            case "NEW": return(ParseKeyword_NEW(Keyword));

            case "DEFAULT": return(Expression.Constant(null));

            case "GLOBAL": return(ParseKeyword_GLOBAL(Keyword));

            case "DEFINE": return(ParseKeyword_DEFINE(Keyword));

            case "ENDIF":
            case "NEXT":
            case "END":
            case "WEND":
            case "ENDWITH":
            case "UNTIL":
            case "ENDFUNC":
            case "ELSE":
            case "ELSEIF":
                return(AutExpression.EndOfBlock());

            case "EXITLOOP":
                ConsumeWS();
                int    goback = 1;
                string str    = GetNbr();
                if (str != "")
                {
                    goback = int.Parse(str);
                }
                if (Contextual.Count > 1 + (goback - 1) * 2)
                {
                    return(Contextual.ElementAt(1 + (goback - 1) * 2));
                }
                else
                {
                    throw new AutoitException(AutoitExceptionType.EXITLLOOPOUTSIDELOOP, LineNumber, Cursor);
                }

            case "CONTINUELOOP":
                int    goback2 = 1;
                string str2    = GetNbr();
                if (str2 != "")
                {
                    goback2 = int.Parse(str2);
                }
                if (Contextual.Count > (goback2 - 1) * 2)
                {
                    return(Contextual.ElementAt((goback2 - 1) * 2));
                }
                else
                {
                    throw new AutoitException(AutoitExceptionType.EXITLLOOPOUTSIDELOOP, LineNumber, Cursor);
                }

            default: return(Parse_CALL(Keyword));
            }
        }
Пример #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[]))));
        }