Example #1
0
 private static bool Else(VM vm, RantPattern source, Stringe tagname, Argument[] args)
 {
     if (!vm.UseElse())
     {
         return(false);
     }
     vm.PushState(VM.State.CreateSub(source, args[0].AsPattern(), vm, vm.CurrentState.Output));
     return(true);
 }
Example #2
0
 private static bool IfDef(VM vm, RantPattern source, Stringe tagname, Argument[] args)
 {
     if (vm.Engine.Flags.Contains(args[0]))
     {
         vm.PushState(VM.State.CreateSub(source, args[1].AsPattern(), vm, vm.CurrentState.Output));
         return(true);
     }
     vm.SetElse();
     return(false);
 }
Example #3
0
        private static bool BranchScope(VM vm, RantPattern source, Stringe tagName, Argument[] args)
        {
            vm.RNG.Branch(args[0].AsString().Hash());
            var state = VM.State.CreateSub(source, args[1].AsPattern(), vm, vm.CurrentState.Output);

            state.Post(new DelegateBlueprint(vm, _ =>
            {
                vm.RNG.Merge();
                return(false);
            }));
            vm.PushState(state);
            return(true);
        }
Example #4
0
 private static bool Quote(VM vm, RantPattern source, Stringe tagname, Argument[] args)
 {
     vm.PushState(VM.State.CreateSub(source, args[0].AsPattern(), vm, vm.CurrentState.Output)
                  .Pre(new DelegateBlueprint(vm, _ =>
     {
         vm.OpenQuote();
         return(false);
     }))
                  .Post(new DelegateBlueprint(vm, _ =>
     {
         vm.CloseQuote();
         return(false);
     })));
     return(true);
 }
Example #5
0
        private static bool DoMath(VM interpreter, Token <R> firstToken, PatternReader reader, State state)
        {
            var tokens = reader.ReadToTokenInParentScope(firstToken.ID, Delimiters.All);

            interpreter.PushState(State.CreateSub(reader.Source, tokens, interpreter));
            state.Pre(new DelegateBlueprint(interpreter, _ =>
            {
                var expression = _.PopResultString().Trim();
                var v          = MathParser.Calculate(_, expression);
                if (!expression.EndsWith(";"))
                {
                    _.Print(_.FormatNumber(v));
                }
                return(false);
            }));
            return(true);
        }
Example #6
0
        private static bool Compare(VM vm, RantPattern source, Stringe tagname, Argument[] args)
        {
            var cmp = new Comparison(args[0].AsString(), args[1].AsString());

            vm.Comparisons.Push(cmp);

            var state = VM.State.CreateSub(source, args[2].AsPattern(), vm, vm.CurrentState.Output);

            state.Post(new DelegateBlueprint(vm, I =>
            {
                I.Comparisons.Pop();
                return(false);
            }));

            vm.PushState(state);

            return(true);
        }
Example #7
0
        private static bool CmpIs(VM vm, RantPattern source, Stringe tagname, Argument[] args)
        {
            var cmp        = vm.Comparisons.Peek();
            var conStrings = args[0].AsString()
                             .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            ComparisonResult e;

            foreach (var conString in conStrings)
            {
                if (!Enum.TryParse(NameToCamel(conString), true, out e))
                {
                    continue;
                }
                if (!cmp.Result.HasFlag(e))
                {
                    continue;
                }
                vm.PushState(VM.State.CreateSub(source, args[1].AsPattern(), vm, vm.CurrentState.Output));
                return(true);
            }

            return(false);
        }
Example #8
0
        public bool Iterate(VM ii, RepeaterBlueprint bp)
        {
            while (ii.CurrentRepeater != null && ii.CurrentRepeater.Finished)
            {
                ii.PopRepeater();
            }

            if (Finished)
            {
                return(false);
            }

            // Queue the next iteration on the current state
            ii.CurrentState.Pre(bp);

            // Push separator if applicable
            if (!IsLast && _attribs.Separator != null && _attribs.Separator.Any())
            {
                var sepState = VM.State.CreateSub(
                    ii.CurrentState.Reader.Source,
                    _attribs.Separator,
                    ii,
                    ii.CurrentState.Output);

                // Make sure that the repeater is not available to the separator pattern
                sepState.Pre(new DelegateBlueprint(ii, _ =>
                {
                    _allowStats = false;
                    return(false);
                }));
                sepState.Post(new DelegateBlueprint(ii, _ =>
                {
                    _allowStats = true;
                    return(false);
                }));

                ii.PushState(sepState);
            }

            VM.State afterState = null;

            // Push postfix if applicable
            if (_attribs.After != null && _attribs.After.Any())
            {
                ii.PushState(afterState = VM.State.CreateSub(
                                 ii.CurrentState.Reader.Source,
                                 _attribs.After,
                                 ii,
                                 ii.CurrentState.Output));
            }

            // Push next item
            var itemState = VM.State.CreateSub(ii.CurrentState.Reader.Source,
                                               _attribs.Sync != null
                ? _block.Items[_attribs.Sync.NextItem(_block.Items.Length)].Item2
                : _block.Items.PickWeighted(ii.RNG, _block.WeightTotal, item => item.Item1).Item2,
                                               ii,
                                               ii.CurrentState.Output);

            // Apply the Next() call to the last state in the repeater iteration
            (afterState ?? itemState).Post(new DelegateBlueprint(ii, _ =>
            {
                Next();
                return(false);
            }));

            ii.PushState(itemState);

            // Push prefix if applicable
            if (_attribs.Before != null && _attribs.Before.Any())
            {
                ii.PushState(VM.State.CreateSub(
                                 ii.CurrentState.Reader.Source,
                                 _attribs.Before,
                                 ii,
                                 ii.CurrentState.Output));
            }

            return(true);
        }