Exemplo n.º 1
0
        public override string Execute(string input, TextWriter output)
        {
            // A bit awkward, but we need to set up a match against the whole input
            // along with the resulting separators and everything, because that's
            // what the Replacer eats.
            var regex   = new Regex(@"\A(?s:.*)\z");
            var matches = new List <MatchContext>();

            matches.Add(new MatchContext(regex.Match(input), regex, Replacer));
            var separators = new List <MatchContext>();
            var startRegex = new Regex(@"\A");

            separators.Add(new MatchContext(startRegex.Match(input), startRegex, Replacer));
            var endRegex = new Regex(@"\z");

            separators.Add(new MatchContext(endRegex.Match(input), endRegex, Replacer));

            string evalInput = Replacer.Process(input, matches, separators, 0);

            string source = ChildStage.Execute(input, output);

            if (Config.Reverse)
            {
                string temp = source;
                source    = evalInput;
                evalInput = temp;
            }

            var interpreter = new Interpreter(source.Split(new[] { '\n' }).Select(line => line.Replace('¶', '\n')).ToList());

            var evalOutput = new StringWriter();

            interpreter.Execute(evalInput, evalOutput);

            string result = evalOutput.ToString();

            if (RegisterWithHistory)
            {
                History.RegisterResult(HistoryIndex, result);
            }

            return(result);
        }
Exemplo n.º 2
0
        public override string Execute(string input, TextWriter output)
        {
            string result = input;

            if (Config.Random)
            {
                while (true)
                {
                    if (Random.RNG.Next(2) == 0)
                    {
                        break;
                    }
                    result = ChildStage.Execute(result, output).ToString();
                }
            }
            else if (Config.RegexParam != null)
            {
                while (Config.RegexParam.Match(result).Success ^ Config.Reverse)
                {
                    result = ChildStage.Execute(result, output).ToString();
                }
            }
            else
            {
                bool limitedIterations = false;
                int  iterationCount    = 0;
                if (Config.StringParam != null)
                {
                    limitedIterations = true;

                    // A bit awkward, but we need to set up a match against the whole input
                    // along with the resulting separators and everything, because that's
                    // what the Replacer eats.
                    var regex   = new Regex(@"\A(?s:.*)\z");
                    var matches = new List <MatchContext>();
                    matches.Add(new MatchContext(regex.Match(input), regex, Replacer));
                    var separators = new List <MatchContext>();
                    var startRegex = new Regex(@"\A");
                    separators.Add(new MatchContext(startRegex.Match(input), startRegex, Replacer));
                    var endRegex = new Regex(@"\z");
                    separators.Add(new MatchContext(endRegex.Match(input), endRegex, Replacer));

                    string countString = Replacer.Process(input, matches, separators, 0);

                    var countRegex = new Regex(@"-?\d+");
                    var countMatch = countRegex.Match(countString);

                    iterationCount = countMatch.Success ? int.Parse(countMatch.Value) : 0;
                }
                else if (Config.GetLimitCount() > 0)
                {
                    limitedIterations = true;

                    iterationCount = Config.GetLimit(0).End;
                }

                if (limitedIterations && iterationCount < 0)
                {
                    for (int i = 0; i < -iterationCount; ++i)
                    {
                        result = ChildStage.Execute(result, output).ToString();
                    }
                }
                else
                {
                    int    i = 0;
                    string lastResult;
                    do
                    {
                        if (limitedIterations && i >= iterationCount)
                        {
                            break;
                        }
                        ++i;
                        lastResult = result;
                        result     = ChildStage.Execute(lastResult, output).ToString();
                    } while (lastResult != result);
                }
            }

            if (RegisterWithHistory)
            {
                History.RegisterResult(HistoryIndex, result);
            }

            return(result);
        }