private void SetupRantPatterns()
        {
            nameGenMale = RantPattern.FromString("<name-male> <surname>");
            nameGenFemale = RantPattern.FromString("<name-female> <surname>");

            teamName = RantPattern.FromString("[case:title]<noun> {(0.3)FC|(0.3)United|}");
            leagueName = RantPattern.FromString("[case:title]{The <noun> League}");
        }
Exemple #2
0
		/// <summary>
		/// Adds a RantPattern containing a subroutine to this module.
		/// </summary>
		/// <param name="name">The name of the function to add.</param>
		/// <param name="pattern">The pattern that will make up the body of the function.</param>
		public void AddSubroutineFunction(string name, RantPattern pattern)
		{
			var action = (pattern.Action.GetType() == typeof(RASequence) ? 
				((RASequence)pattern.Action).Actions[0] : 
				pattern.Action);
			if (action.GetType() != typeof(RADefineSubroutine))
				throw new RantRuntimeException(pattern, pattern.Code, "Attempted to add non-subroutine pattern to a module.");
			_objects[name] = new RantObject(action);
		}
Exemple #3
0
        /// <summary>
        /// Returns a pattern with the specified name from the engine's cache. If the pattern doesn't exist, it is loaded from file.
        /// </summary>
        /// <param name="name">The name or path of the pattern to retrieve.</param>
        /// <returns></returns>
        internal RantPattern GetPattern(string name)
        {
            RantPattern pattern;

            if (_patternCache.TryGetValue(name, out pattern))
            {
                return(pattern);
            }
            return(_patternCache[name] = RantPattern.FromFile(name));
        }
