Пример #1
0
        public static async Task <List <Variable> > GetArgsAsync(ParsingScript script,
                                                                 char start, char end, Action <bool> outList)
        {
            List <Variable> args   = new List <Variable>();
            bool            isList = script.StillValid() && script.Current == Constants.START_GROUP;

            if (!script.StillValid() || script.Current == Constants.END_STATEMENT)
            {
                return(args);
            }

            ParsingScript tempScript = script.GetTempScript(script.String, script.Pointer);

            /*ParsingScript tempScript = new ParsingScript(script.String, script.Pointer);
             * tempScript.ParentScript = script;
             * tempScript.InTryBlock = script.InTryBlock;*/

            if (script.Current != start && script.TryPrev() != start &&
                (script.Current == ' ' || script.TryPrev() == ' '))
            { // Allow functions with space separated arguments
                start = ' ';
                end   = Constants.END_STATEMENT;
            }

            // ScriptingEngine - body is unsed (used in Debugging) but GetBodyBetween has sideeffects
#pragma warning disable 219
            string body = Utils.GetBodyBetween(tempScript, start, end);
#pragma warning restore 219
            // After the statement above tempScript.Parent will point to the last
            // character belonging to the body between start and end characters.

            while (script.Pointer < tempScript.Pointer)
            {
                Variable item = await Utils.GetItemAsync(script, false);

                args.Add(item);
                if (script.Pointer < tempScript.Pointer)
                {
                    script.MoveForwardIf(Constants.END_GROUP);
                    script.MoveForwardIf(Constants.NEXT_ARG);
                }
                if (script.Pointer == tempScript.Pointer - 1)
                {
                    script.MoveForwardIf(Constants.END_ARG, Constants.END_GROUP);
                }
            }

            if (script.Pointer <= tempScript.Pointer)
            {
                // Eat closing parenthesis, if there is one, but only if it closes
                // the current argument list, not one after it.
                script.MoveForwardIf(Constants.END_ARG, end);
            }

            script.MoveForwardIf(Constants.SPACE);
            //script.MoveForwardIf(Constants.SPACE, Constants.END_STATEMENT);
            outList(isList);
            return(args);
        }
Пример #2
0
        async Task ProcessArrayForAsync(ParsingScript script, string forString)
        {
            var    tokens  = forString.Split(' ');
            var    sep     = tokens.Length > 2 ? tokens[1] : "";
            string varName = tokens[0];

            if (sep != Constants.FOR_EACH && sep != Constants.FOR_IN && sep != Constants.FOR_OF)
            {
                int index = forString.IndexOf(Constants.FOR_EACH);
                if (index <= 0 || index == forString.Length - 1)
                {
                    Utils.ThrowErrorMsg("Expecting: for(item :/in/of array)",
                                        script, Constants.FOR);
                }
                varName = forString.Substring(0, index);
            }

            ParsingScript forScript = script.GetTempScript(forString, varName.Length + sep.Length + 1);

            forScript.Debugger = script.Debugger;

            Variable arrayValue = await Utils.GetItemAsync(forScript);

            if (arrayValue.Type == Variable.VarType.STRING)
            {
                arrayValue = new Variable(new List <string>(arrayValue.ToString().ToCharArray().Select(c => c.ToString())));
            }

            int cycles = arrayValue.Count;

            if (cycles == 0)
            {
                SkipBlock(script);
                return;
            }
            int startForCondition = script.Pointer;

            for (int i = 0; i < cycles; i++)
            {
                script.Pointer = startForCondition;
                Variable current = arrayValue.GetValue(i);
                ParserFunction.AddGlobalOrLocalVariable(varName,
                                                        new GetVarFunction(current));
                Variable result = await ProcessBlockAsync(script);

                if (result.IsReturn || result.Type == Variable.VarType.BREAK)
                {
                    //script.Pointer = startForCondition;
                    //SkipBlock(script);
                    //return;
                    break;
                }
            }
            script.Pointer = startForCondition;
            SkipBlock(script);
        }
Пример #3
0
        async Task ProcessArrayForAsync(ParsingScript script, string forString)
        {
            int index = forString.IndexOf(Constants.FOR_EACH);

            if (index <= 0 || index == forString.Length - 1)
            {
                throw new ArgumentException("Expecting: for(item : array)");
            }

            string varName = forString.Substring(0, index);


            ParsingScript forScript = new ParsingScript(forString, 0, script.Char2Line);

            forScript.ParentScript = script;
            forScript.Filename     = script.Filename;
            forScript.Debugger     = script.Debugger;
            forScript.Pointer      = index + Constants.FOR_EACH.Length;
            Variable arrayValue = await Utils.GetItemAsync(forScript);

            if (arrayValue.Type == Variable.VarType.STRING)
            {
                arrayValue = new Variable(new List <string>(arrayValue.ToString().ToCharArray().Select(c => c.ToString())));
            }

            int cycles = arrayValue.Count;

            if (cycles == 0)
            {
                SkipBlock(script);
                return;
            }
            int startForCondition = script.Pointer;

            for (int i = 0; i < cycles; i++)
            {
                script.Pointer = startForCondition;
                Variable current = arrayValue.GetValue(i);
                ParserFunction.AddGlobalOrLocalVariable(varName,
                                                        new GetVarFunction(current));
                Variable result = await ProcessBlockAsync(script);

                if (result.IsReturn || result.Type == Variable.VarType.BREAK)
                {
                    //script.Pointer = startForCondition;
                    //SkipBlock(script);
                    //return;
                    break;
                }
            }
            script.Pointer = startForCondition;
            SkipBlock(script);
        }