Пример #1
0
 private static IEnumerator <RantAction> Then(Sandbox sb, RantAction conditionPassPattern)
 {
     if (sb.Engine.Flags.All(flag => sb.ConditionFlags.Contains(flag) == sb.FlagConditionExpectedResult))
     {
         yield return(conditionPassPattern);
     }
 }
Пример #2
0
 private static IEnumerator <RantAction> Else(Sandbox sb, RantAction conditionFailPattern)
 {
     if (sb.Engine.Flags.Any(flag => sb.ConditionFlags.Contains(flag) != sb.FlagConditionExpectedResult))
     {
         yield return(conditionFailPattern);
     }
 }
Пример #3
0
        private static IEnumerator <RantAction> NumberFormatRange(Sandbox sb,
                                                                  [RantDescription("The number format to use.")]
                                                                  NumberFormat format,
                                                                  [RantDescription("The pattern to run.")]
                                                                  RantAction rangeAction)
        {
            var oldFmtMap = new Dictionary <OutputChain, NumberFormat>();

            sb.Output.Do(chain =>
            {
                oldFmtMap[chain] = chain.Last.NumberFormatter.NumberFormat;
                chain.Last.NumberFormatter.NumberFormat = format;
            });

            yield return(rangeAction);

            NumberFormat fmt;

            sb.Output.Do(chain =>
            {
                if (!oldFmtMap.TryGetValue(chain, out fmt))
                {
                    return;
                }
                chain.Last.NumberFormatter.NumberFormat = fmt;
            });
        }
Пример #4
0
 private static void Sep(Sandbox sb,
                         [RantDescription("The separator pattern to run between iterations of the next block.")]
                         RantAction separator)
 {
     sb.CurrentBlockAttribs.IsSeries  = false;
     sb.CurrentBlockAttribs.Separator = separator;
 }
Пример #5
0
        private static IEnumerator <RantAction> Branch(Sandbox sb, string id, RantAction branchAction)
        {
            sb.RNG.Branch(id.Hash());
            yield return(branchAction);

            sb.RNG.Merge();
        }
Пример #6
0
        protected void AddToOutput(RantAction action)
        {
            // NOTE: kinda sucks we can't include the caller's name here because this method's signature must stay just as it is
            if (!outputDelegates.Any())
            {
                throw new RantInternalException($"{GetType().Name}.AddToOutput({action.GetType().Name}): Output delegate stack is empty.");
            }

            outputDelegates.Peek()(action);
        }
Пример #7
0
 private static void Sep(Sandbox sb,
                         [RantDescription("The separator pattern to run between items.")]
                         RantAction separator,
                         [RantDescription("The conjunction pattern to run before the last item.")]
                         RantAction conjunction)
 {
     sb.CurrentBlockAttribs.IsSeries       = true;
     sb.CurrentBlockAttribs.Separator      = separator;
     sb.CurrentBlockAttribs.EndConjunction = conjunction;
 }
Пример #8
0
 private static void RepSep(Sandbox sb,
                            [RantDescription("The number of times to repeat the next block.")]
                            int times,
                            [RantDescription("The separator pattern to run between iterations of the next block.")]
                            RantAction separator)
 {
     sb.CurrentBlockAttribs.IsSeries    = false;
     sb.CurrentBlockAttribs.Repetitions = times;
     sb.CurrentBlockAttribs.Separator   = separator;
 }
Пример #9
0
        private static IEnumerator <RantAction> Quote(Sandbox sb,
                                                      [RantDescription("The pattern to run whose output will be surrounded in quotes.")]
                                                      RantAction quoteAction)
        {
            sb.IncreaseQuote();
            sb.PrintOpeningQuote();
            yield return(quoteAction);

            sb.PrintClosingQuote();
            sb.DecreaseQuote();
        }
Пример #10
0
 private static IEnumerator <RantAction> Even(Sandbox sb,
                                              [RantDescription("The pattern to run when the condition is met.")]
                                              RantAction action)
 {
     if (!sb.Blocks.Any())
     {
         yield break;
     }
     if (sb.Blocks.Peek().Iteration % 2 == 0)
     {
         yield return(action);
     }
 }
Пример #11
0
        private static IEnumerator <RantAction> Ends(Sandbox sb,
                                                     [RantDescription("The pattern to run when the condition is met.")]
                                                     RantAction action)
        {
            if (!sb.Blocks.Any())
            {
                yield break;
            }
            var block = sb.Blocks.Peek();

            if (block.Iteration == 1 || block.Iteration == block.Count)
            {
                yield return(action);
            }
        }
Пример #12
0
 private static IEnumerator <RantAction> NotNth(Sandbox sb,
                                                [RantDescription("The interval at which the pattern should not be run.")]
                                                int interval,
                                                [RantDescription("The pattern to run when the condition is satisfied.")]
                                                RantAction pattern)
 {
     if (!sb.Blocks.Any())
     {
         yield break;
     }
     if (sb.Blocks.Peek().Iteration % interval == 0)
     {
         yield break;
     }
     yield return(pattern);
 }
Пример #13
0
 private static IEnumerator <RantAction> NotNthO(Sandbox sb,
                                                 [RantDescription("The interval at which the pattern should not be run.")]
                                                 int interval,
                                                 [RantDescription("The number of iterations to offset the interval by.")]
                                                 int offset,
                                                 [RantDescription("The pattern to run when the condition is satisfied.")]
                                                 RantAction pattern)
 {
     if (!sb.Blocks.Any())
     {
         yield break;
     }
     if (Util.Mod(sb.Blocks.Peek().Iteration - offset, interval) == 0)
     {
         yield break;
     }
     yield return(pattern);
 }