Exemple #4
0
        private static bool GenerationSet(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
        {
            var  gStr = args[0].GetString();
            long g;

            if (!Int64.TryParse(gStr, out g))
            {
                throw Error(source, tagName, "Invalid generation value '\{gStr}'");
            }
            interpreter.RNG.Generation = g;
            return(false);
        }
Exemple #5
0
        /// <summary>
        /// Adds a RantPattern containing a subroutine to this module.
        /// </summary>
        /// <param name="name">The name of the function to add.</param>
        /// <param name="pattern">The pattern that will make up the body of the function.</param>
        public void AddSubroutineFunction(string name, RantPattern pattern)
        {
            var action = (pattern.Action.GetType() == typeof(RASequence) ?
                          ((RASequence)pattern.Action).Actions[0] :
                          pattern.Action);

            if (action.GetType() != typeof(RADefineSubroutine))
            {
                throw new RantRuntimeException(pattern, pattern.Code, "Attempted to add non-subroutine pattern to a module.");
            }
            _objects[name] = new RantObject(action);
        }
Exemple #6
0
        private static bool BranchScope(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
        {
            interpreter.RNG.Branch(args[0].GetString().Hash());
            var state = Interpreter.State.CreateSub(source, args[1].GetTokens(), interpreter, interpreter.CurrentState.Output);

            state.AddPostBlueprint(new DelegateBlueprint(interpreter, _ =>
            {
                interpreter.RNG.Merge();
                return(false);
            }));
            interpreter.PushState(state);
            return(true);
        }
Exemple #7
0
        private static bool DefineFlag(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
        {
            foreach (var flag in args.Where(flag => !String.IsNullOrEmpty(flag)))
            {
                if (!ValidateName(flag))
                {
                    throw Error(source, tagname, "Invalid flag name '\{flag}'");
                }

                interpreter.Engine.Flags.Add(flag);
            }
            return(false);
        }
Exemple #8
0
        private static bool CharacterMulti(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
        {
            int count;

            if (!ParseInt(args[1], out count) || count < 0)
            {
                throw Error(source, tagname, "Invalid character count specified. Must be a non-negative number greater than zero.");
            }
            for (int i = 0; i < count; i++)
            {
                interpreter.Print(SelectFromRanges(args[0], interpreter.RNG));
            }
            return(false);
        }
Exemple #9
0
 private static bool Break(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     if (interpreter.CurrentRepeater == null)
     {
         return(false);
     }
     interpreter.CurrentRepeater.Finish();
     Interpreter.State state = null;
     while (!interpreter.BaseStates.Contains(state = interpreter.CurrentState))
     {
         interpreter.PopState();
     }
     interpreter.BaseStates.Remove(state);
     return(true);
 }
Exemple #10
0
 internal RantRuntimeException(RantPattern source, Stringe token, string message = "A generic syntax error was encountered.")
     : base((token != null ? ($"({source.Name} @ Ln {token.Line}, Col {token.Column}): ") : "") + message)
 {
     _source = source.Code;
     if (token != null)
     {
         _line   = token.Line;
         _col    = token.Column;
         _index  = token.Offset;
         _length = token.Length;
     }
     else
     {
         _line   = _col = 1;
         _index  = 0;
         _length = 0;
     }
 }
 internal RantRuntimeException(RantPattern source, Stringe token, string message = "A generic syntax error was encountered.") 
     : base((token != null ? ($"({source.Name} @ Ln {token.Line}, Col {token.Column}): ") : "") + message)
 {
     _source = source.Code;
     if (token != null)
     {
         _line = token.Line;
         _col = token.Column;
         _index = token.Offset;
         _length = token.Length;
     }
     else
     {
         _line = _col = 1;
         _index = 0;
         _length = 0;
     }
 }
Exemple #12
0
        private static bool Compare(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
        {
            var cmp = new Comparison(args[0].GetString(), args[1].GetString());

            interpreter.Comparisons.Push(cmp);

            var state = Interpreter.State.CreateSub(source, args[2].GetTokens(), interpreter, interpreter.CurrentState.Output);

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

            interpreter.PushState(state);

            return(true);
        }
Exemple #13
0
        internal RantRuntimeException(IEnumerable <Token <R> > tokens, RantPattern source, string message = "A generic syntax error was encountered.")
            : base((tokens != null ? ($"({source.Name} @ Ln {tokens.First().Line}, Col {tokens.First().Column}): ") : "") + message)
        {
            _source = source.Code;

            if (tokens != null)
            {
                var first = tokens.First();
                var last  = tokens.Last();
                _line   = first.Line;
                _col    = first.Column;
                _index  = first.Offset;
                _length = (last.Offset + last.Length) - first.Offset;
            }
            else
            {
                _line   = _col = 1;
                _index  = 0;
                _length = 0;
            }
        }
        internal RantRuntimeException(IEnumerable<Token<R>> tokens, RantPattern source, string message = "A generic syntax error was encountered.")
            : base((tokens != null ? ($"({source.Name} @ Ln {tokens.First().Line}, Col {tokens.First().Column}): ") : "") + message)
        {
            _source = source.Code;
            
            if (tokens != null)
            {
                var first = tokens.First();
                var last = tokens.Last();
                _line = first.Line;
                _col = first.Column;
                _index = first.Offset;
                _length = (last.Offset + last.Length) - first.Offset;
            }
            else
            {

                _line = _col = 1;
                _index = 0;
                _length = 0;
            }
        }
Exemple #15
0
        private static bool CmpIs(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
        {
            var cmp        = interpreter.Comparisons.Peek();
            var conStrings = args[0].GetString()
                             .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;
                }
                interpreter.PushState(Interpreter.State.CreateSub(source, args[1].GetTokens(), interpreter, interpreter.CurrentState.Output));
                return(true);
            }

            return(false);
        }
Exemple #16
0
 /// <summary>
 /// Executes the specified pattern and returns a series of outputs.
 /// </summary>
 /// <param name="input">The patten to execute.</param>
 /// <param name="seed">The seed to generate output with.</param>
 /// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
 /// <param name="timeout">The maximum number of seconds that the pattern will execute for.</param>
 /// <returns></returns>
 public IEnumerable<RantOutput> DoSerial(RantPattern input, long seed, int charLimit = 0, double timeout = -1) =>
     new Sandbox(this, input, new RNG(seed), charLimit).RunSerial(timeout);
Exemple #17
0
 private static bool Character(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(SelectFromRanges(args[0], interpreter.RNG));
     return(false);
 }
Exemple #18
0
 private static bool NumberDec(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.Print(interpreter.RNG.NextDouble());
     return(false);
 }
Exemple #19
0
 public static State CreateSub(RantPattern derivedSource, IEnumerable <Token <R> > tokens,
                               Interpreter interpreter, ChannelStack output = null)
 {
     return(new State(interpreter, derivedSource, tokens, output ?? new ChannelStack(interpreter.FormatStyle, interpreter.CharLimit)));
 }
Exemple #20
0
 private static bool Mark(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.SetMarker(args[0]);
     return(false);
 }
Exemple #21
0
 private static bool ClearTarget(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.CurrentState.Output.ClearTarget(args[0]);
     return(false);
 }
Exemple #22
0
 private static bool Src(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(source.Code);
     return(false);
 }
Exemple #23
0
 private static bool Branch(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.RNG.Branch(args[0].GetString().Hash());
     return(false);
 }
Exemple #24
0
		/// <summary>
		/// Executes the specified pattern using a custom random number generator and returns the resulting output.
		/// </summary>
		/// <param name="input">The pattern to execute.</param>
		/// <param name="rng">The random number generator to use when generating output.</param>
		/// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
		/// <param name="timeout">The maximum number of seconds that the pattern will execute for.</param>
		/// <param name="args">The arguments to pass to the pattern.</param>
		/// <returns></returns>
		public RantOutput Do(RantPattern input, RNG rng, int charLimit = 0, double timeout = -1, RantPatternArgs args = null) => 
            RunVM(new Sandbox(this, input, rng, charLimit, args), timeout);
Exemple #25
0
 private static bool Merge(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.RNG.Merge();
     return(false);
 }
Exemple #26
0
 private static bool Generation(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.Print(interpreter.RNG.Generation);
     return(false);
 }
Exemple #27
0
		/// <summary>
		/// Executes the specified pattern and returns a series of outputs.
		/// </summary>
		/// <param name="input">The pattern to execute.</param>
		/// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
		/// <param name="timeout">The maximum number of seconds that the pattern will execute for.</param>
		/// <param name="args">The arguments to pass to the pattern.</param>
		/// <returns></returns>
		public IEnumerable<RantOutput> DoSerial(RantPattern input, int charLimit = 0, double timeout = -1, RantPatternArgs args = null) =>
            new Sandbox(this, input, new RNG(Seeds.NextRaw()), charLimit, args).RunSerial(timeout);
Exemple #28
0
 private static bool Length(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(args[0].GetString().Length);
     return(false);
 }
Exemple #29
0
 private static bool SendOverwrite(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.CurrentState.Output.WriteToTarget(args[0], args[1], true);
     return(false);
 }
Exemple #30
0
		/// <summary>
		/// Executes the specified pattern using a custom seed and returns the resulting output.
		/// </summary>
		/// <param name="input">The pattern to execute.</param>
		/// <param name="seed">The seed to generate output with.</param>
		/// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
		/// <param name="timeout">The maximum number of seconds that the pattern will execute for.</param>
		/// <param name="args">The arguments to pass to the pattern.</param>
		/// <returns></returns>
		public RantOutput Do(RantPattern input, long seed, int charLimit = 0, double timeout = -1, RantPatternArgs args = null) => 
            RunVM(new Sandbox(this, input, new RNG(seed), charLimit, args), timeout);
Exemple #31
0
 private static bool Dist(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(interpreter.GetMarkerDistance(args[0], args[1]));
     return(false);
 }
Exemple #32
0
 internal static RantPattern Derived(RantPattern source, IEnumerable <Token <R> > tokens) => new RantPattern(source, tokens);
Exemple #33
0
 public static State Create(RantPattern source, Interpreter interpreter)
 {
     return(new State(interpreter, source, interpreter.CurrentState.Output));
 }
Exemple #34
0
 public PatternReader(RantPattern source)
 {
     _source = source;
     _tokens = source.Tokens.ToArray();
     _pos    = 0;
 }
Exemple #35
0
 /// <summary>
 /// Executes the specified pattern using a custom random number generator and returns the resulting output.
 /// </summary>
 /// <param name="input">The pattern to execute.</param>
 /// <param name="rng">The random number generator to use when generating output.</param>
 /// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
 /// <returns></returns>
 public Output Do(RantPattern input, RNG rng, int charLimit = 0)
 {
     return(new Interpreter(this, input, rng, charLimit).Run());
 }
Exemple #36
0
 /// <summary>
 /// Executes the specified pattern using a custom seed and returns the resulting output.
 /// </summary>
 /// <param name="input">The pattern to execute.</param>
 /// <param name="seed">The seed to generate output with.</param>
 /// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
 /// <returns></returns>
 public Output Do(RantPattern input, long seed, int charLimit = 0)
 {
     return(new Interpreter(this, input, new RNG(seed), charLimit).Run());
 }
Exemple #37
0
 /// <summary>
 /// Executes the specified pattern using a custom random number generator and returns the resulting output.
 /// </summary>
 /// <param name="input">The pattern to execute.</param>
 /// <param name="rng">The random number generator to use when generating output.</param>
 /// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
 /// <param name="timeout">The maximum number of seconds that the pattern will execute for.</param>
 /// <returns></returns>
 public RantOutput Do(RantPattern input, RNG rng, int charLimit = 0, double timeout = -1) => 
     RunVM(new Sandbox(this, input, rng, charLimit), timeout);
Exemple #38
0
 // Used for applying a different name to subroutines
 internal static RantPattern Derived(string name, RantPattern source, IEnumerable <Token <R> > tokens) => new RantPattern(name, source, tokens);
Exemple #39
0
 private static bool SyncReseed(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.SyncSeed(args[0].GetString(), args[1].GetString());
     return(false);
 }
Exemple #40
0
		/// <summary>
		/// Executes the specified pattern and returns the resulting output.
		/// </summary>
		/// <param name="input">The pattern to execute.</param>
		/// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
		/// <param name="timeout">The maximum number of seconds that the pattern will execute for.</param>
		/// <param name="args">The arguments to pass to the pattern.</param>
		/// <returns></returns>
		public RantOutput Do(RantPattern input, int charLimit = 0, double timeout = -1, RantPatternArgs args = null) => 
            RunVM(new Sandbox(this, input, new RNG(Seeds.NextRaw()), charLimit, args), timeout);
Exemple #41
0
 /// <summary>
 /// Adds the specified pattern to the package.
 /// </summary>
 /// <param name="pattern">The pattern to add.</param>
 public void AddPattern(RantPattern pattern)
     => (_patterns ?? (_patterns = new HashSet<RantPattern>())).Add(pattern);
Exemple #42
0
		/// <summary>
		/// Executes the specified pattern and returns a series of outputs.
		/// </summary>
		/// <param name="input">The pattero to execute.</param>
		/// <param name="rng">The random number generator to use when generating output.</param>
		/// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
		/// <param name="timeout">The maximum number of seconds that the pattern will execute for.</param>
		/// <param name="args">The arguments to pass to the pattern.</param>
		/// <returns></returns>
		public IEnumerable<RantOutput> DoSerial(RantPattern input, RNG rng, int charLimit = 0, double timeout = -1, RantPatternArgs args = null) =>
            new Sandbox(this, input, rng, charLimit, args).RunSerial(timeout);