Ejemplo n.º 1
0
 /// <summary>
 /// Queries the vocabulary according to the specified criteria and returns a random match.
 /// </summary>
 /// <param name="rng">The random number generator to randomize the match with.</param>
 /// <param name="query">The search criteria to use.</param>
 /// <returns></returns>
 public string Query(RNG rng, Query query)
 {
     Dictionary wordList;
     return !_wordLists.TryGetValue(query.Name, out wordList) 
         ? "MISSINGDIC" 
         : wordList.Query(rng, query);
 }
Ejemplo n.º 2
0
 public Synchronizer(SyncType type, long seed)
 {
     _type = type;
     _rng = new RNG(seed);
     _index = 0;
     _state = null;
     _pinned = false;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Executes the specified source using a custom random number generator and returns the resulting output.
 /// </summary>
 /// <param name="input">The source 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(Source input, RNG rng, int charLimit = 0)
 {
     return new Interpreter(this, input, rng, charLimit).Run();
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Loads the file located at the specified path and executes it using a custom seed, returning the resulting output.
 /// </summary>
 /// <param name="path">The path to the file 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 DoFile(string path, RNG rng, int charLimit = 0)
 {
     return new Interpreter(this, Source.FromFile(path), rng, charLimit).Run();
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Executes the specified string using a custom random number generator and returns the resulting output.
 /// </summary>
 /// <param name="input">The input string 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(string input, RNG rng, int charLimit = 0)
 {
     return new Interpreter(this, Source.FromString(input), rng, charLimit).Run();
 }
Ejemplo n.º 6
0
 public static string Unescape(string sequence, RNG rng = null)
 {
     var match = Lexer.EscapeRegex.Match(sequence);
     var count = Int32.Parse(Alt(match.Groups["count"].Value, "1"));
     bool unicode = match.Groups["unicode"].Success;
     var code = Alt(match.Groups["code"].Value, match.Groups["unicode"].Value);
     var sb = new StringBuilder();
     if (unicode)
     {
             sb.Append((char)Convert.ToUInt16(code, 16), count);
     }
     else if (code[0] == 'N') // The only escape sequence that returns more than 1 character without a quantifier
     {
         for (int i = 0; i < count; i++)
             sb.Append(Environment.NewLine);
     }
     else
     {
         Func<RNG, char> func;
         if (_escapeChars.TryGetValue(code[0], out func))
         {
             for (int i = 0; i < count; i++)
                 sb.Append(func(rng));
         }
         else
         {
             for (int i = 0; i < count; i++)
                 sb.Append(code[0]);
         }
     }
     return sb.ToString();
 }
Ejemplo n.º 7
0
        internal string Query(RNG rng, Query query)
        {
            var index = String.IsNullOrEmpty(query.Subtype) ? 0 : GetSubtypeIndex(query.Subtype);
            if (index == -1)
            {
                return "BAD_SUBTYPE";
            }

            IEnumerable<DictionaryEntry> pool = _words;

            pool = query.Exclusive
                ? pool.Where(e => e.Classes.Any() && e.Classes.All(c => query.ClassFilters.Any(set => set.Any(t => t.Item2 == c))))
                : pool.Where(e => query.ClassFilters.All(set => set.Any(t => t.Item1 == (e.Classes.Contains(t.Item2)))));

            pool = query.RegexFilters.Aggregate(pool, (current, regex) => current.Where(e => regex.Item1 == regex.Item2.IsMatch(e.Values[index])));

            if (!pool.Any())
            {
                return "NOT_FOUND";
            }

            int number = String.IsNullOrEmpty(query.Carrier) ? rng.Next(pool.Sum(e => e.Weight)) + 1
                : rng.PeekAt(query.Carrier.Hash(), pool.Sum(e => e.Weight));

            foreach (var e in pool)
            {
                if (number <= e.Weight) return e.Values[index];
                number -= e.Weight;
            }

            return "NOT_FOUND";
        }
Ejemplo n.º 8
0
 public Interpreter(Engine engine, Source input, RNG rng, int limitChars = 0)
 {
     _mainSource = input;
     _rng = rng;
     _engine = engine;
     _charLimit = new Limit<int>(0, limitChars, (a, b) => a + b, (a, b) => b == 0 || a <= b);
     _output = new ChannelStack(_charLimit);
     _mainState = State.Create(input, this);
 }