Пример #14
0
        /// <summary>
        /// Creates a new RantObject instance from the specified object.
        /// </summary>
        /// <param name="obj">The value to assign to the object.</param>
        public RantObject(object obj)
        {
            if (obj == null)
            {
                return;
            }

            if (obj is string)
            {
                _string = obj.ToString();
                Type    = RantObjectType.String;
            }
            else if (obj is bool)
            {
                _boolean = (bool)obj;
                Type     = RantObjectType.Boolean;
            }
            else if (IsNumber(obj))
            {
                _number = (double)obj;
                Type    = RantObjectType.Number;
            }
            else if (obj is List <RantObject> )
            {
                _list = (List <RantObject>)obj;
                Type  = RantObjectType.List;
            }
            else if (obj.GetType().IsArray)
            {
                _list = ((object[])obj).Select(o => new RantObject(o)).ToList();
                Type  = RantObjectType.List;
            }
            else if (obj is RantPattern)
            {
                _pattern = (RantPattern)obj;
            }
            else if (obj is RantAction)
            {
                _action = (RantAction)obj;
                Type    = RantObjectType.Action;
            }
        }
Пример #15
0
        private static IEnumerator <RantAction> Chan(Sandbox sb, string channelName, ChannelVisibility visibility, RantAction pattern)
        {
            sb.Output.OpenChannel(channelName, visibility);
            yield return(pattern);

            sb.Output.CloseChannel();
        }
Пример #16
0
 private static void After(Sandbox sb,
                           [RantDescription("The pattern to run after each iteration of the next block.")]
                           RantAction afterAction)
 {
     sb.CurrentBlockAttribs.After = afterAction;
 }
Пример #17
0
 private static void Before(Sandbox sb,
                            [RantDescription("The pattern to run before each iteration of the next block.")]
                            RantAction beforeAction)
 {
     sb.CurrentBlockAttribs.Before = beforeAction;
 }
Пример #18
0
 internal void AddActionFunction(string name, RantAction body)
 {
     _objects[name] = new RantObject(body);
 }
Пример #19
0
 private static void Start(Sandbox sb,
                           [RantDescription("The pattern to run before the next block.")]
                           RantAction beforePattern)
 {
     sb.CurrentBlockAttribs.Start = beforePattern;
 }
Пример #20
0
 internal RantObject(RantAction action)
 {
     Type    = RantObjectType.Action;
     _action = action;
 }
Пример #21
0
 private static void End(Sandbox sb,
                         [RantDescription("The pattern to run after the next block.")]
                         RantAction endPattern)
 {
     sb.CurrentBlockAttribs.End = endPattern;
 }
Пример #22
0
        private IEnumerable <Parselet> LeftCurly(Token <R> token)
        {
            reader.SkipSpace();

            // LOOK AT ME. I'M THE COMPILER NOW
            Token <R> readToken = null;
            var       actions   = new List <RantAction>();
            var       sequences = new List <RantAction>();

            List <_ <int, double> >     constantWeights = null;
            List <_ <int, RantAction> > dynamicWeights  = null;

            while (!reader.End)
            {
                readToken = reader.ReadToken();

                // TODO: kinda stupid having this handle it's own whitespace when we have a parselet for whitespace
                if (readToken.ID == R.Whitespace)
                {
                    switch (reader.PeekType())
                    {
                    case R.RightCurly:
                    case R.Pipe:
                        continue;
                    }
                }
                else if (readToken.ID == R.LeftParen) // weight
                {
                    RantAction weightAction = null;

                    // i like this AddToOutput thing because it's just a delegate that takes in a RantAction.
                    // it can do anything with the RantAction, in this case it sets it to our weightAction
                    // :>
                    yield return(Parselet.GetParselet("BlockWeight", readToken, a => weightAction = a));

                    constantWeights = constantWeights ?? new List <_ <int, double> >();
                    dynamicWeights  = dynamicWeights ?? new List <_ <int, RantAction> >();

                    if (weightAction is RAText) // constant
                    {
                        var    strWeight = (weightAction as RAText).Text;
                        double d;

                        if (!Util.ParseDouble(strWeight, out d))
                        {
                            compiler.SyntaxError(weightAction.Range, $"Invalid weight value '{strWeight}'.");
                        }

                        constantWeights.Add(_.Create(sequences.Count, d));
                    }
                    else // dynamic
                    {
                        // TODO: there's some weird issue going on with doubles being seen as dynamic weights
                        dynamicWeights.Add(_.Create(sequences.Count, weightAction));
                    }

                    continue;
                }
                else if (readToken.ID == R.Pipe)
                {
                    // add action to block and continue
                    sequences.Add(actions.Count == 1 ? actions[0] : new RASequence(actions, readToken));
                    reader.SkipSpace();
                    actions.Clear();
                    continue;
                }
                else if (readToken.ID == R.RightCurly)
                {
                    // add action to block and return
                    sequences.Add(actions.Count == 1 ? actions[0] : new RASequence(actions, readToken));
                    AddToOutput(new RABlock(Stringe.Range(token, readToken), sequences, dynamicWeights, constantWeights));
                    yield break;
                }

                yield return(Parselet.GetParselet(readToken, actions.Add));
            }

            compiler.SyntaxError(token, "Unterminated block: unexpected end of file.");
        }