Exemplo n.º 1
0
        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}");
        }
Exemplo n.º 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);
		}
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
 }
Exemplo n.º 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;
     }
 }
Exemplo n.º 11
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;
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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;
            }
        }
Exemplo n.º 14
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;
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
Exemplo n.º 17
0
 private static bool Character(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(SelectFromRanges(args[0], interpreter.RNG));
     return(false);
 }
Exemplo n.º 18
0
 private static bool NumberDec(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.Print(interpreter.RNG.NextDouble());
     return(false);
 }
Exemplo n.º 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)));
 }
Exemplo n.º 20
0
 private static bool Mark(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.SetMarker(args[0]);
     return(false);
 }
Exemplo n.º 21
0
 private static bool ClearTarget(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.CurrentState.Output.ClearTarget(args[0]);
     return(false);
 }
Exemplo n.º 22
0
 private static bool Src(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(source.Code);
     return(false);
 }
Exemplo n.º 23
0
 private static bool Branch(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.RNG.Branch(args[0].GetString().Hash());
     return(false);
 }
Exemplo n.º 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);
Exemplo n.º 25
0
 private static bool Merge(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.RNG.Merge();
     return(false);
 }
Exemplo n.º 26
0
 private static bool Generation(Interpreter interpreter, RantPattern source, Stringe tagName, Argument[] args)
 {
     interpreter.Print(interpreter.RNG.Generation);
     return(false);
 }
Exemplo n.º 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);
Exemplo n.º 28
0
 private static bool Length(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(args[0].GetString().Length);
     return(false);
 }
Exemplo n.º 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);
 }
Exemplo n.º 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);
Exemplo n.º 31
0
 private static bool Dist(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.Print(interpreter.GetMarkerDistance(args[0], args[1]));
     return(false);
 }
Exemplo n.º 32
0
 internal static RantPattern Derived(RantPattern source, IEnumerable <Token <R> > tokens) => new RantPattern(source, tokens);
Exemplo n.º 33
0
 public static State Create(RantPattern source, Interpreter interpreter)
 {
     return(new State(interpreter, source, interpreter.CurrentState.Output));
 }
Exemplo n.º 34
0
 public PatternReader(RantPattern source)
 {
     _source = source;
     _tokens = source.Tokens.ToArray();
     _pos    = 0;
 }
Exemplo n.º 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());
 }
Exemplo n.º 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());
 }
Exemplo n.º 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);
Exemplo n.º 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);
Exemplo n.º 39
0
 private static bool SyncReseed(Interpreter interpreter, RantPattern source, Stringe tagname, Argument[] args)
 {
     interpreter.SyncSeed(args[0].GetString(), args[1].GetString());
     return(false);
 }
Exemplo n.º 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);
Exemplo n.º 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);
Exemplo n.º 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);