Example #1
0
        void ProcessArrayFor(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);
            Variable      arrayValue = forScript.ExecuteFrom(index + 1);

            int cycles            = arrayValue.TotalElements();
            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 = ProcessBlock(script);
                if (result.IsReturn || result.Type == Variable.VarType.BREAK)
                {
                    script.Pointer = startForCondition;
                    SkipBlock(script);
                    return;
                }
            }
        }
Example #2
0
        void ProcessCanonicalFor(ParsingScript script, string forString)
        {
            string[] forTokens = forString.Split(Constants.END_STATEMENT);
            if (forTokens.Length != 3)
            {
                throw new ArgumentException("Expecting: for(init; condition; loopStatement)");
            }

            int startForCondition = script.Pointer;

            ParsingScript initScript = new ParsingScript(forTokens[0] + Constants.END_STATEMENT);
            ParsingScript condScript = new ParsingScript(forTokens[1] + Constants.END_STATEMENT);
            ParsingScript loopScript = new ParsingScript(forTokens[2] + Constants.END_STATEMENT);

            initScript.ParentScript = script;
            condScript.ParentScript = script;
            loopScript.ParentScript = script;

            initScript.ExecuteFrom(0);

            int  cycles     = 0;
            bool stillValid = true;

            while (stillValid)
            {
                Variable condResult = condScript.ExecuteFrom(0);
                stillValid = Convert.ToBoolean(condResult.Value);
                if (!stillValid)
                {
                    break;
                }

                if (MAX_LOOPS > 0 && ++cycles >= MAX_LOOPS)
                {
                    throw new ArgumentException("Looks like an infinite loop after " +
                                                cycles + " cycles.");
                }

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

            script.Pointer = startForCondition;
            SkipBlock(script);
        }